1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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              
75              projectBuilder = getContainer().lookup(ProjectBuilder.class);
76          }
77      }
78  
79      protected ProjectBuilder getProjectBuilder() {
80          return projectBuilder;
81      }
82  
83      
84      
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     
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 }