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.api.Session;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.bridge.MavenRepositorySystem;
32  import org.apache.maven.execution.DefaultMavenExecutionRequest;
33  import org.apache.maven.execution.DefaultMavenExecutionResult;
34  import org.apache.maven.execution.MavenSession;
35  import org.apache.maven.internal.impl.DefaultLookup;
36  import org.apache.maven.internal.impl.DefaultSession;
37  import org.apache.maven.internal.impl.DefaultSessionFactory;
38  import org.apache.maven.internal.impl.InternalMavenSession;
39  import org.apache.maven.model.building.ModelBuildingException;
40  import org.apache.maven.model.building.ModelProblem;
41  import org.apache.maven.session.scope.internal.SessionScope;
42  import org.codehaus.plexus.PlexusContainer;
43  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
44  import org.codehaus.plexus.testing.PlexusTest;
45  import org.eclipse.aether.DefaultRepositoryCache;
46  import org.eclipse.aether.DefaultRepositorySystemSession;
47  import org.eclipse.aether.RepositorySystem;
48  import org.junit.jupiter.api.BeforeEach;
49  
50  /**
51   */
52  @PlexusTest
53  public abstract class AbstractMavenProjectTestCase {
54      protected ProjectBuilder projectBuilder;
55  
56      @Inject
57      protected MavenRepositorySystem repositorySystem;
58  
59      @Inject
60      protected RepositorySystem repoSystem;
61  
62      @Inject
63      protected PlexusContainer container;
64  
65      public PlexusContainer getContainer() {
66          return container;
67      }
68  
69      @BeforeEach
70      public void setUp() throws Exception {
71          if (getContainer().hasComponent(ProjectBuilder.class, "test")) {
72              projectBuilder = getContainer().lookup(ProjectBuilder.class, "test");
73          } else {
74              // default over to the main project builder...
75              projectBuilder = getContainer().lookup(ProjectBuilder.class);
76          }
77      }
78  
79      protected ProjectBuilder getProjectBuilder() {
80          return projectBuilder;
81      }
82  
83      // ----------------------------------------------------------------------
84      // Local repository
85      // ----------------------------------------------------------------------
86  
87      protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
88          File markerFile = getFileForClasspathResource("local-repo/marker.txt");
89  
90          return markerFile.getAbsoluteFile().getParentFile();
91      }
92  
93      protected static File getFileForClasspathResource(String resource)
94              throws FileNotFoundException, URISyntaxException {
95          ClassLoader cloader = Thread.currentThread().getContextClassLoader();
96  
97          URL resourceUrl = cloader.getResource(resource);
98  
99          if (resourceUrl == null) {
100             throw new FileNotFoundException("Unable to find: " + resource);
101         }
102 
103         return new File(resourceUrl.toURI());
104     }
105 
106     protected ArtifactRepository getLocalRepository() throws Exception {
107         return repositorySystem.createLocalRepository(getLocalRepositoryPath());
108     }
109 
110     // ----------------------------------------------------------------------
111     // Project building
112     // ----------------------------------------------------------------------
113 
114     protected MavenProject getProjectWithDependencies(File pom) throws Exception {
115         ProjectBuildingRequest configuration = newBuildingRequest();
116         configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
117         configuration.setProcessPlugins(false);
118         configuration.setResolveDependencies(true);
119 
120         try {
121             return projectBuilder.build(pom, configuration).getProject();
122         } catch (Exception e) {
123             Throwable cause = e.getCause();
124             if (cause instanceof ModelBuildingException modelBuildingException) {
125                 String message = "In: " + pom + "\n\n";
126                 for (ModelProblem problem : modelBuildingException.getProblems()) {
127                     message += problem + "\n";
128                 }
129                 System.out.println(message);
130             }
131 
132             throw e;
133         }
134     }
135 
136     protected MavenProject getProject(File pom) throws Exception {
137         ProjectBuildingRequest configuration = newBuildingRequest();
138 
139         return projectBuilder.build(pom, configuration).getProject();
140     }
141 
142     protected MavenProject getProjectFromRemoteRepository(final File pom) throws Exception {
143         final ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
144         configuration.setLocalRepository(this.getLocalRepository());
145         configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
146         initRepoSession(configuration);
147 
148         return projectBuilder.build(pom, configuration).getProject();
149     }
150 
151     protected ProjectBuildingRequest newBuildingRequest() throws Exception {
152         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
153         configuration.setLocalRepository(getLocalRepository());
154         configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
155         initRepoSession(configuration);
156         return configuration;
157     }
158 
159     protected void initRepoSession(ProjectBuildingRequest request) throws ComponentLookupException {
160         File localRepo = new File(request.getLocalRepository().getBasedir());
161         DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
162 
163         DefaultSessionFactory defaultSessionFactory =
164                 new DefaultSessionFactory(repoSystem, repositorySystem, new DefaultLookup(container), null);
165 
166         MavenSession session = new MavenSession(
167                 getContainer(), repoSession, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult());
168         session.setSession(defaultSessionFactory.newSession(session));
169 
170         DefaultSession s = new DefaultSession(session, null, null, null, null, null);
171         SessionScope scope = container.lookup(SessionScope.class);
172         scope.enter();
173         scope.seed(Session.class, s);
174         scope.seed(InternalMavenSession.class, s);
175 
176         repoSession.setCache(new DefaultRepositoryCache());
177         repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
178         request.setRepositorySession(repoSession);
179     }
180 }