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 java.io.File;
22 import java.io.FileNotFoundException;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import java.util.Arrays;
26
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
29 import org.apache.maven.model.building.ModelBuildingException;
30 import org.apache.maven.model.building.ModelProblem;
31 import org.apache.maven.repository.RepositorySystem;
32 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
33 import org.codehaus.plexus.ContainerConfiguration;
34 import org.codehaus.plexus.PlexusConstants;
35 import org.codehaus.plexus.PlexusTestCase;
36 import org.eclipse.aether.DefaultRepositorySystemSession;
37
38
39
40
41 public abstract class AbstractMavenProjectTestCase extends PlexusTestCase {
42 protected ProjectBuilder projectBuilder;
43
44 protected RepositorySystem repositorySystem;
45
46 @Override
47 protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
48 super.customizeContainerConfiguration(containerConfiguration);
49 containerConfiguration.setAutoWiring(true);
50 containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
51 }
52
53 protected void setUp() throws Exception {
54 super.setUp();
55
56 if (getContainer().hasComponent(ProjectBuilder.class, "test")) {
57 projectBuilder = lookup(ProjectBuilder.class, "test");
58 } else {
59
60 projectBuilder = lookup(ProjectBuilder.class);
61 }
62
63 repositorySystem = lookup(RepositorySystem.class);
64 }
65
66 @Override
67 protected void tearDown() throws Exception {
68 projectBuilder = null;
69
70 super.tearDown();
71 }
72
73 protected ProjectBuilder getProjectBuilder() {
74 return projectBuilder;
75 }
76
77 @Override
78 protected String getCustomConfigurationName() {
79 String name = AbstractMavenProjectTestCase.class.getName().replace('.', '/') + ".xml";
80 System.out.println(name);
81 return name;
82 }
83
84
85
86
87
88 protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
89 File markerFile = getFileForClasspathResource("local-repo/marker.txt");
90
91 return markerFile.getAbsoluteFile().getParentFile();
92 }
93
94 protected static File getFileForClasspathResource(String resource)
95 throws FileNotFoundException, URISyntaxException {
96 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
97
98 URL resourceUrl = cloader.getResource(resource);
99
100 if (resourceUrl == null) {
101 throw new FileNotFoundException("Unable to find: " + resource);
102 }
103
104 return new File(resourceUrl.toURI());
105 }
106
107 protected ArtifactRepository getLocalRepository() throws Exception {
108 ArtifactRepositoryLayout repoLayout = lookup(ArtifactRepositoryLayout.class, "legacy");
109
110 ArtifactRepository r = repositorySystem.createArtifactRepository(
111 "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null);
112
113 return r;
114 }
115
116
117
118
119
120 protected MavenProject getProjectWithDependencies(File pom) throws Exception {
121 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
122 configuration.setLocalRepository(getLocalRepository());
123 configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
124 configuration.setProcessPlugins(false);
125 configuration.setResolveDependencies(true);
126 initRepoSession(configuration);
127
128 try {
129 return projectBuilder.build(pom, configuration).getProject();
130 } catch (Exception e) {
131 Throwable cause = e.getCause();
132 if (cause instanceof ModelBuildingException) {
133 String message = "In: " + pom + "\n\n";
134 for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
135 message += problem + "\n";
136 }
137 System.out.println(message);
138 fail(message);
139 }
140
141 throw e;
142 }
143 }
144
145 protected MavenProject getProject(File pom) throws Exception {
146 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
147 configuration.setLocalRepository(getLocalRepository());
148 initRepoSession(configuration);
149
150 return projectBuilder.build(pom, configuration).getProject();
151 }
152
153 protected void initRepoSession(ProjectBuildingRequest request) {
154 File localRepo = new File(request.getLocalRepository().getBasedir());
155 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
156 session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
157 request.setRepositorySession(session);
158 }
159 }