1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.report.projectinfo;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.List;
25
26 import com.meterware.httpunit.HttpUnitOptions;
27 import org.apache.maven.doxia.tools.SiteTool;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.plugin.LegacySupport;
31 import org.apache.maven.plugin.MojoExecution;
32 import org.apache.maven.plugin.descriptor.MojoDescriptor;
33 import org.apache.maven.plugin.descriptor.PluginDescriptor;
34 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35 import org.apache.maven.plugin.testing.ArtifactStubFactory;
36 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
37 import org.apache.maven.project.DefaultProjectBuildingRequest;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.project.ProjectBuilder;
40 import org.apache.maven.project.ProjectBuildingRequest;
41 import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils;
42 import org.apache.maven.report.projectinfo.stubs.DependencyArtifactStubFactory;
43 import org.codehaus.plexus.i18n.I18N;
44 import org.eclipse.aether.DefaultRepositorySystemSession;
45 import org.eclipse.aether.RepositorySystem;
46 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
47 import org.eclipse.aether.repository.LocalRepository;
48
49
50
51
52
53
54
55
56 public abstract class AbstractProjectInfoTestCase extends AbstractMojoTestCase {
57 private ArtifactStubFactory artifactStubFactory;
58
59
60
61
62 private MavenProject testMavenProject;
63
64
65
66
67 private I18N i18n;
68
69 @Override
70 protected void setUp() throws Exception {
71
72 super.setUp();
73
74 HttpUnitOptions.setScriptingEnabled(false);
75
76 i18n = getContainer().lookup(I18N.class);
77 setVariableValueToObject(i18n, "defaultBundleName", "project-info-reports");
78
79 artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
80 artifactStubFactory.getWorkingDir().mkdirs();
81 }
82
83 @Override
84 protected void tearDown() throws Exception {
85 super.tearDown();
86 }
87
88
89
90
91
92
93
94 protected String getString(String key) {
95 if (key == null || key.isEmpty()) {
96 throw new IllegalArgumentException("The key cannot be empty");
97 }
98
99 return i18n.getString(key, SiteTool.DEFAULT_LOCALE).trim();
100 }
101
102
103
104
105
106
107
108
109
110 protected String prepareTitle(String projectTitle, String shortTitle) {
111 if (projectTitle == null || projectTitle.isEmpty()) {
112 throw new IllegalArgumentException("The name cannot be empty");
113 }
114
115 if (shortTitle == null || shortTitle.isEmpty()) {
116 throw new IllegalArgumentException("The title cannot be empty");
117 }
118
119 return String.format("%s \u2013 %s", shortTitle, projectTitle);
120 }
121
122
123
124
125
126
127 protected MavenProject getTestMavenProject() {
128 return testMavenProject;
129 }
130
131
132
133
134
135
136
137
138 protected File getGeneratedReport(String name) throws IOException {
139 String outputDirectory =
140 getBasedir() + "/target/test-harness/" + getTestMavenProject().getArtifactId();
141
142 File report = new File(outputDirectory, name);
143 if (!report.exists()) {
144 throw new IOException("File not found. Attempted: " + report);
145 }
146
147 return report;
148 }
149
150
151
152
153
154
155
156
157
158 protected File generateReport(String goal, String pluginXml) throws Exception {
159 File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
160 AbstractProjectInfoReport mojo = createReportMojo(goal, pluginXmlFile);
161 return generateReport(mojo, pluginXmlFile);
162 }
163
164 protected AbstractProjectInfoReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
165 MavenSession mavenSession = newMavenSession(new MavenProjectStub());
166
167 getContainer()
168 .addComponent(
169 new RepositoryUtils(
170 lookup(ProjectBuilder.class), lookup(RepositorySystem.class), () -> mavenSession),
171 RepositoryUtils.class,
172 null);
173
174 AbstractProjectInfoReport mojo = (AbstractProjectInfoReport) lookupMojo(goal, pluginXmlFile);
175 assertNotNull("Mojo not found.", mojo);
176
177 LegacySupport legacySupport = lookup(LegacySupport.class);
178 legacySupport.setSession(mavenSession);
179
180 DefaultRepositorySystemSession repoSession =
181 (DefaultRepositorySystemSession) legacySupport.getRepositorySession();
182 repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
183 .newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));
184
185 List<MavenProject> reactorProjects =
186 mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
187
188 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
189 setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
190 setVariableValueToObject(mojo, "session", legacySupport.getSession());
191 setVariableValueToObject(mojo, "repoSession", legacySupport.getRepositorySession());
192 setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
193 setVariableValueToObject(mojo, "remoteProjectRepositories", project.getRemoteProjectRepositories());
194 setVariableValueToObject(mojo, "remoteRepositories", project.getRemoteArtifactRepositories());
195 setVariableValueToObject(mojo, "pluginRepositories", project.getPluginArtifactRepositories());
196 setVariableValueToObject(mojo, "siteDirectory", new File(project.getBasedir(), "src/site"));
197 return mojo;
198 }
199
200 protected File generateReport(AbstractProjectInfoReport mojo, File pluginXmlFile) throws Exception {
201 mojo.execute();
202
203 ProjectBuilder builder = lookup(ProjectBuilder.class);
204
205 ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
206 buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
207
208 testMavenProject = builder.build(pluginXmlFile, buildingRequest).getProject();
209
210 File outputDir = mojo.getReportOutputDirectory();
211 String filename = mojo.getOutputPath() + ".html";
212
213 return new File(outputDir, filename);
214 }
215
216 private MojoExecution getMockMojoExecution() {
217 MojoDescriptor md = new MojoDescriptor();
218 md.setGoal(getGoal());
219
220 MojoExecution me = new MojoExecution(md);
221
222 PluginDescriptor pd = new PluginDescriptor();
223 Plugin p = new Plugin();
224 p.setGroupId("org.apache.maven.plugins");
225 p.setArtifactId("maven-project-info-reports-plugin");
226 pd.setPlugin(p);
227 md.setPluginDescriptor(pd);
228
229 return me;
230 }
231
232 protected abstract String getGoal();
233 }