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.VerificationException;
23  import org.apache.maven.it.Verifier;
24  import org.apache.maven.it.util.FileUtils;
25  import org.apache.maven.it.util.ResourceExtractor;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Properties;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.mortbay.jetty.Handler;
37  import org.mortbay.jetty.Request;
38  import org.mortbay.jetty.Server;
39  import org.mortbay.jetty.handler.AbstractHandler;
40  
41  /**
42   * This is a test set for <a href="http://jira.codehaus.org/browse/MNG-3415">MNG-3415</a>.
43   * 
44   * @version $Id: MavenITmng3415JunkRepositoryMetadataTest.java 1165035 2011-09-04 14:40:36Z hboutemy $
45   */
46  public class MavenITmng3415JunkRepositoryMetadataTest
47      extends AbstractMavenIntegrationTestCase
48  {
49      private static final String RESOURCE_BASE = "/mng-3415";
50  
51      public MavenITmng3415JunkRepositoryMetadataTest()
52      {
53          // we're going to control the test execution according to the maven version present within each test method.
54          // all methods should execute as long as we're using maven 2.0.9+, but the specific tests may vary a little
55          // depending on which version we're using above 2.0.8.
56          super( "(2.0.8,)" ); // only test in 2.0.9+
57      }
58  
59      /**
60       * This test simply verifies that when a metadata transfer fails (network error, etc.)
61       * no metadata file is written to the local repository.
62       *
63       * Steps executed to verify this test:
64       *
65       * 0. Find the local repository directory:
66       *    a. build the maven-find-local-repo-plugin, then run it, to spit out the path of the
67       *       local repository in use by default. Read the output file to get this path.
68       *       (Yes, it's heavy, but it's reliable.)
69       * 1. Setup the test:
70       *    a. Make sure the metadata for the test-repo is NOT in the local repository.
71       *    b. Make sure the dependency POM IS in the local repository, so we're not
72       *       distracted by failed builds that are unrelated.
73       *    c. Create the settings file for use in this test, which contains the invalid
74       *       remote repository entry.
75       * 2. Build the test project the first time
76       *    a. Verify that a TransferFailedException is in the build output for the test-repo
77       *    b. Verify that the metadata for the dependency POM is NOT in the local
78       *       repository afterwards.
79       * 3. Build the test project the second time
80       *    a. See (2.a) and (2.b) above; the same criteria applies here.
81       */
82      public void testitTransferFailed()
83          throws Exception
84      {
85          String methodName = getMethodName();
86  
87          File testDir = ResourceExtractor.simpleExtractResources( getClass(), RESOURCE_BASE );
88  
89          Verifier verifier;
90  
91          verifier = newVerifier( testDir.getAbsolutePath() );
92          verifier.setAutoclean( false );
93          verifier.deleteArtifacts( "org.apache.maven.its.mng3415" );
94  
95          setupDummyDependency( verifier, testDir, true );
96  
97          Properties filterProps = verifier.newDefaultFilterProperties();
98          filterProps.put( "@protocol@", "invalid" );
99          filterProps.put( "@port@", "0" );
100         File settings = verifier.filterFile( "settings-template.xml", "settings-a.xml", "UTF-8", filterProps );
101 
102         verifier.getCliOptions().add( "-X" );
103         verifier.getCliOptions().add( "-s" );
104         verifier.getCliOptions().add( settings.getName() );
105 
106         verifier.setLogFileName( "log-" + methodName + "-firstBuild.txt" );
107         verifier.executeGoal( "validate" );
108 
109         verifier.verifyErrorFreeLog();
110 
111         assertMetadataMissing( verifier );
112 
113         setupDummyDependency( verifier, testDir, true );
114 
115         verifier.setLogFileName( "log-" + methodName + "-secondBuild.txt" );
116         verifier.executeGoal( "validate" );
117 
118         verifier.verifyErrorFreeLog();
119         verifier.resetStreams();
120 
121         assertMetadataMissing( verifier );
122     }
123 
124     private String getMethodName()
125     {
126         return new Throwable().getStackTrace()[1].getMethodName();
127     }
128 
129     /**
130      * This test simply verifies that when metadata doesn't exist on the remote
131      * repository, a basic metadata file is written to the local repository.
132      *
133      * Steps executed to verify this test:
134      *
135      * 0. Find the local repository directory:
136      *    a. build the maven-find-local-repo-plugin, then run it, to spit out the path of the
137      *       local repository in use by default. Read the output file to get this path.
138      *       (Yes, it's heavy, but it's reliable.)
139      * 1. Setup the test:
140      *    a. Make sure the metadata for the test-repo is NOT in the local repository.
141      *    b. Make sure the dependency POM IS in the local repository, so we're not
142      *       distracted by failed builds that are unrelated.
143      *    c. Create the settings file for use in this test, which contains the VALID
144      *       remote repository entry.
145      * 2. Build the test project the first time
146      *    a. Verify that the remote repository is checked for the metadata file
147      * 3. Build the test project the second time
148      *    a. Verify that the remote repository is NOT checked for the metadata file again
149      *    b. Verify that the file used for updateInterval calculations was NOT changed from
150      *       the first build.
151      */
152     public void testShouldNotRepeatedlyUpdateOnResourceNotFoundException()
153         throws Exception
154     {
155         String methodName = getMethodName();
156 
157         File testDir = ResourceExtractor.simpleExtractResources( getClass(), RESOURCE_BASE );
158 
159         Verifier verifier;
160 
161         verifier = newVerifier( testDir.getAbsolutePath() );
162         verifier.setAutoclean( false );
163         verifier.deleteArtifacts( "org.apache.maven.its.mng3415" );
164 
165         final List requestUris = new ArrayList();
166 
167         Handler repoHandler = new AbstractHandler()
168         {
169             public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
170             {
171                 System.out.println( "Handling " + request.getMethod() + " " + request.getRequestURL() );
172 
173                 requestUris.add( request.getRequestURI() );
174 
175                 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
176 
177                 ( (Request) request ).setHandled( true );
178             }
179         };
180 
181         Server server = new Server( 0 );
182         server.setHandler( repoHandler );
183         server.start();
184 
185         try
186         {
187             int port = server.getConnectors()[0].getLocalPort();
188 
189             Properties filterProps = verifier.newDefaultFilterProperties();
190             filterProps.put( "@protocol@", "http" );
191             filterProps.put( "@port@", Integer.toString( port ) );
192             File settings = verifier.filterFile( "settings-template.xml", "settings-b.xml", "UTF-8", filterProps );
193 
194             verifier.getCliOptions().add( "-X" );
195             verifier.getCliOptions().add( "-s" );
196             verifier.getCliOptions().add( settings.getName() );
197 
198             setupDummyDependency( verifier, testDir, true );
199 
200             verifier.setLogFileName( "log-" + methodName + "-firstBuild.txt" );
201             verifier.executeGoal( "validate" );
202 
203             verifier.verifyErrorFreeLog();
204 
205             assertTrue( requestUris.toString(), 
206                 requestUris.contains( "/org/apache/maven/its/mng3415/missing/1.0-SNAPSHOT/maven-metadata.xml" ) );
207 
208             requestUris.clear();
209 
210             File updateCheckFile = getUpdateCheckFile( verifier );
211             long firstLastMod = updateCheckFile.lastModified();
212 
213             setupDummyDependency( verifier, testDir, false );
214 
215             verifier.setLogFileName( "log-" + methodName + "-secondBuild.txt" );
216             verifier.executeGoal( "validate" );
217 
218             verifier.verifyErrorFreeLog();
219             verifier.resetStreams();
220 
221             assertFalse( requestUris.toString(), 
222                 requestUris.contains( "/org/apache/maven/its/mng3415/missing/1.0-SNAPSHOT/maven-metadata.xml" ) );
223 
224             assertEquals( "Last-modified time should be unchanged from first build through second build for the file we use for updateInterval checks.", firstLastMod, updateCheckFile.lastModified() );
225         }
226         finally
227         {
228             server.stop();
229         }
230     }
231 
232     private void assertMetadataMissing( Verifier verifier )
233         throws IOException
234     {
235         File metadata = getMetadataFile( verifier );
236 
237         assertFalse( "Metadata file should NOT be present in local repository: "
238                      + metadata.getAbsolutePath(), metadata.exists() );
239     }
240 
241     private void setupDummyDependency( Verifier verifier, File testDir, boolean resetUpdateInterval )
242         throws VerificationException, IOException
243     {
244         String gid = "org.apache.maven.its.mng3415";
245         String aid = "missing";
246         String version = "1.0-SNAPSHOT";
247 
248         if ( resetUpdateInterval )
249         {
250             verifier.deleteArtifacts( gid );
251         }
252 
253         File pom = new File( verifier.getArtifactPath( gid, aid, version, "pom" ) );
254 
255         File pomSrc = new File( testDir, "dependency-pom.xml" );
256 
257         System.out.println( "Copying dependency POM\nfrom: " + pomSrc + "\nto: " + pom );
258         FileUtils.copyFile( pomSrc, pom );
259     }
260 
261     private File getMetadataFile( Verifier verifier )
262         throws IOException
263     {
264         String gid = "org.apache.maven.its.mng3415";
265         String aid = "missing";
266         String version = "1.0-SNAPSHOT";
267         String name = "maven-metadata-testing-repo.xml";
268 
269         return new File( verifier.getArtifactMetadataPath( gid, aid, version, name ) );
270     }
271 
272     /**
273      * If the current maven version is < 3.0, we'll use the metadata file itself (old maven-artifact code)...
274      * otherwise, use the new resolver-status.properties file (new artifact code).
275      */
276     private File getUpdateCheckFile( Verifier verifier )
277     {
278         String gid = "org.apache.maven.its.mng3415";
279         String aid = "missing";
280         String version = "1.0-SNAPSHOT";
281         String name;
282 
283         // < 3.0 (including snapshots)
284         if ( matchesVersionRange( "(2.0.8,3.0-alpha-1)" ) )
285         {
286             name = "maven-metadata-testing-repo.xml";
287         }
288         else
289         {
290             name = "resolver-status.properties";
291         }
292 
293         return new File( verifier.getArtifactMetadataPath( gid, aid, version, name ) );
294     }
295 
296 }