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.Collections;
27 import java.util.Iterator;
28 import java.util.List;
31 * Test the legacy artifact discoverer.
33 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34 * @version $Id:LegacyArtifactDiscovererTest.java 437105 2006-08-26 17:22:22 +1000 (Sat, 26 Aug 2006) brett $
36 public class LegacyArtifactDiscovererTest
37 extends AbstractArtifactDiscovererTest
39 private static final List JAVAX_SQL_BLACKLIST = Collections.singletonList( "javax.sql/**" );
41 protected String getLayout()
46 protected File getRepositoryFile()
48 return getTestFile( "src/test/legacy-repository" );
51 public void testDefaultExcludes()
52 throws DiscovererException
54 List artifacts = discoverArtifacts();
55 assertNotNull( "Check artifacts not null", artifacts );
56 boolean found = false;
57 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
59 DiscovererPath dPath = (DiscovererPath) i.next();
61 String path = dPath.getPath();
63 if ( path.indexOf( "CVS" ) >= 0 )
66 assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
69 assertTrue( "Check exclusion was found", found );
71 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
73 Artifact a = (Artifact) i.next();
74 assertFalse( "Check not CVS", a.getFile().getPath().indexOf( "CVS" ) >= 0 );
75 assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
79 public void testStandardExcludes()
80 throws DiscovererException
82 List artifacts = discoverArtifacts();
83 assertNotNull( "Check artifacts not null", artifacts );
84 boolean found = false;
85 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
87 DiscovererPath dPath = (DiscovererPath) i.next();
89 String path = dPath.getPath();
91 if ( "KEYS".equals( path ) )
94 assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
97 assertTrue( "Check exclusion was found", found );
99 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
101 Artifact a = (Artifact) i.next();
102 assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
106 public void testBlacklistedExclude()
107 throws DiscovererException
109 List artifacts = discoverArtifactsWithBlacklist();
110 assertNotNull( "Check artifacts not null", artifacts );
111 boolean found = false;
112 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
114 DiscovererPath dPath = (DiscovererPath) i.next();
116 String path = dPath.getPath();
118 if ( "javax.sql/jars/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) ) )
121 assertEquals( "Check comment is about blacklisting", "Artifact was in the specified list of exclusions",
122 dPath.getComment() );
125 assertTrue( "Check exclusion was found", found );
127 assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
130 public void testKickoutWithShortPath()
131 throws DiscovererException
133 List artifacts = discoverArtifacts();
134 assertNotNull( "Check artifacts not null", artifacts );
135 boolean found = false;
136 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
138 DiscovererPath dPath = (DiscovererPath) i.next();
140 String path = dPath.getPath();
142 if ( "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
145 assertEquals( "Check reason for kickout",
146 "Path does not match a legacy repository path for an artifact", dPath.getComment() );
149 assertTrue( "Check kickout was found", found );
151 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
153 Artifact a = (Artifact) i.next();
154 assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
158 public void testKickoutWithLongPath()
159 throws DiscovererException
161 List artifacts = discoverArtifacts();
162 assertNotNull( "Check artifacts not null", artifacts );
163 boolean found = false;
164 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
166 DiscovererPath dPath = (DiscovererPath) i.next();
168 String path = dPath.getPath();
170 if ( "invalid/jars/1.0/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
173 assertEquals( "Check reason for kickout",
174 "Path does not match a legacy repository path for an artifact", dPath.getComment() );
177 assertTrue( "Check kickout was found", found );
179 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
181 Artifact a = (Artifact) i.next();
182 assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
186 public void testKickoutWithInvalidType()
187 throws DiscovererException
189 List artifacts = discoverArtifacts();
190 assertNotNull( "Check artifacts not null", artifacts );
191 boolean found = false;
192 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
194 DiscovererPath dPath = (DiscovererPath) i.next();
196 String path = dPath.getPath();
198 if ( "invalid/foo/invalid-1.0.foo".equals( path.replace( '\\', '/' ) ) )
201 assertEquals( "Check reason for kickout", "Path artifact type does not corresspond to an artifact type",
202 dPath.getComment() );
205 assertTrue( "Check kickout was found", found );
207 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
209 Artifact a = (Artifact) i.next();
210 assertFalse( "Check not invalid-1.0.foo", "invalid-1.0.foo".equals( a.getFile().getName() ) );
214 public void testKickoutWithNoExtension()
215 throws DiscovererException
217 List artifacts = discoverArtifacts();
218 assertNotNull( "Check artifacts not null", artifacts );
219 boolean found = false;
220 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
222 DiscovererPath dPath = (DiscovererPath) i.next();
224 String path = dPath.getPath();
226 if ( "invalid/jars/no-extension".equals( path.replace( '\\', '/' ) ) )
229 assertEquals( "Check reason for kickout", "Path filename does not have an extension",
230 dPath.getComment() );
233 assertTrue( "Check kickout was found", found );
235 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
237 Artifact a = (Artifact) i.next();
238 assertFalse( "Check not 'no-extension'", "no-extension".equals( a.getFile().getName() ) );
242 public void testKickoutWithWrongExtension()
243 throws DiscovererException
245 List artifacts = discoverArtifacts();
246 assertNotNull( "Check artifacts not null", artifacts );
247 boolean found = false;
248 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
250 DiscovererPath dPath = (DiscovererPath) i.next();
252 String path = dPath.getPath();
254 if ( "invalid/jars/invalid-1.0.rar".equals( path.replace( '\\', '/' ) ) )
257 assertEquals( "Check reason for kickout", "Path type does not match the extension",
258 dPath.getComment() );
261 assertTrue( "Check kickout was found", found );
263 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
265 Artifact a = (Artifact) i.next();
266 assertFalse( "Check not 'invalid-1.0.rar'", "invalid-1.0.rar".equals( a.getFile().getName() ) );
270 public void testKickoutWithNoVersion()
271 throws DiscovererException
273 List artifacts = discoverArtifacts();
274 assertNotNull( "Check artifacts not null", artifacts );
275 boolean found = false;
276 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
278 DiscovererPath dPath = (DiscovererPath) i.next();
280 String path = dPath.getPath();
282 if ( "invalid/jars/invalid.jar".equals( path.replace( '\\', '/' ) ) )
285 assertEquals( "Check reason for kickout", "Path filename version is empty", dPath.getComment() );
288 assertTrue( "Check kickout was found", found );
290 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
292 Artifact a = (Artifact) i.next();
293 assertFalse( "Check not 'invalid.jar'", "invalid.jar".equals( a.getFile().getName() ) );
297 public void testInclusion()
298 throws DiscovererException
300 List artifacts = discoverArtifactsWithSnapshots();
301 assertNotNull( "Check artifacts not null", artifacts );
303 assertTrue( "Check normal included",
304 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
307 public void testTextualVersion()
308 throws DiscovererException
310 List artifacts = discoverArtifactsWithSnapshots();
311 assertNotNull( "Check artifacts not null", artifacts );
313 assertTrue( "Check normal included",
314 artifacts.contains( createArtifact( "org.apache.maven", "testing", "UNKNOWN" ) ) );
317 public void testArtifactWithClassifier()
318 throws DiscovererException
320 List artifacts = discoverArtifactsWithSnapshots();
321 assertNotNull( "Check artifacts not null", artifacts );
323 assertTrue( "Check normal included",
324 artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
327 public void testJavaSourcesInclusion()
328 throws DiscovererException
330 List artifacts = discoverArtifactsWithSnapshots();
331 assertNotNull( "Check artifacts not null", artifacts );
333 assertTrue( "Check normal included", artifacts.contains(
334 createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
337 public void testDistributionInclusion()
338 throws DiscovererException
340 List artifacts = discoverArtifactsWithSnapshots();
341 assertNotNull( "Check artifacts not null", artifacts );
343 assertTrue( "Check zip included",
344 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
346 assertTrue( "Check tar.gz included",
347 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
350 public void testSnapshotInclusion()
351 throws DiscovererException
353 List artifacts = discoverArtifactsWithSnapshots();
354 assertNotNull( "Check artifacts not null", artifacts );
356 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
357 assertTrue( "Check snapshot included",
358 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0-20050611.112233-1" ) ) );
361 public void testSnapshotExclusion()
362 throws DiscovererException
364 List artifacts = discoverArtifacts();
365 assertNotNull( "Check artifacts not null", artifacts );
367 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
368 assertFalse( "Check snapshot included",
369 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0-20050611.112233-1" ) ) );
372 public void testFileSet()
373 throws DiscovererException
375 List artifacts = discoverArtifactsWithSnapshots();
376 assertNotNull( "Check artifacts not null", artifacts );
378 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
380 Artifact artifact = (Artifact) i.next();
381 assertNotNull( "Check file is set", artifact.getFile() );
385 public void testRepositorySet()
386 throws MalformedURLException, DiscovererException
388 List artifacts = discoverArtifactsWithSnapshots();
389 assertNotNull( "Check artifacts not null", artifacts );
391 String url = repository.getUrl();
392 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
394 Artifact artifact = (Artifact) i.next();
395 assertNotNull( "Check repository set", artifact.getRepository() );
396 assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
400 public void testWrongArtifactPackaging()
401 throws ComponentLookupException, DiscovererException
405 discoverer.buildArtifact( "org.apache.maven.test/jars/artifactId-1.0.jar.md5" );
407 fail( "Artifact should be null for wrong package extension" );
409 catch ( DiscovererException e )
415 public void testNoArtifactId()
416 throws DiscovererException
420 discoverer.buildArtifact( "groupId/jars/-1.0.jar" );
422 fail( "Artifact should be null when artifactId is missing" );
424 catch ( DiscovererException e )
431 discoverer.buildArtifact( "groupId/jars/1.0.jar" );
433 fail( "Artifact should be null when artifactId is missing" );
435 catch ( DiscovererException e )
441 public void testNoType()
442 throws ComponentLookupException, DiscovererException
446 discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
448 fail( "Artifact should be null for no type" );
450 catch ( DiscovererException e )
456 public void testSnapshot()
457 throws ComponentLookupException, DiscovererException
459 String testPath = "org.apache.maven.test/jars/maven-model-1.0-SNAPSHOT.jar";
461 Artifact artifact = discoverer.buildArtifact( testPath );
463 assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-SNAPSHOT" ), artifact );
466 public void testFinal()
467 throws ComponentLookupException, DiscovererException
469 String testPath = "org.apache.maven.test/jars/maven-model-1.0-final-20060606.jar";
471 Artifact artifact = discoverer.buildArtifact( testPath );
473 assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-final-20060606" ), artifact );
476 public void testNormal()
477 throws ComponentLookupException, DiscovererException
479 String testPath = "javax.sql/jars/jdbc-2.0.jar";
481 Artifact artifact = discoverer.buildArtifact( testPath );
483 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
486 public void testJavadoc()
487 throws ComponentLookupException, DiscovererException
489 String testPath = "javax.sql/javadoc.jars/jdbc-2.0-javadoc.jar";
491 Artifact artifact = discoverer.buildArtifact( testPath );
493 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0", "javadoc.jar", "javadoc" ), artifact );
496 public void testSources()
497 throws ComponentLookupException, DiscovererException
499 String testPath = "javax.sql/java-sources/jdbc-2.0-sources.jar";
501 Artifact artifact = discoverer.buildArtifact( testPath );
503 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0", "java-source", "sources" ), artifact );
506 public void testPlugin()
507 throws ComponentLookupException, DiscovererException
509 String testPath = "maven/plugins/maven-test-plugin-1.8.jar";
511 Artifact artifact = discoverer.buildArtifact( testPath );
513 assertEquals( createArtifact( "maven", "maven-test-plugin", "1.8", "plugin" ), artifact );
517 private List discoverArtifacts()
518 throws DiscovererException
520 return discoverer.discoverArtifacts( repository, null, new SnapshotArtifactFilter() );
523 private List discoverArtifactsWithBlacklist()
524 throws DiscovererException
526 return discoverer.discoverArtifacts( repository, JAVAX_SQL_BLACKLIST, new SnapshotArtifactFilter() );
529 private List discoverArtifactsWithSnapshots()
530 throws DiscovererException
532 return discoverer.discoverArtifacts( repository, null, new AcceptAllArtifactFilter() );