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