1   package org.apache.maven.repository.legacy;
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 java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.DefaultArtifact;
29  import org.apache.maven.artifact.factory.ArtifactFactory;
30  import org.apache.maven.artifact.metadata.ArtifactMetadata;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
33  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
34  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
35  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
36  import org.apache.maven.artifact.versioning.VersionRange;
37  import org.apache.maven.wagon.ResourceDoesNotExistException;
38  import org.apache.maven.wagon.TransferFailedException;
39  import org.apache.maven.wagon.UnsupportedProtocolException;
40  import org.apache.maven.wagon.Wagon;
41  import org.apache.maven.wagon.authorization.AuthorizationException;
42  import org.apache.maven.wagon.events.TransferEvent;
43  import org.apache.maven.wagon.events.TransferListener;
44  import org.apache.maven.wagon.observers.AbstractTransferListener;
45  import org.apache.maven.wagon.observers.Debug;
46  import org.codehaus.plexus.PlexusTestCase;
47  import org.codehaus.plexus.util.FileUtils;
48  import org.easymock.MockControl;
49  
50  /**
51   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
52   * @version $Id: DefaultWagonManagerTest.java 888488 2009-12-08 17:12:02Z bentmann $
53   */
54  public class DefaultWagonManagerTest
55      extends PlexusTestCase
56  {
57      private DefaultWagonManager wagonManager;
58  
59      private TransferListener transferListener = new Debug();
60  
61      private ArtifactFactory artifactFactory;
62  
63      private ArtifactRepositoryFactory artifactRepositoryFactory;
64  
65      protected void setUp()
66          throws Exception
67      {
68          super.setUp();
69          wagonManager = (DefaultWagonManager) lookup( WagonManager.class );
70          artifactFactory = lookup( ArtifactFactory.class );
71          artifactRepositoryFactory = lookup( ArtifactRepositoryFactory.class );
72      }
73  
74      @Override
75      protected void tearDown()
76          throws Exception
77      {
78          wagonManager = null;
79          artifactFactory = null;
80          super.tearDown();
81      }
82  
83      public void testUnnecessaryRepositoryLookup()
84          throws Exception
85      {
86          Artifact artifact = createTestPomArtifact( "target/test-data/get-missing-pom" );
87  
88          List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>();
89          repos.add( artifactRepositoryFactory.createArtifactRepository( "repo1", "string://url1",
90                                                                         new ArtifactRepositoryLayoutStub(), null, null ) );
91          repos.add( artifactRepositoryFactory.createArtifactRepository( "repo2", "string://url2",
92                                                                         new ArtifactRepositoryLayoutStub(), null, null ) );
93  
94          StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" );
95          wagon.addExpectedContent( repos.get( 0 ).getLayout().pathOf( artifact ), "expected" );
96          wagon.addExpectedContent( repos.get( 1 ).getLayout().pathOf( artifact ), "expected" );
97  
98          class TransferListener
99              extends AbstractTransferListener
100         {
101             public List<TransferEvent> events = new ArrayList<TransferEvent>();
102 
103             @Override
104             public void transferInitiated( TransferEvent transferEvent )
105             {
106                 events.add( transferEvent );
107             }
108         }
109 
110         TransferListener listener = new TransferListener();
111         wagonManager.getArtifact( artifact, repos, listener, false );
112         assertEquals( 1, listener.events.size() );
113     }
114 
115     public void testGetMissingJar() throws TransferFailedException, UnsupportedProtocolException, IOException
116     {
117         Artifact artifact = createTestArtifact( "target/test-data/get-missing-jar", "jar" );
118 
119         ArtifactRepository repo = createStringRepo();
120 
121         try
122         {
123             wagonManager.getArtifact( artifact, repo, null, false );
124 
125             fail();
126         }
127         catch ( ResourceDoesNotExistException e )
128         {
129             assertTrue( true );
130         }
131 
132         assertFalse( artifact.getFile().exists() );
133     }
134 
135     public void testGetMissingJarForced() throws TransferFailedException, UnsupportedProtocolException, IOException
136     {
137         Artifact artifact = createTestArtifact( "target/test-data/get-missing-jar", "jar" );
138 
139         ArtifactRepository repo = createStringRepo();
140 
141         try
142         {
143             wagonManager.getArtifact( artifact, repo, null, false );
144 
145             fail();
146         }
147         catch ( ResourceDoesNotExistException e )
148         {
149             assertTrue( true );
150         }
151 
152         assertFalse( artifact.getFile().exists() );
153     }
154 
155     public void testGetRemoteJar()
156         throws TransferFailedException, ResourceDoesNotExistException, UnsupportedProtocolException, IOException,
157         AuthorizationException
158     {
159         Artifact artifact = createTestArtifact( "target/test-data/get-remote-jar", "jar" );
160 
161         ArtifactRepository repo = createStringRepo();
162 
163         StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" );
164         wagon.addExpectedContent( repo.getLayout().pathOf( artifact ), "expected" );
165 
166         MockControl control = MockControl.createControl( UpdateCheckManager.class );
167         control.replay();
168 
169         wagonManager.getArtifact( artifact, repo, null, false );
170 
171         assertTrue( artifact.getFile().exists() );
172         assertEquals( "expected", FileUtils.fileRead( artifact.getFile(), "UTF-8" ) );
173 
174         control.verify();
175     }
176 
177     private Artifact createTestPomArtifact( String directory )
178         throws IOException
179     {
180         File testData = getTestFile( directory );
181         FileUtils.deleteDirectory( testData );
182         testData.mkdirs();
183 
184         Artifact artifact = artifactFactory.createProjectArtifact( "test", "test", "1.0" );
185         artifact.setFile( new File( testData, "test-1.0.pom" ) );
186         assertFalse( artifact.getFile().exists() );
187         return artifact;
188     }
189 
190     private Artifact createTestArtifact( String directory, String type )
191         throws IOException
192     {
193         return createTestArtifact( directory, "1.0", type );
194     }
195 
196     private Artifact createTestArtifact( String directory, String version, String type )
197         throws IOException
198     {
199         File testData = getTestFile( directory );
200         FileUtils.deleteDirectory( testData );
201         testData.mkdirs();
202 
203         Artifact artifact = artifactFactory.createBuildArtifact( "test", "test", version, type );
204         artifact.setFile( new File( testData, "test-" + version + "." + artifact.getArtifactHandler().getExtension() ) );
205         assertFalse( artifact.getFile().exists() );
206         return artifact;
207     }
208 
209     private ArtifactRepository createStringRepo()
210     {
211         return artifactRepositoryFactory.createArtifactRepository( "id", "string://url", new ArtifactRepositoryLayoutStub(), null, null );
212     }
213 
214     /**
215      * Build an ArtifactRepository object.
216      *
217      * @param id
218      * @param url
219      * @return
220      */
221     private ArtifactRepository getRepo( String id, String url )
222     {
223         return artifactRepositoryFactory.createArtifactRepository( id, url, new DefaultRepositoryLayout(), null, null );
224     }
225 
226     /**
227      * Build an ArtifactRepository object.
228      *
229      * @param id
230      * @return
231      */
232     private ArtifactRepository getRepo( String id )
233     {
234         return getRepo( id, "http://something" );
235     }
236 
237     public void testDefaultWagonManager()
238         throws Exception
239     {
240         assertWagon( "a" );
241 
242         assertWagon( "b" );
243 
244         assertWagon( "c" );
245 
246         assertWagon( "string" );
247 
248         try
249         {
250             assertWagon( "d" );
251 
252             fail( "Expected :" + UnsupportedProtocolException.class.getName() );
253         }
254         catch ( UnsupportedProtocolException e )
255         {
256             // ok
257             assertTrue( true );
258         }
259     }
260 
261     /**
262      * Check that transfer listeners are properly removed after getArtifact and putArtifact
263      */
264     public void testWagonTransferListenerRemovedAfterGetArtifactAndPutArtifact()
265         throws Exception
266     {
267         Artifact artifact = createTestArtifact( "target/test-data/transfer-listener", "jar" );
268         ArtifactRepository repo = createStringRepo();
269         StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" );
270         wagon.addExpectedContent( repo.getLayout().pathOf( artifact ), "expected" );
271 
272         /* getArtifact */
273         assertFalse( "Transfer listener is registered before test",
274                      wagon.getTransferEventSupport().hasTransferListener( transferListener ) );
275         wagonManager.getArtifact( artifact, repo, transferListener, false );
276         assertFalse( "Transfer listener still registered after getArtifact",
277                      wagon.getTransferEventSupport().hasTransferListener( transferListener ) );
278 
279         /* putArtifact */
280         File sampleFile = getTestFile( "target/test-file" );
281         FileUtils.fileWrite( sampleFile.getAbsolutePath(), "sample file" );
282 
283         assertFalse( "Transfer listener is registered before test", wagon.getTransferEventSupport().hasTransferListener( transferListener ) );
284         wagonManager.putArtifact( sampleFile, artifact, repo, transferListener );
285         assertFalse( "Transfer listener still registered after putArtifact", wagon.getTransferEventSupport().hasTransferListener( transferListener ) );
286     }
287 
288     /**
289      * Checks the verification of checksums.
290      */
291     public void xtestChecksumVerification()
292         throws Exception
293     {
294         ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL );
295 
296         ArtifactRepository repo = artifactRepositoryFactory.createArtifactRepository( "id", "string://url", new ArtifactRepositoryLayoutStub(), policy, policy );
297 
298         Artifact artifact =
299             new DefaultArtifact( "sample.group", "sample-art", VersionRange.createFromVersion( "1.0" ), "scope",
300                                  "jar", "classifier", null );
301         artifact.setFile( getTestFile( "target/sample-art" ) );
302 
303         StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" );
304 
305         wagon.clearExpectedContent();
306         wagon.addExpectedContent( "path", "lower-case-checksum" );
307         wagon.addExpectedContent( "path.sha1", "2a25dc564a3b34f68237fc849066cbc7bb7a36a1" );
308 
309         try
310         {
311             wagonManager.getArtifact( artifact, repo, null, false );
312         }
313         catch ( ChecksumFailedException e )
314         {
315             fail( "Checksum verification did not pass: " + e.getMessage() );
316         }
317 
318         wagon.clearExpectedContent();
319         wagon.addExpectedContent( "path", "upper-case-checksum" );
320         wagon.addExpectedContent( "path.sha1", "B7BB97D7D0B9244398D9B47296907F73313663E6" );
321 
322         try
323         {
324             wagonManager.getArtifact( artifact, repo, null, false );
325         }
326         catch ( ChecksumFailedException e )
327         {
328             fail( "Checksum verification did not pass: " + e.getMessage() );
329         }
330 
331         wagon.clearExpectedContent();
332         wagon.addExpectedContent( "path", "expected-failure" );
333         wagon.addExpectedContent( "path.sha1", "b7bb97d7d0b9244398d9b47296907f73313663e6" );
334 
335         try
336         {
337             wagonManager.getArtifact( artifact, repo, null, false );
338             fail( "Checksum verification did not fail" );
339         }
340         catch ( ChecksumFailedException e )
341         {
342             // expected
343         }
344 
345         wagon.clearExpectedContent();
346         wagon.addExpectedContent( "path", "lower-case-checksum" );
347         wagon.addExpectedContent( "path.md5", "50b2cf50a103a965efac62b983035cac" );
348 
349         try
350         {
351             wagonManager.getArtifact( artifact, repo, null, false );
352         }
353         catch ( ChecksumFailedException e )
354         {
355             fail( "Checksum verification did not pass: " + e.getMessage() );
356         }
357 
358         wagon.clearExpectedContent();
359         wagon.addExpectedContent( "path", "upper-case-checksum" );
360         wagon.addExpectedContent( "path.md5", "842F568FCCFEB7E534DC72133D42FFDC" );
361 
362         try
363         {
364             wagonManager.getArtifact( artifact, repo, null, false );
365         }
366         catch ( ChecksumFailedException e )
367         {
368             fail( "Checksum verification did not pass: " + e.getMessage() );
369         }
370 
371         wagon.clearExpectedContent();
372         wagon.addExpectedContent( "path", "expected-failure" );
373         wagon.addExpectedContent( "path.md5", "b7bb97d7d0b9244398d9b47296907f73313663e6" );
374 
375         try
376         {
377             wagonManager.getArtifact( artifact, repo, null, false );
378             fail( "Checksum verification did not fail" );
379         }
380         catch ( ChecksumFailedException e )
381         {
382             // expected
383         }
384     }
385 
386     public void testPerLookupInstantiation()
387         throws Exception
388     {
389         String protocol = "perlookup";
390 
391         Wagon one = wagonManager.getWagon( protocol );
392         Wagon two = wagonManager.getWagon( protocol );
393 
394         assertNotSame( one, two );
395     }
396 
397     private void assertWagon( String protocol )
398         throws Exception
399     {
400         Wagon wagon = wagonManager.getWagon( protocol );
401 
402         assertNotNull( "Check wagon, protocol=" + protocol, wagon );
403     }
404 
405     private final class ArtifactRepositoryLayoutStub
406         implements ArtifactRepositoryLayout
407     {
408         public String getId()
409         {
410             return "test";
411         }
412 
413         public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
414         {
415             return "path";
416         }
417 
418         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
419         {
420             return "path";
421         }
422 
423         public String pathOf( Artifact artifact )
424         {
425             return "path";
426         }
427     }
428 
429 }