1 package org.apache.maven.archiva.discoverer;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.archiva.discoverer.filter.AcceptAllArtifactFilter;
20 import org.apache.maven.archiva.discoverer.filter.SnapshotArtifactFilter;
21 import org.apache.maven.artifact.Artifact;
22 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
34 * Test the default artifact discoverer.
36 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37 * @version $Id:DefaultArtifactDiscovererTest.java 437105 2006-08-26 17:22:22 +1000 (Sat, 26 Aug 2006) brett $
39 public class DefaultArtifactDiscovererTest
40 extends AbstractArtifactDiscovererTest
42 private static final List JAVAX_BLACKLIST = Collections.singletonList( "javax/**" );
44 protected String getLayout()
49 protected File getRepositoryFile()
51 return getTestFile( "src/test/repository" );
54 public void testDefaultExcludes()
55 throws DiscovererException
57 List artifacts = discoverArtifacts();
58 assertNotNull( "Check artifacts not null", artifacts );
59 boolean found = false;
60 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
62 DiscovererPath dPath = (DiscovererPath) i.next();
64 String path = dPath.getPath();
66 boolean b = path.indexOf( "CVS" ) >= 0;
70 assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
73 assertTrue( "Check exclusion was found", found );
75 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
77 Artifact a = (Artifact) i.next();
78 assertFalse( "Check not CVS", a.getFile().getPath().indexOf( "CVS" ) >= 0 );
79 assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
83 public void testStandardExcludes()
84 throws DiscovererException
86 List artifacts = discoverArtifacts();
87 assertNotNull( "Check artifacts not null", artifacts );
88 boolean found = false;
89 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
91 DiscovererPath dPath = (DiscovererPath) i.next();
93 String path = dPath.getPath();
95 if ( "KEYS".equals( path ) )
98 assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
101 assertTrue( "Check exclusion was found", found );
103 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
105 Artifact a = (Artifact) i.next();
106 assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
110 public void testBlacklistedExclude()
111 throws DiscovererException
113 List artifacts = discoverArtifactsWithBlacklist( JAVAX_BLACKLIST );
114 assertNotNull( "Check artifacts not null", artifacts );
115 boolean found = false;
116 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
118 DiscovererPath dPath = (DiscovererPath) i.next();
120 String path = dPath.getPath();
122 if ( "javax/sql/jdbc/2.0/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) ) )
125 assertEquals( "Check comment is about blacklisting", "Artifact was in the specified list of exclusions",
126 dPath.getComment() );
129 assertTrue( "Check exclusion was found", found );
131 assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
134 public void testKickoutWithShortPath()
135 throws DiscovererException
137 List artifacts = discoverArtifacts();
138 assertNotNull( "Check artifacts not null", artifacts );
139 boolean found = false;
140 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
142 DiscovererPath dPath = (DiscovererPath) i.next();
144 String path = dPath.getPath();
146 if ( "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
149 assertEquals( "Check reason for kickout", "Path is too short to build an artifact from",
150 dPath.getComment() );
154 assertTrue( "Check kickout was found", found );
156 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
158 Artifact a = (Artifact) i.next();
159 assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
163 public void testKickoutWithWrongArtifactId()
164 throws DiscovererException
166 List artifacts = discoverArtifacts();
167 assertNotNull( "Check artifacts not null", artifacts );
168 boolean found = false;
169 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
171 DiscovererPath dPath = (DiscovererPath) i.next();
173 String path = dPath.getPath();
175 if ( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar".equals(
176 path.replace( '\\', '/' ) ) )
179 assertEquals( "Check reason for kickout", "Path filename does not correspond to an artifact",
180 dPath.getComment() );
183 assertTrue( "Check kickout was found", found );
185 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
187 Artifact a = (Artifact) i.next();
188 assertFalse( "Check not wrong jar",
189 "wrong-artifactId-1.0-20050611.112233-1.jar".equals( a.getFile().getName() ) );
193 public void testKickoutWithNoType()
194 throws DiscovererException
196 List artifacts = discoverArtifacts();
197 assertNotNull( "Check artifacts not null", artifacts );
198 boolean found = false;
199 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
201 DiscovererPath dPath = (DiscovererPath) i.next();
203 String path = dPath.getPath();
205 if ( "invalid/invalid/1/invalid-1".equals( path.replace( '\\', '/' ) ) )
208 assertEquals( "Check reason for kickout", "Path filename does not have an extension",
209 dPath.getComment() );
212 assertTrue( "Check kickout was found", found );
214 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
216 Artifact a = (Artifact) i.next();
217 assertFalse( "Check not 'invalid-1'", "invalid-1".equals( a.getFile().getName() ) );
221 public void testKickoutWithWrongVersion()
222 throws DiscovererException
224 List artifacts = discoverArtifacts();
225 assertNotNull( "Check artifacts not null", artifacts );
226 boolean found = false;
227 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
229 DiscovererPath dPath = (DiscovererPath) i.next();
231 String path = dPath.getPath();
233 if ( "invalid/invalid/1.0/invalid-2.0.jar".equals( path.replace( '\\', '/' ) ) )
236 assertEquals( "Check reason for kickout", "Built artifact version does not match path version",
237 dPath.getComment() );
240 assertTrue( "Check kickout was found", found );
242 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
244 Artifact a = (Artifact) i.next();
245 assertFalse( "Check not 'invalid-2.0.jar'", "invalid-2.0.jar".equals( a.getFile().getName() ) );
249 public void testKickoutWithLongerVersion()
250 throws DiscovererException
252 List artifacts = discoverArtifacts();
253 assertNotNull( "Check artifacts not null", artifacts );
254 boolean found = false;
255 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
257 DiscovererPath dPath = (DiscovererPath) i.next();
259 String path = dPath.getPath();
261 if ( "invalid/invalid/1.0/invalid-1.0b.jar".equals( path.replace( '\\', '/' ) ) )
264 assertEquals( "Check reason for kickout", "Path version does not corresspond to an artifact version",
265 dPath.getComment() );
268 assertTrue( "Check kickout was found", found );
270 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
272 Artifact a = (Artifact) i.next();
273 assertFalse( "Check not 'invalid-1.0b.jar'", "invalid-1.0b.jar".equals( a.getFile().getName() ) );
277 public void testKickoutWithWrongSnapshotVersion()
278 throws DiscovererException
280 List artifacts = discoverArtifacts();
281 assertNotNull( "Check artifacts not null", artifacts );
282 boolean found = false;
283 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
285 DiscovererPath dPath = (DiscovererPath) i.next();
287 String path = dPath.getPath();
289 if ( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
292 assertEquals( "Check reason for kickout",
293 "Failed to create a snapshot artifact: invalid:invalid:jar:1.0:runtime",
294 dPath.getComment() );
297 assertTrue( "Check kickout was found", found );
299 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
301 Artifact a = (Artifact) i.next();
302 assertFalse( "Check not 'invalid-1.0.jar'", "invalid-1.0.jar".equals( a.getFile().getName() ) );
306 public void testKickoutWithSnapshotBaseVersion()
307 throws DiscovererException
309 List artifacts = discoverArtifacts();
310 assertNotNull( "Check artifacts not null", artifacts );
311 boolean found = false;
312 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
314 DiscovererPath dPath = (DiscovererPath) i.next();
316 String path = dPath.getPath();
318 if ( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar".equals(
319 path.replace( '\\', '/' ) ) )
322 assertEquals( "Check reason for kickout",
323 "Built snapshot artifact base version does not match path version: invalid:invalid:jar:1.0-SNAPSHOT:runtime; should have been version: 1.0-20050611.123456-1",
324 dPath.getComment() );
327 assertTrue( "Check kickout was found", found );
329 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
331 Artifact a = (Artifact) i.next();
332 assertFalse( "Check not 'invalid-1.0-20050611-123456-1.jar'",
333 "invalid-1.0-20050611.123456-1.jar".equals( a.getFile().getName() ) );
337 public void testInclusion()
338 throws DiscovererException
340 List artifacts = discoverArtifactsWithSnapshots();
341 assertNotNull( "Check artifacts not null", artifacts );
343 assertTrue( "Check normal included",
344 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
347 public void testArtifactWithClassifier()
348 throws DiscovererException
350 List artifacts = discoverArtifactsWithSnapshots();
351 assertNotNull( "Check artifacts not null", artifacts );
353 assertTrue( "Check normal included",
354 artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
357 public void testJavaSourcesInclusion()
358 throws DiscovererException
360 List artifacts = discoverArtifactsWithSnapshots();
361 assertNotNull( "Check artifacts not null", artifacts );
363 assertTrue( "Check normal included", artifacts.contains(
364 createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
367 public void testTestSourcesInclusion()
368 throws DiscovererException
370 List artifacts = discoverArtifactsWithSnapshots();
371 assertNotNull( "Check artifacts not null", artifacts );
373 assertTrue( "Check normal included", artifacts.contains(
374 createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "test-sources" ) ) );
377 public void testDistributionInclusion()
378 throws DiscovererException
380 List artifacts = discoverArtifactsWithSnapshots();
381 assertNotNull( "Check artifacts not null", artifacts );
383 assertTrue( "Check zip included",
384 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
386 assertTrue( "Check tar.gz included",
387 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
390 public void testSnapshotInclusion()
391 throws DiscovererException
393 List artifacts = discoverArtifactsWithSnapshots();
394 assertNotNull( "Check artifacts not null", artifacts );
396 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
397 assertTrue( "Check snapshot included",
398 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ) ) );
401 public void testSnapshotInclusionWithClassifier()
402 throws DiscovererException
404 List artifacts = discoverArtifactsWithSnapshots();
405 assertNotNull( "Check artifacts not null", artifacts );
407 assertTrue( "Check snapshot included", artifacts.contains(
408 createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ) ) );
411 public void testSnapshotExclusion()
412 throws DiscovererException
414 List artifacts = discoverArtifacts();
415 assertNotNull( "Check artifacts not null", artifacts );
417 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
418 assertFalse( "Check snapshot included",
419 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
422 public void testFileSet()
423 throws DiscovererException
425 List artifacts = discoverArtifactsWithSnapshots();
426 assertNotNull( "Check artifacts not null", artifacts );
428 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
430 Artifact artifact = (Artifact) i.next();
431 assertNotNull( "Check file is set", artifact.getFile() );
435 public void testRepositorySet()
436 throws MalformedURLException, DiscovererException
438 List artifacts = discoverArtifactsWithSnapshots();
439 assertNotNull( "Check artifacts not null", artifacts );
441 String url = repository.getUrl();
442 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
444 Artifact artifact = (Artifact) i.next();
445 assertNotNull( "Check repository set", artifact.getRepository() );
446 assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
450 public void testStandalonePoms()
451 throws DiscovererException
453 List artifacts = discoverArtifacts();
455 // cull down to actual artifacts (only standalone poms will have type = pom)
456 Map keyedArtifacts = new HashMap();
457 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
459 Artifact a = (Artifact) i.next();
460 String key = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion();
461 if ( !"pom".equals( a.getType() ) || !keyedArtifacts.containsKey( key ) )
463 keyedArtifacts.put( key, a );
467 List models = new ArrayList();
469 for ( Iterator i = keyedArtifacts.values().iterator(); i.hasNext(); )
471 Artifact a = (Artifact) i.next();
473 if ( "pom".equals( a.getType() ) )
479 assertEquals( 4, models.size() );
481 // Define order we expect
482 Collections.sort( models );
484 Iterator itr = models.iterator();
485 Artifact model = (Artifact) itr.next();
486 assertEquals( "org.apache.maven", model.getGroupId() );
487 assertEquals( "B", model.getArtifactId() );
488 assertEquals( "1.0", model.getVersion() );
489 model = (Artifact) itr.next();
490 assertEquals( "org.apache.maven", model.getGroupId() );
491 assertEquals( "B", model.getArtifactId() );
492 assertEquals( "2.0", model.getVersion() );
493 model = (Artifact) itr.next();
494 assertEquals( "org.apache.maven", model.getGroupId() );
495 assertEquals( "discovery", model.getArtifactId() );
496 assertEquals( "1.0", model.getVersion() );
497 model = (Artifact) itr.next();
498 assertEquals( "org.apache.testgroup", model.getGroupId() );
499 assertEquals( "discovery", model.getArtifactId() );
500 assertEquals( "1.0", model.getVersion() );
503 public void testShortPath()
504 throws ComponentLookupException
508 discoverer.buildArtifact( "invalid/invalid-1.0.jar" );
510 fail( "Artifact should be null for short paths" );
512 catch ( DiscovererException e )
518 public void testWrongArtifactId()
519 throws ComponentLookupException
524 discoverer.buildArtifact( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
526 fail( "Artifact should be null for wrong ArtifactId" );
528 catch ( DiscovererException e )
534 public void testNoType()
535 throws ComponentLookupException
539 discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
541 fail( "Artifact should be null for no type" );
543 catch ( DiscovererException e )
549 public void testWrongVersion()
550 throws ComponentLookupException
554 discoverer.buildArtifact( "invalid/invalid/1.0/invalid-2.0.jar" );
556 fail( "Artifact should be null for wrong version" );
558 catch ( DiscovererException e )
564 public void testLongVersion()
565 throws ComponentLookupException
569 discoverer.buildArtifact( "invalid/invalid/1.0/invalid-1.0b.jar" );
571 fail( "Artifact should be null for long version" );
573 catch ( DiscovererException e )
579 public void testWrongSnapshotVersion()
580 throws ComponentLookupException
584 discoverer.buildArtifact( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
586 fail( "Artifact should be null for wrong snapshot version" );
588 catch ( DiscovererException e )
594 public void testSnapshotBaseVersion()
595 throws ComponentLookupException
599 discoverer.buildArtifact( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
601 fail( "Artifact should be null for snapshot base version" );
603 catch ( DiscovererException e )
609 public void testPathWithClassifier()
610 throws ComponentLookupException, DiscovererException
612 String testPath = "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar";
614 Artifact artifact = discoverer.buildArtifact( testPath );
616 assertEquals( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ), artifact );
619 public void testWithJavaSourceInclusion()
620 throws ComponentLookupException, DiscovererException
622 String testPath = "org/apache/maven/testing/1.0/testing-1.0-sources.jar";
624 Artifact artifact = discoverer.buildArtifact( testPath );
626 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ), artifact );
629 public void testDistributionArtifacts()
630 throws ComponentLookupException, DiscovererException
632 String testPath = "org/apache/maven/testing/1.0/testing-1.0.tar.gz";
634 Artifact artifact = discoverer.buildArtifact( testPath );
636 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ), artifact );
638 testPath = "org/apache/maven/testing/1.0/testing-1.0.zip";
640 artifact = discoverer.buildArtifact( testPath );
642 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ), artifact );
645 public void testSnapshot()
646 throws ComponentLookupException, DiscovererException
648 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar";
650 Artifact artifact = discoverer.buildArtifact( testPath );
652 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ), artifact );
654 testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar";
656 artifact = discoverer.buildArtifact( testPath );
658 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ), artifact );
661 public void testNormal()
662 throws ComponentLookupException, DiscovererException
664 String testPath = "javax/sql/jdbc/2.0/jdbc-2.0.jar";
666 Artifact artifact = discoverer.buildArtifact( testPath );
668 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
671 public void testSnapshotWithClassifier()
672 throws ComponentLookupException, DiscovererException
674 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar";
676 Artifact artifact = discoverer.buildArtifact( testPath );
678 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ),
682 private List discoverArtifactsWithSnapshots()
683 throws DiscovererException
685 return discoverer.discoverArtifacts( repository, null, new AcceptAllArtifactFilter() );
688 private List discoverArtifactsWithBlacklist( List list )
689 throws DiscovererException
691 return discoverer.discoverArtifacts( repository, list, new SnapshotArtifactFilter() );
694 private List discoverArtifacts()
695 throws DiscovererException
697 return discoverer.discoverArtifacts( repository, null, new SnapshotArtifactFilter() );