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-4500">MNG-4500</a>.
41   *
42   * @author Benjamin Bentmann
43   */
44  public class MavenITmng4500NoUpdateOfTimestampedSnapshotsTest
45      extends AbstractMavenIntegrationTestCase
46  {
47  
48      public MavenITmng4500NoUpdateOfTimestampedSnapshotsTest()
49      {
50          super( "[2.0.3,3.0-alpha-1),[3.0-alpha-6,)" );
51      }
52  
53      /**
54       * Test that timestamped snapshots are treated as immutable, i.e. Maven should never check for updates of them
55       * once downloaded from a remote repo regardless of the update policy.
56       *
57       * @throws Exception in case of failure
58       */
59      public void testit()
60          throws Exception
61      {
62          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4500" );
63  
64          String pomUri = "/repo/org/apache/maven/its/mng4500/dep/0.1-SNAPSHOT/dep-0.1-20091219.230823-1.pom";
65          String jarUri = "/repo/org/apache/maven/its/mng4500/dep/0.1-SNAPSHOT/dep-0.1-20091219.230823-1.jar";
66  
67          final List<String> requestedUris = Collections.synchronizedList( new ArrayList<String>() );
68  
69          AbstractHandler logHandler = new AbstractHandler()
70          {
71              @Override
72              public void handle( String target, Request baseRequest, HttpServletRequest request,
73                                  HttpServletResponse response )
74              {
75                  requestedUris.add( request.getRequestURI() );
76              }
77          };
78  
79          ResourceHandler repoHandler = new ResourceHandler();
80          repoHandler.setResourceBase( testDir.getAbsolutePath() );
81  
82          HandlerList handlerList = new HandlerList();
83          handlerList.addHandler( logHandler );
84          handlerList.addHandler( repoHandler );
85          handlerList.addHandler( new DefaultHandler() );
86  
87          Server server = new Server( 0 );
88          server.setHandler( handlerList );
89  
90          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
91          try
92          {
93              server.start();
94              if ( server.isFailed() )
95              {
96                  fail( "Couldn't bind the server socket to a free port!" );
97              }
98              int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
99              System.out.println( "Bound server socket to the port " + port );
100             verifier.setAutoclean( false );
101             verifier.deleteDirectory( "target" );
102             verifier.deleteArtifacts( "org.apache.maven.its.mng4500" );
103             Properties filterProps = verifier.newDefaultFilterProperties();
104             filterProps.setProperty( "@port@", Integer.toString( port ) );
105             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
106             verifier.addCliOption( "-s" );
107             verifier.addCliOption( "settings.xml" );
108 
109             verifier.setLogFileName( "log-1.txt" );
110             verifier.executeGoal( "validate" );
111             verifier.verifyErrorFreeLog();
112 
113             List<String> classpath = verifier.loadLines( "target/classpath.txt", "UTF-8" );
114             assertTrue( classpath.toString(), classpath.contains( "dep-0.1-SNAPSHOT.jar" ) );
115             assertTrue( requestedUris.toString(), requestedUris.contains( pomUri ) );
116             assertTrue( requestedUris.toString(), requestedUris.contains( jarUri ) );
117 
118             requestedUris.clear();
119 
120             verifier.setLogFileName( "log-2.txt" );
121             verifier.executeGoal( "validate" );
122             verifier.verifyErrorFreeLog();
123 
124             classpath = verifier.loadLines( "target/classpath.txt", "UTF-8" );
125             assertTrue( classpath.toString(), classpath.contains( "dep-0.1-SNAPSHOT.jar" ) );
126             assertFalse( requestedUris.toString(), requestedUris.contains( pomUri ) );
127             assertFalse( requestedUris.toString(), requestedUris.contains( jarUri ) );
128         }
129         finally
130         {
131             verifier.resetStreams();
132             server.stop();
133             server.join();
134         }
135     }
136 }