View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.nio.charset.StandardCharsets;
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.artifact.repository.ArtifactRepositoryPolicy;
33  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34  import org.apache.maven.execution.DefaultMavenExecutionRequest;
35  import org.apache.maven.execution.DefaultMavenExecutionResult;
36  import org.apache.maven.execution.MavenSession;
37  import org.apache.maven.plugin.LegacySupport;
38  import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory;
39  import org.codehaus.plexus.ContainerConfiguration;
40  import org.codehaus.plexus.PlexusConstants;
41  import org.codehaus.plexus.PlexusTestCase;
42  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
43  import org.eclipse.aether.DefaultRepositorySystemSession;
44  import org.eclipse.aether.RepositorySystemSession;
45  import org.eclipse.aether.collection.DependencyGraphTransformer;
46  import org.eclipse.aether.collection.DependencyManager;
47  import org.eclipse.aether.collection.DependencySelector;
48  import org.eclipse.aether.collection.DependencyTraverser;
49  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
50  import org.eclipse.aether.repository.LocalRepository;
51  import org.eclipse.aether.repository.WorkspaceReader;
52  import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
53  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
54  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
55  import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
56  import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
57  import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
58  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
59  import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
60  import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
61  import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
62  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
63  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
64  import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
65  import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
66  
67  /**
68   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
69   */
70  public abstract class AbstractArtifactComponentTestCase extends PlexusTestCase {
71      protected ArtifactFactory artifactFactory;
72  
73      protected ArtifactRepositoryFactory artifactRepositoryFactory;
74  
75      @Override
76      protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
77          super.customizeContainerConfiguration(containerConfiguration);
78          containerConfiguration.setAutoWiring(true);
79          containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
80      }
81  
82      @Override
83      protected void setUp() throws Exception {
84          super.setUp();
85          artifactFactory = lookup(ArtifactFactory.class);
86          artifactRepositoryFactory = lookup(ArtifactRepositoryFactory.class);
87  
88          RepositorySystemSession repoSession = initRepoSession();
89          MavenSession session = new MavenSession(
90                  getContainer(), repoSession, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult());
91  
92          LegacySupport legacySupport = lookup(LegacySupport.class);
93          legacySupport.setSession(session);
94      }
95  
96      @Override
97      protected void tearDown() throws Exception {
98          release(artifactFactory);
99  
100         super.tearDown();
101     }
102 
103     protected abstract String component();
104 
105     /**
106      * Return an existing file, not a directory - causes creation to fail.
107      *
108      * @throws Exception
109      */
110     protected ArtifactRepository badLocalRepository() throws Exception {
111         String path = "target/test-repositories/" + component() + "/bad-local-repository";
112 
113         File f = new File(getBasedir(), path);
114 
115         f.createNewFile();
116 
117         ArtifactRepositoryLayout repoLayout =
118                 (ArtifactRepositoryLayout) lookup(ArtifactRepositoryLayout.ROLE, "default");
119 
120         return artifactRepositoryFactory.createArtifactRepository(
121                 "test", "file://" + f.getPath(), repoLayout, null, null);
122     }
123 
124     protected String getRepositoryLayout() {
125         return "default";
126     }
127 
128     protected ArtifactRepository localRepository() throws Exception {
129         String path = "target/test-repositories/" + component() + "/local-repository";
130 
131         File f = new File(getBasedir(), path);
132 
133         ArtifactRepositoryLayout repoLayout =
134                 (ArtifactRepositoryLayout) lookup(ArtifactRepositoryLayout.ROLE, "default");
135 
136         return artifactRepositoryFactory.createArtifactRepository(
137                 "local", "file://" + f.getPath(), repoLayout, null, null);
138     }
139 
140     protected ArtifactRepository remoteRepository() throws Exception {
141         String path = "target/test-repositories/" + component() + "/remote-repository";
142 
143         File f = new File(getBasedir(), path);
144 
145         ArtifactRepositoryLayout repoLayout =
146                 (ArtifactRepositoryLayout) lookup(ArtifactRepositoryLayout.ROLE, "default");
147 
148         return artifactRepositoryFactory.createArtifactRepository(
149                 "test",
150                 "file://" + f.getPath(),
151                 repoLayout,
152                 new ArtifactRepositoryPolicy(),
153                 new ArtifactRepositoryPolicy());
154     }
155 
156     protected ArtifactRepository badRemoteRepository() throws Exception {
157         ArtifactRepositoryLayout repoLayout =
158                 (ArtifactRepositoryLayout) lookup(ArtifactRepositoryLayout.ROLE, "default");
159 
160         return artifactRepositoryFactory.createArtifactRepository(
161                 "test", "http://foo.bar/repository", repoLayout, null, null);
162     }
163 
164     protected void assertRemoteArtifactPresent(Artifact artifact) throws Exception {
165         ArtifactRepository remoteRepo = remoteRepository();
166 
167         String path = remoteRepo.pathOf(artifact);
168 
169         File file = new File(remoteRepo.getBasedir(), path);
170 
171         if (!file.exists()) {
172             fail("Remote artifact " + file + " should be present.");
173         }
174     }
175 
176     protected void assertLocalArtifactPresent(Artifact artifact) throws Exception {
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             fail("Local artifact " + file + " should be present.");
185         }
186     }
187 
188     protected void assertRemoteArtifactNotPresent(Artifact artifact) throws Exception {
189         ArtifactRepository remoteRepo = remoteRepository();
190 
191         String path = remoteRepo.pathOf(artifact);
192 
193         File file = new File(remoteRepo.getBasedir(), path);
194 
195         if (file.exists()) {
196             fail("Remote artifact " + file + " should not be present.");
197         }
198     }
199 
200     protected void assertLocalArtifactNotPresent(Artifact artifact) throws Exception {
201         ArtifactRepository localRepo = localRepository();
202 
203         String path = localRepo.pathOf(artifact);
204 
205         File file = new File(localRepo.getBasedir(), path);
206 
207         if (file.exists()) {
208             fail("Local artifact " + file + " should not be present.");
209         }
210     }
211 
212     // ----------------------------------------------------------------------
213     //
214     // ----------------------------------------------------------------------
215 
216     protected List<ArtifactRepository> remoteRepositories() throws Exception {
217         List<ArtifactRepository> remoteRepositories = new ArrayList<>();
218 
219         remoteRepositories.add(remoteRepository());
220 
221         return remoteRepositories;
222     }
223 
224     // ----------------------------------------------------------------------
225     // Test artifact generation for unit tests
226     // ----------------------------------------------------------------------
227 
228     protected Artifact createLocalArtifact(String artifactId, String version) throws Exception {
229         Artifact artifact = createArtifact(artifactId, version);
230 
231         createArtifact(artifact, localRepository());
232 
233         return artifact;
234     }
235 
236     protected Artifact createRemoteArtifact(String artifactId, String version) throws Exception {
237         Artifact artifact = createArtifact(artifactId, version);
238 
239         createArtifact(artifact, remoteRepository());
240 
241         return artifact;
242     }
243 
244     protected void createLocalArtifact(Artifact artifact) throws Exception {
245         createArtifact(artifact, localRepository());
246     }
247 
248     protected void createRemoteArtifact(Artifact artifact) throws Exception {
249         createArtifact(artifact, remoteRepository());
250     }
251 
252     protected void createArtifact(Artifact artifact, ArtifactRepository repository) throws Exception {
253         String path = repository.pathOf(artifact);
254 
255         File artifactFile = new File(repository.getBasedir(), path);
256 
257         if (!artifactFile.getParentFile().exists()) {
258             artifactFile.getParentFile().mkdirs();
259         }
260         try (Writer writer = new OutputStreamWriter(new FileOutputStream(artifactFile), StandardCharsets.ISO_8859_1)) {
261             writer.write(artifact.getId());
262         }
263     }
264 
265     protected Artifact createArtifact(String artifactId, String version) throws Exception {
266         return createArtifact(artifactId, version, "jar");
267     }
268 
269     protected Artifact createArtifact(String artifactId, String version, String type) throws Exception {
270         return createArtifact("org.apache.maven", artifactId, version, type);
271     }
272 
273     protected Artifact createArtifact(String groupId, String artifactId, String version, String type) throws Exception {
274         Artifact a = artifactFactory.createBuildArtifact(groupId, artifactId, version, type);
275 
276         return a;
277     }
278 
279     protected void deleteLocalArtifact(Artifact artifact) throws Exception {
280         deleteArtifact(artifact, localRepository());
281     }
282 
283     protected void deleteArtifact(Artifact artifact, ArtifactRepository repository) throws Exception {
284         String path = repository.pathOf(artifact);
285 
286         File artifactFile = new File(repository.getBasedir(), path);
287 
288         if (artifactFile.exists()) {
289             if (!artifactFile.delete()) {
290                 throw new IOException("Failure while attempting to delete artifact " + artifactFile);
291             }
292         }
293     }
294 
295     protected RepositorySystemSession initRepoSession() throws Exception {
296         DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
297         session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));
298         DependencyTraverser depTraverser = new FatArtifactTraverser();
299         session.setDependencyTraverser(depTraverser);
300 
301         DependencyManager depManager = new ClassicDependencyManager();
302         session.setDependencyManager(depManager);
303 
304         DependencySelector depFilter = new AndDependencySelector(
305                 new ScopeDependencySelector("test", "provided"),
306                 new OptionalDependencySelector(),
307                 new ExclusionDependencySelector());
308         session.setDependencySelector(depFilter);
309 
310         DependencyGraphTransformer transformer = new ConflictResolver(
311                 new NearestVersionSelector(), new JavaScopeSelector(),
312                 new SimpleOptionalitySelector(), new JavaScopeDeriver());
313         transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner());
314         session.setDependencyGraphTransformer(transformer);
315 
316         LocalRepository localRepo = new LocalRepository(localRepository().getBasedir());
317         session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(session, localRepo));
318         try {
319             session.setWorkspaceReader(lookup(WorkspaceReader.class, "test"));
320         } catch (ComponentLookupException e) {
321             // no reader, nothing to do...
322         }
323 
324         return session;
325     }
326 }