View Javadoc
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.Collections;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.AbstractArtifactComponentTestCase;
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
33  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
34  import org.apache.maven.artifact.metadata.ResolutionGroup;
35  import org.apache.maven.artifact.repository.ArtifactRepository;
36  import org.apache.maven.artifact.versioning.ArtifactVersion;
37  import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest;
38  
39  // It would be cool if there was a hook that i could use to setup a test environment.
40  // I want to setup a local/remote repositories for testing but i don't want to have
41  // to change them when i change the layout of the repositories. So i want to generate
42  // the structure i want to test by using the artifact handler manager which dictates
43  // the layout used for a particular artifact type.
44  
45  /**
46   * @author Jason van Zyl
47   */
48  public class ArtifactResolverTest
49      extends AbstractArtifactComponentTestCase
50  {
51      private DefaultArtifactResolver artifactResolver;
52  
53      private Artifact projectArtifact;
54  
55      @Override
56      protected void setUp()
57          throws Exception
58      {
59          super.setUp();
60  
61          artifactResolver = (DefaultArtifactResolver) lookup( ArtifactResolver.class );
62  
63          projectArtifact = createLocalArtifact( "project", "3.0" );
64      }
65  
66      @Override
67      protected void tearDown()
68          throws Exception
69      {
70          artifactFactory = null;
71          projectArtifact = null;
72          super.tearDown();
73      }
74  
75      @Override
76      protected String component()
77      {
78          return "resolver";
79      }
80  
81      public void testResolutionOfASingleArtifactWhereTheArtifactIsPresentInTheLocalRepository()
82          throws Exception
83      {
84          Artifact a = createLocalArtifact( "a", "1.0" );
85  
86          artifactResolver.resolve( a, remoteRepositories(), localRepository() );
87  
88          assertLocalArtifactPresent( a );
89      }
90  
91      public void testResolutionOfASingleArtifactWhereTheArtifactIsNotPresentLocallyAndMustBeRetrievedFromTheRemoteRepository()
92          throws Exception
93      {
94          Artifact b = createRemoteArtifact( "b", "1.0-SNAPSHOT" );
95          deleteLocalArtifact( b );
96          artifactResolver.resolve( b, remoteRepositories(), localRepository() );
97          assertLocalArtifactPresent( b );
98      }
99  
100     @Override
101     protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
102         throws Exception
103     {
104         // for the anonymous classes
105         return super.createArtifact( groupId, artifactId, version, type );
106     }
107 
108     public void testTransitiveResolutionWhereAllArtifactsArePresentInTheLocalRepository()
109         throws Exception
110     {
111         Artifact g = createLocalArtifact( "g", "1.0" );
112 
113         Artifact h = createLocalArtifact( "h", "1.0" );
114 
115         ArtifactResolutionResult result = artifactResolver.resolveTransitively( Collections.singleton( g ), projectArtifact, remoteRepositories(), localRepository(), null );
116 
117         printErrors( result );
118 
119         assertEquals( 2, result.getArtifacts().size() );
120 
121         assertTrue( result.getArtifacts().contains( g ) );
122 
123         assertTrue( result.getArtifacts().contains( h ) );
124 
125         assertLocalArtifactPresent( g );
126 
127         assertLocalArtifactPresent( h );
128     }
129 
130     public void testTransitiveResolutionWhereAllArtifactsAreNotPresentInTheLocalRepositoryAndMustBeRetrievedFromTheRemoteRepository()
131         throws Exception
132     {
133         Artifact i = createRemoteArtifact( "i", "1.0-SNAPSHOT" );
134         deleteLocalArtifact( i );
135 
136         Artifact j = createRemoteArtifact( "j", "1.0-SNAPSHOT" );
137         deleteLocalArtifact( j );
138 
139         ArtifactResolutionResult result = artifactResolver.resolveTransitively( Collections.singleton( i ), projectArtifact, remoteRepositories(), localRepository(), null );
140 
141         printErrors( result );
142 
143         assertEquals( 2, result.getArtifacts().size() );
144 
145         assertTrue( result.getArtifacts().contains( i ) );
146 
147         assertTrue( result.getArtifacts().contains( j ) );
148 
149         assertLocalArtifactPresent( i );
150 
151         assertLocalArtifactPresent( j );
152     }
153 
154     public void testResolutionFailureWhenArtifactNotPresentInRemoteRepository()
155         throws Exception
156     {
157         Artifact k = createArtifact( "k", "1.0" );
158 
159         try
160         {
161             artifactResolver.resolve( k, remoteRepositories(), localRepository() );
162             fail( "Resolution succeeded when it should have failed" );
163         }
164         catch ( ArtifactNotFoundException expected )
165         {
166             assertTrue( true );
167         }
168     }
169 
170     public void testResolutionOfAnArtifactWhereOneRemoteRepositoryIsBadButOneIsGood()
171         throws Exception
172     {
173         Artifact l = createRemoteArtifact( "l", "1.0-SNAPSHOT" );
174         deleteLocalArtifact( l );
175 
176         List<ArtifactRepository> repositories = new ArrayList<>();
177         repositories.add( remoteRepository() );
178         repositories.add( badRemoteRepository() );
179 
180         artifactResolver.resolve( l, repositories, localRepository() );
181 
182         assertLocalArtifactPresent( l );
183     }
184 
185     public void testTransitiveResolutionOrder()
186         throws Exception
187     {
188         Artifact m = createLocalArtifact( "m", "1.0" );
189 
190         Artifact n = createLocalArtifact( "n", "1.0" );
191 
192         ArtifactMetadataSource mds = new ArtifactMetadataSource()
193         {
194             public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository,
195                                              List<ArtifactRepository> remoteRepositories )
196                 throws ArtifactMetadataRetrievalException
197             {
198                 Set<Artifact> dependencies = new HashSet<>();
199 
200                 return new ResolutionGroup( artifact, dependencies, remoteRepositories );
201             }
202 
203             public List<ArtifactVersion> retrieveAvailableVersions( Artifact artifact,
204                                                                     ArtifactRepository localRepository,
205                                                                     List<ArtifactRepository> remoteRepositories )
206                 throws ArtifactMetadataRetrievalException
207             {
208                 throw new UnsupportedOperationException( "Cannot get available versions in this test case" );
209             }
210 
211             public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
212                                                                                             Artifact artifact,
213                                                                                             ArtifactRepository localRepository,
214                                                                                             ArtifactRepository remoteRepository )
215                 throws ArtifactMetadataRetrievalException
216             {
217                 throw new UnsupportedOperationException( "Cannot get available versions in this test case" );
218             }
219 
220             public ResolutionGroup retrieve( MetadataResolutionRequest request )
221                 throws ArtifactMetadataRetrievalException
222             {
223                 return retrieve( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories() );
224             }
225 
226             public List<ArtifactVersion> retrieveAvailableVersions( MetadataResolutionRequest request )
227                 throws ArtifactMetadataRetrievalException
228             {
229                 return retrieveAvailableVersions( request.getArtifact(), request.getLocalRepository(), request.getRemoteRepositories() );
230             }
231         };
232 
233         ArtifactResolutionResult result = null;
234 
235         Set<Artifact> set = new LinkedHashSet<>();
236         set.add( n );
237         set.add( m );
238 
239         result =
240             artifactResolver.resolveTransitively( set, projectArtifact, remoteRepositories(), localRepository(), mds );
241 
242         printErrors( result );
243 
244         Iterator<Artifact> i = result.getArtifacts().iterator();
245         assertEquals( "n should be first", n, i.next() );
246         assertEquals( "m should be second", m, i.next() );
247 
248         // inverse order
249         set = new LinkedHashSet<>();
250         set.add( m );
251         set.add( n );
252 
253         result =
254             artifactResolver.resolveTransitively( set, projectArtifact, remoteRepositories(), localRepository(), mds );
255 
256         printErrors( result );
257 
258         i = result.getArtifacts().iterator();
259         assertEquals( "m should be first", m, i.next() );
260         assertEquals( "n should be second", n, i.next() );
261     }
262 
263     private void printErrors( ArtifactResolutionResult result )
264     {
265         if ( result.hasMissingArtifacts() )
266         {
267             for ( Artifact artifact : result.getMissingArtifacts() )
268             {
269                 System.err.println( "Missing: " + artifact );
270             }
271         }
272 
273         if ( result.hasExceptions() )
274         {
275             for ( Exception e : result.getExceptions() )
276             {
277                 e.printStackTrace();
278             }
279         }
280     }
281 
282 }