Skip to content
Closed
Show file tree
Hide file tree
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
15 changes: 6 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,6 @@ under the License.
</exclusion>
</exclusions>
</dependency>

<!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
Expand All @@ -246,7 +238,7 @@ under the License.
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.27.2</version>
<version>2.35.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -279,6 +271,11 @@ under the License.
<version>${resolverVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.repository.LocalRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
Expand All @@ -50,8 +54,8 @@
public abstract class AbstractPmdReportTestCase extends AbstractMojoTestCase {
private ArtifactStubFactory artifactStubFactory;

@Override
protected void setUp() throws Exception {
@BeforeEach
public void setUp() throws Exception {
super.setUp();
CapturingPrintStream.init(true);

Expand All @@ -61,8 +65,8 @@ protected void setUp() throws Exception {
sessionScope.enter();
}

@Override
protected void tearDown() throws Exception {
@AfterEach
public void tearDown() throws Exception {
SessionScope lookup = lookup(SessionScope.class);
lookup.exit();
super.tearDown();
Expand All @@ -84,7 +88,7 @@ protected File generateReport(String goal, String pluginXml) throws Exception {

protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception {
AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile);
assertNotNull("Mojo not found.", mojo);
assertNotNull(mojo, "Mojo not found.");

SessionScope sessionScope = lookup(SessionScope.class);
MavenSession mavenSession = newMavenSession(new MavenProjectStub());
Expand Down
35 changes: 28 additions & 7 deletions src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.reporting.MavenReportException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
* @version $Id$
Expand All @@ -42,15 +50,16 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
/**
* {@inheritDoc}
*/
@Override
protected void setUp() throws Exception {
@BeforeEach
public void setUp() throws Exception {
super.setUp();
FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit"));
}

/**
* Test CPDReport given the default configuration
*/
@Test
public void testDefaultConfiguration() throws Exception {
File generatedReport =
generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml");
Expand All @@ -71,6 +80,7 @@ public void testDefaultConfiguration() throws Exception {
/**
* Test CPDReport with the text renderer given as "format=txt"
*/
@Test
public void testTxtFormat() throws Exception {
generateReport(getGoal(), "custom-configuration/cpd-txt-format-configuration-plugin-config.xml");

Expand All @@ -91,6 +101,7 @@ public void testTxtFormat() throws Exception {
/**
* Test CpdReport using custom configuration
*/
@Test
public void testCustomConfiguration() throws Exception {
File generatedReport =
generateReport(getGoal(), "custom-configuration/cpd-custom-configuration-plugin-config.xml");
Expand All @@ -113,6 +124,7 @@ public void testCustomConfiguration() throws Exception {
/**
* Test CPDReport with invalid format
*/
@Test
public void testInvalidFormat() throws Exception {
try {
File testPom = new File(
Expand All @@ -129,6 +141,7 @@ public void testInvalidFormat() throws Exception {
}
}

@Test
public void testWriteNonHtml() throws Exception {
generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml");

Expand All @@ -152,6 +165,7 @@ public void testWriteNonHtml() throws Exception {
*
* @throws Exception
*/
@Test
public void testIncludeXmlInReports() throws Exception {
generateReport(getGoal(), "default-configuration/cpd-report-include-xml-in-reports-config.xml");

Expand All @@ -172,24 +186,27 @@ public void testIncludeXmlInReports() throws Exception {
assertEquals(str, siteReportContent);
}

@Test
public void testSkipEmptyReportConfiguration() throws Exception {
// verify the generated files do not exist because PMD was skipped
File generatedReport = generateReport(getGoal(), "empty-report/cpd-skip-empty-report-plugin-config.xml");
assertFalse(new File(generatedReport.getAbsolutePath()).exists());
}

@Test
public void testEmptyReportConfiguration() throws Exception {
// verify the generated files do exist, even if there are no violations
File generatedReport = generateReport(getGoal(), "empty-report/cpd-empty-report-plugin-config.xml");
assertTrue(
generatedReport.getAbsolutePath() + " does not exist",
new File(generatedReport.getAbsolutePath()).exists());
new File(generatedReport.getAbsolutePath()).exists(),
generatedReport.getAbsolutePath() + " does not exist");

String str = readFile(generatedReport);
assertFalse(lowerCaseContains(str, "Hello.java"));
assertTrue(str.contains("CPD found no problems in your source code."));
}

@Test
public void testCpdEncodingConfiguration() throws Exception {
String originalEncoding = System.getProperty("file.encoding");
try {
Expand All @@ -207,6 +224,7 @@ public void testCpdEncodingConfiguration() throws Exception {
}
}

@Test
public void testCpdJavascriptConfiguration() throws Exception {
generateReport(getGoal(), "default-configuration/cpd-javascript-plugin-config.xml");

Expand All @@ -218,6 +236,7 @@ public void testCpdJavascriptConfiguration() throws Exception {
assertTrue(lowerCaseContains(str, "SampleDup.js"));
}

@Test
public void testCpdJspConfiguration() throws Exception {
generateReport(getGoal(), "default-configuration/cpd-jsp-plugin-config.xml");

Expand All @@ -229,6 +248,7 @@ public void testCpdJspConfiguration() throws Exception {
assertTrue(lowerCaseContains(str, "sampleDup.jsp"));
}

@Test
public void testExclusionsConfiguration() throws Exception {
generateReport(getGoal(), "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml");

Expand All @@ -239,6 +259,7 @@ public void testExclusionsConfiguration() throws Exception {
assertEquals(0, StringUtils.countMatches(str, "<duplication"));
}

@Test
public void testWithCpdErrors() throws Exception {
try {
generateReport(getGoal(), "CpdReportTest/with-cpd-errors/pom.xml");
Expand All @@ -255,16 +276,16 @@ private static void assertMavenReportException(String expectedMessage, Exception
MavenReportException cause = (MavenReportException) exception.getCause();
String message = cause.getMessage();
assertTrue(
"Wrong message: expected: " + expectedMessage + ", but was: " + message,
message.contains(expectedMessage));
message.contains(expectedMessage),
"Wrong message: expected: " + expectedMessage + ", but was: " + message);
}

private static void assertReportContains(String expectedMessage) throws IOException {
Path path = Paths.get(getBasedir(), "target/test/unit/CpdReportTest/with-cpd-errors/target/cpd.xml");
String report = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

assertTrue(
"Expected '" + expectedMessage + "' in cpd.xml, but was:\n" + report, report.contains(expectedMessage));
report.contains(expectedMessage), "Expected '" + expectedMessage + "' in cpd.xml, but was:\n" + report);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
* @version $Id$
*/
public class CpdViolationCheckMojoTest extends AbstractPmdReportTestCase {

@Test
public void testDefaultConfiguration() throws Exception {
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");

Expand All @@ -45,6 +51,7 @@ public void testDefaultConfiguration() throws Exception {
}
}

@Test
public void testNotFailOnViolation() throws Exception {
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");

Expand All @@ -55,6 +62,7 @@ public void testNotFailOnViolation() throws Exception {
cpdViolationCheckMojo.execute();
}

@Test
public void testException() throws Exception {
try {
File testPom = new File(
Expand All @@ -70,6 +78,7 @@ public void testException() throws Exception {
}
}

@Test
public void testExclusionsConfiguration() throws Exception {
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");

Expand Down
Loading
Loading