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