001 package org.apache.maven.repository.legacy;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.io.File;
023 import java.io.IOException;
024 import java.util.ArrayList;
025 import java.util.List;
026
027 import org.apache.maven.artifact.Artifact;
028 import org.apache.maven.artifact.DefaultArtifact;
029 import org.apache.maven.artifact.factory.ArtifactFactory;
030 import org.apache.maven.artifact.metadata.ArtifactMetadata;
031 import org.apache.maven.artifact.repository.ArtifactRepository;
032 import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
033 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
034 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
035 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
036 import org.apache.maven.artifact.versioning.VersionRange;
037 import org.apache.maven.wagon.ResourceDoesNotExistException;
038 import org.apache.maven.wagon.TransferFailedException;
039 import org.apache.maven.wagon.UnsupportedProtocolException;
040 import org.apache.maven.wagon.Wagon;
041 import org.apache.maven.wagon.authorization.AuthorizationException;
042 import org.apache.maven.wagon.events.TransferEvent;
043 import org.apache.maven.wagon.events.TransferListener;
044 import org.apache.maven.wagon.observers.AbstractTransferListener;
045 import org.apache.maven.wagon.observers.Debug;
046 import org.codehaus.plexus.PlexusTestCase;
047 import org.codehaus.plexus.util.FileUtils;
048 import org.easymock.MockControl;
049
050 /**
051 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
052 */
053 public class DefaultWagonManagerTest
054 extends PlexusTestCase
055 {
056 private DefaultWagonManager wagonManager;
057
058 private TransferListener transferListener = new Debug();
059
060 private ArtifactFactory artifactFactory;
061
062 private ArtifactRepositoryFactory artifactRepositoryFactory;
063
064 protected void setUp()
065 throws Exception
066 {
067 super.setUp();
068 wagonManager = (DefaultWagonManager) lookup( WagonManager.class );
069 artifactFactory = lookup( ArtifactFactory.class );
070 artifactRepositoryFactory = lookup( ArtifactRepositoryFactory.class );
071 }
072
073 @Override
074 protected void tearDown()
075 throws Exception
076 {
077 wagonManager = null;
078 artifactFactory = null;
079 super.tearDown();
080 }
081
082 public void testUnnecessaryRepositoryLookup()
083 throws Exception
084 {
085 Artifact artifact = createTestPomArtifact( "target/test-data/get-missing-pom" );
086
087 List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>();
088 repos.add( artifactRepositoryFactory.createArtifactRepository( "repo1", "string://url1",
089 new ArtifactRepositoryLayoutStub(), null, null ) );
090 repos.add( artifactRepositoryFactory.createArtifactRepository( "repo2", "string://url2",
091 new ArtifactRepositoryLayoutStub(), null, null ) );
092
093 StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" );
094 wagon.addExpectedContent( repos.get( 0 ).getLayout().pathOf( artifact ), "expected" );
095 wagon.addExpectedContent( repos.get( 1 ).getLayout().pathOf( artifact ), "expected" );
096
097 class TransferListener
098 extends AbstractTransferListener
099 {
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 * Build an ArtifactRepository object.
215 *
216 * @param id
217 * @param url
218 * @return
219 */
220 private ArtifactRepository getRepo( String id, String url )
221 {
222 return artifactRepositoryFactory.createArtifactRepository( id, url, new DefaultRepositoryLayout(), null, null );
223 }
224
225 /**
226 * Build an ArtifactRepository object.
227 *
228 * @param id
229 * @return
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 // ok
256 assertTrue( true );
257 }
258 }
259
260 /**
261 * Check that transfer listeners are properly removed after getArtifact and putArtifact
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 /* getArtifact */
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 /* putArtifact */
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 * Checks the verification of checksums.
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 // expected
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 // expected
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 }