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