1 package org.apache.maven.repository.discovery;
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.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
24 import org.apache.maven.model.Model;
25 import org.codehaus.plexus.PlexusTestCase;
26 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29 import java.net.MalformedURLException;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.Iterator;
33 import java.util.List;
36 * Test the default artifact discoverer.
38 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40 * @todo test location of poms, checksums
42 public class DefaultArtifactDiscovererTest
43 extends PlexusTestCase
45 private ArtifactDiscoverer discoverer;
47 private ArtifactFactory factory;
49 private ArtifactRepository repository;
51 protected void setUp()
56 discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "default" );
58 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
60 File basedir = getTestFile( "src/test/repository" );
61 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
63 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
64 repository = factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
67 public void testDefaultExcludes()
69 List artifacts = discoverer.discoverArtifacts( repository, null, false );
70 assertNotNull( "Check artifacts not null", artifacts );
71 boolean found = false;
72 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
74 String path = (String) i.next();
76 found = path.indexOf( ".svn" ) >= 0;
78 assertTrue( "Check exclusion was found", found );
80 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
82 Artifact a = (Artifact) i.next();
83 assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
87 public void testStandardExcludes()
89 List artifacts = discoverer.discoverArtifacts( repository, null, false );
90 assertNotNull( "Check artifacts not null", artifacts );
91 boolean found = false;
92 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
94 String path = (String) i.next();
96 found = "KEYS".equals( path );
98 assertTrue( "Check exclusion was found", found );
100 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
102 Artifact a = (Artifact) i.next();
103 assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
107 public void testBlacklistedExclude()
109 List artifacts = discoverer.discoverArtifacts( repository, "javax/**", false );
110 assertNotNull( "Check artifacts not null", artifacts );
111 boolean found = false;
112 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
114 String path = (String) i.next();
116 found = "javax/sql/jdbc/2.0/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) );
118 assertTrue( "Check exclusion was found", found );
120 assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
123 public void testKickoutWithShortPath()
125 List artifacts = discoverer.discoverArtifacts( repository, null, false );
126 assertNotNull( "Check artifacts not null", artifacts );
127 boolean found = false;
128 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
130 String path = (String) i.next();
132 found = "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) );
134 assertTrue( "Check kickout was found", found );
136 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
138 Artifact a = (Artifact) i.next();
139 assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
143 public void testKickoutWithWrongArtifactId()
145 List artifacts = discoverer.discoverArtifacts( repository, null, false );
146 assertNotNull( "Check artifacts not null", artifacts );
147 boolean found = false;
148 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
150 String path = (String) i.next();
152 found = "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar".equals(
153 path.replace( '\\', '/' ) );
155 assertTrue( "Check kickout was found", found );
157 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
159 Artifact a = (Artifact) i.next();
160 assertFalse( "Check not wrong jar",
161 "wrong-artifactId-1.0-20050611.112233-1.jar".equals( a.getFile().getName() ) );
165 public void testKickoutWithNoType()
167 List artifacts = discoverer.discoverArtifacts( repository, null, false );
168 assertNotNull( "Check artifacts not null", artifacts );
169 boolean found = false;
170 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
172 String path = (String) i.next();
174 found = "invalid/invalid/1/invalid-1".equals( path.replace( '\\', '/' ) );
176 assertTrue( "Check kickout was found", found );
178 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
180 Artifact a = (Artifact) i.next();
181 assertFalse( "Check not 'invalid-1'", "invalid-1".equals( a.getFile().getName() ) );
185 public void testKickoutWithWrongVersion()
187 List artifacts = discoverer.discoverArtifacts( repository, null, false );
188 assertNotNull( "Check artifacts not null", artifacts );
189 boolean found = false;
190 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
192 String path = (String) i.next();
194 found = "invalid/invalid/1.0/invalid-2.0.jar".equals( path.replace( '\\', '/' ) );
196 assertTrue( "Check kickout was found", found );
198 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
200 Artifact a = (Artifact) i.next();
201 assertFalse( "Check not 'invalid-2.0.jar'", "invalid-2.0.jar".equals( a.getFile().getName() ) );
205 public void testKickoutWithLongerVersion()
207 List artifacts = discoverer.discoverArtifacts( repository, null, false );
208 assertNotNull( "Check artifacts not null", artifacts );
209 boolean found = false;
210 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
212 String path = (String) i.next();
214 found = "invalid/invalid/1.0/invalid-1.0b.jar".equals( path.replace( '\\', '/' ) );
216 assertTrue( "Check kickout was found", found );
218 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
220 Artifact a = (Artifact) i.next();
221 assertFalse( "Check not 'invalid-1.0b.jar'", "invalid-1.0b.jar".equals( a.getFile().getName() ) );
225 public void testKickoutWithWrongSnapshotVersion()
227 List artifacts = discoverer.discoverArtifacts( repository, null, false );
228 assertNotNull( "Check artifacts not null", artifacts );
229 boolean found = false;
230 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
232 String path = (String) i.next();
234 found = "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar".equals( path.replace( '\\', '/' ) );
236 assertTrue( "Check kickout was found", found );
238 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
240 Artifact a = (Artifact) i.next();
241 assertFalse( "Check not 'invalid-1.0.jar'", "invalid-1.0.jar".equals( a.getFile().getName() ) );
245 public void testKickoutWithSnapshotBaseVersion()
247 List artifacts = discoverer.discoverArtifacts( repository, null, false );
248 assertNotNull( "Check artifacts not null", artifacts );
249 boolean found = false;
250 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
252 String path = (String) i.next();
254 found = "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar".equals(
255 path.replace( '\\', '/' ) );
257 assertTrue( "Check kickout was found", found );
259 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
261 Artifact a = (Artifact) i.next();
262 assertFalse( "Check not 'invalid-1.0-20050611-123456-1.jar'",
263 "invalid-1.0-20050611.123456-1.jar".equals( a.getFile().getName() ) );
267 public void testInclusion()
269 List artifacts = discoverer.discoverArtifacts( repository, null, true );
270 assertNotNull( "Check artifacts not null", artifacts );
272 assertTrue( "Check normal included",
273 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
276 public void testArtifactWithClassifier()
278 List artifacts = discoverer.discoverArtifacts( repository, null, true );
279 assertNotNull( "Check artifacts not null", artifacts );
281 assertTrue( "Check normal included",
282 artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
285 public void testJavaSourcesInclusion()
287 List artifacts = discoverer.discoverArtifacts( repository, null, true );
288 assertNotNull( "Check artifacts not null", artifacts );
290 assertTrue( "Check normal included", artifacts.contains(
291 createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
294 public void testDistributionInclusion()
296 List artifacts = discoverer.discoverArtifacts( repository, null, true );
297 assertNotNull( "Check artifacts not null", artifacts );
299 assertTrue( "Check zip included",
300 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
302 assertTrue( "Check tar.gz included",
303 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
306 public void testSnapshotInclusion()
308 List artifacts = discoverer.discoverArtifacts( repository, null, true );
309 assertNotNull( "Check artifacts not null", artifacts );
311 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
312 assertTrue( "Check snapshot included",
313 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ) ) );
316 public void testSnapshotInclusionWithClassifier()
318 List artifacts = discoverer.discoverArtifacts( repository, null, true );
319 assertNotNull( "Check artifacts not null", artifacts );
321 assertTrue( "Check snapshot included", artifacts.contains(
322 createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ) ) );
325 public void testSnapshotExclusion()
327 List artifacts = discoverer.discoverArtifacts( repository, null, false );
328 assertNotNull( "Check artifacts not null", artifacts );
330 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
331 assertFalse( "Check snapshot included",
332 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
335 public void testFileSet()
337 List artifacts = discoverer.discoverArtifacts( repository, null, true );
338 assertNotNull( "Check artifacts not null", artifacts );
340 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
342 Artifact artifact = (Artifact) i.next();
343 assertNotNull( "Check file is set", artifact.getFile() );
347 public void testRepositorySet()
348 throws MalformedURLException
350 List artifacts = discoverer.discoverArtifacts( repository, null, true );
351 assertNotNull( "Check artifacts not null", artifacts );
353 String url = repository.getUrl();
354 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
356 Artifact artifact = (Artifact) i.next();
357 assertNotNull( "Check repository set", artifact.getRepository() );
358 assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
362 public void testStandalonePoms()
364 List models = discoverer.discoverStandalonePoms( repository, null, false );
365 assertEquals( 4, models.size() );
367 // Define order we expect
368 Collections.sort( models, new Comparator()
370 public int compare( Object o1, Object o2 )
372 Model m1 = (Model) o1;
373 Model m2 = (Model) o2;
375 int result = m1.getGroupId().compareTo( m2.getGroupId() );
378 result = m1.getArtifactId().compareTo( m2.getArtifactId() );
382 result = m1.getVersion().compareTo( m2.getVersion() );
388 Iterator itr = models.iterator();
389 Model model = (Model) itr.next();
390 assertEquals( "org.apache.maven", model.getGroupId() );
391 assertEquals( "B", model.getArtifactId() );
392 assertEquals( "1.0", model.getVersion() );
393 model = (Model) itr.next();
394 assertEquals( "org.apache.maven", model.getGroupId() );
395 assertEquals( "B", model.getArtifactId() );
396 assertEquals( "2.0", model.getVersion() );
397 model = (Model) itr.next();
398 assertEquals( "org.apache.maven", model.getGroupId() );
399 assertEquals( "discovery", model.getArtifactId() );
400 assertEquals( "1.0", model.getVersion() );
401 model = (Model) itr.next();
402 assertEquals( "org.apache.testgroup", model.getGroupId() );
403 assertEquals( "discovery", model.getArtifactId() );
404 assertEquals( "1.0", model.getVersion() );
407 public void testShortPath()
408 throws ComponentLookupException
410 String testPath = "invalid/invalid-1.0.jar";
412 Artifact artifact = getArtifactFromPath( testPath );
414 assertNull( "Artifact should be null for short paths", artifact );
417 public void testWrongArtifactId()
418 throws ComponentLookupException
420 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar";
422 Artifact artifact = getArtifactFromPath( testPath );
424 assertNull( "Artifact should be null for wrong ArtifactId", artifact );
427 public void testNoType()
428 throws ComponentLookupException
430 String testPath = "invalid/invalid/1/invalid-1";
432 Artifact artifact = getArtifactFromPath( testPath );
434 assertNull( "Artifact should be null for no type", artifact );
437 public void testWrongVersion()
438 throws ComponentLookupException
440 String testPath = "invalid/invalid/1.0/invalid-2.0.jar";
442 Artifact artifact = getArtifactFromPath( testPath );
444 assertNull( "Artifact should be null for wrong version", artifact );
447 public void testLongVersion()
448 throws ComponentLookupException
450 String testPath = "invalid/invalid/1.0/invalid-1.0b.jar";
452 Artifact artifact = getArtifactFromPath( testPath );
454 assertNull( "Artifact should be null for long version", artifact );
457 public void testWrongSnapshotVersion()
458 throws ComponentLookupException
460 String testPath = "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar";
462 Artifact artifact = getArtifactFromPath( testPath );
464 assertNull( "Artifact should be null for wrong snapshot version", artifact );
467 public void testSnapshotBaseVersion()
468 throws ComponentLookupException
470 String testPath = "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar";
472 Artifact artifact = getArtifactFromPath( testPath );
474 assertNull( "Artifact should be null for snapshot base version", artifact );
477 public void testPathWithClassifier()
478 throws ComponentLookupException
480 String testPath = "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar";
482 Artifact artifact = getArtifactFromPath( testPath );
484 assertNotNull( "Artifact path with classifier error", artifact );
486 assertEquals( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ), artifact );
489 public void testWithJavaSourceInclusion()
490 throws ComponentLookupException
492 String testPath = "org/apache/maven/testing/1.0/testing-1.0-sources.jar";
494 Artifact artifact = getArtifactFromPath( testPath );
496 assertNotNull( "Artifact path with java source inclusion error", artifact );
498 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ), artifact );
501 public void testDistributionArtifacts()
502 throws ComponentLookupException
504 String testPath = "org/apache/maven/testing/1.0/testing-1.0.tar.gz";
506 Artifact artifact = getArtifactFromPath( testPath );
508 assertNotNull( "tar.gz distribution artifact error", artifact );
510 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ), artifact );
512 testPath = "org/apache/maven/testing/1.0/testing-1.0.zip";
514 artifact = getArtifactFromPath( testPath );
516 assertNotNull( "zip distribution artifact error", artifact );
518 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ), artifact );
521 public void testSnapshot()
522 throws ComponentLookupException
524 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar";
526 Artifact artifact = getArtifactFromPath( testPath );
528 assertNotNull( "Artifact path with invalid snapshot error", artifact );
530 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ), artifact );
532 testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar";
534 artifact = getArtifactFromPath( testPath );
536 assertNotNull( "Artifact path with snapshot error", artifact );
538 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ), artifact );
541 public void testNormal()
542 throws ComponentLookupException
544 String testPath = "javax/sql/jdbc/2.0/jdbc-2.0.jar";
546 Artifact artifact = getArtifactFromPath( testPath );
548 assertNotNull( "Normal artifact path error", artifact );
550 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
553 public void testSnapshotWithClassifier()
554 throws ComponentLookupException
556 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar";
558 Artifact artifact = getArtifactFromPath( testPath );
560 assertNotNull( "Artifact path with snapshot and classifier error", artifact );
562 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ),
566 private Artifact getArtifactFromPath( String path )
568 return discoverer.buildArtifact( path );
571 private Artifact createArtifact( String groupId, String artifactId, String version )
573 return factory.createArtifact( groupId, artifactId, version, null, "jar" );
576 private Artifact createArtifact( String groupId, String artifactId, String version, String type )
578 return factory.createArtifact( groupId, artifactId, version, null, type );
581 private Artifact createArtifact( String groupId, String artifactId, String version, String type, String classifier )
583 return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );