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.ArrayList;
33  import java.util.Collections;
34  import java.util.List;
35  import java.util.Properties;
36  
37  /**
38   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4348">MNG-4348</a>.
39   *
40   * @author Benjamin Bentmann
41   *
42   */
43  public class MavenITmng4348NoUnnecessaryRepositoryAccessTest
44      extends AbstractMavenIntegrationTestCase
45  {
46  
47      public MavenITmng4348NoUnnecessaryRepositoryAccessTest()
48      {
49          super( ALL_MAVEN_VERSIONS );
50      }
51  
52      /**
53       * Test that the (remote) repos are not accessed during execution of a mojo that does not require dependency
54       * resolution. In detail, Maven should neither touch POMs, JARs nor metadata.
55       *
56       * @throws Exception in case of failure
57       */
58      public void testit()
59          throws Exception
60      {
61          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4348" );
62  
63          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
64  
65          final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
66  
67          Handler repoHandler = new AbstractHandler()
68          {
69              @Override
70              public void handle( String target, Request baseRequest, HttpServletRequest request,
71                                  HttpServletResponse response )
72              {
73                  System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
74  
75                  // NOTE: Old Maven versions use the test repo also to check for plugin updates so we need to filter
76                  if ( request.getRequestURI().startsWith( "/org/apache/maven/its/mng4348" ) )
77                  {
78                      requestedUris.add( request.getRequestURI() );
79                  }
80  
81                  response.setStatus( HttpServletResponse.SC_NOT_FOUND );
82  
83                  ( (Request) request ).setHandled( true );
84              }
85          };
86  
87          Server server = new Server( 0 );
88          server.setHandler( repoHandler );
89  
90          try
91          {
92              server.start();
93              if ( server.isFailed() )
94              {
95                  fail( "Couldn't bind the server socket to a free port!" );
96              }
97              int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
98              System.out.println( "Bound server socket to the port " + port );
99              verifier.setAutoclean( false );
100             verifier.deleteArtifacts( "org.apache.maven.its.mng4348" );
101             verifier.deleteDirectory( "target" );
102             Properties filterProps = verifier.newDefaultFilterProperties();
103             filterProps.setProperty( "@port@", Integer.toString( port ) );
104             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
105             verifier.addCliOption( "--settings" );
106             verifier.addCliOption( "settings.xml" );
107             verifier.executeGoal( "validate" );
108             verifier.verifyErrorFreeLog();
109             verifier.resetStreams();
110         }
111         finally
112         {
113             server.stop();
114             server.join();
115         }
116 
117         verifier.assertFilePresent( "target/touch.txt" );
118         assertEquals( new ArrayList<String>(), requestedUris );
119     }
120 }