1 package org.apache.maven.archiva.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.codehaus.plexus.component.repository.exception.ComponentLookupException;
23 import java.net.MalformedURLException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
32 * Test the default artifact discoverer.
34 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
36 * @todo test location of poms, checksums
38 public class DefaultArtifactDiscovererTest
39 extends AbstractArtifactDiscovererTest
42 protected String getLayout()
47 protected File getRepositoryFile()
49 return getTestFile( "src/test/repository" );
52 public void testDefaultExcludes()
53 throws DiscovererException
55 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
56 assertNotNull( "Check artifacts not null", artifacts );
57 boolean found = false;
58 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
60 DiscovererPath dPath = (DiscovererPath) i.next();
62 String path = dPath.getPath();
64 boolean b = path.indexOf( ".svn" ) >= 0;
68 assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
71 assertTrue( "Check exclusion was found", found );
73 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
75 Artifact a = (Artifact) i.next();
76 assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
80 public void testStandardExcludes()
81 throws DiscovererException
83 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
84 assertNotNull( "Check artifacts not null", artifacts );
85 boolean found = false;
86 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
88 DiscovererPath dPath = (DiscovererPath) i.next();
90 String path = dPath.getPath();
92 if ( "KEYS".equals( path ) )
95 assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
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()
108 throws DiscovererException
110 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, "javax/**", false );
111 assertNotNull( "Check artifacts not null", artifacts );
112 boolean found = false;
113 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
115 DiscovererPath dPath = (DiscovererPath) i.next();
117 String path = dPath.getPath();
119 if ( "javax/sql/jdbc/2.0/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) ) )
122 assertEquals( "Check comment is about blacklisting", "Artifact was in the specified list of exclusions",
123 dPath.getComment() );
126 assertTrue( "Check exclusion was found", found );
128 assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
131 public void testKickoutWithShortPath()
132 throws DiscovererException
134 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
135 assertNotNull( "Check artifacts not null", artifacts );
136 boolean found = false;
137 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
139 DiscovererPath dPath = (DiscovererPath) i.next();
141 String path = dPath.getPath();
143 if ( "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
146 assertEquals( "Check reason for kickout", "Path is too short to build an artifact from",
147 dPath.getComment() );
151 assertTrue( "Check kickout was found", found );
153 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
155 Artifact a = (Artifact) i.next();
156 assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
160 public void testKickoutWithWrongArtifactId()
161 throws DiscovererException
163 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
164 assertNotNull( "Check artifacts not null", artifacts );
165 boolean found = false;
166 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
168 DiscovererPath dPath = (DiscovererPath) i.next();
170 String path = dPath.getPath();
172 if ( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar".equals(
173 path.replace( '\\', '/' ) ) )
176 assertEquals( "Check reason for kickout", "Path filename does not correspond to an artifact",
177 dPath.getComment() );
180 assertTrue( "Check kickout was found", found );
182 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
184 Artifact a = (Artifact) i.next();
185 assertFalse( "Check not wrong jar",
186 "wrong-artifactId-1.0-20050611.112233-1.jar".equals( a.getFile().getName() ) );
190 public void testKickoutWithNoType()
191 throws DiscovererException
193 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
194 assertNotNull( "Check artifacts not null", artifacts );
195 boolean found = false;
196 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
198 DiscovererPath dPath = (DiscovererPath) i.next();
200 String path = dPath.getPath();
202 if ( "invalid/invalid/1/invalid-1".equals( path.replace( '\\', '/' ) ) )
205 assertEquals( "Check reason for kickout", "Path filename does not have an extension",
206 dPath.getComment() );
209 assertTrue( "Check kickout was found", found );
211 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
213 Artifact a = (Artifact) i.next();
214 assertFalse( "Check not 'invalid-1'", "invalid-1".equals( a.getFile().getName() ) );
218 public void testKickoutWithWrongVersion()
219 throws DiscovererException
221 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
222 assertNotNull( "Check artifacts not null", artifacts );
223 boolean found = false;
224 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
226 DiscovererPath dPath = (DiscovererPath) i.next();
228 String path = dPath.getPath();
230 if ( "invalid/invalid/1.0/invalid-2.0.jar".equals( path.replace( '\\', '/' ) ) )
233 assertEquals( "Check reason for kickout", "Built artifact version does not match path version",
234 dPath.getComment() );
237 assertTrue( "Check kickout was found", found );
239 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
241 Artifact a = (Artifact) i.next();
242 assertFalse( "Check not 'invalid-2.0.jar'", "invalid-2.0.jar".equals( a.getFile().getName() ) );
246 public void testKickoutWithLongerVersion()
247 throws DiscovererException
249 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
250 assertNotNull( "Check artifacts not null", artifacts );
251 boolean found = false;
252 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
254 DiscovererPath dPath = (DiscovererPath) i.next();
256 String path = dPath.getPath();
258 if ( "invalid/invalid/1.0/invalid-1.0b.jar".equals( path.replace( '\\', '/' ) ) )
261 assertEquals( "Check reason for kickout", "Path version does not corresspond to an artifact version",
262 dPath.getComment() );
265 assertTrue( "Check kickout was found", found );
267 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
269 Artifact a = (Artifact) i.next();
270 assertFalse( "Check not 'invalid-1.0b.jar'", "invalid-1.0b.jar".equals( a.getFile().getName() ) );
274 public void testKickoutWithWrongSnapshotVersion()
275 throws DiscovererException
277 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
278 assertNotNull( "Check artifacts not null", artifacts );
279 boolean found = false;
280 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
282 DiscovererPath dPath = (DiscovererPath) i.next();
284 String path = dPath.getPath();
286 if ( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
289 assertEquals( "Check reason for kickout",
290 "Failed to create a snapshot artifact: invalid:invalid:jar:1.0:runtime",
291 dPath.getComment() );
294 assertTrue( "Check kickout was found", found );
296 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
298 Artifact a = (Artifact) i.next();
299 assertFalse( "Check not 'invalid-1.0.jar'", "invalid-1.0.jar".equals( a.getFile().getName() ) );
303 public void testKickoutWithSnapshotBaseVersion()
304 throws DiscovererException
306 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
307 assertNotNull( "Check artifacts not null", artifacts );
308 boolean found = false;
309 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
311 DiscovererPath dPath = (DiscovererPath) i.next();
313 String path = dPath.getPath();
315 if ( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar".equals(
316 path.replace( '\\', '/' ) ) )
319 assertEquals( "Check reason for kickout",
320 "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",
321 dPath.getComment() );
324 assertTrue( "Check kickout was found", found );
326 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
328 Artifact a = (Artifact) i.next();
329 assertFalse( "Check not 'invalid-1.0-20050611-123456-1.jar'",
330 "invalid-1.0-20050611.123456-1.jar".equals( a.getFile().getName() ) );
334 public void testInclusion()
335 throws DiscovererException
337 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
338 assertNotNull( "Check artifacts not null", artifacts );
340 assertTrue( "Check normal included",
341 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
344 public void testArtifactWithClassifier()
345 throws DiscovererException
347 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
348 assertNotNull( "Check artifacts not null", artifacts );
350 assertTrue( "Check normal included",
351 artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
354 public void testJavaSourcesInclusion()
355 throws DiscovererException
357 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
358 assertNotNull( "Check artifacts not null", artifacts );
360 assertTrue( "Check normal included", artifacts.contains(
361 createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
364 public void testDistributionInclusion()
365 throws DiscovererException
367 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
368 assertNotNull( "Check artifacts not null", artifacts );
370 assertTrue( "Check zip included",
371 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
373 assertTrue( "Check tar.gz included",
374 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
377 public void testSnapshotInclusion()
378 throws DiscovererException
380 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
381 assertNotNull( "Check artifacts not null", artifacts );
383 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
384 assertTrue( "Check snapshot included",
385 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ) ) );
388 public void testSnapshotInclusionWithClassifier()
389 throws DiscovererException
391 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
392 assertNotNull( "Check artifacts not null", artifacts );
394 assertTrue( "Check snapshot included", artifacts.contains(
395 createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ) ) );
398 public void testSnapshotExclusion()
399 throws DiscovererException
401 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
402 assertNotNull( "Check artifacts not null", artifacts );
404 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
405 assertFalse( "Check snapshot included",
406 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
409 public void testFileSet()
410 throws DiscovererException
412 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
413 assertNotNull( "Check artifacts not null", artifacts );
415 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
417 Artifact artifact = (Artifact) i.next();
418 assertNotNull( "Check file is set", artifact.getFile() );
422 public void testRepositorySet()
423 throws MalformedURLException, DiscovererException
425 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
426 assertNotNull( "Check artifacts not null", artifacts );
428 String url = repository.getUrl();
429 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
431 Artifact artifact = (Artifact) i.next();
432 assertNotNull( "Check repository set", artifact.getRepository() );
433 assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
437 public void testStandalonePoms()
438 throws DiscovererException
440 List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
442 // cull down to actual artifacts (only standalone poms will have type = pom)
443 Map keyedArtifacts = new HashMap();
444 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
446 Artifact a = (Artifact) i.next();
447 String key = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion();
448 if ( !"pom".equals( a.getType() ) || !keyedArtifacts.containsKey( key ) )
450 keyedArtifacts.put( key, a );
454 List models = new ArrayList();
456 for ( Iterator i = keyedArtifacts.values().iterator(); i.hasNext(); )
458 Artifact a = (Artifact) i.next();
460 if ( "pom".equals( a.getType() ) )
466 assertEquals( 4, models.size() );
468 // Define order we expect
469 Collections.sort( models );
471 Iterator itr = models.iterator();
472 Artifact model = (Artifact) itr.next();
473 assertEquals( "org.apache.maven", model.getGroupId() );
474 assertEquals( "B", model.getArtifactId() );
475 assertEquals( "1.0", model.getVersion() );
476 model = (Artifact) itr.next();
477 assertEquals( "org.apache.maven", model.getGroupId() );
478 assertEquals( "B", model.getArtifactId() );
479 assertEquals( "2.0", model.getVersion() );
480 model = (Artifact) itr.next();
481 assertEquals( "org.apache.maven", model.getGroupId() );
482 assertEquals( "discovery", model.getArtifactId() );
483 assertEquals( "1.0", model.getVersion() );
484 model = (Artifact) itr.next();
485 assertEquals( "org.apache.testgroup", model.getGroupId() );
486 assertEquals( "discovery", model.getArtifactId() );
487 assertEquals( "1.0", model.getVersion() );
490 public void testShortPath()
491 throws ComponentLookupException
495 discoverer.buildArtifact( "invalid/invalid-1.0.jar" );
497 fail( "Artifact should be null for short paths" );
499 catch ( DiscovererException e )
505 public void testWrongArtifactId()
506 throws ComponentLookupException
511 discoverer.buildArtifact( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
513 fail( "Artifact should be null for wrong ArtifactId" );
515 catch ( DiscovererException e )
521 public void testNoType()
522 throws ComponentLookupException
526 discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
528 fail( "Artifact should be null for no type" );
530 catch ( DiscovererException e )
536 public void testWrongVersion()
537 throws ComponentLookupException
541 discoverer.buildArtifact( "invalid/invalid/1.0/invalid-2.0.jar" );
543 fail( "Artifact should be null for wrong version" );
545 catch ( DiscovererException e )
551 public void testLongVersion()
552 throws ComponentLookupException
556 discoverer.buildArtifact( "invalid/invalid/1.0/invalid-1.0b.jar" );
558 fail( "Artifact should be null for long version" );
560 catch ( DiscovererException e )
566 public void testWrongSnapshotVersion()
567 throws ComponentLookupException
571 discoverer.buildArtifact( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
573 fail( "Artifact should be null for wrong snapshot version" );
575 catch ( DiscovererException e )
581 public void testSnapshotBaseVersion()
582 throws ComponentLookupException
586 discoverer.buildArtifact( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
588 fail( "Artifact should be null for snapshot base version" );
590 catch ( DiscovererException e )
596 public void testPathWithClassifier()
597 throws ComponentLookupException, DiscovererException
599 String testPath = "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar";
601 Artifact artifact = discoverer.buildArtifact( testPath );
603 assertEquals( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ), artifact );
606 public void testWithJavaSourceInclusion()
607 throws ComponentLookupException, DiscovererException
609 String testPath = "org/apache/maven/testing/1.0/testing-1.0-sources.jar";
611 Artifact artifact = discoverer.buildArtifact( testPath );
613 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ), artifact );
616 public void testDistributionArtifacts()
617 throws ComponentLookupException, DiscovererException
619 String testPath = "org/apache/maven/testing/1.0/testing-1.0.tar.gz";
621 Artifact artifact = discoverer.buildArtifact( testPath );
623 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ), artifact );
625 testPath = "org/apache/maven/testing/1.0/testing-1.0.zip";
627 artifact = discoverer.buildArtifact( testPath );
629 assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ), artifact );
632 public void testSnapshot()
633 throws ComponentLookupException, DiscovererException
635 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar";
637 Artifact artifact = discoverer.buildArtifact( testPath );
639 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ), artifact );
641 testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar";
643 artifact = discoverer.buildArtifact( testPath );
645 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ), artifact );
648 public void testNormal()
649 throws ComponentLookupException, DiscovererException
651 String testPath = "javax/sql/jdbc/2.0/jdbc-2.0.jar";
653 Artifact artifact = discoverer.buildArtifact( testPath );
655 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
658 public void testSnapshotWithClassifier()
659 throws ComponentLookupException, DiscovererException
661 String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar";
663 Artifact artifact = discoverer.buildArtifact( testPath );
665 assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ),