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.bridge.MavenRepositorySystem;
31  import org.apache.maven.execution.DefaultMavenExecutionRequest;
32  import org.apache.maven.execution.DefaultMavenExecutionResult;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.internal.impl.DefaultLookup;
35  import org.apache.maven.internal.impl.DefaultSession;
36  import org.apache.maven.internal.impl.DefaultSessionFactory;
37  import org.apache.maven.model.building.ModelBuildingException;
38  import org.apache.maven.model.building.ModelProblem;
39  import org.codehaus.plexus.PlexusContainer;
40  import org.codehaus.plexus.testing.PlexusTest;
41  import org.eclipse.aether.DefaultRepositoryCache;
42  import org.eclipse.aether.DefaultRepositorySystemSession;
43  import org.eclipse.aether.RepositorySystem;
44  import org.junit.jupiter.api.BeforeEach;
45  
46  import static org.mockito.Mockito.mock;
47  
48  /**
49   */
50  @PlexusTest
51  public abstract class AbstractMavenProjectTestCase {
52      protected ProjectBuilder projectBuilder;
53  
54      @Inject
55      protected MavenRepositorySystem repositorySystem;
56  
57      @Inject
58      protected PlexusContainer container;
59  
60      public PlexusContainer getContainer() {
61          return container;
62      }
63  
64      @BeforeEach
65      public void setUp() throws Exception {
66          if (getContainer().hasComponent(ProjectBuilder.class, "test")) {
67              projectBuilder = getContainer().lookup(ProjectBuilder.class, "test");
68          } else {
69              // default over to the main project builder...
70              projectBuilder = getContainer().lookup(ProjectBuilder.class);
71          }
72      }
73  
74      protected ProjectBuilder getProjectBuilder() {
75          return projectBuilder;
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         return repositorySystem.createLocalRepository(getLocalRepositoryPath());
103     }
104 
105     // ----------------------------------------------------------------------
106     // Project building
107     // ----------------------------------------------------------------------
108 
109     protected MavenProject getProjectWithDependencies(File pom) throws Exception {
110         ProjectBuildingRequest configuration = newBuildingRequest();
111         configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
112         configuration.setProcessPlugins(false);
113         configuration.setResolveDependencies(true);
114 
115         try {
116             return projectBuilder.build(pom, configuration).getProject();
117         } catch (Exception e) {
118             Throwable cause = e.getCause();
119             if (cause instanceof ModelBuildingException) {
120                 String message = "In: " + pom + "\n\n";
121                 for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
122                     message += problem + "\n";
123                 }
124                 System.out.println(message);
125             }
126 
127             throw e;
128         }
129     }
130 
131     protected MavenProject getProject(File pom) throws Exception {
132         ProjectBuildingRequest configuration = newBuildingRequest();
133 
134         return projectBuilder.build(pom, configuration).getProject();
135     }
136 
137     protected MavenProject getProjectFromRemoteRepository(final File pom) throws Exception {
138         final ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
139         configuration.setLocalRepository(this.getLocalRepository());
140         configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
141         initRepoSession(configuration);
142 
143         return projectBuilder.build(pom, configuration).getProject();
144     }
145 
146     protected ProjectBuildingRequest newBuildingRequest() throws Exception {
147         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
148         configuration.setLocalRepository(getLocalRepository());
149         configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
150         initRepoSession(configuration);
151         return configuration;
152     }
153 
154     protected void initRepoSession(ProjectBuildingRequest request) {
155         File localRepo = new File(request.getLocalRepository().getBasedir());
156         DefaultRepositorySystemSession repoSession = new DefaultRepositorySystemSession(h -> false);
157 
158         DefaultSessionFactory defaultSessionFactory =
159                 new DefaultSessionFactory(mock(RepositorySystem.class), null, new DefaultLookup(container), null);
160 
161         MavenSession session = new MavenSession(
162                 getContainer(), repoSession, new DefaultMavenExecutionRequest(), new DefaultMavenExecutionResult());
163         session.setSession(defaultSessionFactory.newSession(session));
164 
165         new DefaultSession(session, null, null, null, null, null);
166 
167         repoSession.setCache(new DefaultRepositoryCache());
168         repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
169         request.setRepositorySession(repoSession);
170     }
171 }