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