diff --git a/pom.xml b/pom.xml
index 294d598b..e9b63915 100644
--- a/pom.xml
+++ b/pom.xml
@@ -231,16 +231,10 @@ under the License.
-
- junit
- junit
- 4.13.2
- test
-
org.apache.maven.plugin-testing
maven-plugin-testing-harness
- 4.0.0-alpha-2
+ 3.4.0
test
@@ -285,6 +279,16 @@ under the License.
2.20.0
test
+
+ org.junit.vintage
+ junit-vintage-engine
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ test
+
org.slf4j
slf4j-simple
diff --git a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java b/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java
deleted file mode 100644
index 6d2fa962..00000000
--- a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.maven.plugins.pmd;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
-
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.model.Plugin;
-import org.apache.maven.plugin.LegacySupport;
-import org.apache.maven.plugin.MojoExecution;
-import org.apache.maven.plugin.descriptor.MojoDescriptor;
-import org.apache.maven.plugin.descriptor.PluginDescriptor;
-import org.apache.maven.plugin.testing.AbstractMojoTestCase;
-import org.apache.maven.plugin.testing.ArtifactStubFactory;
-import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
-import org.apache.maven.project.DefaultProjectBuildingRequest;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.project.ProjectBuildingRequest;
-import org.apache.maven.session.scope.internal.SessionScope;
-import org.eclipse.aether.DefaultRepositorySystemSession;
-import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
-import org.eclipse.aether.repository.LocalRepository;
-
-/**
- * @author Vincent Siveton
- * @version $Id$
- * @since 2.5
- */
-public abstract class AbstractPmdReportTestCase extends AbstractMojoTestCase {
- private ArtifactStubFactory artifactStubFactory;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- CapturingPrintStream.init(true);
-
- artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
- artifactStubFactory.getWorkingDir().mkdirs();
- SessionScope sessionScope = lookup(SessionScope.class);
- sessionScope.enter();
- }
-
- @Override
- protected void tearDown() throws Exception {
- SessionScope lookup = lookup(SessionScope.class);
- lookup.exit();
- super.tearDown();
- }
-
- /**
- * Generate the report and return the generated file.
- *
- * @param goal the mojo goal
- * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/"
- * @return the generated HTML file
- * @throws Exception if any
- */
- protected File generateReport(String goal, String pluginXml) throws Exception {
- File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
- AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile);
- return generateReport(mojo, pluginXmlFile);
- }
-
- protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
- AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile);
- assertNotNull("Mojo not found.", mojo);
-
- SessionScope sessionScope = lookup(SessionScope.class);
- MavenSession mavenSession = newMavenSession(new MavenProjectStub());
- sessionScope.seed(MavenSession.class, mavenSession);
-
- DefaultRepositorySystemSession repositorySession =
- (DefaultRepositorySystemSession) mavenSession.getRepositorySession();
- repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
- .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir())));
-
- List reactorProjects =
- mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
-
- setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
- setVariableValueToObject(mojo, "session", mavenSession);
- setVariableValueToObject(mojo, "repoSession", repositorySession);
- setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
- setVariableValueToObject(
- mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
- setVariableValueToObject(
- mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
- return mojo;
- }
-
- protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception {
- mojo.execute();
-
- ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
- buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
-
- File outputDir = mojo.getReportOutputDirectory();
- String filename = mojo.getOutputPath() + ".html";
-
- return new File(outputDir, filename);
- }
-
- /**
- * Read the contents of the specified file into a string.
- */
- protected String readFile(File file) throws IOException {
- return new String(Files.readAllBytes(file.toPath()));
- }
-
- /**
- * Checks whether the string contained is contained in
- * the given text, ignoring case.
- *
- * @param text the string in which the search is executed
- * @param contains the string to be searched for
- * @return true if the text contains the string, otherwise false
- */
- public static boolean lowerCaseContains(String text, String contains) {
- return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
- }
-
- private MojoExecution getMockMojoExecution() {
- MojoDescriptor mojoDescriptor = new MojoDescriptor();
- mojoDescriptor.setGoal(getGoal());
-
- MojoExecution execution = new MojoExecution(mojoDescriptor);
-
- PluginDescriptor pluginDescriptor = new PluginDescriptor();
- Plugin plugin = new Plugin();
- plugin.setGroupId("org.apache.maven.plugins");
- plugin.setArtifactId("maven-pmd-plugin");
- pluginDescriptor.setPlugin(plugin);
- mojoDescriptor.setPluginDescriptor(pluginDescriptor);
-
- return execution;
- }
-
- protected abstract String getGoal();
-}
diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
index bfd5fd26..0f5fab6c 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
@@ -18,6 +18,7 @@
*/
package org.apache.maven.plugins.pmd;
+import javax.inject.Inject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -27,33 +28,107 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.Locale;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.api.plugin.testing.Basedir;
+import org.apache.maven.api.plugin.testing.InjectMojo;
+import org.apache.maven.api.plugin.testing.MojoParameter;
+import org.apache.maven.api.plugin.testing.MojoTest;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReportException;
+import org.codehaus.plexus.testing.PlexusExtension;
+import org.eclipse.aether.DefaultRepositorySystemSession;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
import org.w3c.dom.Document;
+import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
/**
* @author Maria Odea Ching
* @version $Id$
*/
-public class CpdReportTest extends AbstractPmdReportTestCase {
+@MojoTest
+public class CpdReportTest {
+
+ @Inject
+ private MavenSession mavenSession;
+
+ @Inject
+ private DefaultRepositorySystemSessionFactory repoSessionFactory;
+
+ @Inject
+ private MavenProject testMavenProject;
+
+ @Inject
+ private MojoExecution mojoExecution;
+
+ /**
+ * Checks whether the string contained is contained in
+ * the given text, ignoring case.
+ *
+ * @param text the string in which the search is executed
+ * @param contains the string to be searched for
+ * @return true if the text contains the string, otherwise false
+ */
+ public static boolean lowerCaseContains(String text, String contains) {
+ return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
+ }
+
/**
* {@inheritDoc}
*/
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit"));
+ @BeforeEach
+ public void setUp() {
+ ArtifactRepository localRepo = Mockito.mock(ArtifactRepository.class);
+ Mockito.when(localRepo.getBasedir())
+ .thenReturn(new File(PlexusExtension.getBasedir(), "target/local-repo").getAbsolutePath());
+
+ MavenExecutionRequest request = new DefaultMavenExecutionRequest();
+ request.setLocalRepository(localRepo);
+
+ RemoteRepository centralRepo =
+ new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build();
+
+ DefaultRepositorySystemSession systemSession = repoSessionFactory.newRepositorySession(request);
+ Mockito.when(mavenSession.getRepositorySession()).thenReturn(systemSession);
+ Mockito.when(testMavenProject.getRemoteProjectRepositories())
+ .thenReturn(Collections.singletonList(centralRepo));
+
+ Mockito.when(mojoExecution.getPlugin()).thenReturn(new Plugin());
}
/**
* Test CPDReport given the default configuration
*/
- public void testDefaultConfiguration() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-default-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testDefaultConfiguration(CpdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(new File(generatedReport.getAbsolutePath()).exists());
// check if the CPD files were generated
@@ -69,10 +144,14 @@ public void testDefaultConfiguration() throws Exception {
}
/**
- * Test CPDReport with the text renderer given as "format=txt"
- */
- public void testTxtFormat() throws Exception {
- generateReport(getGoal(), "custom-configuration/cpd-txt-format-configuration-plugin-config.xml");
+ * // * Test CPDReport with the text renderer given as "format=txt"
+ * // */
+ @Basedir("/unit/custom-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-txt-format-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testTxtFormat(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
// check if the CPD files were generated
File xmlFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml");
@@ -91,9 +170,17 @@ public void testTxtFormat() throws Exception {
/**
* Test CpdReport using custom configuration
*/
- public void testCustomConfiguration() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "custom-configuration/cpd-custom-configuration-plugin-config.xml");
+ @Basedir("/unit/custom-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-custom-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testCustomConfiguration(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check if the CPD files were generated
@@ -113,24 +200,27 @@ public void testCustomConfiguration() throws Exception {
/**
* Test CPDReport with invalid format
*/
- public void testInvalidFormat() throws Exception {
+ @Basedir("/unit/invalid-format")
+ @InjectMojo(goal = "cpd", pom = "cpd-invalid-format-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testInvalidFormat(AbstractPmdReport mojo) throws MojoExecutionException {
try {
- File testPom = new File(
- getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml");
- AbstractPmdReport mojo = createReportMojo(getGoal(), testPom);
- setVariableValueToObject(
- mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots());
- generateReport(mojo, testPom);
-
- // TODO this should be a more specific subclass
+ mojo.execute();
+
+ // TODO should have more specific exception
fail("RuntimeException must be thrown");
} catch (RuntimeException e) {
assertMavenReportException("Can't find CPD custom format xhtml", e);
}
}
- public void testWriteNonHtml() throws Exception {
- generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-default-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testWriteNonHtml(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
@@ -152,8 +242,12 @@ public void testWriteNonHtml() throws Exception {
*
* @throws Exception
*/
- public void testIncludeXmlInReports() throws Exception {
- generateReport(getGoal(), "default-configuration/cpd-report-include-xml-in-reports-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-report-include-xml-in-reports-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testIncludeXmlInReports(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
assertTrue(generatedFile.exists());
@@ -172,30 +266,50 @@ public void testIncludeXmlInReports() throws Exception {
assertEquals(str, siteReportContent);
}
- public void testSkipEmptyReportConfiguration() throws Exception {
- // verify the generated files do not exist because PMD was skipped
- File generatedReport = generateReport(getGoal(), "empty-report/cpd-skip-empty-report-plugin-config.xml");
+ @Basedir("/unit/empty-report")
+ @InjectMojo(goal = "cpd", pom = "cpd-skip-empty-report-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testSkipEmptyReportConfiguration(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertFalse(new File(generatedReport.getAbsolutePath()).exists());
}
- public void testEmptyReportConfiguration() throws Exception {
- // verify the generated files do exist, even if there are no violations
- File generatedReport = generateReport(getGoal(), "empty-report/cpd-empty-report-plugin-config.xml");
+ @Basedir("/unit/empty-report")
+ @InjectMojo(goal = "cpd", pom = "cpd-empty-report-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testEmptyReportConfiguration(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(
- generatedReport.getAbsolutePath() + " does not exist",
- new File(generatedReport.getAbsolutePath()).exists());
+ new File(generatedReport.getAbsolutePath()).exists(),
+ generatedReport.getAbsolutePath() + " does not exist");
String str = readFile(generatedReport);
assertFalse(lowerCaseContains(str, "Hello.java"));
assertTrue(str.contains("CPD found no problems in your source code."));
}
- public void testCpdEncodingConfiguration() throws Exception {
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-default-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testCpdEncodingConfiguration(AbstractPmdReport mojo) throws Exception {
String originalEncoding = System.getProperty("file.encoding");
try {
System.setProperty("file.encoding", "UTF-16");
- generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml");
+ mojo.execute();
// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
@@ -207,8 +321,12 @@ public void testCpdEncodingConfiguration() throws Exception {
}
}
- public void testCpdJavascriptConfiguration() throws Exception {
- generateReport(getGoal(), "default-configuration/cpd-javascript-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-javascript-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testCpdJavascriptConfiguration(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
// verify the generated file exists and violations are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
@@ -218,8 +336,12 @@ public void testCpdJavascriptConfiguration() throws Exception {
assertTrue(lowerCaseContains(str, "SampleDup.js"));
}
- public void testCpdJspConfiguration() throws Exception {
- generateReport(getGoal(), "default-configuration/cpd-jsp-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-jsp-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testCpdJspConfiguration(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
// verify the generated file exists and violations are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
@@ -229,8 +351,12 @@ public void testCpdJspConfiguration() throws Exception {
assertTrue(lowerCaseContains(str, "sampleDup.jsp"));
}
- public void testExclusionsConfiguration() throws Exception {
- generateReport(getGoal(), "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "cpd", pom = "cpd-report-cpd-exclusions-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testExclusionsConfiguration(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
// verify the generated file exists and no duplications are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
@@ -239,9 +365,13 @@ public void testExclusionsConfiguration() throws Exception {
assertEquals(0, StringUtils.countMatches(str, "Maria Odea Ching
* @version $Id$
*/
-public class CpdViolationCheckMojoTest extends AbstractPmdReportTestCase {
+public class CpdViolationCheckMojoTest extends AbstractMojoTestCase {
+
+ private ArtifactStubFactory artifactStubFactory;
public void testDefaultConfiguration() throws Exception {
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
@@ -36,7 +55,7 @@ public void testDefaultConfiguration() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/cpd-check-default-configuration-plugin-config.xml");
- CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom);
+ CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom);
cpdViolationCheckMojo.execute();
fail("MojoFailureException should be thrown.");
@@ -51,7 +70,7 @@ public void testNotFailOnViolation() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml");
- CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom);
+ CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom);
cpdViolationCheckMojo.execute();
}
@@ -60,7 +79,7 @@ public void testException() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/custom-configuration/cpd-check-exception-test-plugin-config.xml");
- CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom);
+ CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom);
cpdViolationCheckMojo.project = new MavenProject();
cpdViolationCheckMojo.execute();
@@ -76,14 +95,88 @@ public void testExclusionsConfiguration() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml");
- CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom);
+ CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom);
// this call shouldn't throw an exception, as the classes with duplications have been excluded
cpdViolationCheckMojo.execute();
}
@Override
- protected String getGoal() {
- return "cpd-check";
+ protected void setUp() throws Exception {
+ super.setUp();
+ CapturingPrintStream.init(true);
+
+ artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
+ artifactStubFactory.getWorkingDir().mkdirs();
+ SessionScope sessionScope = lookup(SessionScope.class);
+ sessionScope.enter();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ SessionScope lookup = lookup(SessionScope.class);
+ lookup.exit();
+ super.tearDown();
+ }
+
+ /**
+ * Generate the report and return the generated file.
+ *
+ * @param goal the mojo goal
+ * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/"
+ * @return the generated HTML file
+ * @throws Exception if any
+ */
+ protected File generateReport(String goal, String pluginXml) throws Exception {
+ File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
+ AbstractPmdReport mojo1 = lookupMojo(goal, pluginXmlFile);
+ assertNotNull("Mojo not found.", mojo1);
+
+ SessionScope sessionScope = lookup(SessionScope.class);
+ MavenSession mavenSession = newMavenSession(new MavenProjectStub());
+ sessionScope.seed(MavenSession.class, mavenSession);
+
+ DefaultRepositorySystemSession repositorySession =
+ (DefaultRepositorySystemSession) mavenSession.getRepositorySession();
+ repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
+ .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir())));
+
+ List reactorProjects =
+ mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList();
+
+ setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution());
+ setVariableValueToObject(mojo1, "session", mavenSession);
+ setVariableValueToObject(mojo1, "repoSession", repositorySession);
+ setVariableValueToObject(mojo1, "reactorProjects", reactorProjects);
+ setVariableValueToObject(
+ mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories());
+ setVariableValueToObject(
+ mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site"));
+ AbstractPmdReport mojo = mojo1;
+ mojo.execute();
+
+ ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
+ buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ return new File(outputDir, filename);
+ }
+
+ private MojoExecution getMockMojoExecution() {
+ MojoDescriptor mojoDescriptor = new MojoDescriptor();
+ mojoDescriptor.setGoal("cpd-check");
+
+ MojoExecution execution = new MojoExecution(mojoDescriptor);
+
+ PluginDescriptor pluginDescriptor = new PluginDescriptor();
+ Plugin plugin = new Plugin();
+ plugin.setGroupId("org.apache.maven.plugins");
+ plugin.setArtifactId("maven-pmd-plugin");
+ pluginDescriptor.setPlugin(plugin);
+ mojoDescriptor.setPluginDescriptor(pluginDescriptor);
+
+ return execution;
}
}
diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
index 8cfdd846..66a61741 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
@@ -18,43 +18,121 @@
*/
package org.apache.maven.plugins.pmd;
+import javax.inject.Inject;
+
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URL;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Collections;
+import java.util.Locale;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import net.sourceforge.pmd.renderers.Renderer;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.api.plugin.testing.Basedir;
+import org.apache.maven.api.plugin.testing.InjectMojo;
+import org.apache.maven.api.plugin.testing.MojoParameter;
+import org.apache.maven.api.plugin.testing.MojoTest;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.pmd.exec.PmdExecutor;
+import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReportException;
-import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.testing.PlexusExtension;
+import org.eclipse.aether.DefaultRepositorySystemSession;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
/**
* @author Maria Odea Ching
* @version $Id$
*/
-public class PmdReportTest extends AbstractPmdReportTestCase {
+@MojoTest
+public class PmdReportTest {
+
+ @Inject
+ private MavenSession mavenSession;
+
+ @Inject
+ private DefaultRepositorySystemSessionFactory repoSessionFactory;
+
+ @Inject
+ private MavenProject testMavenProject;
+
+ @Inject
+ private MojoExecution mojoExecution;
+
+ /**
+ * Checks whether the string contained is contained in
+ * the given text, ignoring case.
+ *
+ * @param text the string in which the search is executed
+ * @param contains the string to be searched for
+ * @return true if the text contains the string, otherwise false
+ */
+ public static boolean lowerCaseContains(String text, String contains) {
+ return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
+ }
/**
* {@inheritDoc}
*/
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- org.apache.commons.io.FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit"));
+ @BeforeEach
+ public void setUp() throws Exception {
+ CapturingPrintStream.init(true);
+ ArtifactRepository localRepo = Mockito.mock(ArtifactRepository.class);
+ Mockito.when(localRepo.getBasedir())
+ .thenReturn(new File(PlexusExtension.getBasedir(), "target/local-repo").getAbsolutePath());
+
+ MavenExecutionRequest request = new DefaultMavenExecutionRequest();
+ request.setLocalRepository(localRepo);
+
+ RemoteRepository centralRepo =
+ new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build();
+
+ DefaultRepositorySystemSession systemSession = repoSessionFactory.newRepositorySession(request);
+ Mockito.when(mavenSession.getRepositorySession()).thenReturn(systemSession);
+ Mockito.when(mavenSession.getRequest()).thenReturn(request);
+ Mockito.when(testMavenProject.getRemoteProjectRepositories())
+ .thenReturn(Collections.singletonList(centralRepo));
+
+ Plugin plugin = new Plugin();
+ plugin.setGroupId("org.apache.maven.plugins");
+ plugin.setArtifactId("maven-pmd-plugin");
+ Mockito.when(mojoExecution.getPlugin()).thenReturn(plugin);
}
- public void testDefaultConfiguration() throws Exception {
- FileUtils.copyDirectoryStructure(
- new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
- new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "default-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testDefaultConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
- File generatedReport = generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check if the PMD files were generated
@@ -99,13 +177,17 @@ public void testDefaultConfiguration() throws Exception {
assertTrue(output.contains("PMD version: " + AbstractPmdReport.getPmdVersion()));
}
- public void testDefaultConfigurationNotRenderRuleViolationPriority() throws Exception {
- FileUtils.copyDirectoryStructure(
- new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
- new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-report-not-render-rule-priority-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testDefaultConfigurationNotRenderRuleViolationPriority(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
- File generatedReport = generateReport(
- getGoal(), "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml");
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
String str = readFile(generatedReport);
@@ -114,13 +196,17 @@ public void testDefaultConfigurationNotRenderRuleViolationPriority() throws Exce
assertFalse(str.contains("Priority | "));
}
- public void testDefaultConfigurationNoRenderViolationsByPriority() throws Exception {
- FileUtils.copyDirectoryStructure(
- new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
- new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-report-no-render-violations-by-priority.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testDefaultConfigurationNoRenderViolationsByPriority(PmdReport mojo) throws Exception {
+ mojo.execute();
- File generatedReport =
- generateReport(getGoal(), "default-configuration/pmd-report-no-render-violations-by-priority.xml");
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
String str = readFile(generatedReport);
@@ -133,12 +219,12 @@ public void testDefaultConfigurationNoRenderViolationsByPriority() throws Except
assertEquals(1, StringUtils.countMatches(str, "def/configuration/App.java"));
}
- public void testDefaultConfigurationWithAnalysisCache() throws Exception {
- FileUtils.copyDirectoryStructure(
- new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
- new File(getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site"));
-
- generateReport(getGoal(), "default-configuration/pmd-with-analysis-cache-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-with-analysis-cache-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testDefaultConfigurationWithAnalysisCache(PmdReport mojo) throws Exception {
+ mojo.execute();
// check if the PMD analysis cache file has been generated
File cacheFile =
@@ -146,9 +232,17 @@ public void testDefaultConfigurationWithAnalysisCache() throws Exception {
assertTrue(cacheFile.exists());
}
- public void testJavascriptConfiguration() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "default-configuration/javascript-configuration-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "javascript-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testJavascriptConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check if the PMD files were generated
@@ -172,14 +266,11 @@ public void testJavascriptConfiguration() throws Exception {
assertTrue(str.contains("Avoid using global variables"));
}
- public void testFileURL() throws Exception {
- FileUtils.copyDirectoryStructure(
- new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
- new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
-
- File testPom = new File(
- getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml");
- PmdReport mojo = (PmdReport) createReportMojo(getGoal(), testPom);
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "default-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testFileURL(PmdReport mojo) throws Exception {
// Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174).
int port = determineFreePort();
@@ -220,7 +311,12 @@ public void testFileURL() throws Exception {
URL url3 = getClass().getClassLoader().getResource("category/java/errorprone.xml");
mojo.setRulesets(new String[] {url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl});
- File generatedReport = generateReport(mojo, testPom);
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check if the PMD files were generated
@@ -266,8 +362,17 @@ private int determineFreePort() throws IOException {
*
* @throws Exception
*/
- public void testCustomConfiguration() throws Exception {
- File generatedReport = generateReport(getGoal(), "custom-configuration/custom-configuration-plugin-config.xml");
+ @Basedir("/unit/custom-configuration")
+ @InjectMojo(goal = "pmd", pom = "custom-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testCustomConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check the generated files
@@ -289,8 +394,8 @@ public void testCustomConfiguration() throws Exception {
assertFalse(lowerCaseContains(str, "Avoid using if...else statements without curly braces"));
assertFalse(
- "unnecessary constructor should not be triggered because of low priority",
- lowerCaseContains(str, "Avoid unnecessary constructors - the compiler will generate these for you"));
+ lowerCaseContains(str, "Avoid unnecessary constructors - the compiler will generate these for you"),
+ "unnecessary constructor should not be triggered because of low priority");
// veryLongVariableNameWithViolation is really too long
assertTrue(lowerCaseContains(str, "veryLongVariableNameWithViolation"));
@@ -303,8 +408,17 @@ public void testCustomConfiguration() throws Exception {
*
* @throws Exception
*/
- public void testSkipConfiguration() throws Exception {
- File generatedReport = generateReport(getGoal(), "custom-configuration/skip-plugin-config.xml");
+ @Basedir("/unit/custom-configuration")
+ @InjectMojo(goal = "pmd", pom = "skip-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testSkipConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertFalse(generatedReport.exists());
// verify the generated files do not exist because PMD was skipped
@@ -319,14 +433,31 @@ public void testSkipConfiguration() throws Exception {
assertTrue(output.contains("Skipping org.apache.maven.plugins:maven-pmd-plugin"));
}
- public void testSkipEmptyReportConfiguration() throws Exception {
- // verify the generated files do not exist because PMD was skipped
- File generatedReport = generateReport(getGoal(), "empty-report/skip-empty-report-plugin-config.xml");
+ @Basedir("/unit/empty-report")
+ @InjectMojo(goal = "pmd", pom = "skip-empty-report-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testSkipEmptyReportConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertFalse(generatedReport.exists());
}
- public void testEmptyReportConfiguration() throws Exception {
- File generatedReport = generateReport(getGoal(), "empty-report/empty-report-plugin-config.xml");
+ @Basedir("/unit/empty-report")
+ @InjectMojo(goal = "pmd", pom = "empty-report-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testEmptyReportConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// verify the generated files do exist, even if there are no violations
@@ -338,26 +469,30 @@ public void testEmptyReportConfiguration() throws Exception {
assertFalse(str.contains("Violations By Priority"));
}
- public void testInvalidFormat() throws Exception {
+ @Basedir("/unit/invalid-format")
+ @InjectMojo(goal = "pmd", pom = "invalid-format-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testInvalidFormat(PmdReport mojo) {
try {
- File testPom =
- new File(getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml");
- AbstractPmdReport mojo = createReportMojo(getGoal(), testPom);
- setVariableValueToObject(
- mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots());
- generateReport(mojo, testPom);
-
- fail("Must nest MavenReportException.");
+
+ mojo.execute();
+
+ fail("Must nested MavenReportException.");
} catch (MojoExecutionException e) {
assertTrue(e.getCause() instanceof MavenReportException);
}
}
- public void testInvalidTargetJdk() throws Exception {
+ @Basedir("/unit/invalid-format")
+ @InjectMojo(goal = "pmd", pom = "invalid-target-jdk-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testInvalidTargetJdk(PmdReport mojo) throws Exception {
try {
- generateReport(getGoal(), "invalid-format/invalid-target-jdk-plugin-config.xml");
+ mojo.execute();
- fail("Must nest MavenReportException.");
+ fail("Must nested MavenReportException.");
} catch (MojoExecutionException e) {
assertTrue(e.getCause() instanceof MavenReportException);
}
@@ -366,9 +501,17 @@ public void testInvalidTargetJdk() throws Exception {
/**
* Verify the pmd.xml file is included in the reports when requested.
*/
- public void testIncludeXmlInReports() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "default-configuration/pmd-report-include-xml-in-reports-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-report-include-xml-in-reports-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testIncludeXmlInReports(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// verify the pmd file is included in site
@@ -386,26 +529,34 @@ public void testIncludeXmlInReports() throws Exception {
/**
* Verify the correct working of the locationTemp method.
*/
- public void testLocationTemp() throws Exception {
-
- File testPom = new File(
- getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml");
- PmdReport mojo = (PmdReport) lookupMojo(getGoal(), testPom);
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "default-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testLocationTemp(PmdReport mojo) {
assertEquals(
- "locationTemp is not correctly encoding filename",
"001-export_format_pmd_language_java_name_some_2520name.xml",
mojo.getLocationTemp(
"http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name",
- 1));
+ 1),
+ "locationTemp is not correctly encoding filename");
}
/**
* Verify that suppressMarker works.
*/
- public void testSuppressMarkerConfiguration() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "default-configuration/pmd-with-suppressMarker-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-with-suppressMarker-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testSuppressMarkerConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check if the PMD files were generated
@@ -427,9 +578,17 @@ public void testSuppressMarkerConfiguration() throws Exception {
assertTrue(report.contains("Avoid unused private fields such as 'unusedVar2'."));
}
- public void testSuppressMarkerConfigurationWithoutRendering() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-with-suppressMarker-no-render-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testSuppressMarkerConfigurationWithoutRendering(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check if the PMD files were generated
@@ -451,8 +610,17 @@ public void testSuppressMarkerConfigurationWithoutRendering() throws Exception {
assertFalse(report.contains("Avoid unused private fields such as 'unusedVar2'."));
}
- public void testJspConfiguration() throws Exception {
- File generatedReport = generateReport(getGoal(), "default-configuration/jsp-configuration-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "jsp-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testJspConfiguration(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
// check if the PMD files were generated
@@ -486,22 +654,35 @@ public void testJspConfiguration() throws Exception {
assertTrue(str.contains("Avoid having style information in JSP files."));
}
- public void testPMDProcessingError() throws Exception {
+ @Basedir("/unit/processing-error")
+ @InjectMojo(goal = "pmd", pom = "pmd-processing-error-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testPMDProcessingError(PmdReport mojo) {
try {
- generateReport(getGoal(), "processing-error/pmd-processing-error-plugin-config.xml");
+ mojo.execute();
+
fail("Expected exception");
} catch (MojoExecutionException e) {
assertTrue(e.getCause().getMessage().endsWith("Found 1 PMD processing error"));
}
}
- public void testPMDProcessingErrorWithDetailsSkipped() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "processing-error/pmd-processing-error-skip-plugin-config.xml");
+ @Basedir("/unit/processing-error")
+ @InjectMojo(goal = "pmd", pom = "pmd-processing-error-skip-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testPMDProcessingErrorWithDetailsSkipped(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
String output = CapturingPrintStream.getOutput();
- assertTrue(output, output.contains("There is 1 PMD processing error:"));
+ assertTrue(output.contains("There is 1 PMD processing error:"), output);
File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml");
assertTrue(generatedFile.exists());
@@ -517,13 +698,21 @@ public void testPMDProcessingErrorWithDetailsSkipped() throws Exception {
assertTrue(html.contains("at line 23, column 5: Encountered"));
}
- public void testPMDProcessingErrorWithDetailsNoReport() throws Exception {
- File generatedReport =
- generateReport(getGoal(), "processing-error/pmd-processing-error-no-report-plugin-config.xml");
+ @Basedir("/unit/processing-error")
+ @InjectMojo(goal = "pmd", pom = "pmd-processing-error-no-report-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testPMDProcessingErrorWithDetailsNoReport(PmdReport mojo) throws Exception {
+ mojo.execute();
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ File generatedReport = new File(outputDir, filename);
assertTrue(generatedReport.exists());
String output = CapturingPrintStream.getOutput();
- assertTrue(output, output.contains("There is 1 PMD processing error:"));
+ assertTrue(output.contains("There is 1 PMD processing error:"), output);
File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml");
assertTrue(generatedFile.exists());
@@ -539,23 +728,31 @@ public void testPMDProcessingErrorWithDetailsNoReport() throws Exception {
assertFalse(html.contains("at line 23, column 5: Encountered"));
}
- public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {
- generateReport(getGoal(), "exclude-roots/pmd-exclude-roots-plugin-config.xml");
+ @Basedir("/unit/exclude-roots")
+ @InjectMojo(goal = "pmd", pom = "pmd-exclude-roots-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testPMDExcludeRootsShouldExcludeSubdirectories(PmdReport mojo) throws Exception {
+ mojo.execute();
File generatedFile = new File(getBasedir(), "target/test/unit/exclude-roots/target/pmd.xml");
assertTrue(generatedFile.exists());
String str = readFile(generatedFile);
- assertTrue("Seems like all directories are excluded now", str.contains("ForLoopShouldBeWhileLoop"));
+ assertTrue(str.contains("ForLoopShouldBeWhileLoop"), "Seems like all directories are excluded now");
assertFalse(
- "Exclusion of an exact source directory not working", str.contains("OverrideBothEqualsAndHashcode"));
+ str.contains("OverrideBothEqualsAndHashcode"), "Exclusion of an exact source directory not working");
assertFalse(
- "Exclusion of base directory with subdirectories not working (MPMD-178)",
- str.contains("JumbledIncrementer"));
+ str.contains("JumbledIncrementer"),
+ "Exclusion of base directory with subdirectories not working (MPMD-178)");
}
- public void testViolationExclusion() throws Exception {
- generateReport(getGoal(), "default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml");
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-report-pmd-exclusions-configuration-plugin-config.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testViolationExclusion(PmdReport mojo) throws Exception {
+ mojo.execute();
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml");
assertTrue(generatedFile.exists());
@@ -564,23 +761,30 @@ public void testViolationExclusion() throws Exception {
assertFalse(str.contains("UnusedPrivateField"));
}
- public void testPmdReportResolveRulesets() throws Exception {
+ @Basedir("/unit/default-configuration")
+ @InjectMojo(goal = "pmd", pom = "pmd-report-resolve-rulesets.xml")
+ @MojoParameter(name = "siteDirectory", value = "src/site")
+ @Test
+ public void testPmdReportResolveRulesets(PmdReport mojo) throws Exception {
int port = determineFreePort();
WireMockServer mockServer = new WireMockServer(port);
mockServer.start();
@@ -627,17 +835,10 @@ public void testPmdReportResolveRulesets() throws Exception {
.withHeader("Content-Type", "text/xml")
.withBody(sonarRuleset)));
- FileUtils.copyDirectoryStructure(
- new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
- new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
-
- File testPom =
- new File(getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml");
- PmdReport mojo = (PmdReport) createReportMojo(getGoal(), testPom);
mojo.rulesets[3] = sonarExportRulesetUrl;
mojo.rulesets[4] = myRulesetUrl;
mojo.rulesets[5] = notAInternalRulesetUrl;
- generateReport(mojo, testPom);
+ mojo.execute();
// these are the rulesets, that have been copied to target/pmd/rulesets
File generatedFile = new File(
@@ -669,8 +870,10 @@ public void testPmdReportResolveRulesets() throws Exception {
mockServer.stop();
}
- @Override
- protected String getGoal() {
- return "pmd";
+ /**
+ * Read the contents of the specified file into a string.
+ */
+ protected String readFile(File file) throws IOException {
+ return new String(Files.readAllBytes(file.toPath()));
}
}
diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java
index 160d11b2..aa0321fe 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java
@@ -19,15 +19,47 @@
package org.apache.maven.plugins.pmd;
import java.io.File;
-
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.plugin.testing.AbstractMojoTestCase;
+import org.apache.maven.plugin.testing.ArtifactStubFactory;
+import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.session.scope.internal.SessionScope;
+import org.eclipse.aether.DefaultRepositorySystemSession;
+import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
+import org.eclipse.aether.repository.LocalRepository;
/**
* @author Maria Odea Ching
* @version $Id$
*/
-public class PmdViolationCheckMojoTest extends AbstractPmdReportTestCase {
+public class PmdViolationCheckMojoTest extends AbstractMojoTestCase {
+
+ private ArtifactStubFactory artifactStubFactory;
+
+ /**
+ * Checks whether the string contained is contained in
+ * the given text, ignoring case.
+ *
+ * @param text the string in which the search is executed
+ * @param contains the string to be searched for
+ * @return true if the text contains the string, otherwise false
+ */
+ public static boolean lowerCaseContains(String text, String contains) {
+ return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
+ }
public void testDefaultConfiguration() throws Exception {
generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml");
@@ -36,7 +68,7 @@ public void testDefaultConfiguration() throws Exception {
final File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml");
- final PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom);
+ final PmdViolationCheckMojo mojo = lookupMojo("check", testPom);
mojo.execute();
fail("MojoFailureException should be thrown.");
@@ -52,7 +84,7 @@ public void testNotFailOnViolation() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml");
- final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
+ final PmdViolationCheckMojo pmdViolationMojo = lookupMojo("check", testPom);
pmdViolationMojo.execute();
}
@@ -62,13 +94,13 @@ public void testMaxAllowedViolations() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml");
- final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
+ final PmdViolationCheckMojo pmdViolationMojo = lookupMojo("check", testPom);
pmdViolationMojo.execute();
testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml");
- final PmdViolationCheckMojo pmdViolationMojoFail = lookupMojo(getGoal(), testPom);
+ final PmdViolationCheckMojo pmdViolationMojoFail = lookupMojo("check", testPom);
try {
pmdViolationMojoFail.execute();
@@ -86,13 +118,13 @@ public void testFailurePriority() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml");
- PmdViolationCheckMojo pmdViolationCheckMojo = lookupMojo(getGoal(), testPom);
+ PmdViolationCheckMojo pmdViolationCheckMojo = lookupMojo("check", testPom);
pmdViolationCheckMojo.execute();
testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml");
- pmdViolationCheckMojo = lookupMojo(getGoal(), testPom);
+ pmdViolationCheckMojo = lookupMojo("check", testPom);
try {
pmdViolationCheckMojo.execute();
@@ -109,7 +141,7 @@ public void testException() throws Exception {
final File testPom = new File(
getBasedir(),
"src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
- final PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom);
+ final PmdViolationCheckMojo mojo = lookupMojo("check", testPom);
mojo.project = new MavenProject();
mojo.execute();
@@ -125,14 +157,96 @@ public void testViolationExclusion() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml");
- final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom);
+ final PmdViolationCheckMojo pmdViolationMojo = lookupMojo("check", testPom);
// this call shouldn't throw an exception, as the classes with violations have been excluded
pmdViolationMojo.execute();
}
@Override
- protected String getGoal() {
- return "check";
+ protected void setUp() throws Exception {
+ super.setUp();
+ CapturingPrintStream.init(true);
+
+ artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false);
+ artifactStubFactory.getWorkingDir().mkdirs();
+ SessionScope sessionScope = lookup(SessionScope.class);
+ sessionScope.enter();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ SessionScope lookup = lookup(SessionScope.class);
+ lookup.exit();
+ super.tearDown();
+ }
+
+ /**
+ * Generate the report and return the generated file.
+ *
+ * @param goal the mojo goal
+ * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/"
+ * @return the generated HTML file
+ * @throws Exception if any
+ */
+ protected File generateReport(String goal, String pluginXml) throws Exception {
+ File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml);
+ AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile);
+ return generateReport(mojo);
+ }
+
+ protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
+ AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile);
+ assertNotNull("Mojo not found.", mojo);
+
+ SessionScope sessionScope = lookup(SessionScope.class);
+ MavenSession mavenSession = newMavenSession(new MavenProjectStub());
+ sessionScope.seed(MavenSession.class, mavenSession);
+
+ DefaultRepositorySystemSession repositorySession =
+ (DefaultRepositorySystemSession) mavenSession.getRepositorySession();
+ repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
+ .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir())));
+
+ List reactorProjects =
+ mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();
+
+ setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
+ setVariableValueToObject(mojo, "session", mavenSession);
+ setVariableValueToObject(mojo, "repoSession", repositorySession);
+ setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
+ setVariableValueToObject(
+ mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
+ setVariableValueToObject(
+ mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
+ return mojo;
+ }
+
+ protected File generateReport(AbstractPmdReport mojo) throws Exception {
+ mojo.execute();
+
+ ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
+ buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession());
+
+ File outputDir = mojo.getReportOutputDirectory();
+ String filename = mojo.getOutputPath() + ".html";
+
+ return new File(outputDir, filename);
+ }
+
+ private MojoExecution getMockMojoExecution() {
+ MojoDescriptor mojoDescriptor = new MojoDescriptor();
+ mojoDescriptor.setGoal("check");
+
+ MojoExecution execution = new MojoExecution(mojoDescriptor);
+
+ PluginDescriptor pluginDescriptor = new PluginDescriptor();
+ Plugin plugin = new Plugin();
+ plugin.setGroupId("org.apache.maven.plugins");
+ plugin.setArtifactId("maven-pmd-plugin");
+ pluginDescriptor.setPlugin(plugin);
+ mojoDescriptor.setPluginDescriptor(pluginDescriptor);
+
+ return execution;
}
}
diff --git a/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java b/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java
index 3aaaf4a1..90397895 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java
@@ -23,11 +23,13 @@
import java.net.URL;
import java.net.URLClassLoader;
-import junit.framework.TestCase;
import org.apache.commons.lang3.SystemUtils;
-import org.junit.Assert;
+import org.junit.jupiter.api.Test;
-public class ExecutorTest extends TestCase {
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ExecutorTest {
+ @Test
public void testBuildClasspath() throws MalformedURLException {
String basename = "home/test/dir with space/mylib.jar";
String pathname = new File("/", basename).getPath();
@@ -39,6 +41,6 @@ public void testBuildClasspath() throws MalformedURLException {
StringBuilder classpath = new StringBuilder();
Executor.buildClasspath(classpath, mockedClassLoader);
- Assert.assertEquals(pathname + File.pathSeparator, classpath.toString());
+ assertEquals(pathname + File.pathSeparator, classpath.toString());
}
}
diff --git a/src/test/resources/unit/CpdReportTest/with-cpd-errors/pom.xml b/src/test/resources/unit/CpdReportTest/with-cpd-errors/pom.xml
index 8ace0700..041e6747 100644
--- a/src/test/resources/unit/CpdReportTest/with-cpd-errors/pom.xml
+++ b/src/test/resources/unit/CpdReportTest/with-cpd-errors/pom.xml
@@ -41,7 +41,7 @@ under the License.
${basedir}/target/test/unit/CpdReportTest/with-cpd-errors/target/site/xref
100
- ${basedir}/src/test/resources/unit/CpdReportTest/with-cpd-errors/src/main/java
+ ${basedir}
UTF-8
diff --git a/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml b/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml
index 6fd8a79e..cc96f99d 100644
--- a/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml
+++ b/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml
@@ -44,7 +44,7 @@ under the License.
**/Sample.java
- ${basedir}/src/test/resources/unit/custom-configuration/
+ ${basedir}
UTF-8
diff --git a/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml
index ffc29032..9135b2a2 100644
--- a/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml
+++ b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml
@@ -40,7 +40,7 @@ under the License.
false
30
- ${basedir}/src/test/resources/unit/custom-configuration/
+ ${basedir}
UTF-8
diff --git a/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml b/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml
index d1ff3f43..86fb5e92 100644
--- a/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml
+++ b/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml
@@ -43,7 +43,7 @@ under the License.
ISO-8859-1
rulesets/java/maven-pmd-plugin-default.xml
- ${basedir}/src/test/resources/unit/custom-configuration/resources/rulesets/custom.xml
+ ${basedir}/resources/rulesets/custom.xml
1.4
4
@@ -52,7 +52,7 @@ under the License.
**/AnotherSample.java
- ${basedir}/src/test/resources/unit/custom-configuration/
+ ${basedir}
diff --git a/src/test/resources/unit/custom-configuration/skip-plugin-config.xml b/src/test/resources/unit/custom-configuration/skip-plugin-config.xml
index 6f6f159e..db661418 100644
--- a/src/test/resources/unit/custom-configuration/skip-plugin-config.xml
+++ b/src/test/resources/unit/custom-configuration/skip-plugin-config.xml
@@ -44,6 +44,9 @@ under the License.
1.4
4
true
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml
index a201c3ee..2d53555f 100644
--- a/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml
@@ -41,7 +41,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
100
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
UTF-8
diff --git a/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml
index 66b45328..c3b5bba6 100644
--- a/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml
@@ -41,7 +41,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
100
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
UTF-8
diff --git a/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml
index 089fb484..92cfee2a 100644
--- a/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml
@@ -44,7 +44,7 @@ under the License.
**/*.js
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
UTF-8
diff --git a/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml
index 3cb44b30..0e0442cb 100644
--- a/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml
@@ -44,7 +44,7 @@ under the License.
**/*.jsp
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
UTF-8
diff --git a/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml
index 76efc020..9e1a6869 100644
--- a/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml
@@ -41,10 +41,10 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
100
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
UTF-8
- ${basedir}/src/test/resources/unit/excludes/my-exclude-cpd.properties
+ ${basedir}/excludes/my-exclude-cpd.properties
diff --git a/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml b/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml
index afe562b1..c80008e7 100644
--- a/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml
+++ b/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml
@@ -41,7 +41,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
100
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
UTF-8
true
diff --git a/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml
index fb99dbf1..16bc1d3f 100644
--- a/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml
@@ -42,7 +42,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
UTF-8
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
diff --git a/src/test/resources/unit/default-configuration/excludes/my-exclude-cpd.properties b/src/test/resources/unit/default-configuration/excludes/my-exclude-cpd.properties
new file mode 100644
index 00000000..c58713bc
--- /dev/null
+++ b/src/test/resources/unit/default-configuration/excludes/my-exclude-cpd.properties
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+def.configuration.App,def.configuration.AppSample
diff --git a/src/test/resources/unit/default-configuration/excludes/pmd_exclude.properties b/src/test/resources/unit/default-configuration/excludes/pmd_exclude.properties
new file mode 100644
index 00000000..07b5d528
--- /dev/null
+++ b/src/test/resources/unit/default-configuration/excludes/pmd_exclude.properties
@@ -0,0 +1,21 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+def.configuration.App=UnusedPrivateField,EmptyCatchBlock,UselessParentheses
+def.configuration.App2=UnnecessaryImport
+def.configuration.AppSample=UnusedPrivateField,UnusedFormalParameter,UnusedPrivateMethod,UselessParentheses
diff --git a/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml
index 791540f1..99dd156a 100644
--- a/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml
@@ -49,7 +49,7 @@ under the License.
**/*.js
- ${basedir}/src/test/resources/unit/default-configuration/js
+ ${basedir}/js
diff --git a/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml
index a0fa7f44..98849624 100644
--- a/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml
@@ -51,7 +51,7 @@ under the License.
**/*.jsp
- ${basedir}/src/test/resources/unit/default-configuration/jsp
+ ${basedir}/jsp
diff --git a/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml
index d715f865..2c777086 100644
--- a/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml
@@ -37,6 +37,9 @@ under the License.
${basedir}/target/test/unit/default-configuration/target
true
true
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml
index 65ab4361..9ee1351a 100644
--- a/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml
@@ -38,6 +38,9 @@ under the License.
true
3
true
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml
index e7a1ddc3..03c510ea 100644
--- a/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml
@@ -39,6 +39,9 @@ under the License.
3
true
3
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml
index debd26c3..a696288b 100644
--- a/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml
@@ -38,6 +38,9 @@ under the License.
true
1
false
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml
index 118fb50e..3918e903 100644
--- a/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml
@@ -39,6 +39,9 @@ under the License.
3
true
5
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml
index 48733374..b3df097b 100644
--- a/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml
@@ -37,6 +37,9 @@ under the License.
${basedir}/target/test/unit/default-configuration/target
false
false
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml
index 6c2e6b51..482d98d5 100644
--- a/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml
@@ -40,6 +40,9 @@ under the License.
false
true
${basedir}/src/test/resources/unit/excludes/pmd_exclude.properties
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml b/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml
index c737e21c..0e20e429 100644
--- a/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml
+++ b/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml
@@ -36,16 +36,16 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site
${basedir}/target/test/unit/default-configuration/target
- ${basedir}/target/test/unit/default-configuration/target/pmd/rulesets
+ ${basedir}/target/pmd/rulesets
- ${basedir}/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml
+ ${basedir}/rulesets/custom-rules.xml
xml
true
- ${basedir}/target/test/unit/default-configuration/target/site/xref
+ ${basedir}/target/site/xref
UTF-8
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
diff --git a/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml b/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml
index 88e269bf..f1cb23f2 100644
--- a/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml
@@ -40,6 +40,9 @@ under the License.
xml
UTF-8
true
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml b/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml
index 9194f6f9..83428190 100644
--- a/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml
+++ b/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml
@@ -42,7 +42,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
UTF-8
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
false
diff --git a/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml
index 572a5e1e..8f0456f8 100644
--- a/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml
@@ -42,7 +42,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
UTF-8
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
false
diff --git a/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml
index 3387d3ed..47710e66 100644
--- a/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml
@@ -42,9 +42,9 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
UTF-8
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
- ${basedir}/src/test/resources/unit/excludes/pmd_exclude.properties
+ ${basedir}/excludes/pmd_exclude.properties
diff --git a/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml b/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml
index b03474bb..dc537126 100644
--- a/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml
+++ b/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml
@@ -38,7 +38,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target
${basedir}/target/test/unit/default-configuration/target/pmd/rulesets
- ${basedir}/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml
+ ${basedir}/rulesets/custom-rules.xml
category/java/bestpractices.xml
category/java/design.xml
@@ -51,7 +51,7 @@ under the License.
${basedir}/target/test/unit/default-configuration/target/site/xref
UTF-8
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
diff --git a/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml
index 642f124e..031f7716 100644
--- a/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml
@@ -44,7 +44,7 @@ under the License.
true
${basedir}/target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
diff --git a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml
index d3889696..371ca5ef 100644
--- a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml
@@ -44,7 +44,7 @@ under the License.
SUPPRESSME
false
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
diff --git a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml
index efd85233..2e93506c 100644
--- a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml
@@ -43,7 +43,7 @@ under the License.
UTF-8
SUPPRESSME
- ${basedir}/src/test/resources/unit/default-configuration/
+ ${basedir}
diff --git a/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml b/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml
index 9109cda4..2a977e47 100644
--- a/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml
+++ b/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml
@@ -34,10 +34,10 @@ under the License.
maven-pmd-plugin
- ${basedir}/target/test/unit/empty-report/target/site
- ${basedir}/target/test/unit/empty-report/target
+ ${basedir}/target/test/unit/skip-empty-report/target/site
+ ${basedir}/target/test/unit/skip-empty-report/target
- ${basedir}/src/test/resources/unit/empty-report/java/
+ ${basedir}
UTF-8
100
diff --git a/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml b/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml
index 2aeff05f..c0d4d4d2 100644
--- a/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml
+++ b/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml
@@ -34,11 +34,11 @@ under the License.
maven-pmd-plugin
- ${basedir}/target/test/unit/empty-report/target/site
- ${basedir}/target/test/unit/empty-report/target
- ${basedir}/target/test/unit/empty-report/target/pmd/rulesets
+ ${basedir}/target/test/unit/skip-empty-report/target/site
+ ${basedir}/target/test/unit/skip-empty-report/target
+ ${basedir}/target/test/unit/skip-empty-report/target/pmd/rulesets
- ${basedir}/src/test/resources/unit/empty-report/java/
+ ${basedir}/java
UTF-8
true
diff --git a/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml b/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml
index bec142fe..23e1e7e7 100644
--- a/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml
+++ b/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml
@@ -40,14 +40,14 @@ under the License.
xml
UTF-8
- ${basedir}/src/test/resources/unit/exclude-roots/src
- ${basedir}/src/test/resources/unit/exclude-roots/baseroot/src1
- ${basedir}/src/test/resources/unit/exclude-roots/baseroot/src2
- ${basedir}/src/test/resources/unit/exclude-roots/othersrc
+ ${basedir}/src
+ ${basedir}/baseroot/src1
+ ${basedir}/baseroot/src2
+ ${basedir}/othersrc
- ${basedir}/src/test/resources/unit/exclude-roots/baseroot
- ${basedir}/src/test/resources/unit/exclude-roots/othersrc
+ ${basedir}/baseroot
+ ${basedir}/othersrc
diff --git a/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml b/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml
index 8393c5e2..5fff1c91 100644
--- a/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml
+++ b/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml
@@ -39,6 +39,7 @@ under the License.
xhtml
false
${basedir}/target/test/unit/invalid-format/target/site/xref
+ ${basedir}
25
diff --git a/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml b/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml
index e6ccad46..4b47ed43 100644
--- a/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml
+++ b/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml
@@ -41,6 +41,9 @@ under the License.
false
${basedir}/target/test/unit/invalid-format/target/site/xref
ISO-8859-1
+
+ ${basedir}
+
diff --git a/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml b/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml
index 40f087f2..2b53aa56 100644
--- a/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml
+++ b/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml
@@ -42,7 +42,7 @@ under the License.
true
false
- ${basedir}/src/test/resources/unit/processing-error/src
+ ${basedir}/src
diff --git a/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml b/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml
index f08cee46..0c3e7d78 100644
--- a/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml
+++ b/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml
@@ -41,7 +41,7 @@ under the License.
UTF-8
false
- ${basedir}/src/test/resources/unit/processing-error/src
+ ${basedir}/src
diff --git a/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml b/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml
index 55c8a4d9..5a91ffc2 100644
--- a/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml
+++ b/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml
@@ -41,7 +41,7 @@ under the License.
UTF-8
true
- ${basedir}/src/test/resources/unit/processing-error/src
+ ${basedir}/src