Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

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;

Expand Down Expand Up @@ -73,13 +74,13 @@ public void testTxtFormat() throws Exception {
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
Expand Down Expand Up @@ -125,7 +126,7 @@ public void testInvalidFormat() throws Exception {
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");
}
Expand Down Expand Up @@ -211,7 +212,7 @@ public void testCpdEncodingConfiguration() throws Exception {
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);
Expand All @@ -222,7 +223,7 @@ public void testCpdJavascriptConfiguration() throws Exception {
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);
Expand All @@ -233,7 +234,7 @@ public void testCpdJspConfiguration() throws Exception {
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);
Expand All @@ -244,22 +245,19 @@ public void testWithCpdErrors() throws Exception {
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");
}
}

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));
Expand Down