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.project;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.FileNotFoundException;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  import java.util.Arrays;
28  
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
31  import org.apache.maven.model.building.ModelBuildingException;
32  import org.apache.maven.model.building.ModelProblem;
33  import org.apache.maven.repository.RepositorySystem;
34  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
35  import org.codehaus.plexus.ContainerConfiguration;
36  import org.codehaus.plexus.PlexusContainer;
37  import org.codehaus.plexus.testing.PlexusTestConfiguration;
38  import org.eclipse.aether.DefaultRepositorySystemSession;
39  import org.junit.jupiter.api.BeforeEach;
40  
41  /**
42   * @author Jason van Zyl
43   */
44  public abstract class AbstractMavenProjectTestCase implements PlexusTestConfiguration {
45  
46      @Inject
47      protected PlexusContainer container;
48  
49      protected ProjectBuilder projectBuilder;
50  
51      @Inject
52      protected RepositorySystem repositorySystem;
53  
54      @Inject
55      protected ArtifactRepositoryLayout repoLayout;
56  
57      @BeforeEach
58      protected void setUp() throws Exception {
59  
60          if (container.hasComponent(ProjectBuilder.class, "test")) {
61              projectBuilder = container.lookup(ProjectBuilder.class, "test");
62          } else {
63              // default over to the main project builder...
64              projectBuilder = container.lookup(ProjectBuilder.class);
65          }
66      }
67  
68      protected ProjectBuilder getProjectBuilder() {
69          return projectBuilder;
70      }
71  
72      @Override
73      public void customizeConfiguration(ContainerConfiguration containerConfiguration) {
74          String name = AbstractMavenProjectTestCase.class.getName().replace('.', '/') + ".xml";
75          containerConfiguration.setContainerConfiguration(name);
76      }
77  
78      // ----------------------------------------------------------------------
79      // Local repository
80      // ----------------------------------------------------------------------
81  
82      protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
83          File markerFile = getFileForClasspathResource("local-repo/marker.txt");
84  
85          return markerFile.getAbsoluteFile().getParentFile();
86      }
87  
88      protected static File getFileForClasspathResource(String resource)
89              throws FileNotFoundException, URISyntaxException {
90          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
91  
92          URL resourceUrl = cloader.getResource(resource);
93  
94          if (resourceUrl == null) {
95              throw new FileNotFoundException("Unable to find: " + resource);
96          }
97  
98          return new File(resourceUrl.toURI());
99      }
100 
101     protected ArtifactRepository getLocalRepository() throws Exception {
102 
103         ArtifactRepository r = repositorySystem.createArtifactRepository(
104                 "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null);
105 
106         return r;
107     }
108 
109     // ----------------------------------------------------------------------
110     // Project building
111     // ----------------------------------------------------------------------
112 
113     protected MavenProject getProjectWithDependencies(File pom) throws Exception {
114         ProjectBuildingRequest configuration = newBuildingRequest();
115         configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
116         configuration.setProcessPlugins(false);
117         configuration.setResolveDependencies(true);
118 
119         try {
120             return projectBuilder.build(pom, configuration).getProject();
121         } catch (Exception e) {
122             Throwable cause = e.getCause();
123             if (cause instanceof ModelBuildingException) {
124                 String message = "In: " + pom + "\n\n";
125                 for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
126                     message += problem + "\n";
127                 }
128                 System.out.println(message);
129             }
130 
131             throw e;
132         }
133     }
134 
135     protected MavenProject getProject(File pom) throws Exception {
136         ProjectBuildingRequest configuration = newBuildingRequest();
137 
138         return projectBuilder.build(pom, configuration).getProject();
139     }
140 
141     protected MavenProject getProjectFromRemoteRepository(final File pom) throws Exception {
142         final ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
143         configuration.setLocalRepository(this.getLocalRepository());
144         configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
145         initRepoSession(configuration);
146 
147         return projectBuilder.build(pom, configuration).getProject();
148     }
149 
150     protected ProjectBuildingRequest newBuildingRequest() throws Exception {
151         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
152         configuration.setLocalRepository(getLocalRepository());
153         initRepoSession(configuration);
154         return configuration;
155     }
156 
157     protected void initRepoSession(ProjectBuildingRequest request) {
158         File localRepo = new File(request.getLocalRepository().getBasedir());
159         DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
160         repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
161         request.setRepositorySession(repoSession);
162     }
163 }