1   package org.apache.maven.artifact.manager;
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.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.metadata.ArtifactMetadata;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
27  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
28  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
30  import org.apache.maven.wagon.ResourceDoesNotExistException;
31  import org.apache.maven.wagon.TransferFailedException;
32  import org.apache.maven.wagon.UnsupportedProtocolException;
33  import org.apache.maven.wagon.Wagon;
34  import org.apache.maven.wagon.providers.http.HttpWagon;
35  import org.apache.maven.wagon.repository.Repository;
36  import org.codehaus.plexus.PlexusTestCase;
37  import org.codehaus.plexus.util.FileUtils;
38  import org.codehaus.plexus.util.xml.Xpp3Dom;
39  
40  import java.io.File;
41  import java.io.IOException;
42  
43  import edu.umd.cs.mtc.MultithreadedTestCase;
44  import edu.umd.cs.mtc.TestFramework;
45  
46  /**
47   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
48   * @version $Id: DefaultWagonManagerTest.java 798691 2009-07-28 19:57:44Z brett $
49   */
50  public class DefaultWagonManagerTest
51      extends PlexusTestCase
52  {
53      private static final int NUM_EXECUTIONS = 1000;
54  
55      private static final String TEST_USER_AGENT = "Test-Agent/1.0";
56  
57      private WagonManager wagonManager;
58  
59      private ArtifactFactory artifactFactory;
60  
61      protected void setUp()
62          throws Exception
63      {
64          super.setUp();
65  
66          wagonManager = (WagonManager) lookup( WagonManager.ROLE );
67  
68          artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
69      }
70  
71      private Artifact createTestPomArtifact( String directory )
72          throws IOException
73      {
74          File testData = getTestFile( directory );
75          FileUtils.deleteDirectory( testData );
76          testData.mkdirs();
77  
78          Artifact artifact = artifactFactory.createProjectArtifact( "test", "test", "1.0" );
79          artifact.setFile( new File( testData, "test-1.0.pom" ) );
80          assertFalse( artifact.getFile().exists() );
81          return artifact;
82      }
83  
84      private Artifact createTestArtifact( String directory, String type )
85          throws IOException
86      {
87          File testData = getTestFile( directory );
88          FileUtils.deleteDirectory( testData );
89          testData.mkdirs();
90  
91          Artifact artifact = artifactFactory.createBuildArtifact( "test", "test", "1.0", type );
92          artifact.setFile( new File( testData, "test-1.0." + artifact.getArtifactHandler().getExtension() ) );
93          assertFalse( artifact.getFile().exists() );
94          return artifact;
95      }
96  
97      public void testAddMirrorWithNullRepositoryId()
98      {
99          wagonManager.addMirror( null, "test", "http://www.nowhere.com/" );
100     }
101 
102     public void testGetArtifactSha1MissingMd5Present()
103         throws IOException, UnsupportedProtocolException, TransferFailedException, ResourceDoesNotExistException
104     {
105         Artifact artifact = createTestPomArtifact( "target/test-data/get-remote-artifact" );
106 
107         ArtifactRepository repo = createStringRepo();
108 
109         StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" );
110         wagon.addExpectedContent( repo.getLayout().pathOf( artifact ), "expected" );
111         wagon.addExpectedContent( repo.getLayout().pathOf( artifact ) + ".md5", "bad_checksum" );
112 
113         wagonManager.getArtifact( artifact, repo );
114 
115         assertTrue( artifact.getFile().exists() );
116     }
117 
118     private ArtifactRepository createStringRepo()
119     {
120         ArtifactRepository repo =
121             new DefaultArtifactRepository( "id", "string://url", new ArtifactRepositoryLayoutStub() );
122         return repo;
123     }
124 
125     /**
126      * checks the handling of urls
127      */
128     public void testExternalURL()
129     {
130         DefaultWagonManager mgr = new DefaultWagonManager();
131         assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://somehost" ) ) );
132         assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) );
133         assertTrue( mgr.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) );
134         assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) );
135         assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://" ) ) );
136         // these are local
137         assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) );
138         assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) );
139         assertFalse( mgr.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) );
140         assertFalse( mgr.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) );
141         assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://localhost" ) ) );
142         assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) );
143         assertFalse( mgr.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) );
144         assertFalse( mgr.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) );
145 
146         // not a proper url so returns false;
147         assertFalse( mgr.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) );
148         assertFalse( mgr.isExternalRepo( getRepo( "foo", "" ) ) );
149     }
150 
151     /**
152      * Check that lookups with exact matches work and that no matches don't corrupt the repo.
153      */
154     public void testMirrorLookup()
155     {
156         wagonManager.addMirror( "a", "a", "http://a" );
157         wagonManager.addMirror( "b", "b", "http://b" );
158 
159         ArtifactRepository repo = null;
160         repo = wagonManager.getMirrorRepository( getRepo( "a", "http://a.a" ) );
161         assertEquals( "http://a", repo.getUrl() );
162 
163         repo = wagonManager.getMirrorRepository( getRepo( "b", "http://a.a" ) );
164         assertEquals( "http://b", repo.getUrl() );
165 
166         repo = wagonManager.getMirrorRepository( getRepo( "c", "http://c.c" ) );
167         assertEquals( "http://c.c", repo.getUrl() );
168 
169     }
170 
171     /**
172      * Check that wildcards don't override exact id matches.
173      */
174     public void testMirrorWildcardLookup()
175     {
176         wagonManager.addMirror( "a", "a", "http://a" );
177         wagonManager.addMirror( "b", "b", "http://b" );
178         wagonManager.addMirror( "c", "*", "http://wildcard" );
179 
180         ArtifactRepository repo = null;
181         repo = wagonManager.getMirrorRepository( getRepo( "a", "http://a.a" ) );
182         assertEquals( "http://a", repo.getUrl() );
183 
184         repo = wagonManager.getMirrorRepository( getRepo( "b", "http://a.a" ) );
185         assertEquals( "http://b", repo.getUrl() );
186 
187         repo = wagonManager.getMirrorRepository( getRepo( "c", "http://c.c" ) );
188         assertEquals( "http://wildcard", repo.getUrl() );
189 
190     }
191 
192     /**
193      * Check that patterns are processed correctly Valid patterns: * = everything external:* = everything not on the
194      * localhost and not file based. repo,repo1 = repo or repo1 *,!repo1 = everything except repo1
195      */
196     public void testPatterns()
197     {
198         DefaultWagonManager mgr = new DefaultWagonManager();
199 
200         assertTrue( mgr.matchPattern( getRepo( "a" ), "*" ) );
201         assertTrue( mgr.matchPattern( getRepo( "a" ), "*," ) );
202         assertTrue( mgr.matchPattern( getRepo( "a" ), ",*," ) );
203         assertTrue( mgr.matchPattern( getRepo( "a" ), "*," ) );
204 
205         assertTrue( mgr.matchPattern( getRepo( "a" ), "a" ) );
206         assertTrue( mgr.matchPattern( getRepo( "a" ), "a," ) );
207         assertTrue( mgr.matchPattern( getRepo( "a" ), ",a," ) );
208         assertTrue( mgr.matchPattern( getRepo( "a" ), "a," ) );
209 
210         assertFalse( mgr.matchPattern( getRepo( "b" ), "a" ) );
211         assertFalse( mgr.matchPattern( getRepo( "b" ), "a," ) );
212         assertFalse( mgr.matchPattern( getRepo( "b" ), ",a" ) );
213         assertFalse( mgr.matchPattern( getRepo( "b" ), ",a," ) );
214 
215         assertTrue( mgr.matchPattern( getRepo( "a" ), "a,b" ) );
216         assertTrue( mgr.matchPattern( getRepo( "b" ), "a,b" ) );
217 
218         assertFalse( mgr.matchPattern( getRepo( "c" ), "a,b" ) );
219 
220         assertTrue( mgr.matchPattern( getRepo( "a" ), "*" ) );
221         assertTrue( mgr.matchPattern( getRepo( "a" ), "*,b" ) );
222         assertTrue( mgr.matchPattern( getRepo( "a" ), "*,!b" ) );
223 
224         assertFalse( mgr.matchPattern( getRepo( "a" ), "*,!a" ) );
225         assertFalse( mgr.matchPattern( getRepo( "a" ), "!a,*" ) );
226 
227         assertTrue( mgr.matchPattern( getRepo( "c" ), "*,!a" ) );
228         assertTrue( mgr.matchPattern( getRepo( "c" ), "!a,*" ) );
229 
230         assertFalse( mgr.matchPattern( getRepo( "c" ), "!a,!c" ) );
231         assertFalse( mgr.matchPattern( getRepo( "d" ), "!a,!c*" ) );
232     }
233 
234     /**
235      * make sure the external if is fully exercised. We can assume file and ips are also handled because they have a
236      * separate test above.
237      */
238     public void testPatternsWithExternal()
239     {
240         DefaultWagonManager mgr = new DefaultWagonManager();
241 
242         assertTrue( mgr.matchPattern( getRepo( "a", "http://localhost" ), "*" ) );
243         assertFalse( mgr.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) );
244 
245         assertTrue( mgr.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) );
246         assertFalse( mgr.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) );
247         assertTrue( mgr.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) );
248         assertFalse( mgr.matchPattern( getRepo( "a", "http://localhost" ), "!a,external:*" ) );
249 
250         assertFalse( mgr.matchPattern( getRepo( "c", "http://localhost" ), "!a,external:*" ) );
251         assertTrue( mgr.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) );
252     }
253 
254     /**
255      * Check that first match wins
256      */
257     public void testMirrorStopOnFirstMatch()
258     {
259         // exact matches win first
260         wagonManager.addMirror( "a2", "a,b", "http://a2" );
261         wagonManager.addMirror( "a", "a", "http://a" );
262         // make sure repeated entries are skipped
263         wagonManager.addMirror( "a", "a", "http://a3" );
264 
265         wagonManager.addMirror( "b", "b", "http://b" );
266         wagonManager.addMirror( "c", "d,e", "http://de" );
267         wagonManager.addMirror( "c", "*", "http://wildcard" );
268         wagonManager.addMirror( "c", "e,f", "http://ef" );
269 
270         ArtifactRepository repo = null;
271         repo = wagonManager.getMirrorRepository( getRepo( "a", "http://a.a" ) );
272         assertEquals( "http://a", repo.getUrl() );
273 
274         repo = wagonManager.getMirrorRepository( getRepo( "b", "http://a.a" ) );
275         assertEquals( "http://b", repo.getUrl() );
276 
277         repo = wagonManager.getMirrorRepository( getRepo( "c", "http://c.c" ) );
278         assertEquals( "http://wildcard", repo.getUrl() );
279 
280         repo = wagonManager.getMirrorRepository( getRepo( "d", "http://d" ) );
281         assertEquals( "http://de", repo.getUrl() );
282 
283         repo = wagonManager.getMirrorRepository( getRepo( "e", "http://e" ) );
284         assertEquals( "http://de", repo.getUrl() );
285 
286         repo = wagonManager.getMirrorRepository( getRepo( "f", "http://f" ) );
287         assertEquals( "http://wildcard", repo.getUrl() );
288 
289     }
290 
291     /**
292      * Build an ArtifactRepository object.
293      * 
294      * @param id
295      * @param url
296      * @return
297      */
298     private ArtifactRepository getRepo( String id, String url )
299     {
300         return new DefaultArtifactRepository( id, url, new DefaultRepositoryLayout() );
301     }
302 
303     /**
304      * Build an ArtifactRepository object.
305      * 
306      * @param id
307      * @return
308      */
309     private ArtifactRepository getRepo( String id )
310     {
311         return getRepo( id, "http://something" );
312     }
313 
314     public void testDefaultWagonManager()
315         throws Exception
316     {
317         assertWagon( "a" );
318 
319         assertWagon( "b1" );
320 
321         assertWagon( "b2" );
322 
323         assertWagon( "c" );
324 
325         assertWagon( "string" );
326 
327         try
328         {
329             assertWagon( "d" );
330 
331             fail( "Expected :" + UnsupportedProtocolException.class.getName() );
332         }
333         catch ( UnsupportedProtocolException e )
334         {
335             // ok
336             assertTrue( true );
337         }
338     }
339 
340     public void testGetWagonRepository()
341         throws Exception
342     {
343         assertWagonRepository( "a" );
344 
345         assertWagonRepository( "b1" );
346 
347         assertWagonRepository( "b2" );
348 
349         assertWagonRepository( "c" );
350 
351         try
352         {
353             assertWagonRepository( "d" );
354 
355             fail( "Expected :" + UnsupportedProtocolException.class.getName() );
356         }
357         catch ( UnsupportedProtocolException e )
358         {
359             // ok
360             assertTrue( true );
361         }
362     }
363 
364     public void testGetWagonRepositoryNullProtocol()
365         throws Exception
366     {
367         try
368         {
369             Repository repository = new Repository();
370 
371             repository.setProtocol( null );
372 
373             Wagon wagon = wagonManager.getWagon( repository );
374 
375             fail( "Expected :" + UnsupportedProtocolException.class.getName() );
376         }
377         catch ( UnsupportedProtocolException e )
378         {
379             // ok
380             assertTrue( true );
381         }
382     }
383 
384     public void testGetWagonMultithreaded()
385         throws Throwable
386     {
387         DefaultWagonManager manager = (DefaultWagonManager) wagonManager;
388         manager.setHttpUserAgent( TEST_USER_AGENT );
389         assertNotNull( manager.getHttpUserAgent() );
390 
391         TestFramework.runOnce( new MultithreadedTestCase()
392         {
393             private Repository repository;
394 
395             public void initialize()
396             {
397                 repository = new Repository();
398                 repository.setProtocol( "http" );
399                 repository.setId( "server" );
400             }
401 
402             public void thread1()
403                 throws Exception
404             {
405                 for ( int i = 0; i < NUM_EXECUTIONS; i++ )
406                 {
407                     runThread();
408                 }
409             }
410 
411             public void thread2()
412                 throws Exception
413             {
414                 for ( int i = 0; i < NUM_EXECUTIONS; i++ )
415                 {
416                     runThread();
417                 }
418             }
419 
420             private void runThread()
421                 throws Exception
422             {
423                 HttpWagon wagon = (HttpWagon) wagonManager.getWagon( repository );
424                 assertEquals( TEST_USER_AGENT, wagon.getHttpHeaders().getProperty( "User-Agent" ) );
425                 container.release( wagon );
426             }
427         } );
428     }
429 
430     /**
431      * Checks the verification of checksums.
432      */
433     public void testChecksumVerification()
434         throws Exception
435     {
436         ArtifactRepositoryPolicy policy =
437             new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
438                                           ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
439         ArtifactRepository repo =
440             new DefaultArtifactRepository( "id", "string://url", new ArtifactRepositoryLayoutStub(), policy, policy );
441 
442         Artifact artifact = createTestArtifact( "target/test-data/sample-art", "jar" );
443 
444         StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" );
445 
446         artifact.getFile().delete();
447         wagon.clearExpectedContent();
448         wagon.addExpectedContent( "path", "lower-case-checksum" );
449         wagon.addExpectedContent( "path.sha1", "2a25dc564a3b34f68237fc849066cbc7bb7a36a1" );
450 
451         try
452         {
453             wagonManager.getArtifact( artifact, repo );
454         }
455         catch ( ChecksumFailedException e )
456         {
457             fail( "Checksum verification did not pass: " + e.getMessage() );
458         }
459 
460         artifact.getFile().delete();
461         wagon.clearExpectedContent();
462         wagon.addExpectedContent( "path", "upper-case-checksum" );
463         wagon.addExpectedContent( "path.sha1", "B7BB97D7D0B9244398D9B47296907F73313663E6" );
464 
465         try
466         {
467             wagonManager.getArtifact( artifact, repo );
468         }
469         catch ( ChecksumFailedException e )
470         {
471             fail( "Checksum verification did not pass: " + e.getMessage() );
472         }
473 
474         artifact.getFile().delete();
475         wagon.clearExpectedContent();
476         wagon.addExpectedContent( "path", "expected-failure" );
477         wagon.addExpectedContent( "path.sha1", "b7bb97d7d0b9244398d9b47296907f73313663e6" );
478 
479         try
480         {
481             wagonManager.getArtifact( artifact, repo );
482             fail( "Checksum verification did not fail" );
483         }
484         catch ( ChecksumFailedException e )
485         {
486             // expected
487         }
488 
489         artifact.getFile().delete();
490         wagon.clearExpectedContent();
491         wagon.addExpectedContent( "path", "lower-case-checksum" );
492         wagon.addExpectedContent( "path.md5", "50b2cf50a103a965efac62b983035cac" );
493 
494         try
495         {
496             wagonManager.getArtifact( artifact, repo );
497         }
498         catch ( ChecksumFailedException e )
499         {
500             fail( "Checksum verification did not pass: " + e.getMessage() );
501         }
502 
503         artifact.getFile().delete();
504         wagon.clearExpectedContent();
505         wagon.addExpectedContent( "path", "upper-case-checksum" );
506         wagon.addExpectedContent( "path.md5", "842F568FCCFEB7E534DC72133D42FFDC" );
507 
508         try
509         {
510             wagonManager.getArtifact( artifact, repo );
511         }
512         catch ( ChecksumFailedException e )
513         {
514             fail( "Checksum verification did not pass: " + e.getMessage() );
515         }
516 
517         artifact.getFile().delete();
518         wagon.clearExpectedContent();
519         wagon.addExpectedContent( "path", "expected-failure" );
520         wagon.addExpectedContent( "path.md5", "b7bb97d7d0b9244398d9b47296907f73313663e6" );
521 
522         try
523         {
524             wagonManager.getArtifact( artifact, repo );
525             fail( "Checksum verification did not fail" );
526         }
527         catch ( ChecksumFailedException e )
528         {
529             // expected
530         }
531     }
532 
533     private void assertWagon( String protocol )
534         throws Exception
535     {
536         Wagon wagon = wagonManager.getWagon( protocol );
537 
538         assertNotNull( "Check wagon, protocol=" + protocol, wagon );
539     }
540 
541     private void assertWagonRepository( String protocol )
542         throws Exception
543     {
544         Repository repository = new Repository();
545 
546         String s = "value=" + protocol;
547 
548         repository.setId( "id=" + protocol );
549 
550         repository.setProtocol( protocol );
551 
552         Xpp3Dom conf = new Xpp3Dom( "configuration" );
553 
554         Xpp3Dom configurableField = new Xpp3Dom( "configurableField" );
555 
556         configurableField.setValue( s );
557 
558         conf.addChild( configurableField );
559 
560         wagonManager.addConfiguration( repository.getId(), conf );
561 
562         WagonMock wagon = (WagonMock) wagonManager.getWagon( repository );
563 
564         assertNotNull( "Check wagon, protocol=" + protocol, wagon );
565 
566         assertEquals( "Check configuration for wagon, protocol=" + protocol, s, wagon.getConfigurableField() );
567     }
568 
569     public void testWagonWithImplHint()
570         throws Exception
571     {
572         Repository repository = new Repository();
573 
574         repository.setId( "id" );
575 
576         repository.setProtocol( "a" );
577 
578         Xpp3Dom conf = new Xpp3Dom( "configuration" );
579 
580         Xpp3Dom configurableField = new Xpp3Dom( "wagonProvider" );
581 
582         configurableField.setValue( "foo" );
583 
584         conf.addChild( configurableField );
585 
586         wagonManager.addConfiguration( repository.getId(), conf );
587 
588         WagonMock wagon = (WagonMock) wagonManager.getWagon( repository );
589 
590         assertNotNull( "Check wagon", wagon );
591 
592         assertEquals( "Check configuration for wagon", WagonAWithImplementationHint.class, wagon.getClass() );
593 
594         // try again to check configuration alterations don't modify behaviour
595         wagon = (WagonMock) wagonManager.getWagon( repository );
596 
597         assertNotNull( "Check wagon", wagon );
598 
599         assertEquals( "Check configuration for wagon", WagonAWithImplementationHint.class, wagon.getClass() );
600     }
601 
602     private final class ArtifactRepositoryLayoutStub
603         implements ArtifactRepositoryLayout
604     {
605         public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
606         {
607             return "path";
608         }
609 
610         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
611         {
612             return "path";
613         }
614 
615         public String pathOf( Artifact artifact )
616         {
617             return "path";
618         }
619     }
620 
621 }