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.NetworkConnector;
24  import org.eclipse.jetty.server.Request;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.server.handler.AbstractHandler;
27  import org.eclipse.jetty.server.handler.DefaultHandler;
28  import org.eclipse.jetty.server.handler.HandlerList;
29  import org.eclipse.jetty.server.handler.ResourceHandler;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import java.io.File;
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.List;
37  import java.util.Properties;
38  
39  /**
40   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-5064">MNG-5064</a>.
41   *
42   * @author Benjamin Bentmann
43   */
44  public class MavenITmng5064SuppressSnapshotUpdatesTest
45      extends AbstractMavenIntegrationTestCase
46  {
47  
48      public MavenITmng5064SuppressSnapshotUpdatesTest()
49      {
50          super( "[3.0.4,)" );
51      }
52  
53      /**
54       * Verify that snapshot updates can be completely suppressed via the CLI arg -nsu. The initial retrieval of a
55       * missing snapshot should not be suppressed though.
56       *
57       * @throws Exception in case of failure
58       */
59      public void testit()
60          throws Exception
61      {
62          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5064" );
63  
64          String metadataUri = "org/apache/maven/its/mng5064/dep/0.1-SNAPSHOT/maven-metadata.xml";
65  
66          final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
67  
68          AbstractHandler logHandler = new AbstractHandler()
69          {
70              @Override
71              public void handle( String target, Request baseRequest, HttpServletRequest request,
72                                  HttpServletResponse response )
73              {
74                  if ( request.getRequestURI().startsWith( "/repo/" ) )
75                  {
76                      requestedUris.add( request.getRequestURI().substring( 6 ) );
77                  }
78              }
79          };
80  
81          ResourceHandler repoHandler = new ResourceHandler();
82          repoHandler.setResourceBase( testDir.getAbsolutePath() );
83  
84          HandlerList handlerList = new HandlerList();
85          handlerList.addHandler( logHandler );
86          handlerList.addHandler( repoHandler );
87          handlerList.addHandler( new DefaultHandler() );
88  
89          Server server = new Server( 0 );
90          server.setHandler( handlerList );
91          server.start();
92  
93          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
94          try
95          {
96              if ( server.isFailed() )
97              {
98                  fail( "Couldn't bind the server socket to a free port!" );
99              }
100             int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
101             System.out.println( "Bound server socket to the port " + port );
102             verifier.setAutoclean( false );
103             verifier.deleteDirectory( "target" );
104             verifier.deleteArtifacts( "org.apache.maven.its.mng5064" );
105             Properties filterProps = verifier.newDefaultFilterProperties();
106             filterProps.setProperty( "@port@", Integer.toString( port ) );
107             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
108             verifier.addCliOption( "-nsu" );
109             verifier.addCliOption( "-s" );
110             verifier.addCliOption( "settings.xml" );
111 
112             verifier.setLogFileName( "log-1.txt" );
113             verifier.executeGoal( "validate" );
114             verifier.verifyErrorFreeLog();
115 
116             List<String> classpath = verifier.loadLines( "target/classpath.txt", "UTF-8" );
117             assertTrue( classpath.toString(), classpath.contains( "dep-0.1-SNAPSHOT.jar" ) );
118             assertTrue( requestedUris.toString(), requestedUris.contains( metadataUri ) );
119 
120             requestedUris.clear();
121 
122             verifier.setLogFileName( "log-2.txt" );
123             verifier.executeGoal( "validate" );
124             verifier.verifyErrorFreeLog();
125 
126             classpath = verifier.loadLines( "target/classpath.txt", "UTF-8" );
127             assertTrue( classpath.toString(), classpath.contains( "dep-0.1-SNAPSHOT.jar" ) );
128             assertFalse( requestedUris.toString(), requestedUris.contains( metadataUri ) );
129         }
130         finally
131         {
132             verifier.resetStreams();
133             server.stop();
134             server.join();
135         }
136     }
137 }