Skip to content

Commit

Permalink
Prepare for Doxia 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Oct 21, 2023
1 parent 92c55fb commit 4d02d61
Show file tree
Hide file tree
Showing 36 changed files with 190 additions and 153 deletions.
55 changes: 6 additions & 49 deletions pom.xml
Expand Up @@ -28,7 +28,7 @@ under the License.
</parent>

<artifactId>maven-pmd-plugin</artifactId>
<version>3.21.1-SNAPSHOT</version>
<version>4.0.0-M1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven PMD Plugin</name>
Expand Down Expand Up @@ -86,11 +86,10 @@ under the License.
<pmdVersion>6.55.0</pmdVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<aetherVersion>1.0.0.v20140518</aetherVersion>
<doxiaVersion>1.12.0</doxiaVersion>
<compilerPluginVersion>3.11.0</compilerPluginVersion>
<sitePluginVersion>3.12.1</sitePluginVersion>
<projectInfoReportsPluginVersion>3.4.3</projectInfoReportsPluginVersion>
<jxrPluginVersion>3.3.0</jxrPluginVersion>
<sitePluginVersion>4.0.0-M11</sitePluginVersion>
<projectInfoReportsPluginVersion>4.0.0-M1-SNAPSHOT</projectInfoReportsPluginVersion>
<jxrPluginVersion>4.0.0-M1-SNAPSHOT</jxrPluginVersion>
<project.build.outputTimestamp>2023-05-12T20:16:07Z</project.build.outputTimestamp>
</properties>

Expand Down Expand Up @@ -198,56 +197,14 @@ under the License.
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-sink-api</artifactId>
<version>${doxiaVersion}</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-core</artifactId>
<version>${doxiaVersion}</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
<version>1.11.1</version>
<scope>runtime</scope>
<!--
Note: doxia-site-renderer needs to be defined AFTER pmd:
doxia-site-renderer requires transitively dom4j 1.1 which provides a very old version of jaxen (same jar).
PMD requires a newer version of jaxen, which is not compatible.
-->
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-api</artifactId>
</exclusion>
</exclusions>
<version>2.0.0-M8</version>
</dependency>

<!-- shared -->
<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-impl</artifactId>
<version>3.2.0</version>
<version>4.0.0-M11</version>
</dependency>

<!-- plexus -->
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
Expand Up @@ -39,6 +39,7 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.Reporting;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -241,12 +242,6 @@ public abstract class AbstractPmdReport extends AbstractMavenReport {
// Read-only parameters
// ----------------------------------------------------------------------

/**
* The projects in the reactor for aggregation report.
*/
@Parameter(property = "reactorProjects", readonly = true)
protected List<MavenProject> reactorProjects;

/**
* The current build session instance. This is used for
* toolchain manager API calls and for dependency resolver API calls.
Expand All @@ -265,13 +260,21 @@ protected MavenProject getProject() {
return project;
}

protected List<MavenProject> getReactorProjects() {
return reactorProjects;
}

protected MojoExecution getMojoExecution() {
return mojoExecution;
}

protected String constructXRefLocation(boolean test) {
String location = null;
if (linkXRef) {
File xrefLoc = test ? xrefTestLocation : xrefLocation;

String relativePath =
PathTool.getRelativePath(outputDirectory.getAbsolutePath(), xrefLoc.getAbsolutePath());
PathTool.getRelativePath(getReportOutputDirectory().getAbsolutePath(), xrefLoc.getAbsolutePath());
if (relativePath == null || relativePath.isEmpty()) {
relativePath = ".";
}
Expand Down
Expand Up @@ -21,9 +21,15 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.ArtifactStubFactory;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
Expand Down Expand Up @@ -110,8 +116,17 @@ protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) th
repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory()
.newInstance(repoSession, new LocalRepository(artifactStubFactory.getWorkingDir())));

List<MavenProject> reactorProjects =
mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList();

setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution());
setVariableValueToObject(mojo, "session", legacySupport.getSession());
setVariableValueToObject(mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories());
setVariableValueToObject(mojo, "repoSession", legacySupport.getRepositorySession());
setVariableValueToObject(mojo, "reactorProjects", reactorProjects);
setVariableValueToObject(
mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories());
setVariableValueToObject(
mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site"));
return mojo;
}

Expand Down Expand Up @@ -149,4 +164,22 @@ protected String readFile(File file) throws IOException {
public static boolean lowerCaseContains(String text, String contains) {
return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
}

private MojoExecution getMockMojoExecution() {
MojoDescriptor md = new MojoDescriptor();
md.setGoal(getGoal());

MojoExecution me = new MojoExecution(md);

PluginDescriptor pd = new PluginDescriptor();
Plugin p = new Plugin();
p.setGroupId("org.apache.maven.plugins");
p.setArtifactId("maven-pmd-plugin");
pd.setPlugin(p);
md.setPluginDescriptor(pd);

return me;
}

protected abstract String getGoal();
}
32 changes: 18 additions & 14 deletions src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
Expand Up @@ -22,7 +22,6 @@
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.File;
import java.util.Locale;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -39,7 +38,6 @@ public class CpdReportTest extends AbstractPmdReportTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
Locale.setDefault(Locale.ENGLISH);
FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit"));
}

Expand All @@ -50,7 +48,7 @@ protected void setUp() throws Exception {
*/
public void testDefaultConfiguration() throws Exception {
File generatedReport =
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml");
assertTrue(new File(generatedReport.getAbsolutePath()).exists());

// check if the CPD files were generated
Expand All @@ -75,7 +73,7 @@ public void testDefaultConfiguration() throws Exception {
* @throws Exception
*/
public void testTxtFormat() throws Exception {
generateReport("cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml");
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");
Expand All @@ -97,7 +95,8 @@ public void testTxtFormat() throws Exception {
* @throws Exception
*/
public void testCustomConfiguration() throws Exception {
File generatedReport = generateReport("cpd", "custom-configuration/cpd-custom-configuration-plugin-config.xml");
File generatedReport =
generateReport(getGoal(), "custom-configuration/cpd-custom-configuration-plugin-config.xml");
assertTrue(new File(generatedReport.getAbsolutePath()).exists());

// check if the CPD files were generated
Expand All @@ -123,7 +122,7 @@ public void testInvalidFormat() throws Exception {
try {
File testPom = new File(
getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml");
AbstractPmdReport mojo = createReportMojo("cpd", testPom);
AbstractPmdReport mojo = createReportMojo(getGoal(), testPom);
setVariableValueToObject(
mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots());
generateReport(mojo, testPom);
Expand All @@ -135,7 +134,7 @@ public void testInvalidFormat() throws Exception {
}

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

// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
Expand All @@ -157,7 +156,7 @@ public void testWriteNonHtml() throws Exception {
* @throws Exception
*/
public void testIncludeXmlInSite() throws Exception {
generateReport("cpd", "default-configuration/cpd-report-include-xml-in-site-plugin-config.xml");
generateReport(getGoal(), "default-configuration/cpd-report-include-xml-in-site-plugin-config.xml");

File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
assertTrue(new File(generatedFile.getAbsolutePath()).exists());
Expand All @@ -178,13 +177,13 @@ public void testIncludeXmlInSite() throws Exception {

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

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

String str = readFile(generatedReport);
Expand All @@ -197,7 +196,7 @@ public void testCpdEncodingConfiguration() throws Exception {
try {
System.setProperty("file.encoding", "UTF-16");

generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml");

// check if the CPD files were generated
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
Expand All @@ -210,7 +209,7 @@ public void testCpdEncodingConfiguration() throws Exception {
}

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

// verify the generated file to exist and violations are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
Expand All @@ -221,7 +220,7 @@ public void testCpdJavascriptConfiguration() throws Exception {
}

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

// verify the generated file to exist and violations are reported
File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
Expand All @@ -232,12 +231,17 @@ public void testCpdJspConfiguration() throws Exception {
}

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

// verify the generated file to exist 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);
assertEquals(0, StringUtils.countMatches(str, "<duplication"));
}

@Override
protected String getGoal() {
return "cpd";
}
}
Expand Up @@ -36,7 +36,7 @@ public void testDefaultConfiguration() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml");
final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
cpdViolationMojo.execute();

fail("MojoFailureException should be thrown.");
Expand All @@ -56,7 +56,7 @@ public void testNotFailOnViolation() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml");
final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
cpdViolationMojo.execute();

assertTrue(true);
Expand All @@ -67,7 +67,7 @@ public void testException() throws Exception {
final File testPom = new File(
getBasedir(),
"src/test/resources/unit/custom-configuration/pmd-check-exception-test-plugin-config.xml");
final CpdViolationCheckMojo mojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
final CpdViolationCheckMojo mojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);
mojo.execute();

fail("MojoFailureException should be thrown.");
Expand All @@ -82,9 +82,14 @@ public void testExclusionsConfiguration() throws Exception {
File testPom = new File(
getBasedir(),
"src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml");
final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo("cpd-check", testPom);
final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo(getGoal(), testPom);

// this call shouldn't throw an exception, as the classes with duplications have been excluded
cpdViolationMojo.execute();
}

@Override
protected String getGoal() {
return "cpd-check";
}
}

0 comments on commit 4d02d61

Please sign in to comment.