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