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;
28 import java.net.MalformedURLException;
29 import java.util.Iterator;
30 import java.util.List;
33 * Test the default artifact discoverer.
35 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37 * @todo test location of poms, checksums
39 public class DefaultArtifactDiscovererTest
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,
54 "org.apache.maven.repository.discovery.DefaultArtifactDiscoverer" );
56 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
58 File basedir = getTestFile( "src/test/repository" );
59 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
61 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
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/**", 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/jdbc/2.0/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 testKickoutWithWrongArtifactId()
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 = "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar".equals(
151 path.replace( '\\', '/' ) );
153 assertTrue( "Check kickout was found", found );
155 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
157 Artifact a = (Artifact) i.next();
158 assertFalse( "Check not wrong jar",
159 "wrong-artifactId-1.0-20050611.112233-1.jar".equals( a.getFile().getName() ) );
163 public void testKickoutWithNoType()
165 List artifacts = discoverer.discoverArtifacts( repository, null, false );
166 assertNotNull( "Check artifacts not null", artifacts );
167 boolean found = false;
168 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
170 String path = (String) i.next();
172 found = "invalid/invalid/1/invalid-1".equals( path.replace( '\\', '/' ) );
174 assertTrue( "Check kickout was found", found );
176 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
178 Artifact a = (Artifact) i.next();
179 assertFalse( "Check not 'invalid-1'", "invalid-1".equals( a.getFile().getName() ) );
183 public void testKickoutWithWrongVersion()
185 List artifacts = discoverer.discoverArtifacts( repository, null, false );
186 assertNotNull( "Check artifacts not null", artifacts );
187 boolean found = false;
188 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
190 String path = (String) i.next();
192 found = "invalid/invalid/1.0/invalid-2.0.jar".equals( path.replace( '\\', '/' ) );
194 assertTrue( "Check kickout was found", found );
196 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
198 Artifact a = (Artifact) i.next();
199 assertFalse( "Check not 'invalid-2.0.jar'", "invalid-2.0.jar".equals( a.getFile().getName() ) );
203 public void testKickoutWithLongerVersion()
205 List artifacts = discoverer.discoverArtifacts( repository, null, false );
206 assertNotNull( "Check artifacts not null", artifacts );
207 boolean found = false;
208 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
210 String path = (String) i.next();
212 found = "invalid/invalid/1.0/invalid-1.0b.jar".equals( path.replace( '\\', '/' ) );
214 assertTrue( "Check kickout was found", found );
216 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
218 Artifact a = (Artifact) i.next();
219 assertFalse( "Check not 'invalid-1.0b.jar'", "invalid-1.0b.jar".equals( a.getFile().getName() ) );
223 public void testKickoutWithWrongSnapshotVersion()
225 List artifacts = discoverer.discoverArtifacts( repository, null, false );
226 assertNotNull( "Check artifacts not null", artifacts );
227 boolean found = false;
228 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
230 String path = (String) i.next();
232 found = "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar".equals( path.replace( '\\', '/' ) );
234 assertTrue( "Check kickout was found", found );
236 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
238 Artifact a = (Artifact) i.next();
239 assertFalse( "Check not 'invalid-1.0.jar'", "invalid-1.0.jar".equals( a.getFile().getName() ) );
243 public void testKickoutWithSnapshotBaseVersion()
245 List artifacts = discoverer.discoverArtifacts( repository, null, false );
246 assertNotNull( "Check artifacts not null", artifacts );
247 boolean found = false;
248 for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
250 String path = (String) i.next();
252 found = "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar".equals(
253 path.replace( '\\', '/' ) );
255 assertTrue( "Check kickout was found", found );
257 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
259 Artifact a = (Artifact) i.next();
260 assertFalse( "Check not 'invalid-1.0-20050611-123456-1.jar'",
261 "invalid-1.0-20050611.123456-1.jar".equals( a.getFile().getName() ) );
265 public void testInclusion()
267 List artifacts = discoverer.discoverArtifacts( repository, null, true );
268 assertNotNull( "Check artifacts not null", artifacts );
270 assertTrue( "Check normal included",
271 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
274 public void testArtifactWithClassifier()
276 List artifacts = discoverer.discoverArtifacts( repository, null, true );
277 assertNotNull( "Check artifacts not null", artifacts );
279 assertTrue( "Check normal included",
280 artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
283 public void testJavaSourcesInclusion()
285 List artifacts = discoverer.discoverArtifacts( repository, null, true );
286 assertNotNull( "Check artifacts not null", artifacts );
288 assertTrue( "Check normal included", artifacts.contains(
289 createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
292 public void testDistributionInclusion()
294 List artifacts = discoverer.discoverArtifacts( repository, null, true );
295 assertNotNull( "Check artifacts not null", artifacts );
297 assertTrue( "Check zip included",
298 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
300 assertTrue( "Check tar.gz included",
301 artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
304 public void testSnapshotInclusion()
306 List artifacts = discoverer.discoverArtifacts( repository, null, true );
307 assertNotNull( "Check artifacts not null", artifacts );
309 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
310 assertTrue( "Check snapshot included",
311 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ) ) );
314 public void testSnapshotInclusionWithClassifier()
316 List artifacts = discoverer.discoverArtifacts( repository, null, true );
317 assertNotNull( "Check artifacts not null", artifacts );
319 assertTrue( "Check snapshot included", artifacts.contains(
320 createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ) ) );
323 public void testSnapshotExclusion()
325 List artifacts = discoverer.discoverArtifacts( repository, null, false );
326 assertNotNull( "Check artifacts not null", artifacts );
328 assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
329 assertFalse( "Check snapshot included",
330 artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
333 public void testFileSet()
335 List artifacts = discoverer.discoverArtifacts( repository, null, true );
336 assertNotNull( "Check artifacts not null", artifacts );
338 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
340 Artifact artifact = (Artifact) i.next();
341 assertNotNull( "Check file is set", artifact.getFile() );
345 public void testRepositorySet()
346 throws MalformedURLException
348 List artifacts = discoverer.discoverArtifacts( repository, null, true );
349 assertNotNull( "Check artifacts not null", artifacts );
351 String url = repository.getUrl();
352 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
354 Artifact artifact = (Artifact) i.next();
355 assertNotNull( "Check repository set", artifact.getRepository() );
356 assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
360 public void testStandalonePoms()
362 List artifacts = discoverer.discoverStandalonePoms( repository, null, false );
363 assertEquals( 4, artifacts.size() );
364 Iterator itr = artifacts.iterator();
365 //Artifact artifact = (Artifact) itr.next();
366 Model model = (Model) itr.next();
368 createArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging() );
369 assertEquals( "org.apache.maven", artifact.getGroupId() );
370 assertEquals( "B", artifact.getArtifactId() );
371 assertEquals( "1.0", artifact.getVersion() );
372 model = (Model) itr.next();
374 createArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging() );
375 assertEquals( "org.apache.maven", artifact.getGroupId() );
376 assertEquals( "B", artifact.getArtifactId() );
377 assertEquals( "2.0", artifact.getVersion() );
378 model = (Model) itr.next();
380 createArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging() );
381 assertEquals( "org.apache.maven", artifact.getGroupId() );
382 assertEquals( "discovery", artifact.getArtifactId() );
383 assertEquals( "1.0", artifact.getVersion() );
384 model = (Model) itr.next();
386 createArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging() );
387 assertEquals( "org.apache.testgroup", artifact.getGroupId() );
388 assertEquals( "discovery", artifact.getArtifactId() );
389 assertEquals( "1.0", artifact.getVersion() );
392 private Artifact createArtifact( String groupId, String artifactId, String version )
394 return factory.createArtifact( groupId, artifactId, version, null, "jar" );
397 private Artifact createArtifact( String groupId, String artifactId, String version, String type )
399 return factory.createArtifact( groupId, artifactId, version, null, type );
402 private Artifact createArtifact( String groupId, String artifactId, String version, String type, String classifier )
404 return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );