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 testDistributionInclusion()
368 throws DiscovererException
370 List artifacts = discoverArtifactsWithSnapshots();
371 assertNotNull( "Check artifacts not null", artifacts );
373 assertTrue( "Check zip included",
374 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
376 assertTrue( "Check tar.gz included",
377 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
380 public void testSnapshotInclusion()
381 throws DiscovererException
383 List artifacts = discoverArtifactsWithSnapshots();
384 assertNotNull( "Check artifacts not null", artifacts );
386 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
387 assertTrue( "Check snapshot included",
388 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ) ) );
391 public void testSnapshotInclusionWithClassifier()
392 throws DiscovererException
394 List artifacts = discoverArtifactsWithSnapshots();
395 assertNotNull( "Check artifacts not null", artifacts );
397 assertTrue( "Check snapshot included", artifacts.contains(
398 createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ) ) );
401 public void testSnapshotExclusion()
402 throws DiscovererException
404 List artifacts = discoverArtifacts();
405 assertNotNull( "Check artifacts not null", artifacts );
407 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
408 assertFalse( "Check snapshot included",
409 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
412 public void testFileSet()
413 throws DiscovererException
415 List artifacts = discoverArtifactsWithSnapshots();
416 assertNotNull( "Check artifacts not null", artifacts );
418 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
420 Artifact artifact = (Artifact) i.next();
421 assertNotNull( "Check file is set", artifact.getFile() );
425 public void testRepositorySet()
426 throws MalformedURLException, DiscovererException
428 List artifacts = discoverArtifactsWithSnapshots();
429 assertNotNull( "Check artifacts not null", artifacts );
431 String url = repository.getUrl();
432 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
434 Artifact artifact = (Artifact) i.next();
435 assertNotNull( "Check repository set", artifact.getRepository() );
436 assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
440 public void testStandalonePoms()
441 throws DiscovererException
443 List artifacts = discoverArtifacts();
445 // cull down to actual artifacts (only standalone poms will have type = pom)
446 Map keyedArtifacts = new HashMap();
447 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
449 Artifact a = (Artifact) i.next();
450 String key = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion();
451 if ( !"pom".equals( a.getType() ) || !keyedArtifacts.containsKey( key ) )
453 keyedArtifacts.put( key, a );
457 List models = new ArrayList();
459 for ( Iterator i = keyedArtifacts.values().iterator(); i.hasNext(); )
461 Artifact a = (Artifact) i.next();
463 if ( "pom".equals( a.getType() ) )
469 assertEquals( 4, models.size() );
471 // Define order we expect
472 Collections.sort( models );
474 Iterator itr = models.iterator();
475 Artifact model = (Artifact) itr.next();
476 assertEquals( "org.apache.maven", model.getGroupId() );
477 assertEquals( "B", model.getArtifactId() );
478 assertEquals( "1.0", model.getVersion() );
479 model = (Artifact) itr.next();
480 assertEquals( "org.apache.maven", model.getGroupId() );
481 assertEquals( "B", model.getArtifactId() );
482 assertEquals( "2.0", model.getVersion() );
483 model = (Artifact) itr.next();
484 assertEquals( "org.apache.maven", model.getGroupId() );
485 assertEquals( "discovery", model.getArtifactId() );
486 assertEquals( "1.0", model.getVersion() );
487 model = (Artifact) itr.next();
488 assertEquals( "org.apache.testgroup", model.getGroupId() );
489 assertEquals( "discovery", model.getArtifactId() );
490 assertEquals( "1.0", model.getVersion() );
493 public void testShortPath()
494 throws ComponentLookupException
498 discoverer.buildArtifact( "invalid/invalid-1.0.jar" );
500 fail( "Artifact should be null for short paths" );
502 catch ( DiscovererException e )
508 public void testWrongArtifactId()
509 throws ComponentLookupException
514 discoverer.buildArtifact( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
516 fail( "Artifact should be null for wrong ArtifactId" );
518 catch ( DiscovererException e )
524 public void testNoType()
525 throws ComponentLookupException
529 discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
531 fail( "Artifact should be null for no type" );
533 catch ( DiscovererException e )
539 public void testWrongVersion()
540 throws ComponentLookupException
544 discoverer.buildArtifact( "invalid/invalid/1.0/invalid-2.0.jar" );
546 fail( "Artifact should be null for wrong version" );
548 catch ( DiscovererException e )
554 public void testLongVersion()
555 throws ComponentLookupException
559 discoverer.buildArtifact( "invalid/invalid/1.0/invalid-1.0b.jar" );
561 fail( "Artifact should be null for long version" );
563 catch ( DiscovererException e )
569 public void testWrongSnapshotVersion()
570 throws ComponentLookupException
574 discoverer.buildArtifact( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
576 fail( "Artifact should be null for wrong snapshot version" );
578 catch ( DiscovererException e )
584 public void testSnapshotBaseVersion()
585 throws ComponentLookupException
589 discoverer.buildArtifact( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
591 fail( "Artifact should be null for snapshot base version" );
593 catch ( DiscovererException e )
599 public void testPathWithClassifier()
600 throws ComponentLookupException, DiscovererException
602 String testPath = "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar";
604 Artifact artifact = discoverer.buildArtifact( testPath );
606 assertEquals( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ), artifact );
609 public void testWithJavaSourceInclusion()
610 throws ComponentLookupException, DiscovererException
612 String testPath = "org/apache/maven/testing/1.0/testing-1.0-sources.jar";
614 Artifact artifact = discoverer.buildArtifact( testPath );
616 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ), artifact );
619 public void testDistributionArtifacts()
620 throws ComponentLookupException, DiscovererException
622 String testPath = "org/apache/maven/testing/1.0/testing-1.0.tar.gz";
624 Artifact artifact = discoverer.buildArtifact( testPath );
626 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ), artifact );
628 testPath = "org/apache/maven/testing/1.0/testing-1.0.zip";
630 artifact = discoverer.buildArtifact( testPath );
632 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ), artifact );
635 public void testSnapshot()
636 throws ComponentLookupException, DiscovererException
638 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar";
640 Artifact artifact = discoverer.buildArtifact( testPath );
642 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ), artifact );
644 testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar";
646 artifact = discoverer.buildArtifact( testPath );
648 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ), artifact );
651 public void testNormal()
652 throws ComponentLookupException, DiscovererException
654 String testPath = "javax/sql/jdbc/2.0/jdbc-2.0.jar";
656 Artifact artifact = discoverer.buildArtifact( testPath );
658 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
661 public void testSnapshotWithClassifier()
662 throws ComponentLookupException, DiscovererException
664 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar";
666 Artifact artifact = discoverer.buildArtifact( testPath );
668 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ),
672 private List discoverArtifactsWithSnapshots()
673 throws DiscovererException
675 return discoverer.discoverArtifacts( repository, null, new AcceptAllArtifactFilter() );
678 private List discoverArtifactsWithBlacklist( List list )
679 throws DiscovererException
681 return discoverer.discoverArtifacts( repository, list, new SnapshotArtifactFilter() );
684 private List discoverArtifacts()
685 throws DiscovererException
687 return discoverer.discoverArtifacts( repository, null, new SnapshotArtifactFilter() );