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