From 3ef7a53e90aa4ef023150a98c5668c017d6a8a6e Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sun, 29 Dec 2024 13:22:18 -0500 Subject: [PATCH] [MPMD-410] Number agreement --- src/it/MPMD-244-logging/verify.groovy | 2 +- .../maven/plugins/pmd/exec/PmdExecutor.java | 16 +++++++--- .../maven/plugins/pmd/PmdReportTest.java | 32 ++++++++++--------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/it/MPMD-244-logging/verify.groovy b/src/it/MPMD-244-logging/verify.groovy index 105ed2b7..a974937b 100644 --- a/src/it/MPMD-244-logging/verify.groovy +++ b/src/it/MPMD-244-logging/verify.groovy @@ -19,7 +19,7 @@ File buildLog = new File( basedir, 'build.log' ) assert buildLog.exists() -assert buildLog.text.contains( "PMD processing errors" ) +assert buildLog.text.contains( "PMD processing error" ) assert buildLog.text.contains( "ParseException: Parse exception" ) assert buildLog.text.contains( "at line 24, column 5: Encountered" ) diff --git a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java index 4e93d968..5c9d0fd2 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java +++ b/src/main/java/org/apache/maven/plugins/pmd/exec/PmdExecutor.java @@ -227,16 +227,22 @@ private PmdResult run() throws MavenReportException { if (!request.isSkipPmdError()) { LOG.error("PMD processing errors:"); LOG.error(getErrorsAsString(errors, request.isDebugEnabled())); - throw new MavenReportException("Found " + errors.size() + " PMD processing errors"); + String msg = errors.size() > 1 + ? "Found " + errors.size() + " PMD processing errors" + : "Found 1 PMD processing error"; + throw new MavenReportException(msg); } - LOG.warn("There are {} PMD processing errors:", errors.size()); + String message = errors.size() > 1 + ? "There are " + errors.size() + " PMD processing errors:" + : "There is 1 PMD processing error:"; + LOG.warn(message); LOG.warn(getErrorsAsString(errors, request.isDebugEnabled())); } report = removeExcludedViolations(report); - // always write XML report, as this might be needed by the check mojo - // we need to output it even if the file list is empty or we have no violations - // so the "check" goals can check for violations + // Always write the XML report, as this might be needed by the check mojo. + // We need to output it even if the file list is empty or there are no violations + // so the "check" goals can check for violations. try { writeXmlReport(report); } catch (IOException e) { 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 886e5231..26da4903 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -500,7 +500,7 @@ public void testPMDProcessingError() throws Exception { generateReport(getGoal(), "processing-error/pmd-processing-error-plugin-config.xml"); fail("Expected exception"); } catch (MojoExecutionException e) { - assertTrue(e.getCause().getMessage().endsWith("Found 1 PMD processing errors")); + assertTrue(e.getCause().getMessage().endsWith("Found 1 PMD processing error")); } } @@ -510,19 +510,20 @@ public void testPMDProcessingErrorWithDetailsSkipped() throws Exception { assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath())); String output = CapturingPrintStream.getOutput(); - assertTrue(output.contains("There are 1 PMD processing errors:")); + assertTrue(output, output.contains("There is 1 PMD processing error:")); File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml"); assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath())); - String str = readFile(generatedFile); - assertTrue(str.contains("ParseException:")); + // The parse exception must be in the XML report - assertTrue(str.contains("at line 23, column 5: Encountered")); + String xml = readFile(generatedFile); + assertTrue(xml.contains("ParseException:")); + assertTrue(xml.contains("at line 23, column 5: Encountered")); - str = readFile(generatedReport); // The parse exception must also be in the HTML report - assertTrue(str.contains("ParseException:")); - assertTrue(str.contains("at line 23, column 5: Encountered")); + String html = readFile(generatedReport); + assertTrue(html.contains("ParseException:")); + assertTrue(html.contains("at line 23, column 5: Encountered")); } public void testPMDProcessingErrorWithDetailsNoReport() throws Exception { @@ -531,19 +532,20 @@ public void testPMDProcessingErrorWithDetailsNoReport() throws Exception { assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath())); String output = CapturingPrintStream.getOutput(); - assertTrue(output.contains("There are 1 PMD processing errors:")); + assertTrue(output, output.contains("There is 1 PMD processing error:")); File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml"); assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath())); - String str = readFile(generatedFile); + // The parse exception must be in the XML report - assertTrue(str.contains("ParseException:")); - assertTrue(str.contains("at line 23, column 5: Encountered")); + String xml = readFile(generatedFile); + assertTrue(xml.contains("ParseException:")); + assertTrue(xml.contains("at line 23, column 5: Encountered")); - str = readFile(generatedReport); // The parse exception must NOT be in the HTML report, since reportProcessingErrors is false - assertFalse(str.contains("ParseException:")); - assertFalse(str.contains("at line 23, column 5: Encountered")); + String html = readFile(generatedReport); + assertFalse(html.contains("ParseException:")); + assertFalse(html.contains("at line 23, column 5: Encountered")); } public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {