Skip to content
23 changes: 10 additions & 13 deletions src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,9 @@ public void testFileURL() throws Exception {
mockServer.stop();
}

private int determineFreePort() {
private int determineFreePort() throws IOException {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException("Couldn't find a free port.", e);
}
}

Expand Down Expand Up @@ -349,25 +347,24 @@ public void testInvalidFormat() throws Exception {
mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots());
generateReport(mojo, testPom);

fail("Must throw MavenReportException.");
} catch (Exception e) {
assertTrue(true);
fail("Must nest MavenReportException.");
} catch (MojoExecutionException e) {
assertTrue(e.getCause() instanceof MavenReportException);
}
}

public void testInvalidTargetJdk() throws Exception {
try {
generateReport(getGoal(), "empty-report/invalid-format/invalid-target-jdk-plugin-config.xml");
generateReport(getGoal(), "invalid-format/invalid-target-jdk-plugin-config.xml");

fail("Must throw MavenReportException.");
} catch (Exception e) {
assertTrue(true);
fail("Must nest MavenReportException.");
} catch (MojoExecutionException e) {
assertTrue(e.getCause() instanceof MavenReportException);
}
}

/**
* verify the pmd.xml file is included in the reports when requested.
* @throws Exception
* Verify the pmd.xml file is included in the reports when requested.
*/
public void testIncludeXmlInReports() throws Exception {
File generatedReport =
Expand Down Expand Up @@ -559,7 +556,7 @@ public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {
assertFalse(
"Exclusion of an exact source directory not working", str.contains("OverrideBothEqualsAndHashcode"));
assertFalse(
"Exclusion of basedirectory with subdirectories not working (MPMD-178)",
"Exclusion of base directory with subdirectories not working (MPMD-178)",
str.contains("JumbledIncrementer"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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>
Expand All @@ -42,7 +43,7 @@ public void testDefaultConfiguration() throws Exception {
mojo.execute();

fail("MojoFailureException should be thrown.");
} catch (final Exception e) {
} catch (final MojoFailureException e) {
assertTrue(
e.getMessage().startsWith("PMD " + AbstractPmdReport.getPmdVersion() + " has found 8 violations."));
}
Expand All @@ -56,8 +57,6 @@ public void testNotFailOnViolation() throws Exception {
"src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml");
final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo(getGoal(), testPom);
pmdViolationMojo.execute();

assertTrue(true);
}

public void testMaxAllowedViolations() throws Exception {
Expand Down Expand Up @@ -90,16 +89,17 @@ public void testFailurePriority() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml");
PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo(getGoal(), testPom);
pmdViolationMojo.execute();
PmdViolationCheckMojo pmdViolationCheckMojo = (PmdViolationCheckMojo) lookupMojo(getGoal(), testPom);
pmdViolationCheckMojo.execute();

testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml");
pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo(getGoal(), testPom);
pmdViolationCheckMojo = (PmdViolationCheckMojo) lookupMojo(getGoal(), testPom);

try {
pmdViolationMojo.execute();
fail("Exception Expected");
pmdViolationCheckMojo.execute();
fail("MojoFailureException expected");
} catch (final MojoFailureException e) {
assertTrue(e.getMessage()
.startsWith("PMD " + AbstractPmdReport.getPmdVersion()
Expand All @@ -113,11 +113,12 @@ public void testException() throws Exception {
getBasedir(),
"src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
final PmdViolationCheckMojo mojo = (PmdViolationCheckMojo) lookupMojo(getGoal(), testPom);
mojo.project = new MavenProject();
mojo.execute();

fail("MojoFailureException should be thrown.");
} catch (final Exception e) {
assertTrue(true);
} catch (final MojoFailureException e) {
assertNotNull(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,16 @@ under the License.
<artifactId>custom-configuration</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2006</inceptionYear>
<name>Maven PMD Violation Check Custom Configuration Test</name>
<url>http://maven.apache.org</url>
<build>
<finalName>custom-configuration</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<configuration>
<sourceDirectory>${basedir}/src/test/resources/unit/custom-configuration</sourceDirectory>
<targetDirectory>${basedir}/target/test/unit/custom-configuration/target</targetDirectory>
<rulesetsTargetDirectory>${basedir}/target/test/unit/custom-configuration/target/pmd/rulesets</rulesetsTargetDirectory>
<failOnViolation>false</failOnViolation>
<language>java</language>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ under the License.
<targetDirectory>${basedir}/target/test/unit/default-configuration/target</targetDirectory>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<!-- <failurePriority>5</failurePriority> -->
</configuration>
</plugin>
</plugins>
Expand Down