View Javadoc

1   package org.apache.maven.artifact;
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.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.maven.artifact.factory.ArtifactFactory;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.plugin.LegacySupport;
33  import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
34  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
35  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
36  import org.apache.maven.execution.DefaultMavenExecutionRequest;
37  import org.apache.maven.execution.DefaultMavenExecutionResult;
38  import org.apache.maven.execution.MavenSession;
39  import org.codehaus.plexus.PlexusTestCase;
40  import org.sonatype.aether.RepositorySystemSession;
41  import org.sonatype.aether.collection.DependencyGraphTransformer;
42  import org.sonatype.aether.collection.DependencyManager;
43  import org.sonatype.aether.collection.DependencySelector;
44  import org.sonatype.aether.collection.DependencyTraverser;
45  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
46  import org.sonatype.aether.repository.LocalRepository;
47  import org.sonatype.aether.util.DefaultRepositorySystemSession;
48  import org.sonatype.aether.util.graph.manager.ClassicDependencyManager;
49  import org.sonatype.aether.util.graph.selector.AndDependencySelector;
50  import org.sonatype.aether.util.graph.selector.ExclusionDependencySelector;
51  import org.sonatype.aether.util.graph.selector.OptionalDependencySelector;
52  import org.sonatype.aether.util.graph.selector.ScopeDependencySelector;
53  import org.sonatype.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
54  import org.sonatype.aether.util.graph.transformer.NearestVersionConflictResolver;
55  import org.sonatype.aether.util.graph.transformer.ConflictMarker;
56  import org.sonatype.aether.util.graph.transformer.JavaDependencyContextRefiner;
57  import org.sonatype.aether.util.graph.transformer.JavaEffectiveScopeCalculator;
58  import org.sonatype.aether.util.graph.traverser.FatArtifactTraverser;
59  
60  /**
61   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
62   */
63  public abstract class AbstractArtifactComponentTestCase
64      extends PlexusTestCase
65  {
66      protected ArtifactFactory artifactFactory;
67  
68      protected ArtifactRepositoryFactory artifactRepositoryFactory;
69      
70      @Override
71      protected void setUp()
72          throws Exception
73      {
74          super.setUp();
75          artifactFactory = lookup( ArtifactFactory.class);        
76          artifactRepositoryFactory = lookup( ArtifactRepositoryFactory.class );
77  
78          RepositorySystemSession repoSession = initRepoSession();
79          MavenSession session =
80              new MavenSession( getContainer(), repoSession, new DefaultMavenExecutionRequest(),
81                                new DefaultMavenExecutionResult() );
82  
83          LegacySupport legacySupport = lookup(LegacySupport.class);
84          legacySupport.setSession( session );
85      }
86      
87      @Override
88      protected void tearDown()
89          throws Exception
90      {
91          release( artifactFactory );
92          
93          super.tearDown();
94      }
95  
96      protected abstract String component();
97  
98      /**
99       * Return an existing file, not a directory - causes creation to fail.
100      * 
101      * @throws Exception
102      */
103     protected ArtifactRepository badLocalRepository()
104         throws Exception
105     {
106         String path = "target/test-repositories/" + component() + "/bad-local-repository";
107 
108         File f = new File( getBasedir(), path );
109 
110         f.createNewFile();
111 
112         ArtifactRepositoryLayout repoLayout =
113             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
114 
115         return artifactRepositoryFactory.createArtifactRepository( "test", "file://" + f.getPath(), repoLayout, null, null );
116     }
117 
118     protected String getRepositoryLayout()
119     {
120         return "default";
121     }
122 
123     protected ArtifactRepository localRepository()
124         throws Exception
125     {
126         String path = "target/test-repositories/" + component() + "/local-repository";
127 
128         File f = new File( getBasedir(), path );
129 
130         ArtifactRepositoryLayout repoLayout =
131             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
132 
133         return artifactRepositoryFactory.createArtifactRepository( "local", "file://" + f.getPath(), repoLayout, null, null );
134     }
135 
136     protected ArtifactRepository remoteRepository()
137         throws Exception
138     {
139         String path = "target/test-repositories/" + component() + "/remote-repository";
140 
141         File f = new File( getBasedir(), path );
142 
143         ArtifactRepositoryLayout repoLayout =
144             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
145 
146         return artifactRepositoryFactory.createArtifactRepository( "test", "file://" + f.getPath(), repoLayout,
147                                               new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() );
148     }
149 
150     protected ArtifactRepository badRemoteRepository()
151         throws Exception
152     {
153         ArtifactRepositoryLayout repoLayout =
154             (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
155 
156         return artifactRepositoryFactory.createArtifactRepository( "test", "http://foo.bar/repository", repoLayout, null, null );
157     }
158 
159     protected void assertRemoteArtifactPresent( Artifact artifact )
160         throws Exception
161     {
162         ArtifactRepository remoteRepo = remoteRepository();
163 
164         String path = remoteRepo.pathOf( artifact );
165 
166         File file = new File( remoteRepo.getBasedir(), path );
167 
168         if ( !file.exists() )
169         {
170             fail( "Remote artifact " + file + " should be present." );
171         }
172     }
173 
174     protected void assertLocalArtifactPresent( Artifact artifact )
175         throws Exception
176     {
177         ArtifactRepository localRepo = localRepository();
178 
179         String path = localRepo.pathOf( artifact );
180 
181         File file = new File( localRepo.getBasedir(), path );
182 
183         if ( !file.exists() )
184         {
185             fail( "Local artifact " + file + " should be present." );
186         }
187     }
188 
189     protected void assertRemoteArtifactNotPresent( Artifact artifact )
190         throws Exception
191     {
192         ArtifactRepository remoteRepo = remoteRepository();
193 
194         String path = remoteRepo.pathOf( artifact );
195 
196         File file = new File( remoteRepo.getBasedir(), path );
197 
198         if ( file.exists() )
199         {
200             fail( "Remote artifact " + file + " should not be present." );
201         }
202     }
203 
204     protected void assertLocalArtifactNotPresent( Artifact artifact )
205         throws Exception
206     {
207         ArtifactRepository localRepo = localRepository();
208 
209         String path = localRepo.pathOf( artifact );
210 
211         File file = new File( localRepo.getBasedir(), path );
212 
213         if ( file.exists() )
214         {
215             fail( "Local artifact " + file + " should not be present." );
216         }
217     }
218 
219     // ----------------------------------------------------------------------
220     //
221     // ----------------------------------------------------------------------
222 
223     protected List<ArtifactRepository> remoteRepositories()
224         throws Exception
225     {
226         List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
227 
228         remoteRepositories.add( remoteRepository() );
229 
230         return remoteRepositories;
231     }
232 
233     // ----------------------------------------------------------------------
234     // Test artifact generation for unit tests
235     // ----------------------------------------------------------------------
236 
237     protected Artifact createLocalArtifact( String artifactId, String version )
238         throws Exception
239     {
240         Artifact artifact = createArtifact( artifactId, version );
241 
242         createArtifact( artifact, localRepository() );
243 
244         return artifact;
245     }
246 
247     protected Artifact createRemoteArtifact( String artifactId, String version )
248         throws Exception
249     {
250         Artifact artifact = createArtifact( artifactId, version );
251 
252         createArtifact( artifact, remoteRepository() );
253 
254         return artifact;
255     }
256 
257     protected void createLocalArtifact( Artifact artifact )
258         throws Exception
259     {
260         createArtifact( artifact, localRepository() );
261     }
262 
263     protected void createRemoteArtifact( Artifact artifact )
264         throws Exception
265     {
266         createArtifact( artifact, remoteRepository() );
267     }
268 
269     protected void createArtifact( Artifact artifact, ArtifactRepository repository )
270         throws Exception
271     {
272         String path = repository.pathOf( artifact );
273 
274         File artifactFile = new File( repository.getBasedir(), path );
275 
276         if ( !artifactFile.getParentFile().exists() )
277         {
278             artifactFile.getParentFile().mkdirs();
279         }
280 
281         Writer writer = new OutputStreamWriter( new FileOutputStream( artifactFile ), "ISO-8859-1" );
282 
283         writer.write( artifact.getId() );
284 
285         writer.close();
286     }
287 
288     protected Artifact createArtifact( String artifactId, String version )
289         throws Exception
290     {
291         return createArtifact( artifactId, version, "jar" );
292     }
293 
294     protected Artifact createArtifact( String artifactId, String version, String type )
295         throws Exception
296     {
297         return createArtifact( "org.apache.maven", artifactId, version, type );
298     }
299 
300     protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
301         throws Exception
302     {
303         Artifact a = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
304                 
305         return a;
306     }
307 
308     protected void deleteLocalArtifact( Artifact artifact )
309         throws Exception
310     {
311         deleteArtifact( artifact, localRepository() );
312     }
313 
314     protected void deleteArtifact( Artifact artifact, ArtifactRepository repository )
315         throws Exception
316     {
317         String path = repository.pathOf( artifact );
318 
319         File artifactFile = new File( repository.getBasedir(), path );
320 
321         if ( artifactFile.exists() )
322         {
323             if ( !artifactFile.delete() )
324             {
325                 throw new IOException( "Failure while attempting to delete artifact " + artifactFile );
326             }
327         }
328     }
329 
330     protected RepositorySystemSession initRepoSession()
331         throws Exception
332     {
333         DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
334         session.setIgnoreMissingArtifactDescriptor( true );
335         session.setIgnoreInvalidArtifactDescriptor( true );
336         DependencyTraverser depTraverser = new FatArtifactTraverser();
337         session.setDependencyTraverser( depTraverser );
338 
339         DependencyManager depManager = new ClassicDependencyManager();
340         session.setDependencyManager( depManager );
341 
342         DependencySelector depFilter =
343             new AndDependencySelector( new ScopeDependencySelector( "test", "provided" ),
344                                        new OptionalDependencySelector(), new ExclusionDependencySelector() );
345         session.setDependencySelector( depFilter );
346 
347         DependencyGraphTransformer transformer =
348             new ChainedDependencyGraphTransformer( new ConflictMarker(), new JavaEffectiveScopeCalculator(),
349                                                    new NearestVersionConflictResolver(),
350                                                    new JavaDependencyContextRefiner() );
351         session.setDependencyGraphTransformer( transformer );
352 
353         session.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepository().getBasedir() ) );
354 
355         return session;
356     }
357 
358 }