View Javadoc
1   package org.apache.maven.it;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.it.util.ResourceExtractor;
23  import org.eclipse.jetty.server.Handler;
24  import org.eclipse.jetty.server.NetworkConnector;
25  import org.eclipse.jetty.server.Request;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.server.handler.AbstractHandler;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.File;
32  import java.util.Deque;
33  import java.util.Properties;
34  import java.util.concurrent.ConcurrentLinkedDeque;
35  
36  /**
37   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4555">MNG-4555</a>.
38   *
39   * @author Benjamin Bentmann
40   */
41  public class MavenITmng4555MetaversionResolutionOfflineTest
42      extends AbstractMavenIntegrationTestCase
43  {
44  
45      public MavenITmng4555MetaversionResolutionOfflineTest()
46      {
47          super( "[2.0.3,3.0-alpha-1),[3.0-beta-1,)" );
48      }
49  
50      /**
51       * Verify that resolution of the metaversion RELEASE respects offline mode.
52       *
53       * @throws Exception in case of failure
54       */
55      public void testit()
56          throws Exception
57      {
58          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4555" );
59  
60          final Deque<String> uris = new ConcurrentLinkedDeque<>();
61  
62          Handler repoHandler = new AbstractHandler()
63          {
64              @Override
65              public void handle( String target, Request baseRequest, HttpServletRequest request,
66                                  HttpServletResponse response )
67              {
68                  String uri = request.getRequestURI();
69  
70                  if ( uri.startsWith( "/repo/org/apache/maven/its/mng4555" ) )
71                  {
72                      uris.add( uri.substring( 34 ) );
73                  }
74  
75                  response.setStatus( HttpServletResponse.SC_NOT_FOUND );
76                  ( (Request) request ).setHandled( true );
77              }
78          };
79  
80          Server server = new Server( 0 );
81          server.setHandler( repoHandler );
82          server.start();
83  
84          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
85          verifier.setAutoclean( false );
86          verifier.deleteDirectory( "target" );
87          verifier.deleteArtifacts( "org.apache.maven.its.mng4555" );
88          try
89          {
90              if ( server.isFailed() )
91              {
92                  fail( "Couldn't bind the server socket to a free port!" );
93              }
94              int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
95              System.out.println( "Bound server socket to the port " + port );
96              Properties filterProps = verifier.newDefaultFilterProperties();
97              filterProps.setProperty( "@port@", Integer.toString( port ) );
98              verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
99              verifier.addCliOption( "--offline" );
100             verifier.addCliOption( "--settings" );
101             verifier.addCliOption( "settings.xml" );
102             verifier.executeGoal( "validate" );
103         }
104         catch ( VerificationException e )
105         {
106             // expected
107         }
108         finally
109         {
110             verifier.resetStreams();
111             server.stop();
112             server.join();
113         }
114 
115         assertTrue( uris.toString(), uris.isEmpty() );
116     }
117 }