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