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.io.IOException;
33  import java.io.PrintWriter;
34  import java.util.Date;
35  import java.util.Deque;
36  import java.util.List;
37  import java.util.Properties;
38  import java.util.concurrent.ConcurrentLinkedDeque;
39  
40  /**
41   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4326">MNG-4326</a>.
42   *
43   * @author Benjamin Bentmann
44   */
45  public class MavenITmng4326LocalSnapshotSuppressesRemoteCheckTest
46      extends AbstractMavenIntegrationTestCase
47  {
48  
49      public MavenITmng4326LocalSnapshotSuppressesRemoteCheckTest()
50      {
51          super( "[3.0-beta-1,)" );
52      }
53  
54      /**
55       * Verify that locally built/installed snapshot artifacts suppress remote update checks (as long as the local copy
56       * still satisfies the update policy configured for the remote repository).
57       *
58       * @throws Exception in case of failure
59       */
60      public void testit()
61          throws Exception
62      {
63          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4326" );
64  
65          // setup: install a local snapshot
66          Verifier verifier = newVerifier( new File( testDir, "dependency" ).getAbsolutePath() );
67          verifier.setAutoclean( false );
68          verifier.deleteArtifacts( "org.apache.maven.its.mng4326" );
69          verifier.deleteDirectory( "target" );
70          verifier.executeGoal( "validate" );
71          verifier.verifyErrorFreeLog();
72          verifier.resetStreams();
73  
74          final Deque<String> uris = new ConcurrentLinkedDeque<>();
75  
76          Handler repoHandler = new AbstractHandler()
77          {
78              @Override
79              public void handle( String target, Request baseRequest, HttpServletRequest request,
80                                  HttpServletResponse response )
81                  throws IOException
82              {
83                  PrintWriter writer = response.getWriter();
84  
85                  String uri = request.getRequestURI();
86  
87                  if ( uri.startsWith( "/repo/org/apache/maven/its/mng4326" ) && !uri.endsWith( ".md5" ) && !uri.endsWith(
88                      ".sha1" ) )
89                  {
90                      uris.add( uri.substring( 34 ) );
91                  }
92  
93                  if ( uri.endsWith( "dep/0.1-SNAPSHOT/maven-metadata.xml" ) )
94                  {
95                      java.text.DateFormat fmt = new java.text.SimpleDateFormat( "yyyyMMddHHmmss" );
96                      fmt.setTimeZone( java.util.TimeZone.getTimeZone( "UTC" ) );
97                      String now = fmt.format( new Date( System.currentTimeMillis() + 3000 ) );
98  
99                      response.setStatus( HttpServletResponse.SC_OK );
100                     writer.println( "<metadata>" );
101                     writer.println( "  <groupId>org.apache.maven.its.mng4326</groupId>" );
102                     writer.println( "  <artifactId>dep</artifactId>" );
103                     writer.println( "  <version>0.1-SNAPSHOT</version>" );
104                     writer.println( "  <versioning>" );
105                     writer.println( "    <snapshot>" );
106                     writer.println( "      <timestamp>20100329.235556</timestamp>" );
107                     writer.println( "      <buildNumber>1</buildNumber>" );
108                     writer.println( "    </snapshot>" );
109                     writer.println( "    <lastUpdated>" + now + "</lastUpdated>" );
110                     writer.println( "  </versioning>" );
111                     writer.println( "</metadata>" );
112                 }
113                 else if ( uri.endsWith( ".pom" ) )
114                 {
115                     response.setStatus( HttpServletResponse.SC_OK );
116                     writer.println( "<project>" );
117                     writer.println( "  <modelVersion>4.0.0</modelVersion>" );
118                     writer.println( "  <groupId>org.apache.maven.its.mng4326</groupId>" );
119                     writer.println( "  <artifactId>dep</artifactId>" );
120                     writer.println( "  <version>0.1-SNAPSHOT</version>" );
121                     writer.println( "</project>" );
122                 }
123                 else if ( uri.endsWith( ".jar" ) )
124                 {
125                     response.setStatus( HttpServletResponse.SC_OK );
126                     writer.println( "empty" );
127                 }
128                 else
129                 {
130                     response.setStatus( HttpServletResponse.SC_NOT_FOUND );
131                 }
132 
133                 ( (Request) request ).setHandled( true );
134             }
135         };
136 
137         Server server = new Server( 0 );
138         server.setHandler( repoHandler );
139 
140         try
141         {
142             server.start();
143             if ( server.isFailed() )
144             {
145                 fail( "Couldn't bind the server socket to a free port!" );
146             }
147             int port = ( (NetworkConnector) server.getConnectors()[0] ).getLocalPort();
148             System.out.println( "Bound server socket to the port " + port );
149             // test 1: resolve snapshot, just built local copy should suppress daily remote update check
150             verifier = newVerifier( new File( testDir, "test" ).getAbsolutePath() );
151             verifier.setAutoclean( false );
152             Properties filterProps = verifier.newDefaultFilterProperties();
153             filterProps.setProperty( "@port@", Integer.toString( port ) );
154             verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", filterProps );
155             verifier.addCliOption( "--settings" );
156             verifier.addCliOption( "settings.xml" );
157             verifier.setLogFileName( "log-daily.txt" );
158             verifier.deleteDirectory( "target" );
159             verifier.executeGoal( "validate" );
160             verifier.verifyErrorFreeLog();
161 
162             List<String> cp = verifier.loadLines( "target/classpath.txt", "UTF-8" );
163             assertTrue( cp.toString(), cp.contains( "dep-0.1-SNAPSHOT.jar" ) );
164 
165             assertFalse( uris.toString(), uris.contains( "/dep/0.1-SNAPSHOT/maven-metadata.xml" ) );
166             assertFalse( uris.toString(), uris.contains( "/dep/0.1-SNAPSHOT/dep-0.1-20100329.235556-1.jar" ) );
167 
168             uris.clear();
169 
170             // test 2: force snapshot updates, remote metadata and artifacts should be fetched
171             verifier.addCliOption( "-U" );
172             verifier.setLogFileName( "log-force.txt" );
173             verifier.deleteDirectory( "target" );
174             verifier.executeGoal( "validate" );
175             verifier.verifyErrorFreeLog();
176 
177             cp = verifier.loadLines( "target/classpath.txt", "UTF-8" );
178             assertTrue( cp.toString(), cp.contains( "dep-0.1-SNAPSHOT.jar" ) );
179 
180             assertTrue( uris.toString(), uris.contains( "/dep/0.1-SNAPSHOT/maven-metadata.xml" ) );
181             assertTrue( uris.toString(), uris.contains( "/dep/0.1-SNAPSHOT/dep-0.1-20100329.235556-1.jar" ) );
182         }
183         finally
184         {
185             verifier.resetStreams();
186             server.stop();
187         }
188     }
189 }