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.codehaus.plexus.PlexusTestCase;
25 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
28 import java.net.MalformedURLException;
29 import java.util.Iterator;
30 import java.util.List;
33 * Test the legacy artifact discoverer.
35 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37 * @todo share as much as possible with default via abstract test case
39 public class LegacyArtifactDiscovererTest
40 extends PlexusTestCase
42 private ArtifactDiscoverer discoverer;
44 private ArtifactFactory factory;
46 private ArtifactRepository repository;
48 protected void setUp()
53 discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "legacy" );
55 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
57 File basedir = getTestFile( "src/test/legacy-repository" );
59 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
61 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
62 repository = factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
65 public void testDefaultExcludes()
67 List artifacts = discoverer.discoverArtifacts( repository, null, false );
68 assertNotNull( "Check artifacts not null", artifacts );
69 boolean found = false;
70 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
72 String path = (String) i.next();
74 found = path.indexOf( ".svn" ) >= 0;
76 assertTrue( "Check exclusion was found", found );
78 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
80 Artifact a = (Artifact) i.next();
81 assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
85 public void testStandardExcludes()
87 List artifacts = discoverer.discoverArtifacts( repository, null, false );
88 assertNotNull( "Check artifacts not null", artifacts );
89 boolean found = false;
90 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
92 String path = (String) i.next();
94 found = "KEYS".equals( path );
96 assertTrue( "Check exclusion was found", found );
98 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
100 Artifact a = (Artifact) i.next();
101 assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
105 public void testBlacklistedExclude()
107 List artifacts = discoverer.discoverArtifacts( repository, "javax.sql/**", false );
108 assertNotNull( "Check artifacts not null", artifacts );
109 boolean found = false;
110 for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
112 String path = (String) i.next();
114 found = "javax.sql/jars/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) );
116 assertTrue( "Check exclusion was found", found );
118 assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
121 public void testKickoutWithShortPath()
123 List artifacts = discoverer.discoverArtifacts( repository, null, false );
124 assertNotNull( "Check artifacts not null", artifacts );
125 boolean found = false;
126 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
128 String path = (String) i.next();
130 found = "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) );
132 assertTrue( "Check kickout was found", found );
134 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
136 Artifact a = (Artifact) i.next();
137 assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
141 public void testKickoutWithLongPath()
143 List artifacts = discoverer.discoverArtifacts( repository, null, false );
144 assertNotNull( "Check artifacts not null", artifacts );
145 boolean found = false;
146 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
148 String path = (String) i.next();
150 found = "invalid/jars/1.0/invalid-1.0.jar".equals( path.replace( '\\', '/' ) );
152 assertTrue( "Check kickout was found", found );
154 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
156 Artifact a = (Artifact) i.next();
157 assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
161 public void testKickoutWithInvalidType()
163 List artifacts = discoverer.discoverArtifacts( repository, null, false );
164 assertNotNull( "Check artifacts not null", artifacts );
165 boolean found = false;
166 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
168 String path = (String) i.next();
170 found = "invalid/foo/invalid-1.0.foo".equals( path.replace( '\\', '/' ) );
172 assertTrue( "Check kickout was found", found );
174 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
176 Artifact a = (Artifact) i.next();
177 assertFalse( "Check not invalid-1.0.foo", "invalid-1.0.foo".equals( a.getFile().getName() ) );
181 public void testKickoutWithNoExtension()
183 List artifacts = discoverer.discoverArtifacts( repository, null, false );
184 assertNotNull( "Check artifacts not null", artifacts );
185 boolean found = false;
186 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
188 String path = (String) i.next();
190 found = "invalid/jars/no-extension".equals( path.replace( '\\', '/' ) );
192 assertTrue( "Check kickout was found", found );
194 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
196 Artifact a = (Artifact) i.next();
197 assertFalse( "Check not 'no-extension'", "no-extension".equals( a.getFile().getName() ) );
201 public void testKickoutWithWrongExtension()
203 List artifacts = discoverer.discoverArtifacts( repository, null, false );
204 assertNotNull( "Check artifacts not null", artifacts );
205 boolean found = false;
206 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
208 String path = (String) i.next();
210 found = "invalid/jars/invalid-1.0.rar".equals( path.replace( '\\', '/' ) );
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.0.rar'", "invalid-1.0.rar".equals( a.getFile().getName() ) );
221 public void testKickoutWithNoVersion()
223 List artifacts = discoverer.discoverArtifacts( repository, null, false );
224 assertNotNull( "Check artifacts not null", artifacts );
225 boolean found = false;
226 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
228 String path = (String) i.next();
230 found = "invalid/jars/invalid.jar".equals( path.replace( '\\', '/' ) );
232 assertTrue( "Check kickout was found", found );
234 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
236 Artifact a = (Artifact) i.next();
237 assertFalse( "Check not 'invalid.jar'", "invalid.jar".equals( a.getFile().getName() ) );
241 public void testInclusion()
243 List artifacts = discoverer.discoverArtifacts( repository, null, true );
244 assertNotNull( "Check artifacts not null", artifacts );
246 assertTrue( "Check normal included",
247 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
250 public void testTextualVersion()
252 List artifacts = discoverer.discoverArtifacts( repository, null, true );
253 assertNotNull( "Check artifacts not null", artifacts );
255 assertTrue( "Check normal included",
256 artifacts.contains( createArtifact( "org.apache.maven", "testing", "UNKNOWN" ) ) );
259 public void testArtifactWithClassifier()
261 List artifacts = discoverer.discoverArtifacts( repository, null, true );
262 assertNotNull( "Check artifacts not null", artifacts );
264 assertTrue( "Check normal included",
265 artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
268 public void testJavaSourcesInclusion()
270 List artifacts = discoverer.discoverArtifacts( repository, null, true );
271 assertNotNull( "Check artifacts not null", artifacts );
273 assertTrue( "Check normal included", artifacts.contains(
274 createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
277 public void testDistributionInclusion()
279 List artifacts = discoverer.discoverArtifacts( repository, null, true );
280 assertNotNull( "Check artifacts not null", artifacts );
282 assertTrue( "Check zip included",
283 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
285 assertTrue( "Check tar.gz included",
286 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
289 public void testSnapshotInclusion()
291 List artifacts = discoverer.discoverArtifacts( repository, null, true );
292 assertNotNull( "Check artifacts not null", artifacts );
294 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
295 assertTrue( "Check snapshot included",
296 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0-20050611.112233-1" ) ) );
299 public void testSnapshotExclusion()
301 List artifacts = discoverer.discoverArtifacts( repository, null, false );
302 assertNotNull( "Check artifacts not null", artifacts );
304 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
305 assertFalse( "Check snapshot included",
306 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0-20050611.112233-1" ) ) );
309 public void testFileSet()
311 List artifacts = discoverer.discoverArtifacts( repository, null, true );
312 assertNotNull( "Check artifacts not null", artifacts );
314 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
316 Artifact artifact = (Artifact) i.next();
317 assertNotNull( "Check file is set", artifact.getFile() );
321 public void testRepositorySet()
322 throws MalformedURLException
324 List artifacts = discoverer.discoverArtifacts( repository, null, true );
325 assertNotNull( "Check artifacts not null", artifacts );
327 String url = repository.getUrl();
328 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
330 Artifact artifact = (Artifact) i.next();
331 assertNotNull( "Check repository set", artifact.getRepository() );
332 assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
336 public void testWrongArtifactPackaging()
337 throws ComponentLookupException
339 String testPath = "org.apache.maven.test/jars/artifactId-1.0.jar.md5";
341 Artifact artifact = getArtifactFromPath( testPath );
343 assertNull( "Artifact should be null for wrong package extension", artifact );
346 public void testNoArtifactid()
348 String testPath = "groupId/jars/-1.0.jar";
350 Artifact artifact = getArtifactFromPath( testPath );
352 assertNull( "Artifact should be null when artifactId is missing", artifact );
354 testPath = "groupId/jars/1.0.jar";
356 artifact = getArtifactFromPath( testPath );
358 assertNull( "Artifact should be null when artifactId is missing", artifact );
361 public void testNoType()
362 throws ComponentLookupException
364 String testPath = "invalid/invalid/1/invalid-1";
366 Artifact artifact = getArtifactFromPath( testPath );
368 assertNull( "Artifact should be null for no type", artifact );
371 public void testSnapshot()
372 throws ComponentLookupException
374 String testPath = "org.apache.maven.test/jars/maven-model-1.0-SNAPSHOT.jar";
376 Artifact artifact = getArtifactFromPath( testPath );
378 assertNotNull( "Artifact path with invalid snapshot error", artifact );
380 assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-SNAPSHOT" ), artifact );
383 public void testFinal()
384 throws ComponentLookupException
386 String testPath = "org.apache.maven.test/jars/maven-model-1.0-final-20060606.jar";
388 Artifact artifact = getArtifactFromPath( testPath );
390 assertNotNull( "Artifact path with invalid snapshot error", artifact );
392 assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-final-20060606" ), artifact );
395 public void testNormal()
396 throws ComponentLookupException
398 String testPath = "javax.sql/jars/jdbc-2.0.jar";
400 Artifact artifact = getArtifactFromPath( testPath );
402 assertNotNull( "Normal artifact path error", artifact );
404 assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
407 private Artifact getArtifactFromPath( String path )
409 return discoverer.buildArtifact( path );
412 private Artifact createArtifact( String groupId, String artifactId, String version )
414 return factory.createArtifact( groupId, artifactId, version, null, "jar" );
417 private Artifact createArtifact( String groupId, String artifactId, String version, String type )
419 return factory.createArtifact( groupId, artifactId, version, null, type );
422 private Artifact createArtifact( String groupId, String artifactId, String version, String type, String classifier )
424 return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );