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;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Properties;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.InvalidRepositoryException;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.bridge.MavenRepositorySystem;
33  import org.apache.maven.execution.DefaultMavenExecutionRequest;
34  import org.apache.maven.execution.DefaultMavenExecutionResult;
35  import org.apache.maven.execution.MavenExecutionRequest;
36  import org.apache.maven.execution.MavenSession;
37  import org.apache.maven.internal.impl.DefaultLookup;
38  import org.apache.maven.internal.impl.DefaultSessionFactory;
39  import org.apache.maven.model.Build;
40  import org.apache.maven.model.Dependency;
41  import org.apache.maven.model.Exclusion;
42  import org.apache.maven.model.Model;
43  import org.apache.maven.model.Plugin;
44  import org.apache.maven.model.Repository;
45  import org.apache.maven.model.RepositoryPolicy;
46  import org.apache.maven.project.DefaultProjectBuildingRequest;
47  import org.apache.maven.project.MavenProject;
48  import org.apache.maven.project.ProjectBuildingRequest;
49  import org.apache.maven.repository.internal.MavenSessionBuilderSupplier;
50  import org.codehaus.plexus.PlexusContainer;
51  import org.codehaus.plexus.testing.PlexusTest;
52  import org.codehaus.plexus.util.FileUtils;
53  import org.eclipse.aether.RepositorySystem;
54  import org.eclipse.aether.RepositorySystemSession;
55  import org.eclipse.aether.repository.LocalRepository;
56  
57  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
58  import static org.mockito.Mockito.mock;
59  
60  @PlexusTest
61  public abstract class AbstractCoreMavenComponentTestCase {
62  
63      @Inject
64      protected PlexusContainer container;
65  
66      @Inject
67      protected RepositorySystem repositorySystem;
68  
69      @Inject
70      protected MavenRepositorySystem mavenRepositorySystem;
71  
72      @Inject
73      protected org.apache.maven.project.ProjectBuilder projectBuilder;
74  
75      protected abstract String getProjectsDirectory();
76  
77      protected PlexusContainer getContainer() {
78          return container;
79      }
80  
81      protected File getProject(String name) throws Exception {
82          File source = new File(new File(getBasedir(), getProjectsDirectory()), name);
83          File target = new File(new File(getBasedir(), "target"), name);
84          FileUtils.copyDirectoryStructureIfModified(source, target);
85          return new File(target, "pom.xml");
86      }
87  
88      protected MavenExecutionRequest createMavenExecutionRequest(File pom) throws Exception {
89          MavenExecutionRequest request = new DefaultMavenExecutionRequest()
90                  .setPom(pom)
91                  .setProjectPresent(true)
92                  .setShowErrors(true)
93                  .setPluginGroups(Arrays.asList("org.apache.maven.plugins"))
94                  .setLocalRepository(getLocalRepository())
95                  .setRemoteRepositories(getRemoteRepositories())
96                  .setPluginArtifactRepositories(getPluginArtifactRepositories())
97                  .setGoals(Arrays.asList("package"));
98  
99          if (pom != null) {
100             request.setMultiModuleProjectDirectory(pom.getParentFile());
101         }
102 
103         return request;
104     }
105 
106     // layer the creation of a project builder configuration with a request, but this will need to be
107     // a Maven subclass because we don't want to couple maven to the project builder which we need to
108     // separate.
109     protected MavenSession createMavenSession(File pom) throws Exception {
110         return createMavenSession(pom, new Properties());
111     }
112 
113     protected MavenSession createMavenSession(File pom, Properties executionProperties) throws Exception {
114         return createMavenSession(pom, executionProperties, false);
115     }
116 
117     protected MavenSession createMavenSession(File pom, Properties executionProperties, boolean includeModules)
118             throws Exception {
119         MavenExecutionRequest request = createMavenExecutionRequest(pom);
120 
121         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest()
122                 .setLocalRepository(request.getLocalRepository())
123                 .setRemoteRepositories(request.getRemoteRepositories())
124                 .setPluginArtifactRepositories(request.getPluginArtifactRepositories())
125                 .setSystemProperties(executionProperties)
126                 .setUserProperties(new Properties());
127 
128         List<MavenProject> projects = new ArrayList<>();
129 
130         if (pom != null) {
131             MavenProject project = projectBuilder.build(pom, configuration).getProject();
132 
133             projects.add(project);
134             if (includeModules) {
135                 for (String module : project.getModules()) {
136                     File modulePom = new File(pom.getParentFile(), module);
137                     if (modulePom.isDirectory()) {
138                         modulePom = new File(modulePom, "pom.xml");
139                     }
140                     projects.add(projectBuilder.build(modulePom, configuration).getProject());
141                 }
142             }
143         } else {
144             MavenProject project = createStubMavenProject();
145             project.setRemoteArtifactRepositories(request.getRemoteRepositories());
146             project.setPluginArtifactRepositories(request.getPluginArtifactRepositories());
147             projects.add(project);
148         }
149 
150         initRepoSession(configuration);
151 
152         DefaultSessionFactory defaultSessionFactory =
153                 new DefaultSessionFactory(mock(RepositorySystem.class), null, new DefaultLookup(container), null);
154 
155         MavenSession session = new MavenSession(
156                 getContainer(), configuration.getRepositorySession(), request, new DefaultMavenExecutionResult());
157         session.setProjects(projects);
158         session.setAllProjects(session.getProjects());
159         session.setSession(defaultSessionFactory.getSession(session));
160 
161         return session;
162     }
163 
164     protected void initRepoSession(ProjectBuildingRequest request) throws Exception {
165         File localRepoDir = new File(request.getLocalRepository().getBasedir());
166         LocalRepository localRepo = new LocalRepository(localRepoDir, "simple");
167         RepositorySystemSession session = new MavenSessionBuilderSupplier(repositorySystem)
168                 .get()
169                 .withLocalRepositories(localRepo)
170                 .build();
171         request.setRepositorySession(session);
172     }
173 
174     protected MavenProject createStubMavenProject() {
175         Model model = new Model();
176         model.setGroupId("org.apache.maven.test");
177         model.setArtifactId("maven-test");
178         model.setVersion("1.0");
179         return new MavenProject(model);
180     }
181 
182     protected List<ArtifactRepository> getRemoteRepositories() throws InvalidRepositoryException {
183         File repoDir = new File(getBasedir(), "src/test/remote-repo").getAbsoluteFile();
184 
185         RepositoryPolicy policy = new RepositoryPolicy();
186         policy.setEnabled(true);
187         policy.setChecksumPolicy("ignore");
188         policy.setUpdatePolicy("always");
189 
190         Repository repository = new Repository();
191         repository.setId(MavenRepositorySystem.DEFAULT_REMOTE_REPO_ID);
192         repository.setUrl("file://" + repoDir.toURI().getPath());
193         repository.setReleases(policy);
194         repository.setSnapshots(policy);
195 
196         return Arrays.asList(mavenRepositorySystem.buildArtifactRepository(repository));
197     }
198 
199     protected List<ArtifactRepository> getPluginArtifactRepositories() throws InvalidRepositoryException {
200         return getRemoteRepositories();
201     }
202 
203     protected ArtifactRepository getLocalRepository() throws InvalidRepositoryException {
204         File repoDir = new File(getBasedir(), "target/local-repo").getAbsoluteFile();
205 
206         return mavenRepositorySystem.createLocalRepository(repoDir);
207     }
208 
209     protected class ProjectBuilder {
210         private MavenProject project;
211 
212         public ProjectBuilder(MavenProject project) {
213             this.project = project;
214         }
215 
216         public ProjectBuilder(String groupId, String artifactId, String version) {
217             Model model = new Model();
218             model.setModelVersion("4.0.0");
219             model.setGroupId(groupId);
220             model.setArtifactId(artifactId);
221             model.setVersion(version);
222             model.setBuild(new Build());
223             project = new MavenProject(model);
224         }
225 
226         public ProjectBuilder setGroupId(String groupId) {
227             project.setGroupId(groupId);
228             return this;
229         }
230 
231         public ProjectBuilder setArtifactId(String artifactId) {
232             project.setArtifactId(artifactId);
233             return this;
234         }
235 
236         public ProjectBuilder setVersion(String version) {
237             project.setVersion(version);
238             return this;
239         }
240 
241         // Dependencies
242         //
243         public ProjectBuilder addDependency(String groupId, String artifactId, String version, String scope) {
244             return addDependency(groupId, artifactId, version, scope, (Exclusion) null);
245         }
246 
247         public ProjectBuilder addDependency(
248                 String groupId, String artifactId, String version, String scope, Exclusion exclusion) {
249             return addDependency(groupId, artifactId, version, scope, null, exclusion);
250         }
251 
252         public ProjectBuilder addDependency(
253                 String groupId, String artifactId, String version, String scope, String systemPath) {
254             return addDependency(groupId, artifactId, version, scope, systemPath, null);
255         }
256 
257         public ProjectBuilder addDependency(
258                 String groupId,
259                 String artifactId,
260                 String version,
261                 String scope,
262                 String systemPath,
263                 Exclusion exclusion) {
264             Dependency d = new Dependency();
265             d.setGroupId(groupId);
266             d.setArtifactId(artifactId);
267             d.setVersion(version);
268             d.setScope(scope);
269 
270             if (systemPath != null && scope.equals(Artifact.SCOPE_SYSTEM)) {
271                 d.setSystemPath(systemPath);
272             }
273 
274             if (exclusion != null) {
275                 d.addExclusion(exclusion);
276             }
277 
278             project.getDependencies().add(d);
279 
280             return this;
281         }
282 
283         // Plugins
284         //
285         public ProjectBuilder addPlugin(Plugin plugin) {
286             project.getBuildPlugins().add(plugin);
287             return this;
288         }
289 
290         public MavenProject get() {
291             return project;
292         }
293     }
294 }