You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2025/05/01 17:16:48 UTC
(maven-pmd-plugin) branch master updated: Fix failed tests in CPD reports that were hidden by overly broad catch clauses (#192)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new bbee689 Fix failed tests in CPD reports that were hidden by overly broad catch clauses (#192)
bbee689 is described below
commit bbee6897a569b1cf2907bc6fa26bef85154ae2d2
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Thu May 1 17:16:40 2025 +0000
Fix failed tests in CPD reports that were hidden by overly broad catch clauses (#192)
* fix CPD tests
* fix exception that should be thrown
---
.../maven/plugins/pmd/CpdViolationCheckMojo.java | 13 +++----
.../apache/maven/plugins/pmd/CpdReportTest.java | 20 +++++------
.../plugins/pmd/CpdViolationCheckMojoTest.java | 41 ++++++++++------------
... => cpd-check-exception-test-plugin-config.xml} | 4 +--
.../pmd-check-exception-test-plugin-config.xml | 2 +-
...-check-default-configuration-plugin-config.xml} | 16 +++++----
6 files changed, 49 insertions(+), 47 deletions(-)
diff --git a/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java b/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java
index 6d9e204..4781cec 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java
@@ -44,12 +44,6 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@Mojo(name = "cpd-check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
@Execute(goal = "cpd")
public class CpdViolationCheckMojo extends AbstractPmdViolationCheckMojo<Duplication> {
- /**
- * Default constructor. Initializes with the correct {@link ExcludeDuplicationsFromFile}.
- */
- public CpdViolationCheckMojo() {
- super(new ExcludeDuplicationsFromFile());
- }
/**
* Skip the CPD violation checks. Most useful on the command line via "-Dcpd.skip=true".
@@ -65,6 +59,13 @@ public class CpdViolationCheckMojo extends AbstractPmdViolationCheckMojo<Duplica
@Parameter(property = "cpd.failOnViolation", defaultValue = "true", required = true)
protected boolean failOnViolation;
+ /**
+ * Default constructor. Initializes with the correct {@link ExcludeDuplicationsFromFile}.
+ */
+ public CpdViolationCheckMojo() {
+ super(new ExcludeDuplicationsFromFile());
+ }
+
/**
* {@inheritDoc}
*/
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 4ca1a4c..52a61ef 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
@@ -55,7 +55,7 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
// check the contents of cpd.html
String str = readFile(generatedReport);
@@ -88,18 +88,18 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
}
/**
- * Test CPDReport using custom configuration
+ * Test CpdReport using custom configuration
*
* @throws Exception
*/
public void testCustomConfiguration() throws Exception {
File generatedReport =
generateReport(getGoal(), "custom-configuration/cpd-custom-configuration-plugin-config.xml");
- assertTrue(new File(generatedReport.getAbsolutePath()).exists());
+ assertTrue(generatedReport.exists());
// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
String str = readFile(generatedReport);
// Contents that should NOT be in the report
@@ -137,7 +137,7 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document pmdCpdDocument = builder.parse(generatedFile);
@@ -158,7 +158,7 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
generateReport(getGoal(), "default-configuration/cpd-report-include-xml-in-reports-config.xml");
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document pmdCpdDocument = builder.parse(generatedFile);
@@ -201,7 +201,7 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
String str = readFile(generatedFile);
assertTrue(lowerCaseContains(str, "AppSample.java"));
} finally {
@@ -214,7 +214,7 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
// verify the generated file exists and violations are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
String str = readFile(generatedFile);
assertTrue(lowerCaseContains(str, "Sample.js"));
assertTrue(lowerCaseContains(str, "SampleDup.js"));
@@ -225,7 +225,7 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
// verify the generated file exists and violations are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
String str = readFile(generatedFile);
assertTrue(lowerCaseContains(str, "sample.jsp"));
assertTrue(lowerCaseContains(str, "sampleDup.jsp"));
@@ -236,7 +236,7 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
// verify the generated file exists and no duplications are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ assertTrue(generatedFile.exists());
String str = readFile(generatedFile);
assertEquals(0, StringUtils.countMatches(str, "<duplication"));
}
diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java
index 18428bf..9b2b303 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java
@@ -20,6 +20,9 @@ package org.apache.maven.plugins.pmd;
import java.io.File;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+
/**
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
* @version $Id$
@@ -29,47 +32,41 @@ public class CpdViolationCheckMojoTest extends AbstractPmdReportTestCase {
public void testDefaultConfiguration() throws Exception {
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
- // clear the output from previous pmd:cpd execution
- CapturingPrintStream.init(true);
-
try {
File testPom = new File(
getBasedir(),
- "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml");
- final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
- cpdViolationMojo.execute();
+ "src/test/resources/unit/default-configuration/cpd-check-default-configuration-plugin-config.xml");
+ CpdViolationCheckMojo cpdViolationCheckMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
+ cpdViolationCheckMojo.execute();
fail("MojoFailureException should be thrown.");
- } catch (final Exception e) {
- assertTrue(e.getMessage()
- .startsWith("CPD " + AbstractPmdReport.getPmdVersion() + " has found 1 duplication."));
+ } catch (final MojoFailureException e) {
+ assertTrue(e.getMessage().startsWith("CPD " + AbstractPmdReport.getPmdVersion() + " has found 1 duplicat"));
}
}
public void testNotFailOnViolation() throws Exception {
-
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml");
- final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
- cpdViolationMojo.execute();
-
- assertTrue(true);
+ CpdViolationCheckMojo cpdViolationCheckMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
+ cpdViolationCheckMojo.execute();
}
public void testException() throws Exception {
try {
- final File testPom = new File(
+ File testPom = new File(
getBasedir(),
- "src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
- final CpdViolationCheckMojo mojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
- mojo.execute();
+ "src/test/resources/unit/custom-configuration/cpd-check-exception-test-plugin-config.xml");
+ CpdViolationCheckMojo cpdViolationCheckMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
+ cpdViolationCheckMojo.project = new MavenProject();
+ cpdViolationCheckMojo.execute();
fail("MojoFailureException should be thrown.");
- } catch (final Exception e) {
- assertTrue(true);
+ } catch (MojoFailureException e) {
+ assertNotNull(e.getMessage());
}
}
@@ -79,10 +76,10 @@ public class CpdViolationCheckMojoTest extends AbstractPmdReportTestCase {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml");
- final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
+ CpdViolationCheckMojo cpdViolationCheckMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
// this call shouldn't throw an exception, as the classes with duplications have been excluded
- cpdViolationMojo.execute();
+ cpdViolationCheckMojo.execute();
}
@Override
diff --git a/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml b/src/test/resources/unit/custom-configuration/cpd-check-exception-test-plugin-config.xml
similarity index 90%
copy from src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml
copy to src/test/resources/unit/custom-configuration/cpd-check-exception-test-plugin-config.xml
index 7091107..9c4881f 100644
--- a/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml
+++ b/src/test/resources/unit/custom-configuration/cpd-check-exception-test-plugin-config.xml
@@ -23,7 +23,7 @@ under the License.
<artifactId>custom-configuration</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
- <name>Maven PMD Violation Check Custom Configuration Test</name>
+ <name>Maven CPD Violation Check Custom Configuration Test</name>
<build>
<finalName>custom-configuration</finalName>
<plugins>
@@ -31,7 +31,7 @@ under the License.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
- <targetDirectory>${basedir}/target/test/unit/custom-configuration/target</targetDirectory>
+ <targetDirectory>${basedir}/no/such/directory/causes/exception</targetDirectory>
<failOnViolation>false</failOnViolation>
</configuration>
</plugin>
diff --git a/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml b/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml
index 7091107..6648af0 100644
--- a/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml
+++ b/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml
@@ -31,7 +31,7 @@ under the License.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
- <targetDirectory>${basedir}/target/test/unit/custom-configuration/target</targetDirectory>
+ <targetDirectory>${basedir}/no/such/directory/causes/exception</targetDirectory>
<failOnViolation>false</failOnViolation>
</configuration>
</plugin>
diff --git a/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-check-default-configuration-plugin-config.xml
similarity index 69%
copy from src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml
copy to src/test/resources/unit/default-configuration/cpd-check-default-configuration-plugin-config.xml
index 7091107..3db64f8 100644
--- a/src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml
+++ b/src/test/resources/unit/default-configuration/cpd-check-default-configuration-plugin-config.xml
@@ -19,20 +19,24 @@ under the License.
<project>
<modelVersion>4.0.0</modelVersion>
- <groupId>custom.configuration</groupId>
- <artifactId>custom-configuration</artifactId>
+ <groupId>def.configuration</groupId>
+ <artifactId>default-configuration</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
- <name>Maven PMD Violation Check Custom Configuration Test</name>
+ <inceptionYear>2006</inceptionYear>
+ <name>Maven CPD Plugin Default Configuration Test</name>
+ <url>http://maven.apache.org</url>
<build>
- <finalName>custom-configuration</finalName>
+ <finalName>default-configuration</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
- <targetDirectory>${basedir}/target/test/unit/custom-configuration/target</targetDirectory>
- <failOnViolation>false</failOnViolation>
+ <project implementation="org.apache.maven.plugins.pmd.stubs.DefaultConfigurationMavenProjectStub"/>
+ <targetDirectory>${basedir}/target/test/unit/default-configuration/target</targetDirectory>
+ <failOnViolation>true</failOnViolation>
+ <printFailingErrors>true</printFailingErrors>
</configuration>
</plugin>
</plugins>