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 68bd6f3 Avoid catching raw exception (#198)
68bd6f3 is described below
commit 68bd6f3e7c8a0921f3699b93c56f2b92e7696aee
Author: Elliotte Rusty Harold <[email protected]>
AuthorDate: Wed Apr 23 15:05:25 2025 +0000
Avoid catching raw exception (#198)
* Avoid catching raw exception
---
.../apache/maven/plugins/pmd/CpdReportTest.java | 30 ++++++++++------------
1 file changed, 14 insertions(+), 16 deletions(-)
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 a07e195..4ca1a4c 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
@@ -25,6 +25,7 @@ import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.reporting.MavenReportException;
import org.w3c.dom.Document;
@@ -73,13 +74,13 @@ public class CpdReportTest extends
AbstractPmdReportTestCase {
generateReport(getGoal(),
"custom-configuration/cpd-txt-format-configuration-plugin-config.xml");
// check if the CPD files were generated
- File generatedFile = new File(getBasedir(),
"target/test/unit/custom-configuration/target/cpd.xml");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
- generatedFile = new File(getBasedir(),
"target/test/unit/custom-configuration/target/cpd.txt");
- assertTrue(new File(generatedFile.getAbsolutePath()).exists());
+ File xmlFile = new File(getBasedir(),
"target/test/unit/custom-configuration/target/cpd.xml");
+ assertTrue(new File(xmlFile.getAbsolutePath()).exists());
+ File txtFile = new File(getBasedir(),
"target/test/unit/custom-configuration/target/cpd.txt");
+ assertTrue(new File(txtFile.getAbsolutePath()).exists());
// check the contents of cpd.txt
- String str = readFile(generatedFile);
+ String str = readFile(txtFile);
// Contents that should NOT be in the report
assertFalse(lowerCaseContains(str, "public static void main( String[]
args )"));
// Contents that should be in the report
@@ -125,7 +126,7 @@ public class CpdReportTest extends
AbstractPmdReportTestCase {
generateReport(mojo, testPom);
fail("MavenReportException must be thrown");
- } catch (Exception e) {
+ } catch (MojoExecutionException e) {
assertMavenReportException("There was 1 error while executing
CPD", e);
assertLogOutputContains("Can't find CPD custom format xhtml");
}
@@ -211,7 +212,7 @@ public class CpdReportTest extends
AbstractPmdReportTestCase {
public void testCpdJavascriptConfiguration() throws Exception {
generateReport(getGoal(),
"default-configuration/cpd-javascript-plugin-config.xml");
- // verify the generated file to exist and violations are reported
+ // 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());
String str = readFile(generatedFile);
@@ -222,7 +223,7 @@ public class CpdReportTest extends
AbstractPmdReportTestCase {
public void testCpdJspConfiguration() throws Exception {
generateReport(getGoal(),
"default-configuration/cpd-jsp-plugin-config.xml");
- // verify the generated file to exist and violations are reported
+ // 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());
String str = readFile(generatedFile);
@@ -233,7 +234,7 @@ public class CpdReportTest extends
AbstractPmdReportTestCase {
public void testExclusionsConfiguration() throws Exception {
generateReport(getGoal(),
"default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml");
- // verify the generated file to exist and no duplications are reported
+ // 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());
String str = readFile(generatedFile);
@@ -244,8 +245,8 @@ public class CpdReportTest extends
AbstractPmdReportTestCase {
try {
generateReport(getGoal(), "CpdReportTest/with-cpd-errors/pom.xml");
- fail("MavenReportException must be thrown");
- } catch (Exception e) {
+ fail("MojoExecutionException must be thrown");
+ } catch (MojoExecutionException e) {
assertMavenReportException("There was 1 error while executing
CPD", e);
assertLogOutputContains("Lexical error in file");
assertLogOutputContains("BadFile.java");
@@ -253,13 +254,10 @@ public class CpdReportTest extends
AbstractPmdReportTestCase {
}
private static void assertMavenReportException(String expectedMessage,
Exception exception) {
- // The MavenReportException might be wrapped in another exception
assertTrue(
"Expected MavenReportException, but was: " + exception,
- exception instanceof MavenReportException ||
exception.getCause() instanceof MavenReportException);
- if (exception.getCause() instanceof MavenReportException) {
- exception = (Exception) exception.getCause();
- }
+ exception.getCause() instanceof MavenReportException);
+ exception = (Exception) exception.getCause();
assertTrue(
"Wrong message: expected: " + expectedMessage + ", but was: "
+ exception.toString(),
exception.toString().contains(expectedMessage));