1   package org.apache.maven.artifact.resolver;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.LinkedHashSet;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import org.apache.maven.artifact.Artifact;
34  import org.apache.maven.artifact.factory.ArtifactFactory;
35  import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
36  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
37  import org.apache.maven.artifact.metadata.ResolutionGroup;
38  import org.apache.maven.artifact.repository.ArtifactRepository;
39  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
40  import org.apache.maven.artifact.resolver.filter.ExclusionSetFilter;
41  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
42  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
43  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
44  import org.apache.maven.artifact.versioning.VersionRange;
45  import org.codehaus.plexus.PlexusTestCase;
46  
47  /**
48   * Test the default artifact collector.
49   *
50   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
51   * @version $Id: DefaultArtifactCollectorTest.java 688932 2008-08-26 01:24:27Z jdcasey $
52   */
53  public class DefaultArtifactCollectorTest
54      extends PlexusTestCase
55  {
56      private ArtifactCollector artifactCollector;
57  
58      private ArtifactFactory artifactFactory;
59  
60      private ArtifactSpec projectArtifact;
61  
62      private Source source;
63  
64      private static final String GROUP_ID = "test";
65  
66      protected void setUp()
67          throws Exception
68      {
69          super.setUp();
70  
71          this.source = new Source();
72          this.artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
73          this.artifactCollector = new DefaultArtifactCollector();
74  
75          this.projectArtifact = createArtifactSpec( "project", "1.0", null );
76      }
77  
78      // works, but we don't fail on cycles presently
79      public void disabledtestCircularDependencyNotIncludingCurrentProject()
80          throws ArtifactResolutionException, InvalidVersionSpecificationException
81      {
82          ArtifactSpec a = createArtifactSpec( "a", "1.0" );
83          ArtifactSpec b = a.addDependency( "b", "1.0" );
84          b.addDependency( "a", "1.0" );
85          try
86          {
87              collect( a );
88              fail( "Should have failed on cyclic dependency not involving project" );
89          }
90          catch ( CyclicDependencyException expected )
91          {
92              assertTrue( true );
93          }
94      }
95  
96      // works, but we don't fail on cycles presently
97      public void disabledtestCircularDependencyIncludingCurrentProject()
98          throws ArtifactResolutionException, InvalidVersionSpecificationException
99      {
100         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
101         ArtifactSpec b = a.addDependency( "b", "1.0" );
102         b.addDependency( "project", "1.0" );
103         try
104         {
105             collect( a );
106             fail( "Should have failed on cyclic dependency involving project" );
107         }
108         catch ( CyclicDependencyException expected )
109         {
110             assertTrue( true );
111         }
112     }
113 
114     public void testResolveWithFilter()
115         throws ArtifactResolutionException, InvalidVersionSpecificationException
116     {
117         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
118         ArtifactSpec b = a.addDependency( "b", "1.0" );
119         ArtifactSpec c = a.addDependency( "c", "3.0" );
120 
121         b.addDependency( "c", "2.0" );
122         ArtifactSpec d = b.addDependency( "d", "4.0" );
123 
124         ArtifactResolutionResult res = collect( a );
125         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact, c.artifact, d.artifact} ),
126                       res.getArtifacts() );
127 
128         ArtifactFilter filter = new ExclusionSetFilter( new String[]{"b"} );
129         res = collect( a, filter );
130         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, c.artifact} ), res.getArtifacts() );
131     }
132 
133     public void testResolveCorrectDependenciesWhenDifferentDependenciesOnNearest()
134         throws ArtifactResolutionException, InvalidVersionSpecificationException
135     {
136         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
137         ArtifactSpec b = a.addDependency( "b", "1.0" );
138         ArtifactSpec c2 = b.addDependency( "c", "2.0" );
139         c2.addDependency( "d", "1.0" );
140 
141         ArtifactSpec e = createArtifactSpec( "e", "1.0" );
142         ArtifactSpec c1 = e.addDependency( "c", "1.0" );
143         ArtifactSpec f = c1.addDependency( "f", "1.0" );
144 
145         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, e.artifact} ) );
146         assertEquals( "Check artifact list",
147                       createSet( new Object[]{a.artifact, b.artifact, e.artifact, c1.artifact, f.artifact} ),
148                       res.getArtifacts() );
149         assertEquals( "Check version", "1.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
150     }
151 
152     public void disabledtestResolveCorrectDependenciesWhenDifferentDependenciesOnNewest()
153         throws ArtifactResolutionException, InvalidVersionSpecificationException
154     {
155         // TODO: use newest conflict resolver
156         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
157         ArtifactSpec b = a.addDependency( "b", "1.0" );
158         ArtifactSpec c2 = b.addDependency( "c", "2.0" );
159         ArtifactSpec d = c2.addDependency( "d", "1.0" );
160 
161         ArtifactSpec e = createArtifactSpec( "e", "1.0" );
162         ArtifactSpec c1 = e.addDependency( "c", "1.0" );
163         c1.addDependency( "f", "1.0" );
164 
165         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, e.artifact} ) );
166         assertEquals( "Check artifact list",
167                       createSet( new Object[]{a.artifact, b.artifact, e.artifact, c2.artifact, d.artifact} ),
168                       res.getArtifacts() );
169         assertEquals( "Check version", "2.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
170     }
171 
172     public void disabledtestResolveCorrectDependenciesWhenDifferentDependenciesOnNewestVersionReplaced()
173         throws ArtifactResolutionException, InvalidVersionSpecificationException
174     {
175         // TODO: use newest conflict resolver
176         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
177         ArtifactSpec b1 = a.addDependency( "b", "1.0" );
178         ArtifactSpec c = a.addDependency( "c", "1.0" );
179         ArtifactSpec d2 = b1.addDependency( "d", "2.0" );
180         d2.addDependency( "h", "1.0" );
181         ArtifactSpec d1 = c.addDependency( "d", "1.0" );
182         ArtifactSpec b2 = c.addDependency( "b", "2.0" );
183         ArtifactSpec e = b2.addDependency( "e", "1.0" );
184         ArtifactSpec g = d1.addDependency( "g", "1.0" );
185 
186         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact} ) );
187         Object[] artifacts = new Object[]{a.artifact, c.artifact, d1.artifact, b2.artifact, e.artifact, g.artifact};
188         assertEquals( "Check artifact list", createSet( artifacts ), res.getArtifacts() );
189         assertEquals( "Check version", "1.0", getArtifact( "d", res.getArtifacts() ).getVersion() );
190         assertEquals( "Check version", "2.0", getArtifact( "b", res.getArtifacts() ).getVersion() );
191     }
192 
193     public void testResolveNearestNewestIsNearest()
194         throws ArtifactResolutionException, InvalidVersionSpecificationException
195     {
196         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
197         ArtifactSpec b = a.addDependency( "b", "1.0" );
198         ArtifactSpec c = a.addDependency( "c", "3.0" );
199 
200         b.addDependency( "c", "2.0" );
201 
202         ArtifactResolutionResult res = collect( a );
203         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact, c.artifact} ),
204                       res.getArtifacts() );
205         assertEquals( "Check version", "3.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
206     }
207 
208     public void testResolveNearestOldestIsNearest()
209         throws ArtifactResolutionException, InvalidVersionSpecificationException
210     {
211         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
212         ArtifactSpec b = a.addDependency( "b", "1.0" );
213         ArtifactSpec c = a.addDependency( "c", "2.0" );
214 
215         b.addDependency( "c", "3.0" );
216 
217         ArtifactResolutionResult res = collect( a );
218         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact, c.artifact} ),
219                       res.getArtifacts() );
220         assertEquals( "Check version", "2.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
221     }
222 
223     public void testResolveLocalNewestIsLocal()
224         throws ArtifactResolutionException, InvalidVersionSpecificationException
225     {
226         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
227         a.addDependency( "b", "2.0" );
228         ArtifactSpec b = createArtifactSpec( "b", "3.0" );
229 
230         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
231         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
232         assertEquals( "Check version", "3.0", getArtifact( "b", res.getArtifacts() ).getVersion() );
233     }
234 
235     public void testResolveLocalOldestIsLocal()
236         throws ArtifactResolutionException, InvalidVersionSpecificationException
237     {
238         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
239         a.addDependency( "b", "3.0" );
240         ArtifactSpec b = createArtifactSpec( "b", "2.0" );
241 
242         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
243         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
244         assertEquals( "Check version", "2.0", getArtifact( "b", res.getArtifacts() ).getVersion() );
245     }
246 
247     public void testResolveLocalWithNewerVersionButLesserScope()
248         throws ArtifactResolutionException, InvalidVersionSpecificationException
249     {
250         ArtifactSpec a = createArtifactSpec( "commons-logging", "1.0" );
251         a.addDependency( "junit", "3.7" );
252         ArtifactSpec b = createArtifactSpec( "junit", "3.8.1", Artifact.SCOPE_TEST );
253 
254         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
255         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
256         assertEquals( "Check version", "3.8.1", getArtifact( "junit", res.getArtifacts() ).getVersion() );
257         assertEquals( "Check scope", Artifact.SCOPE_TEST, getArtifact( "junit", res.getArtifacts() ).getScope() );
258     }
259 
260     public void testResolveLocalWithNewerVersionButLesserScopeResolvedFirst()
261         throws ArtifactResolutionException, InvalidVersionSpecificationException
262     {
263         ArtifactSpec b = createArtifactSpec( "junit", "3.8.1", Artifact.SCOPE_TEST );
264         ArtifactSpec a = createArtifactSpec( "commons-logging", "1.0" );
265         a.addDependency( "junit", "3.7" );
266 
267         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
268         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
269         assertEquals( "Check version", "3.8.1", getArtifact( "junit", res.getArtifacts() ).getVersion() );
270         assertEquals( "Check scope", Artifact.SCOPE_TEST, getArtifact( "junit", res.getArtifacts() ).getScope() );
271     }
272 
273     public void testResolveNearestWithRanges()
274         throws ArtifactResolutionException, InvalidVersionSpecificationException
275     {
276         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
277         ArtifactSpec b = a.addDependency( "b", "1.0" );
278         ArtifactSpec c = a.addDependency( "c", "2.0" );
279 
280         b.addDependency( "c", "[1.0,3.0]" );
281 
282         ArtifactResolutionResult res = collect( a );
283         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact, c.artifact} ),
284                       res.getArtifacts() );
285         assertEquals( "Check version", "2.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
286     }
287 
288     public void testResolveRangeWithManagedVersion()
289         throws ArtifactResolutionException, InvalidVersionSpecificationException
290     {
291         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
292         ArtifactSpec b = a.addDependency( "b", "[1.0,3.0]" );
293 
294         ArtifactSpec managedB = createArtifactSpec( "b", "5.0" );
295 
296         ArtifactResolutionResult res = collect( a, managedB.artifact );
297         assertEquals( "Check artifact list", createSet( new Object[] { a.artifact, managedB.artifact } ),
298                       res.getArtifacts() );
299         assertEquals( "Check version", "5.0", getArtifact( "b", res.getArtifacts() ).getVersion() );
300     }
301 
302     public void testCompatibleRanges()
303         throws ArtifactResolutionException, InvalidVersionSpecificationException
304     {
305         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
306         ArtifactSpec b = a.addDependency( "b", "1.0" );
307         a.addDependency( "c", "[2.0,2.5]" );
308         b.addDependency( "c", "[1.0,3.0]" );
309         ArtifactSpec c = createArtifactSpec( "c", "2.5" );
310 
311         ArtifactResolutionResult res = collect( a );
312 
313         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact, c.artifact} ),
314                       res.getArtifacts() );
315         assertEquals( "Check version", "2.5", getArtifact( "c", res.getArtifacts() ).getVersion() );
316     }
317 
318     public void testCompatibleRecommendedVersion()
319         throws ArtifactResolutionException, InvalidVersionSpecificationException
320     {
321         
322         //this test puts two dependencies on C with 3.2 and [1.0,3.0] as the version.
323         //it puts 2.5 in the pretend repo...we should get back 2.5
324         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
325         ArtifactSpec b = a.addDependency( "b", "1.0" );
326         ArtifactSpec b1 = a.addDependency( "b1", "1.0" );
327         b.addDependency( "c", "3.2" );
328         b1.addDependency( "c", "[1.0,3.0]" );
329       
330         //put it in the repo
331         ArtifactSpec c = createArtifactSpec( "c", "2.5" );
332         source.addArtifact( createArtifactSpec( "c", "2.5" ));
333 
334         ArtifactResolutionResult res = collect( a );
335 
336         assertEquals( "Check artifact list", createSet( new Object[] { a.artifact, b.artifact,b1.artifact,c.artifact } ),
337                       res.getArtifacts() );
338         assertEquals( "Check version", "2.5", getArtifact( "c", res.getArtifacts() ).getVersion() );
339     }
340     
341     public void testCompatibleRecommendedVersionWithChildren()
342         throws ArtifactResolutionException, InvalidVersionSpecificationException
343     {
344 
345         // this test puts two dependencies on C with 3.2 and [1.0,3.0] as the version.
346         // it puts 2.5 in the pretend repo...we should get back c2.5 and d1.0
347         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
348         ArtifactSpec b = a.addDependency( "b", "1.0" );
349         ArtifactSpec e = a.addDependency( "e", "1.0" );
350         ArtifactSpec c1 = b.addDependency( "c", "3.2" );
351         ArtifactSpec d1 = c1.addDependency( "d","1.1" );
352         e.addDependency( "c", "[1.0,3.0]" );
353 
354         // put it in the repo
355         ArtifactSpec c = createArtifactSpec( "c", "2.5" );
356         ArtifactSpec d = c.addDependency( "d","1.0" );
357         
358         source.addArtifact( c );
359         source.addArtifact( d );
360         source.addArtifact( c1 );
361         source.addArtifact( d1 );
362 
363         ArtifactResolutionResult res = collect( a );
364 
365         assertEquals( "Check artifact list",
366                       createSet( new Object[] { a.artifact, b.artifact, e.artifact, c.artifact,d.artifact } ), res.getArtifacts() );
367         assertEquals( "Check version", "2.5", getArtifact( "c", res.getArtifacts() ).getVersion() );
368     }
369     
370     public void testInCompatibleRecommendedVersion()
371         throws ArtifactResolutionException, InvalidVersionSpecificationException
372     {
373 
374         // this test puts two dependencies on C with 3.2 and [1.0,3.0] as the version.
375         // it puts 2.5 and 3.0 in the pretend repo...we should get back 3.0
376         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
377         ArtifactSpec b = a.addDependency( "b", "1.0" );
378         ArtifactSpec b1 = a.addDependency( "b1", "1.0" );
379         b.addDependency( "c", "3.2" );
380         b1.addDependency( "c", "[1.0,3.0]" );
381 
382         // put it in the repo
383         ArtifactSpec c = createArtifactSpec( "c", "3.0" );
384         source.addArtifact( createArtifactSpec( "c", "2.5" ) );
385         source.addArtifact( createArtifactSpec( "c", "3.0" ) );
386         source.addArtifact( createArtifactSpec( "c", "3.2" ) );
387 
388         ArtifactResolutionResult res = collect( a );
389 
390         assertEquals( "Check artifact list",
391                       createSet( new Object[] { a.artifact, b.artifact, b1.artifact, c.artifact } ), res.getArtifacts() );
392         assertEquals( "Check version", "3.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
393     }
394 
395     //MNG-2123: this version of the test caused the crash seen in the IT. It was
396     //only happening if the first dependency was not a range but the second one was.
397     public void testInCompatibleRecommendedVersion2()
398         throws ArtifactResolutionException, InvalidVersionSpecificationException
399     {
400 
401         // this test puts two dependencies on C with 3.2 and [1.0,3.0] as the version.
402         // it puts 2.5 and 3.0 in the pretend repo...we should get back 3.0
403         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
404         ArtifactSpec b = a.addDependency( "b", "1.0" );
405         ArtifactSpec b1 = a.addDependency( "b1", "1.0" );
406         b1.addDependency( "c", "3.2" );
407         b.addDependency( "c", "[1.0,3.0]" );
408 
409         // put it in the repo
410         ArtifactSpec c = createArtifactSpec( "c", "3.0" );
411         source.addArtifact( createArtifactSpec( "c", "2.5" ) );
412         source.addArtifact( createArtifactSpec( "c", "3.0" ) );
413         source.addArtifact( createArtifactSpec( "c", "3.2" ) );
414 
415         ArtifactResolutionResult res = collect( a );
416 
417         assertEquals( "Check artifact list",
418                       createSet( new Object[] { a.artifact, b.artifact, b1.artifact, c.artifact } ), res.getArtifacts() );
419         assertEquals( "Check version", "3.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
420     }
421     
422     public void testIncompatibleRanges()
423         throws ArtifactResolutionException, InvalidVersionSpecificationException
424     {
425         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
426         ArtifactSpec b = a.addDependency( "b", "1.0" );
427         a.addDependency( "c", "[2.4,3.0]" );
428 
429         b.addDependency( "c", "[1.0,2.0]" );
430 
431         try
432         {
433             ArtifactResolutionResult res = collect( a );
434             fail( "Should not succeed collecting, got: " + res.getArtifacts() );
435         }
436         catch ( ArtifactResolutionException expected )
437         {
438         }
439     }
440 
441     public void testUnboundedRangeWhenVersionUnavailable()
442         throws ArtifactResolutionException, InvalidVersionSpecificationException
443     {
444         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
445         ArtifactSpec b = a.addDependency( "b", "1.0" );
446         a.addDependency( "c", "[2.0,]" );
447         b.addDependency( "c", "[1.0,]" );
448 
449         try
450         {
451             ArtifactResolutionResult res = collect( a );
452             fail( "Should not succeed collecting, got: " + res.getArtifacts() );
453         }
454         catch ( ArtifactResolutionException expected )
455         {
456             assertTrue( true );
457         }
458     }
459 
460     public void testUnboundedRangeBelowLastRelease()
461         throws ArtifactResolutionException, InvalidVersionSpecificationException
462     {
463         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
464         createArtifactSpec( "c", "1.5" );
465         ArtifactSpec c = createArtifactSpec( "c", "2.0" );
466         createArtifactSpec( "c", "1.1" );
467         a.addDependency( "c", "[1.0,)" );
468 
469         ArtifactResolutionResult res = collect( a );
470 
471         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, c.artifact} ), res.getArtifacts() );
472         assertEquals( "Check version", "2.0", getArtifact( "c", res.getArtifacts() ).getVersion() );
473     }
474 
475     public void testUnboundedRangeAboveLastRelease()
476         throws ArtifactResolutionException, InvalidVersionSpecificationException
477     {
478         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
479         createArtifactSpec( "c", "2.0" );
480         a.addDependency( "c", "[10.0,)" );
481 
482         try
483         {
484             ArtifactResolutionResult res = collect( a );
485             fail( "Should not succeed collecting, got: " + res.getArtifacts() );
486         }
487         catch ( ArtifactResolutionException expected )
488         {
489             assertTrue( true );
490         }
491     }
492 
493     public void testResolveManagedVersion()
494         throws ArtifactResolutionException, InvalidVersionSpecificationException
495     {
496         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
497         a.addDependency( "b", "3.0", Artifact.SCOPE_RUNTIME );
498 
499         Artifact managedVersion = createArtifactSpec( "b", "5.0" ).artifact;
500         Artifact modifiedB = createArtifactSpec( "b", "5.0", Artifact.SCOPE_RUNTIME ).artifact;
501 
502         ArtifactResolutionResult res = collect( a, managedVersion );
503         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedB} ), res.getArtifacts() );
504     }
505 
506     public void testCollectChangesVersionOfOriginatingArtifactIfInDependencyManagementHasDifferentVersion()
507         throws ArtifactResolutionException, InvalidVersionSpecificationException
508     {
509         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
510 
511         Artifact artifact = projectArtifact.artifact;
512         Artifact managedVersion = createArtifactSpec( artifact.getArtifactId(), "2.0" ).artifact;
513 
514         ArtifactResolutionResult result = collect( a, managedVersion );
515 
516         assertEquals( "collect has modified version in originating artifact", "1.0", artifact.getVersion() );
517 
518         Artifact resolvedArtifact = (Artifact) result.getArtifacts().iterator().next();
519 
520         assertEquals( "Resolved version don't match original artifact version", "1.0", resolvedArtifact.getVersion() );
521     }
522 
523     public void testResolveCompileScopeOverTestScope()
524         throws ArtifactResolutionException, InvalidVersionSpecificationException
525     {
526         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
527         ArtifactSpec c = createArtifactSpec( "c", "3.0", Artifact.SCOPE_TEST );
528 
529         a.addDependency( "c", "2.0", Artifact.SCOPE_COMPILE );
530 
531         Artifact modifiedC = createArtifactSpec( "c", "3.0", Artifact.SCOPE_COMPILE ).artifact;
532 
533         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
534         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
535         Artifact artifact = getArtifact( "c", res.getArtifacts() );
536         // local wins now, and irrelevant if not local as test/provided aren't transitive
537 //        assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
538         assertEquals( "Check scope", Artifact.SCOPE_TEST, artifact.getScope() );
539     }
540 
541     public void testResolveRuntimeScopeOverTestScope()
542         throws ArtifactResolutionException, InvalidVersionSpecificationException
543     {
544         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
545         ArtifactSpec c = createArtifactSpec( "c", "3.0", Artifact.SCOPE_TEST );
546 
547         a.addDependency( "c", "2.0", Artifact.SCOPE_RUNTIME );
548 
549         Artifact modifiedC = createArtifactSpec( "c", "3.0", Artifact.SCOPE_RUNTIME ).artifact;
550 
551         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
552         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
553         Artifact artifact = getArtifact( "c", res.getArtifacts() );
554         // local wins now, and irrelevant if not local as test/provided aren't transitive
555 //        assertEquals( "Check scope", Artifact.SCOPE_RUNTIME, artifact.getScope() );
556         assertEquals( "Check scope", Artifact.SCOPE_TEST, artifact.getScope() );
557     }
558 
559     public void testResolveCompileScopeOverRuntimeScope()
560         throws ArtifactResolutionException, InvalidVersionSpecificationException
561     {
562         ArtifactSpec root = createArtifactSpec( "root", "1.0" );
563         ArtifactSpec a = root.addDependency( "a", "1.0" );
564         root.addDependency( "c", "3.0", Artifact.SCOPE_RUNTIME );
565 
566         a.addDependency( "c", "2.0", Artifact.SCOPE_COMPILE );
567 
568         Artifact modifiedC = createArtifactSpec( "c", "3.0", Artifact.SCOPE_COMPILE ).artifact;
569 
570         ArtifactResolutionResult res = collect( createSet( new Object[]{root.artifact} ) );
571         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, root.artifact, modifiedC} ),
572                       res.getArtifacts() );
573         Artifact artifact = getArtifact( "c", res.getArtifacts() );
574         assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
575     }
576 
577     public void testResolveCompileScopeOverProvidedScope()
578         throws ArtifactResolutionException, InvalidVersionSpecificationException
579     {
580         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
581         ArtifactSpec c = createArtifactSpec( "c", "3.0", Artifact.SCOPE_PROVIDED );
582 
583         a.addDependency( "c", "2.0", Artifact.SCOPE_COMPILE );
584 
585         Artifact modifiedC = createArtifactSpec( "c", "3.0", Artifact.SCOPE_COMPILE ).artifact;
586 
587         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
588         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
589         Artifact artifact = getArtifact( "c", res.getArtifacts() );
590         // local wins now, and irrelevant if not local as test/provided aren't transitive
591 //        assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
592         assertEquals( "Check scope", Artifact.SCOPE_PROVIDED, artifact.getScope() );
593     }
594 
595     public void testResolveRuntimeScopeOverProvidedScope()
596         throws ArtifactResolutionException, InvalidVersionSpecificationException
597     {
598         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
599         ArtifactSpec c = createArtifactSpec( "c", "3.0", Artifact.SCOPE_PROVIDED );
600 
601         a.addDependency( "c", "2.0", Artifact.SCOPE_RUNTIME );
602 
603         Artifact modifiedC = createArtifactSpec( "c", "3.0", Artifact.SCOPE_RUNTIME ).artifact;
604 
605         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
606         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
607         Artifact artifact = getArtifact( "c", res.getArtifacts() );
608         // local wins now, and irrelevant if not local as test/provided aren't transitive
609 //        assertEquals( "Check scope", Artifact.SCOPE_RUNTIME, artifact.getScope() );
610         assertEquals( "Check scope", Artifact.SCOPE_PROVIDED, artifact.getScope() );
611     }
612 
613     public void testProvidedScopeNotTransitive()
614         throws ArtifactResolutionException, InvalidVersionSpecificationException
615     {
616         ArtifactSpec a = createArtifactSpec( "a", "1.0", Artifact.SCOPE_PROVIDED );
617         ArtifactSpec b = createArtifactSpec( "b", "1.0" );
618         b.addDependency( "c", "3.0", Artifact.SCOPE_PROVIDED );
619 
620         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
621         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
622     }
623 
624     public void testOptionalNotTransitive()
625         throws ArtifactResolutionException, InvalidVersionSpecificationException
626     {
627         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
628         ArtifactSpec b = createArtifactSpec( "b", "1.0" );
629         b.addDependency( "c", "3.0", true );
630 
631         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
632         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
633     }
634 
635     public void testOptionalIncludedAtRoot()
636         throws ArtifactResolutionException, InvalidVersionSpecificationException
637     {
638         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
639 
640         ArtifactSpec b = createArtifactSpec( "b", "1.0", true );
641 
642         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
643         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
644     }
645 
646     public void testScopeUpdate()
647         throws InvalidVersionSpecificationException, ArtifactResolutionException
648     {
649         /* farthest = compile */
650         checkScopeUpdate( Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE );
651         checkScopeUpdate( Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_COMPILE );
652         checkScopeUpdate( Artifact.SCOPE_COMPILE, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_COMPILE );
653         checkScopeUpdate( Artifact.SCOPE_COMPILE, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_COMPILE );
654         checkScopeUpdate( Artifact.SCOPE_COMPILE, Artifact.SCOPE_TEST, Artifact.SCOPE_COMPILE );
655 
656         /* farthest = provided */
657         checkScopeUpdate( Artifact.SCOPE_PROVIDED, Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE );
658         checkScopeUpdate( Artifact.SCOPE_PROVIDED, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_PROVIDED );
659         checkScopeUpdate( Artifact.SCOPE_PROVIDED, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME );
660         checkScopeUpdate( Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_SYSTEM );
661         checkScopeUpdate( Artifact.SCOPE_PROVIDED, Artifact.SCOPE_TEST, Artifact.SCOPE_TEST );
662 
663         /* farthest = runtime */
664         checkScopeUpdate( Artifact.SCOPE_RUNTIME, Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE );
665         checkScopeUpdate( Artifact.SCOPE_RUNTIME, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_RUNTIME );
666         checkScopeUpdate( Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME );
667         checkScopeUpdate( Artifact.SCOPE_RUNTIME, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_SYSTEM );
668         checkScopeUpdate( Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST, Artifact.SCOPE_RUNTIME );
669 
670         /* farthest = system */
671         checkScopeUpdate( Artifact.SCOPE_SYSTEM, Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE );
672         checkScopeUpdate( Artifact.SCOPE_SYSTEM, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_PROVIDED );
673         checkScopeUpdate( Artifact.SCOPE_SYSTEM, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME );
674         checkScopeUpdate( Artifact.SCOPE_SYSTEM, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_SYSTEM );
675         checkScopeUpdate( Artifact.SCOPE_SYSTEM, Artifact.SCOPE_TEST, Artifact.SCOPE_TEST );
676 
677         /* farthest = test */
678         checkScopeUpdate( Artifact.SCOPE_TEST, Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE );
679         checkScopeUpdate( Artifact.SCOPE_TEST, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_PROVIDED );
680         checkScopeUpdate( Artifact.SCOPE_TEST, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME );
681         checkScopeUpdate( Artifact.SCOPE_TEST, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_SYSTEM );
682         checkScopeUpdate( Artifact.SCOPE_TEST, Artifact.SCOPE_TEST, Artifact.SCOPE_TEST );
683     }
684 
685     private void checkScopeUpdate( String farthestScope, String nearestScope, String expectedScope )
686         throws ArtifactResolutionException, InvalidVersionSpecificationException
687     {
688         checkScopeUpdateDirect( farthestScope, nearestScope, expectedScope );
689         checkScopeUpdateTransitively( farthestScope, nearestScope, expectedScope );
690     }
691 
692     private void checkScopeUpdateTransitively( String farthestScope, String nearestScope, String expectedScope )
693         throws ArtifactResolutionException, InvalidVersionSpecificationException
694     {
695         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
696         ArtifactSpec b = createArtifactSpec( "b", "1.0", nearestScope );
697         ArtifactSpec c = createArtifactSpec( "c", "1.0" );
698         a.addDependency( c );
699         ArtifactSpec dNearest = createArtifactSpec( "d", "2.0" );
700         b.addDependency( dNearest );
701         ArtifactSpec dFarthest = createArtifactSpec( "d", "3.0", farthestScope );
702         c.addDependency( dFarthest );
703 
704         /* system and provided dependencies are not transitive */
705         if ( !Artifact.SCOPE_SYSTEM.equals( nearestScope ) && !Artifact.SCOPE_PROVIDED.equals( nearestScope ) )
706         {
707             checkScopeUpdate( a, b, expectedScope, "2.0" );
708         }
709     }
710 
711     private void checkScopeUpdateDirect( String farthestScope, String nearestScope, String expectedScope )
712         throws ArtifactResolutionException, InvalidVersionSpecificationException
713     {
714         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
715         ArtifactSpec b = createArtifactSpec( "b", "1.0" );
716         ArtifactSpec c = createArtifactSpec( "c", "1.0" );
717         a.addDependency( c );
718         ArtifactSpec dNearest = createArtifactSpec( "d", "2.0", nearestScope );
719         b.addDependency( dNearest );
720         ArtifactSpec dFarthest = createArtifactSpec( "d", "3.0", farthestScope );
721         c.addDependency( dFarthest );
722 
723         checkScopeUpdate( a, b, expectedScope, "2.0" );
724     }
725 
726     private void checkScopeUpdate( ArtifactSpec a, ArtifactSpec b, String expectedScope, String expectedVersion )
727         throws ArtifactResolutionException, InvalidVersionSpecificationException
728     {
729         ScopeArtifactFilter filter;
730         if ( Artifact.SCOPE_PROVIDED.equals( expectedScope ) )
731         {
732             filter = new ScopeArtifactFilter( Artifact.SCOPE_COMPILE );
733         }
734         else if ( Artifact.SCOPE_SYSTEM.equals( expectedScope ) )
735         {
736             filter = new ScopeArtifactFilter( Artifact.SCOPE_COMPILE );
737         }
738         else
739         {
740             filter = new ScopeArtifactFilter( expectedScope );
741         }
742 
743         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ), filter );
744         Artifact artifact = getArtifact( "d", res.getArtifacts() );
745         assertNotNull( "MNG-1895 Dependency was not added to resolution", artifact );
746         assertEquals( "Check scope", expectedScope, artifact.getScope() );
747         assertEquals( "Check version", expectedVersion, artifact.getVersion() );
748 
749         ArtifactSpec d = createArtifactSpec( "d", "1.0" );
750         res = collect( createSet( new Object[]{a.artifact, b.artifact, d.artifact} ), filter );
751         artifact = getArtifact( "d", res.getArtifacts() );
752         assertNotNull( "MNG-1895 Dependency was not added to resolution", artifact );
753         assertEquals( "Check scope", d.artifact.getScope(), artifact.getScope() );
754         assertEquals( "Check version", "1.0", artifact.getVersion() );
755     }
756 
757     public void disabledtestOptionalNotTransitiveButVersionIsInfluential()
758         throws ArtifactResolutionException, InvalidVersionSpecificationException
759     {
760         ArtifactSpec a = createArtifactSpec( "a", "1.0" );
761         ArtifactSpec b = createArtifactSpec( "b", "1.0" );
762         b.addDependency( "c", "3.0", true );
763         ArtifactSpec d = a.addDependency( "d", "1.0" );
764         ArtifactSpec e = d.addDependency( "e", "1.0" );
765         e.addDependency( "c", "2.0" );
766 
767         ArtifactSpec c = createArtifactSpec( "c", "3.0" );
768 
769         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
770         assertEquals( "Check artifact list",
771                       createSet( new Object[]{a.artifact, b.artifact, c.artifact, d.artifact, e.artifact} ),
772                       res.getArtifacts() );
773         Artifact artifact = getArtifact( "c", res.getArtifacts() );
774         assertEquals( "Check version", "3.0", artifact.getVersion() );
775     }
776 
777     public void testTestScopeNotTransitive()
778         throws ArtifactResolutionException, InvalidVersionSpecificationException
779     {
780         ArtifactSpec a = createArtifactSpec( "a", "1.0", Artifact.SCOPE_TEST );
781         ArtifactSpec b = createArtifactSpec( "b", "1.0" );
782         b.addDependency( "c", "3.0", Artifact.SCOPE_TEST );
783 
784         ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
785         assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
786     }
787 
788     public void testResolveOrder()
789         throws ArtifactResolutionException, InvalidVersionSpecificationException
790     {
791         ArtifactSpec m = createArtifactSpec( "m", "1.0" );
792         ArtifactSpec n = createArtifactSpec( "n", "1.0" );
793     
794         ArtifactResolutionResult res = collect( createSet( new Object[]{m.artifact, n.artifact} ) );
795         Iterator i = res.getArtifacts().iterator();
796         assertEquals( "m should be first", m.artifact, i.next() );
797         assertEquals( "n should be second", n.artifact, i.next() );
798 
799         res = collect( createSet( new Object[]{n.artifact, m.artifact} ) );
800         i = res.getArtifacts().iterator();
801         assertEquals( "n should be first", n.artifact, i.next() );
802         assertEquals( "m should be second", m.artifact, i.next() );
803     }
804 
805     private Artifact getArtifact( String id, Set artifacts )
806     {
807         for ( Iterator i = artifacts.iterator(); i.hasNext(); )
808         {
809             Artifact a = (Artifact) i.next();
810             if ( a.getArtifactId().equals( id ) && a.getGroupId().equals( GROUP_ID ) )
811             {
812                 return a;
813             }
814         }
815         return null;
816     }
817 
818     private ArtifactResolutionResult collect( Set artifacts )
819         throws ArtifactResolutionException
820     {
821         return collect( artifacts, null );
822     }
823 
824     private ArtifactResolutionResult collect( Set artifacts, ArtifactFilter filter )
825         throws ArtifactResolutionException
826     {
827         return artifactCollector.collect( artifacts, projectArtifact.artifact, null, null, source, filter,
828                                           Collections.EMPTY_LIST );
829     }
830 
831     private ArtifactResolutionResult collect( ArtifactSpec a )
832         throws ArtifactResolutionException
833     {
834         return artifactCollector.collect( Collections.singleton( a.artifact ), projectArtifact.artifact, null, null,
835                                           source, null, Collections.EMPTY_LIST );
836     }
837 
838     private ArtifactResolutionResult collect( ArtifactSpec a, ArtifactFilter filter )
839         throws ArtifactResolutionException
840     {
841         return artifactCollector.collect( Collections.singleton( a.artifact ), projectArtifact.artifact, null, null,
842                                           source, filter, Collections.EMPTY_LIST );
843     }
844 
845     private ArtifactResolutionResult collect( ArtifactSpec a, Artifact managedVersion )
846         throws ArtifactResolutionException
847     {
848         Map managedVersions = Collections.singletonMap( managedVersion.getDependencyConflictId(), managedVersion );
849         return artifactCollector.collect( Collections.singleton( a.artifact ), projectArtifact.artifact,
850                                           managedVersions, null, null, source, null, Collections.EMPTY_LIST );
851     }
852 
853     private ArtifactSpec createArtifactSpec( String id, String version )
854         throws InvalidVersionSpecificationException
855     {
856         return createArtifactSpec( id, version, Artifact.SCOPE_COMPILE );
857     }
858 
859     private ArtifactSpec createArtifactSpec( String id, String version, boolean optional )
860         throws InvalidVersionSpecificationException
861     {
862         return createArtifactSpec( id, version, Artifact.SCOPE_COMPILE, null, optional );
863     }
864 
865     private ArtifactSpec createArtifactSpec( String id, String version, String scope )
866         throws InvalidVersionSpecificationException
867     {
868         return createArtifactSpec( id, version, scope, null, false );
869     }
870 
871     private ArtifactSpec createArtifactSpec( String id, String version, String scope, String inheritedScope,
872                                          boolean optional )
873         throws InvalidVersionSpecificationException
874     {
875         VersionRange versionRange = VersionRange.createFromVersionSpec( version );
876         Artifact artifact = artifactFactory.createDependencyArtifact( GROUP_ID, id, versionRange, "jar", null, scope,
877                                                                       inheritedScope, optional );
878         ArtifactSpec spec = null;
879         if ( artifact != null )
880         {
881             spec = new ArtifactSpec();
882             spec.artifact = artifact;
883             source.addArtifact( spec );
884         }
885         return spec;
886     }
887 
888     private static Set createSet( Object[] x )
889     {
890         return new LinkedHashSet( Arrays.asList( x ) );
891     }
892 
893     private class ArtifactSpec
894     {
895         private Artifact artifact;
896 
897         private Set dependencies = new HashSet();
898 
899         public ArtifactSpec addDependency( String id, String version )
900             throws InvalidVersionSpecificationException
901         {
902             return addDependency( id, version, Artifact.SCOPE_COMPILE );
903         }
904 
905         public ArtifactSpec addDependency( String id, String version, String scope )
906             throws InvalidVersionSpecificationException
907         {
908             return addDependency( id, version, scope, false );
909         }
910 
911         private ArtifactSpec addDependency( ArtifactSpec dep )
912             throws InvalidVersionSpecificationException
913         {
914             if ( dep != null )
915             {
916                 dependencies.add( dep.artifact );
917             }
918             return dep;
919         }
920 
921         private ArtifactSpec addDependency( String id, String version, String scope, boolean optional )
922             throws InvalidVersionSpecificationException
923         {
924             ArtifactSpec dep = createArtifactSpec( id, version, scope, this.artifact.getScope(), optional );
925             return addDependency( dep );
926         }
927 
928         public ArtifactSpec addDependency( String id, String version, boolean optional )
929             throws InvalidVersionSpecificationException
930         {
931             return addDependency( id, version, Artifact.SCOPE_COMPILE, optional );
932         }
933     }
934 
935     private class Source
936         implements ArtifactMetadataSource
937     {
938         private Map artifacts = new HashMap();
939 
940         private Map versions = new HashMap();
941 
942         public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository,
943                                          List remoteRepositories )
944             throws ArtifactMetadataRetrievalException
945         {
946             String key = getKey( artifact );
947 
948             ArtifactSpec a = (ArtifactSpec) artifacts.get( key );
949             try
950             {
951                 return new ResolutionGroup( artifact, createArtifacts( artifactFactory, a.dependencies,
952                                                                        artifact.getScope(),
953                                                                        artifact.getDependencyFilter() ),
954                                                       Collections.EMPTY_LIST );
955             }
956             catch ( InvalidVersionSpecificationException e )
957             {
958                 throw new ArtifactMetadataRetrievalException( "Invalid version creating artifacts", e, artifact );
959             }
960         }
961 
962         private String getKey( Artifact artifact )
963         {
964             return artifact.getDependencyConflictId() + ":" + artifact.getVersion();
965             //return artifact.getDependencyConflictId();
966         }
967 
968         private Set createArtifacts( ArtifactFactory artifactFactory, Set dependencies, String inheritedScope,
969                                      ArtifactFilter dependencyFilter )
970             throws InvalidVersionSpecificationException
971         {
972             Set projectArtifacts = new HashSet();
973 
974             for ( Iterator i = dependencies.iterator(); i.hasNext(); )
975             {
976                 Artifact d = (Artifact) i.next();
977 
978                 VersionRange versionRange;
979                 if ( d.getVersionRange() != null )
980                 {
981                     versionRange = d.getVersionRange();
982                 }
983                 else
984                 {
985                     versionRange = VersionRange.createFromVersionSpec( d.getVersion() );
986                 }
987                 Artifact artifact;
988                 if ( d.getScope().equals( Artifact.SCOPE_TEST ) || d.getScope().equals( Artifact.SCOPE_PROVIDED ) )
989                 {
990                     /* don't call createDependencyArtifact as it'll ignore test and provided scopes */
991                     artifact = artifactFactory.createArtifact( d.getGroupId(), d.getArtifactId(), d.getVersion(), d
992                         .getScope(), d.getType() );
993                 }
994                 else
995                 {
996                     artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(),
997                                                                          versionRange, d.getType(), d.getClassifier(),
998                                                                          d.getScope(), inheritedScope, d.isOptional() );
999                 }
1000 
1001                 if ( artifact != null && ( dependencyFilter == null || dependencyFilter.include( artifact ) ) )
1002                 {
1003                     artifact.setDependencyFilter( dependencyFilter );
1004 
1005                     projectArtifacts.add( artifact );
1006                 }
1007             }
1008 
1009             return projectArtifacts;
1010         }
1011 
1012         public List retrieveAvailableVersions( Artifact artifact, ArtifactRepository localRepository,
1013                                                List remoteRepositories )
1014             throws ArtifactMetadataRetrievalException
1015         {
1016             List artifactVersions = (List) versions.get( artifact.getDependencyConflictId() );
1017             if ( artifactVersions == null )
1018             {
1019                 artifactVersions = Collections.EMPTY_LIST;
1020             }
1021             return artifactVersions;
1022         }
1023 
1024         public void addArtifact( ArtifactSpec spec )
1025         {
1026             artifacts.put( getKey( spec.artifact ), spec );
1027 
1028             String key = spec.artifact.getDependencyConflictId();
1029             List artifactVersions = (List) versions.get( key );
1030             if ( artifactVersions == null )
1031             {
1032                 artifactVersions = new ArrayList();
1033                 versions.put( key, artifactVersions );
1034             }
1035             if ( spec.artifact.getVersion() != null )
1036             {
1037                 artifactVersions.add( new DefaultArtifactVersion( spec.artifact.getVersion() ) );
1038             }
1039         }
1040 
1041         public Artifact retrieveRelocatedArtifact( Artifact artifact,
1042                                                    ArtifactRepository localRepository,
1043                                                    List remoteRepositories )
1044             throws ArtifactMetadataRetrievalException
1045         {
1046             return artifact;
1047         }
1048     }
1049 }