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