Skip to content

Commit 263017f

Browse files
committed
refactor: inline class
Signed-off-by: Sandra Parsick <sandra@parsick.dev>
1 parent c0063a4 commit 263017f

File tree

5 files changed

+505
-168
lines changed

5 files changed

+505
-168
lines changed

src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java

Lines changed: 0 additions & 160 deletions
This file was deleted.

src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,63 @@
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
2929
import java.nio.file.Paths;
30+
import java.util.Collections;
31+
import java.util.List;
32+
import java.util.Locale;
3033

3134
import org.apache.commons.io.FileUtils;
3235
import org.apache.commons.lang3.StringUtils;
36+
import org.apache.maven.execution.MavenSession;
37+
import org.apache.maven.model.Plugin;
38+
import org.apache.maven.plugin.LegacySupport;
39+
import org.apache.maven.plugin.MojoExecution;
3340
import org.apache.maven.plugin.MojoExecutionException;
41+
import org.apache.maven.plugin.descriptor.MojoDescriptor;
42+
import org.apache.maven.plugin.descriptor.PluginDescriptor;
43+
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
44+
import org.apache.maven.plugin.testing.ArtifactStubFactory;
45+
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
46+
import org.apache.maven.project.DefaultProjectBuildingRequest;
47+
import org.apache.maven.project.MavenProject;
48+
import org.apache.maven.project.ProjectBuildingRequest;
3449
import org.apache.maven.reporting.MavenReportException;
50+
import org.apache.maven.session.scope.internal.SessionScope;
51+
import org.eclipse.aether.DefaultRepositorySystemSession;
52+
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
53+
import org.eclipse.aether.repository.LocalRepository;
3554
import org.w3c.dom.Document;
3655

3756
/**
3857
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
3958
* @version $Id$
4059
*/
41-
public class CpdReportTest extends AbstractPmdReportTestCase {
60+
public class CpdReportTest extends AbstractMojoTestCase {
61+
private ArtifactStubFactory artifactStubFactory;
62+
63+
/**
64+
* Checks whether the string <code>contained</code> is contained in
65+
* the given <code>text</code>, ignoring case.
66+
*
67+
* @param text the string in which the search is executed
68+
* @param contains the string to be searched for
69+
* @return <code>true</code> if the text contains the string, otherwise <code>false</code>
70+
*/
71+
public static boolean lowerCaseContains(String text, String contains) {
72+
return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
73+
}
74+
4275
/**
4376
* {@inheritDoc}
4477
*/
4578
@Override
4679
protected void setUp() throws Exception {
4780
super.setUp();
81+
CapturingPrintStream.init(true);
82+
83+
artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
84+
artifactStubFactory.getWorkingDir().mkdirs();
85+
SessionScope sessionScope = lookup(SessionScope.class);
86+
sessionScope.enter();
4887
FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit"));
4988
}
5089

@@ -267,8 +306,90 @@ private static void assertReportContains(String expectedMessage) throws IOExcept
267306
"Expected '" + expectedMessage + "' in cpd.xml, but was:\n" + report, report.contains(expectedMessage));
268307
}
269308

270-
@Override
271309
protected String getGoal() {
272310
return "cpd";
273311
}
312+
313+
@Override
314+
protected void tearDown() throws Exception {
315+
SessionScope lookup = lookup(SessionScope.class);
316+
lookup.exit();
317+
super.tearDown();
318+
}
319+
320+
/**
321+
* Generate the report and return the generated file.
322+
*
323+
* @param goal the mojo goal
324+
* @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/"
325+
* @return the generated HTML file
326+
* @throws Exception if any
327+
*/
328+
protected File generateReport(String goal, String pluginXml) throws Exception {
329+
File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
330+
AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile);
331+
return generateReport(mojo, pluginXmlFile);
332+
}
333+
334+
protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
335+
AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile);
336+
assertNotNull("Mojo not found.", mojo);
337+
338+
SessionScope sessionScope = lookup(SessionScope.class);
339+
MavenSession mavenSession = newMavenSession(new MavenProjectStub());
340+
sessionScope.seed(MavenSession.class, mavenSession);
341+
342+
DefaultRepositorySystemSession repositorySession =
343+
(DefaultRepositorySystemSession) mavenSession.getRepositorySession();
344+
repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
345+
.newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir())));
346+
347+
List<MavenProject> reactorProjects =
348+
mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
349+
350+
setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
351+
setVariableValueToObject(mojo, "session", mavenSession);
352+
setVariableValueToObject(mojo, "repoSession", repositorySession);
353+
setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
354+
setVariableValueToObject(
355+
mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
356+
setVariableValueToObject(
357+
mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
358+
return mojo;
359+
}
360+
361+
protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception {
362+
mojo.execute();
363+
364+
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
365+
buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
366+
367+
File outputDir = mojo.getReportOutputDirectory();
368+
String filename = mojo.getOutputPath() + ".html";
369+
370+
return new File(outputDir, filename);
371+
}
372+
373+
/**
374+
* Read the contents of the specified file into a string.
375+
*/
376+
protected String readFile(File file) throws IOException {
377+
return new String(Files.readAllBytes(file.toPath()));
378+
}
379+
380+
private MojoExecution getMockMojoExecution() {
381+
MojoDescriptor mojoDescriptor = new MojoDescriptor();
382+
mojoDescriptor.setGoal(getGoal());
383+
384+
MojoExecution execution = new MojoExecution(mojoDescriptor);
385+
386+
PluginDescriptor pluginDescriptor = new PluginDescriptor();
387+
Plugin plugin = new Plugin();
388+
plugin.setGroupId("org.apache.maven.plugins");
389+
plugin.setArtifactId("maven-pmd-plugin");
390+
pluginDescriptor.setPlugin(plugin);
391+
mojoDescriptor.setPluginDescriptor(pluginDescriptor);
392+
393+
return execution;
394+
}
274395
}

0 commit comments

Comments
 (0)