From 263017fc0c56d243619cdcde5f6fcb099d057692 Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Mon, 10 Nov 2025 13:08:48 +0100 Subject: [PATCH 1/8] refactor: inline class Signed-off-by: Sandra Parsick --- .../pmd/AbstractPmdReportTestCase.java | 160 ------------------ .../maven/plugins/pmd/CpdReportTest.java | 125 +++++++++++++- .../pmd/CpdViolationCheckMojoTest.java | 131 +++++++++++++- .../maven/plugins/pmd/PmdReportTest.java | 126 +++++++++++++- .../pmd/PmdViolationCheckMojoTest.java | 131 +++++++++++++- 5 files changed, 505 insertions(+), 168 deletions(-) delete mode 100644 src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java diff --git a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java b/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java deleted file mode 100644 index 6d2fa962..00000000 --- a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.plugins.pmd; - -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.execution.MavenSession; -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; -import org.apache.maven.project.DefaultProjectBuildingRequest; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.session.scope.internal.SessionScope; -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; -import org.eclipse.aether.repository.LocalRepository; - -/** - * @author Vincent Siveton - * @version $Id$ - * @since 2.5 - */ -public abstract class AbstractPmdReportTestCase extends AbstractMojoTestCase { - private ArtifactStubFactory artifactStubFactory; - - @Override - protected void setUp() throws Exception { - super.setUp(); - CapturingPrintStream.init(true); - - artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); - artifactStubFactory.getWorkingDir().mkdirs(); - SessionScope sessionScope = lookup(SessionScope.class); - sessionScope.enter(); - } - - @Override - protected void tearDown() throws Exception { - SessionScope lookup = lookup(SessionScope.class); - lookup.exit(); - super.tearDown(); - } - - /** - * Generate the report and return the generated file. - * - * @param goal the mojo goal - * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" - * @return the generated HTML file - * @throws Exception if any - */ - protected File generateReport(String goal, String pluginXml) throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); - AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); - return generateReport(mojo, pluginXmlFile); - } - - protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { - AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); - assertNotNull("Mojo not found.", mojo); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo, "session", mavenSession); - setVariableValueToObject(mojo, "repoSession", repositorySession); - setVariableValueToObject(mojo, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); - return mojo; - } - - protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - - return new File(outputDir, filename); - } - - /** - * Read the contents of the specified file into a string. - */ - protected String readFile(File file) throws IOException { - return new String(Files.readAllBytes(file.toPath())); - } - - /** - * Checks whether the string contained is contained in - * the given text, ignoring case. - * - * @param text the string in which the search is executed - * @param contains the string to be searched for - * @return true if the text contains the string, otherwise false - */ - public static boolean lowerCaseContains(String text, String contains) { - return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT)); - } - - private MojoExecution getMockMojoExecution() { - MojoDescriptor mojoDescriptor = new MojoDescriptor(); - mojoDescriptor.setGoal(getGoal()); - - MojoExecution execution = new MojoExecution(mojoDescriptor); - - PluginDescriptor pluginDescriptor = new PluginDescriptor(); - Plugin plugin = new Plugin(); - plugin.setGroupId("org.apache.maven.plugins"); - plugin.setArtifactId("maven-pmd-plugin"); - pluginDescriptor.setPlugin(plugin); - mojoDescriptor.setPluginDescriptor(pluginDescriptor); - - return execution; - } - - protected abstract String getGoal(); -} diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index bfd5fd26..0063ac26 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -27,24 +27,63 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Collections; +import java.util.List; +import java.util.Locale; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; +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; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.reporting.MavenReportException; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; +import org.eclipse.aether.repository.LocalRepository; import org.w3c.dom.Document; /** * @author Maria Odea Ching * @version $Id$ */ -public class CpdReportTest extends AbstractPmdReportTestCase { +public class CpdReportTest extends AbstractMojoTestCase { + private ArtifactStubFactory artifactStubFactory; + + /** + * Checks whether the string contained is contained in + * the given text, ignoring case. + * + * @param text the string in which the search is executed + * @param contains the string to be searched for + * @return true if the text contains the string, otherwise false + */ + public static boolean lowerCaseContains(String text, String contains) { + return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT)); + } + /** * {@inheritDoc} */ @Override protected void setUp() throws Exception { super.setUp(); + CapturingPrintStream.init(true); + + artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); + artifactStubFactory.getWorkingDir().mkdirs(); + SessionScope sessionScope = lookup(SessionScope.class); + sessionScope.enter(); FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit")); } @@ -267,8 +306,90 @@ private static void assertReportContains(String expectedMessage) throws IOExcept "Expected '" + expectedMessage + "' in cpd.xml, but was:\n" + report, report.contains(expectedMessage)); } - @Override protected String getGoal() { return "cpd"; } + + @Override + protected void tearDown() throws Exception { + SessionScope lookup = lookup(SessionScope.class); + lookup.exit(); + super.tearDown(); + } + + /** + * Generate the report and return the generated file. + * + * @param goal the mojo goal + * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" + * @return the generated HTML file + * @throws Exception if any + */ + protected File generateReport(String goal, String pluginXml) throws Exception { + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); + AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); + return generateReport(mojo, pluginXmlFile); + } + + protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { + AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); + assertNotNull("Mojo not found.", mojo); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo, "session", mavenSession); + setVariableValueToObject(mojo, "repoSession", repositorySession); + setVariableValueToObject(mojo, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); + return mojo; + } + + protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + return new File(outputDir, filename); + } + + /** + * Read the contents of the specified file into a string. + */ + protected String readFile(File file) throws IOException { + return new String(Files.readAllBytes(file.toPath())); + } + + private MojoExecution getMockMojoExecution() { + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setGoal(getGoal()); + + MojoExecution execution = new MojoExecution(mojoDescriptor); + + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-pmd-plugin"); + pluginDescriptor.setPlugin(plugin); + mojoDescriptor.setPluginDescriptor(pluginDescriptor); + + return execution; + } } diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java index 8622a5a5..b116170d 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java @@ -19,15 +19,49 @@ package org.apache.maven.plugins.pmd; 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.execution.MavenSession; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoFailureException; +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; +import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; +import org.eclipse.aether.repository.LocalRepository; /** * @author Maria Odea Ching * @version $Id$ */ -public class CpdViolationCheckMojoTest extends AbstractPmdReportTestCase { +public class CpdViolationCheckMojoTest extends AbstractMojoTestCase { + + private ArtifactStubFactory artifactStubFactory; + + /** + * Checks whether the string contained is contained in + * the given text, ignoring case. + * + * @param text the string in which the search is executed + * @param contains the string to be searched for + * @return true if the text contains the string, otherwise false + */ + public static boolean lowerCaseContains(String text, String contains) { + return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT)); + } public void testDefaultConfiguration() throws Exception { generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml"); @@ -82,8 +116,101 @@ public void testExclusionsConfiguration() throws Exception { cpdViolationCheckMojo.execute(); } - @Override protected String getGoal() { return "cpd-check"; } + + @Override + protected void setUp() throws Exception { + super.setUp(); + CapturingPrintStream.init(true); + + artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); + artifactStubFactory.getWorkingDir().mkdirs(); + SessionScope sessionScope = lookup(SessionScope.class); + sessionScope.enter(); + } + + @Override + protected void tearDown() throws Exception { + SessionScope lookup = lookup(SessionScope.class); + lookup.exit(); + super.tearDown(); + } + + /** + * Generate the report and return the generated file. + * + * @param goal the mojo goal + * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" + * @return the generated HTML file + * @throws Exception if any + */ + protected File generateReport(String goal, String pluginXml) throws Exception { + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); + AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); + return generateReport(mojo, pluginXmlFile); + } + + protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { + AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); + assertNotNull("Mojo not found.", mojo); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo, "session", mavenSession); + setVariableValueToObject(mojo, "repoSession", repositorySession); + setVariableValueToObject(mojo, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); + return mojo; + } + + protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + return new File(outputDir, filename); + } + + /** + * Read the contents of the specified file into a string. + */ + protected String readFile(File file) throws IOException { + return new String(Files.readAllBytes(file.toPath())); + } + + private MojoExecution getMockMojoExecution() { + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setGoal(getGoal()); + + MojoExecution execution = new MojoExecution(mojoDescriptor); + + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-pmd-plugin"); + pluginDescriptor.setPlugin(plugin); + mojoDescriptor.setPluginDescriptor(pluginDescriptor); + + return execution; + } } 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 8cfdd846..47470505 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -23,22 +23,56 @@ import java.net.ServerSocket; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Collections; +import java.util.List; +import java.util.Locale; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import net.sourceforge.pmd.renderers.Renderer; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; +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; import org.apache.maven.plugins.pmd.exec.PmdExecutor; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.reporting.MavenReportException; +import org.apache.maven.session.scope.internal.SessionScope; import org.codehaus.plexus.util.FileUtils; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; +import org.eclipse.aether.repository.LocalRepository; /** * @author Maria Odea Ching * @version $Id$ */ -public class PmdReportTest extends AbstractPmdReportTestCase { +public class PmdReportTest extends AbstractMojoTestCase { + + private ArtifactStubFactory artifactStubFactory; + + /** + * Checks whether the string contained is contained in + * the given text, ignoring case. + * + * @param text the string in which the search is executed + * @param contains the string to be searched for + * @return true if the text contains the string, otherwise false + */ + public static boolean lowerCaseContains(String text, String contains) { + return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT)); + } /** * {@inheritDoc} @@ -46,6 +80,12 @@ public class PmdReportTest extends AbstractPmdReportTestCase { @Override protected void setUp() throws Exception { super.setUp(); + CapturingPrintStream.init(true); + + artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); + artifactStubFactory.getWorkingDir().mkdirs(); + SessionScope sessionScope = lookup(SessionScope.class); + sessionScope.enter(); org.apache.commons.io.FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit")); } @@ -669,8 +709,90 @@ public void testPmdReportResolveRulesets() throws Exception { mockServer.stop(); } - @Override protected String getGoal() { return "pmd"; } + + @Override + protected void tearDown() throws Exception { + SessionScope lookup = lookup(SessionScope.class); + lookup.exit(); + super.tearDown(); + } + + /** + * Generate the report and return the generated file. + * + * @param goal the mojo goal + * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" + * @return the generated HTML file + * @throws Exception if any + */ + protected File generateReport(String goal, String pluginXml) throws Exception { + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); + AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); + return generateReport(mojo, pluginXmlFile); + } + + protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { + AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); + assertNotNull("Mojo not found.", mojo); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo, "session", mavenSession); + setVariableValueToObject(mojo, "repoSession", repositorySession); + setVariableValueToObject(mojo, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); + return mojo; + } + + protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + return new File(outputDir, filename); + } + + /** + * Read the contents of the specified file into a string. + */ + protected String readFile(File file) throws IOException { + return new String(Files.readAllBytes(file.toPath())); + } + + private MojoExecution getMockMojoExecution() { + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setGoal(getGoal()); + + MojoExecution execution = new MojoExecution(mojoDescriptor); + + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-pmd-plugin"); + pluginDescriptor.setPlugin(plugin); + mojoDescriptor.setPluginDescriptor(pluginDescriptor); + + return execution; + } } diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java index 160d11b2..a9aab382 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java @@ -19,15 +19,49 @@ package org.apache.maven.plugins.pmd; 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.execution.MavenSession; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoFailureException; +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; +import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.session.scope.internal.SessionScope; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; +import org.eclipse.aether.repository.LocalRepository; /** * @author Maria Odea Ching * @version $Id$ */ -public class PmdViolationCheckMojoTest extends AbstractPmdReportTestCase { +public class PmdViolationCheckMojoTest extends AbstractMojoTestCase { + + private ArtifactStubFactory artifactStubFactory; + + /** + * Checks whether the string contained is contained in + * the given text, ignoring case. + * + * @param text the string in which the search is executed + * @param contains the string to be searched for + * @return true if the text contains the string, otherwise false + */ + public static boolean lowerCaseContains(String text, String contains) { + return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT)); + } public void testDefaultConfiguration() throws Exception { generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml"); @@ -131,8 +165,101 @@ public void testViolationExclusion() throws Exception { pmdViolationMojo.execute(); } - @Override protected String getGoal() { return "check"; } + + @Override + protected void setUp() throws Exception { + super.setUp(); + CapturingPrintStream.init(true); + + artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); + artifactStubFactory.getWorkingDir().mkdirs(); + SessionScope sessionScope = lookup(SessionScope.class); + sessionScope.enter(); + } + + @Override + protected void tearDown() throws Exception { + SessionScope lookup = lookup(SessionScope.class); + lookup.exit(); + super.tearDown(); + } + + /** + * Generate the report and return the generated file. + * + * @param goal the mojo goal + * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" + * @return the generated HTML file + * @throws Exception if any + */ + protected File generateReport(String goal, String pluginXml) throws Exception { + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); + AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); + return generateReport(mojo, pluginXmlFile); + } + + protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { + AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); + assertNotNull("Mojo not found.", mojo); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo, "session", mavenSession); + setVariableValueToObject(mojo, "repoSession", repositorySession); + setVariableValueToObject(mojo, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); + return mojo; + } + + protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + return new File(outputDir, filename); + } + + /** + * Read the contents of the specified file into a string. + */ + protected String readFile(File file) throws IOException { + return new String(Files.readAllBytes(file.toPath())); + } + + private MojoExecution getMockMojoExecution() { + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setGoal(getGoal()); + + MojoExecution execution = new MojoExecution(mojoDescriptor); + + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-pmd-plugin"); + pluginDescriptor.setPlugin(plugin); + mojoDescriptor.setPluginDescriptor(pluginDescriptor); + + return execution; + } } From 729ec95096ef90f70523d92f38c42dc6121c44a4 Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Mon, 10 Nov 2025 13:11:48 +0100 Subject: [PATCH 2/8] refactor: inline method Signed-off-by: Sandra Parsick --- .../maven/plugins/pmd/CpdReportTest.java | 32 +++++------- .../pmd/CpdViolationCheckMojoTest.java | 14 ++--- .../maven/plugins/pmd/PmdReportTest.java | 52 +++++++++---------- .../pmd/PmdViolationCheckMojoTest.java | 22 ++++---- 4 files changed, 52 insertions(+), 68 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index 0063ac26..d37573c3 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -92,7 +92,7 @@ protected void setUp() throws Exception { */ public void testDefaultConfiguration() throws Exception { File generatedReport = - generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml"); + generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml"); assertTrue(new File(generatedReport.getAbsolutePath()).exists()); // check if the CPD files were generated @@ -111,7 +111,7 @@ public void testDefaultConfiguration() throws Exception { * Test CPDReport with the text renderer given as "format=txt" */ public void testTxtFormat() throws Exception { - generateReport(getGoal(), "custom-configuration/cpd-txt-format-configuration-plugin-config.xml"); + generateReport("cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml"); // check if the CPD files were generated File xmlFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml"); @@ -132,7 +132,7 @@ public void testTxtFormat() throws Exception { */ public void testCustomConfiguration() throws Exception { File generatedReport = - generateReport(getGoal(), "custom-configuration/cpd-custom-configuration-plugin-config.xml"); + generateReport("cpd", "custom-configuration/cpd-custom-configuration-plugin-config.xml"); assertTrue(generatedReport.exists()); // check if the CPD files were generated @@ -156,7 +156,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(getGoal(), testPom); + AbstractPmdReport mojo = createReportMojo("cpd", testPom); setVariableValueToObject( mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots()); generateReport(mojo, testPom); @@ -169,7 +169,7 @@ public void testInvalidFormat() throws Exception { } public void testWriteNonHtml() throws Exception { - generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml"); + generateReport("cpd", "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"); @@ -192,7 +192,7 @@ public void testWriteNonHtml() throws Exception { * @throws Exception */ public void testIncludeXmlInReports() throws Exception { - generateReport(getGoal(), "default-configuration/cpd-report-include-xml-in-reports-config.xml"); + generateReport("cpd", "default-configuration/cpd-report-include-xml-in-reports-config.xml"); File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -213,13 +213,13 @@ public void testIncludeXmlInReports() throws Exception { 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"); + File generatedReport = generateReport("cpd", "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(getGoal(), "empty-report/cpd-empty-report-plugin-config.xml"); + File generatedReport = generateReport("cpd", "empty-report/cpd-empty-report-plugin-config.xml"); assertTrue( generatedReport.getAbsolutePath() + " does not exist", new File(generatedReport.getAbsolutePath()).exists()); @@ -234,7 +234,7 @@ public void testCpdEncodingConfiguration() throws Exception { try { System.setProperty("file.encoding", "UTF-16"); - generateReport(getGoal(), "default-configuration/cpd-default-configuration-plugin-config.xml"); + generateReport("cpd", "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"); @@ -247,7 +247,7 @@ public void testCpdEncodingConfiguration() throws Exception { } public void testCpdJavascriptConfiguration() throws Exception { - generateReport(getGoal(), "default-configuration/cpd-javascript-plugin-config.xml"); + generateReport("cpd", "default-configuration/cpd-javascript-plugin-config.xml"); // verify the generated file exists and violations are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -258,7 +258,7 @@ public void testCpdJavascriptConfiguration() throws Exception { } public void testCpdJspConfiguration() throws Exception { - generateReport(getGoal(), "default-configuration/cpd-jsp-plugin-config.xml"); + generateReport("cpd", "default-configuration/cpd-jsp-plugin-config.xml"); // verify the generated file exists and violations are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -269,7 +269,7 @@ public void testCpdJspConfiguration() throws Exception { } public void testExclusionsConfiguration() throws Exception { - generateReport(getGoal(), "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml"); + generateReport("cpd", "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml"); // verify the generated file exists and no duplications are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -280,7 +280,7 @@ public void testExclusionsConfiguration() throws Exception { public void testWithCpdErrors() throws Exception { try { - generateReport(getGoal(), "CpdReportTest/with-cpd-errors/pom.xml"); + generateReport("cpd", "CpdReportTest/with-cpd-errors/pom.xml"); fail("MojoExecutionException must be thrown"); } catch (MojoExecutionException e) { @@ -306,10 +306,6 @@ private static void assertReportContains(String expectedMessage) throws IOExcept "Expected '" + expectedMessage + "' in cpd.xml, but was:\n" + report, report.contains(expectedMessage)); } - protected String getGoal() { - return "cpd"; - } - @Override protected void tearDown() throws Exception { SessionScope lookup = lookup(SessionScope.class); @@ -379,7 +375,7 @@ protected String readFile(File file) throws IOException { private MojoExecution getMockMojoExecution() { MojoDescriptor mojoDescriptor = new MojoDescriptor(); - mojoDescriptor.setGoal(getGoal()); + mojoDescriptor.setGoal("cpd"); MojoExecution execution = new MojoExecution(mojoDescriptor); diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java index b116170d..231024d4 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java @@ -70,7 +70,7 @@ public void testDefaultConfiguration() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-check-default-configuration-plugin-config.xml"); - CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom); + CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom); cpdViolationCheckMojo.execute(); fail("MojoFailureException should be thrown."); @@ -85,7 +85,7 @@ public void testNotFailOnViolation() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml"); - CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom); + CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom); cpdViolationCheckMojo.execute(); } @@ -94,7 +94,7 @@ public void testException() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/custom-configuration/cpd-check-exception-test-plugin-config.xml"); - CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom); + CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom); cpdViolationCheckMojo.project = new MavenProject(); cpdViolationCheckMojo.execute(); @@ -110,16 +110,12 @@ public void testExclusionsConfiguration() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml"); - CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo(getGoal(), testPom); + CpdViolationCheckMojo cpdViolationCheckMojo = lookupMojo("cpd-check", testPom); // this call shouldn't throw an exception, as the classes with duplications have been excluded cpdViolationCheckMojo.execute(); } - protected String getGoal() { - return "cpd-check"; - } - @Override protected void setUp() throws Exception { super.setUp(); @@ -200,7 +196,7 @@ protected String readFile(File file) throws IOException { private MojoExecution getMockMojoExecution() { MojoDescriptor mojoDescriptor = new MojoDescriptor(); - mojoDescriptor.setGoal(getGoal()); + mojoDescriptor.setGoal("cpd-check"); MojoExecution execution = new MojoExecution(mojoDescriptor); 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 47470505..38b3b349 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -145,7 +145,7 @@ public void testDefaultConfigurationNotRenderRuleViolationPriority() throws Exce new File(getBasedir(), "target/test/unit/default-configuration/target/site")); File generatedReport = generateReport( - getGoal(), "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml"); + "pmd", "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml"); assertTrue(generatedReport.exists()); String str = readFile(generatedReport); @@ -160,7 +160,7 @@ public void testDefaultConfigurationNoRenderViolationsByPriority() throws Except new File(getBasedir(), "target/test/unit/default-configuration/target/site")); File generatedReport = - generateReport(getGoal(), "default-configuration/pmd-report-no-render-violations-by-priority.xml"); + generateReport("pmd", "default-configuration/pmd-report-no-render-violations-by-priority.xml"); assertTrue(generatedReport.exists()); String str = readFile(generatedReport); @@ -178,7 +178,7 @@ public void testDefaultConfigurationWithAnalysisCache() throws Exception { new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), new File(getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site")); - generateReport(getGoal(), "default-configuration/pmd-with-analysis-cache-plugin-config.xml"); + generateReport("pmd", "default-configuration/pmd-with-analysis-cache-plugin-config.xml"); // check if the PMD analysis cache file has been generated File cacheFile = @@ -188,7 +188,7 @@ public void testDefaultConfigurationWithAnalysisCache() throws Exception { public void testJavascriptConfiguration() throws Exception { File generatedReport = - generateReport(getGoal(), "default-configuration/javascript-configuration-plugin-config.xml"); + generateReport("pmd", "default-configuration/javascript-configuration-plugin-config.xml"); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -219,7 +219,7 @@ public void testFileURL() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml"); - PmdReport mojo = (PmdReport) createReportMojo(getGoal(), testPom); + PmdReport mojo = (PmdReport) createReportMojo("pmd", testPom); // Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174). int port = determineFreePort(); @@ -307,7 +307,7 @@ private int determineFreePort() throws IOException { * @throws Exception */ public void testCustomConfiguration() throws Exception { - File generatedReport = generateReport(getGoal(), "custom-configuration/custom-configuration-plugin-config.xml"); + File generatedReport = generateReport("pmd", "custom-configuration/custom-configuration-plugin-config.xml"); assertTrue(generatedReport.exists()); // check the generated files @@ -344,7 +344,7 @@ public void testCustomConfiguration() throws Exception { * @throws Exception */ public void testSkipConfiguration() throws Exception { - File generatedReport = generateReport(getGoal(), "custom-configuration/skip-plugin-config.xml"); + File generatedReport = generateReport("pmd", "custom-configuration/skip-plugin-config.xml"); assertFalse(generatedReport.exists()); // verify the generated files do not exist because PMD was skipped @@ -361,12 +361,12 @@ public void testSkipConfiguration() throws Exception { public void testSkipEmptyReportConfiguration() throws Exception { // verify the generated files do not exist because PMD was skipped - File generatedReport = generateReport(getGoal(), "empty-report/skip-empty-report-plugin-config.xml"); + File generatedReport = generateReport("pmd", "empty-report/skip-empty-report-plugin-config.xml"); assertFalse(generatedReport.exists()); } public void testEmptyReportConfiguration() throws Exception { - File generatedReport = generateReport(getGoal(), "empty-report/empty-report-plugin-config.xml"); + File generatedReport = generateReport("pmd", "empty-report/empty-report-plugin-config.xml"); assertTrue(generatedReport.exists()); // verify the generated files do exist, even if there are no violations @@ -382,7 +382,7 @@ public void testInvalidFormat() throws Exception { try { File testPom = new File(getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml"); - AbstractPmdReport mojo = createReportMojo(getGoal(), testPom); + AbstractPmdReport mojo = createReportMojo("pmd", testPom); setVariableValueToObject( mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots()); generateReport(mojo, testPom); @@ -395,7 +395,7 @@ public void testInvalidFormat() throws Exception { public void testInvalidTargetJdk() throws Exception { try { - generateReport(getGoal(), "invalid-format/invalid-target-jdk-plugin-config.xml"); + generateReport("pmd", "invalid-format/invalid-target-jdk-plugin-config.xml"); fail("Must nest MavenReportException."); } catch (MojoExecutionException e) { @@ -408,7 +408,7 @@ public void testInvalidTargetJdk() throws Exception { */ public void testIncludeXmlInReports() throws Exception { File generatedReport = - generateReport(getGoal(), "default-configuration/pmd-report-include-xml-in-reports-config.xml"); + generateReport("pmd", "default-configuration/pmd-report-include-xml-in-reports-config.xml"); assertTrue(generatedReport.exists()); // verify the pmd file is included in site @@ -430,7 +430,7 @@ public void testLocationTemp() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml"); - PmdReport mojo = (PmdReport) lookupMojo(getGoal(), testPom); + PmdReport mojo = (PmdReport) lookupMojo("pmd", testPom); assertEquals( "locationTemp is not correctly encoding filename", @@ -445,7 +445,7 @@ public void testLocationTemp() throws Exception { */ public void testSuppressMarkerConfiguration() throws Exception { File generatedReport = - generateReport(getGoal(), "default-configuration/pmd-with-suppressMarker-plugin-config.xml"); + generateReport("pmd", "default-configuration/pmd-with-suppressMarker-plugin-config.xml"); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -469,7 +469,7 @@ public void testSuppressMarkerConfiguration() throws Exception { public void testSuppressMarkerConfigurationWithoutRendering() throws Exception { File generatedReport = - generateReport(getGoal(), "default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml"); + generateReport("pmd", "default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml"); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -492,7 +492,7 @@ public void testSuppressMarkerConfigurationWithoutRendering() throws Exception { } public void testJspConfiguration() throws Exception { - File generatedReport = generateReport(getGoal(), "default-configuration/jsp-configuration-plugin-config.xml"); + File generatedReport = generateReport("pmd", "default-configuration/jsp-configuration-plugin-config.xml"); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -528,7 +528,7 @@ public void testJspConfiguration() throws Exception { public void testPMDProcessingError() throws Exception { try { - generateReport(getGoal(), "processing-error/pmd-processing-error-plugin-config.xml"); + generateReport("pmd", "processing-error/pmd-processing-error-plugin-config.xml"); fail("Expected exception"); } catch (MojoExecutionException e) { assertTrue(e.getCause().getMessage().endsWith("Found 1 PMD processing error")); @@ -537,7 +537,7 @@ public void testPMDProcessingError() throws Exception { public void testPMDProcessingErrorWithDetailsSkipped() throws Exception { File generatedReport = - generateReport(getGoal(), "processing-error/pmd-processing-error-skip-plugin-config.xml"); + generateReport("pmd", "processing-error/pmd-processing-error-skip-plugin-config.xml"); assertTrue(generatedReport.exists()); String output = CapturingPrintStream.getOutput(); @@ -559,7 +559,7 @@ public void testPMDProcessingErrorWithDetailsSkipped() throws Exception { public void testPMDProcessingErrorWithDetailsNoReport() throws Exception { File generatedReport = - generateReport(getGoal(), "processing-error/pmd-processing-error-no-report-plugin-config.xml"); + generateReport("pmd", "processing-error/pmd-processing-error-no-report-plugin-config.xml"); assertTrue(generatedReport.exists()); String output = CapturingPrintStream.getOutput(); @@ -580,7 +580,7 @@ public void testPMDProcessingErrorWithDetailsNoReport() throws Exception { } public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception { - generateReport(getGoal(), "exclude-roots/pmd-exclude-roots-plugin-config.xml"); + generateReport("pmd", "exclude-roots/pmd-exclude-roots-plugin-config.xml"); File generatedFile = new File(getBasedir(), "target/test/unit/exclude-roots/target/pmd.xml"); assertTrue(generatedFile.exists()); @@ -595,7 +595,7 @@ public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception { } public void testViolationExclusion() throws Exception { - generateReport(getGoal(), "default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml"); + generateReport("pmd", "default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml"); File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml"); assertTrue(generatedFile.exists()); @@ -620,7 +620,7 @@ public void testPmdReportCustomRulesNoExternalInfoUrl() throws Exception { new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), new File(getBasedir(), "target/test/unit/default-configuration/target/site")); - File generatedReport = generateReport(getGoal(), "default-configuration/pmd-report-custom-rules.xml"); + File generatedReport = generateReport("pmd", "default-configuration/pmd-report-custom-rules.xml"); assertTrue(generatedReport.exists()); String str = readFile(generatedReport); @@ -673,7 +673,7 @@ public void testPmdReportResolveRulesets() throws Exception { File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml"); - PmdReport mojo = (PmdReport) createReportMojo(getGoal(), testPom); + PmdReport mojo = (PmdReport) createReportMojo("pmd", testPom); mojo.rulesets[3] = sonarExportRulesetUrl; mojo.rulesets[4] = myRulesetUrl; mojo.rulesets[5] = notAInternalRulesetUrl; @@ -709,10 +709,6 @@ public void testPmdReportResolveRulesets() throws Exception { mockServer.stop(); } - protected String getGoal() { - return "pmd"; - } - @Override protected void tearDown() throws Exception { SessionScope lookup = lookup(SessionScope.class); @@ -782,7 +778,7 @@ protected String readFile(File file) throws IOException { private MojoExecution getMockMojoExecution() { MojoDescriptor mojoDescriptor = new MojoDescriptor(); - mojoDescriptor.setGoal(getGoal()); + mojoDescriptor.setGoal("pmd"); MojoExecution execution = new MojoExecution(mojoDescriptor); diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java index a9aab382..8f2194be 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java @@ -70,7 +70,7 @@ public void testDefaultConfiguration() throws Exception { final File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml"); - final PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom); + final PmdViolationCheckMojo mojo = lookupMojo("check", testPom); mojo.execute(); fail("MojoFailureException should be thrown."); @@ -86,7 +86,7 @@ public void testNotFailOnViolation() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml"); - final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom); + final PmdViolationCheckMojo pmdViolationMojo = lookupMojo("check", testPom); pmdViolationMojo.execute(); } @@ -96,13 +96,13 @@ public void testMaxAllowedViolations() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml"); - final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom); + final PmdViolationCheckMojo pmdViolationMojo = lookupMojo("check", testPom); pmdViolationMojo.execute(); testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml"); - final PmdViolationCheckMojo pmdViolationMojoFail = lookupMojo(getGoal(), testPom); + final PmdViolationCheckMojo pmdViolationMojoFail = lookupMojo("check", testPom); try { pmdViolationMojoFail.execute(); @@ -120,13 +120,13 @@ public void testFailurePriority() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml"); - PmdViolationCheckMojo pmdViolationCheckMojo = lookupMojo(getGoal(), testPom); + PmdViolationCheckMojo pmdViolationCheckMojo = lookupMojo("check", testPom); pmdViolationCheckMojo.execute(); testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml"); - pmdViolationCheckMojo = lookupMojo(getGoal(), testPom); + pmdViolationCheckMojo = lookupMojo("check", testPom); try { pmdViolationCheckMojo.execute(); @@ -143,7 +143,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 PmdViolationCheckMojo mojo = lookupMojo(getGoal(), testPom); + final PmdViolationCheckMojo mojo = lookupMojo("check", testPom); mojo.project = new MavenProject(); mojo.execute(); @@ -159,16 +159,12 @@ public void testViolationExclusion() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml"); - final PmdViolationCheckMojo pmdViolationMojo = lookupMojo(getGoal(), testPom); + final PmdViolationCheckMojo pmdViolationMojo = lookupMojo("check", testPom); // this call shouldn't throw an exception, as the classes with violations have been excluded pmdViolationMojo.execute(); } - protected String getGoal() { - return "check"; - } - @Override protected void setUp() throws Exception { super.setUp(); @@ -249,7 +245,7 @@ protected String readFile(File file) throws IOException { private MojoExecution getMockMojoExecution() { MojoDescriptor mojoDescriptor = new MojoDescriptor(); - mojoDescriptor.setGoal(getGoal()); + mojoDescriptor.setGoal("check"); MojoExecution execution = new MojoExecution(mojoDescriptor); From 0fb76b4f1dbc74f77254ba3415cfcc6eeed9934e Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Mon, 10 Nov 2025 13:19:30 +0100 Subject: [PATCH 3/8] refactor: inline method Signed-off-by: Sandra Parsick --- .../maven/plugins/pmd/CpdReportTest.java | 490 +++++++++++++++--- .../pmd/CpdViolationCheckMojoTest.java | 50 +- .../maven/plugins/pmd/PmdReportTest.java | 129 ++++- .../pmd/PmdViolationCheckMojoTest.java | 13 +- 4 files changed, 540 insertions(+), 142 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index d37573c3..c737567f 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -91,8 +91,41 @@ protected void setUp() throws Exception { * Test CPDReport given the default configuration */ public void testDefaultConfiguration() throws Exception { + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + File generatedReport = - generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml"); + new File(outputDir, filename); assertTrue(new File(generatedReport.getAbsolutePath()).exists()); // check if the CPD files were generated @@ -111,7 +144,38 @@ public void testDefaultConfiguration() throws Exception { * Test CPDReport with the text renderer given as "format=txt" */ public void testTxtFormat() throws Exception { - generateReport("cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "custom-configuration/cpd-txt-format-configuration-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // check if the CPD files were generated File xmlFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml"); @@ -131,8 +195,41 @@ public void testTxtFormat() throws Exception { * Test CpdReport using custom configuration */ public void testCustomConfiguration() throws Exception { + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "custom-configuration/cpd-custom-configuration-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + File generatedReport = - generateReport("cpd", "custom-configuration/cpd-custom-configuration-plugin-config.xml"); + new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the CPD files were generated @@ -156,10 +253,39 @@ 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 mojo1 = lookupMojo("cpd", testPom); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; setVariableValueToObject( mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots()); - generateReport(mojo, testPom); + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // TODO this should be a more specific subclass fail("RuntimeException must be thrown"); @@ -169,7 +295,38 @@ public void testInvalidFormat() throws Exception { } public void testWriteNonHtml() throws Exception { - generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // check if the CPD files were generated File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -192,7 +349,38 @@ public void testWriteNonHtml() throws Exception { * @throws Exception */ public void testIncludeXmlInReports() throws Exception { - generateReport("cpd", "default-configuration/cpd-report-include-xml-in-reports-config.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-report-include-xml-in-reports-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -213,13 +401,79 @@ public void testIncludeXmlInReports() 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 pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-skip-empty-report-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); 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 pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-empty-report-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue( generatedReport.getAbsolutePath() + " does not exist", new File(generatedReport.getAbsolutePath()).exists()); @@ -234,7 +488,38 @@ public void testCpdEncodingConfiguration() throws Exception { try { System.setProperty("file.encoding", "UTF-16"); - generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // check if the CPD files were generated File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -247,7 +532,38 @@ public void testCpdEncodingConfiguration() throws Exception { } public void testCpdJavascriptConfiguration() throws Exception { - generateReport("cpd", "default-configuration/cpd-javascript-plugin-config.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-javascript-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // verify the generated file exists and violations are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -258,7 +574,38 @@ public void testCpdJavascriptConfiguration() throws Exception { } public void testCpdJspConfiguration() throws Exception { - generateReport("cpd", "default-configuration/cpd-jsp-plugin-config.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-jsp-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // verify the generated file exists and violations are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -269,7 +616,38 @@ public void testCpdJspConfiguration() throws Exception { } public void testExclusionsConfiguration() throws Exception { - generateReport("cpd", "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // verify the generated file exists and no duplications are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); @@ -280,7 +658,38 @@ public void testExclusionsConfiguration() throws Exception { public void testWithCpdErrors() throws Exception { try { - generateReport("cpd", "CpdReportTest/with-cpd-errors/pom.xml"); + File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "CpdReportTest/with-cpd-errors/pom.xml"); + AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; fail("MojoExecutionException must be thrown"); } catch (MojoExecutionException e) { @@ -313,59 +722,6 @@ protected void tearDown() throws Exception { super.tearDown(); } - /** - * Generate the report and return the generated file. - * - * @param goal the mojo goal - * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" - * @return the generated HTML file - * @throws Exception if any - */ - protected File generateReport(String goal, String pluginXml) throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); - AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); - return generateReport(mojo, pluginXmlFile); - } - - protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { - AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); - assertNotNull("Mojo not found.", mojo); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo, "session", mavenSession); - setVariableValueToObject(mojo, "repoSession", repositorySession); - setVariableValueToObject(mojo, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); - return mojo; - } - - protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - - return new File(outputDir, filename); - } - /** * Read the contents of the specified file into a string. */ diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java index 231024d4..9994736e 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java @@ -19,11 +19,8 @@ package org.apache.maven.plugins.pmd; 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.execution.MavenSession; import org.apache.maven.model.Plugin; @@ -51,18 +48,6 @@ public class CpdViolationCheckMojoTest extends AbstractMojoTestCase { private ArtifactStubFactory artifactStubFactory; - /** - * Checks whether the string contained is contained in - * the given text, ignoring case. - * - * @param text the string in which the search is executed - * @param contains the string to be searched for - * @return true if the text contains the string, otherwise false - */ - public static boolean lowerCaseContains(String text, String contains) { - return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT)); - } - public void testDefaultConfiguration() throws Exception { generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml"); @@ -144,13 +129,8 @@ protected void tearDown() throws Exception { */ protected File generateReport(String goal, String pluginXml) throws Exception { File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); - AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); - return generateReport(mojo, pluginXmlFile); - } - - protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { - AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); - assertNotNull("Mojo not found.", mojo); + AbstractPmdReport mojo1 = lookupMojo(goal, pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); SessionScope sessionScope = lookup(SessionScope.class); MavenSession mavenSession = newMavenSession(new MavenProjectStub()); @@ -162,20 +142,17 @@ protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) th .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); List reactorProjects = - mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo, "session", mavenSession); - setVariableValueToObject(mojo, "repoSession", repositorySession); - setVariableValueToObject(mojo, "reactorProjects", reactorProjects); + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); setVariableValueToObject( - mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); setVariableValueToObject( - mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); - return mojo; - } - - protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; mojo.execute(); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); @@ -187,13 +164,6 @@ protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws return new File(outputDir, filename); } - /** - * Read the contents of the specified file into a string. - */ - protected String readFile(File file) throws IOException { - return new String(Files.readAllBytes(file.toPath())); - } - private MojoExecution getMockMojoExecution() { MojoDescriptor mojoDescriptor = new MojoDescriptor(); mojoDescriptor.setGoal("cpd-check"); 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 38b3b349..7ad37c92 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -219,7 +219,30 @@ public void testFileURL() throws Exception { File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml"); - PmdReport mojo = (PmdReport) createReportMojo("pmd", testPom); + AbstractPmdReport mojo1 = lookupMojo("pmd", testPom); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + PmdReport mojo = (PmdReport) mojo1; // Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174). int port = determineFreePort(); @@ -260,7 +283,15 @@ public void testFileURL() throws Exception { URL url3 = getClass().getClassLoader().getResource("category/java/errorprone.xml"); mojo.setRulesets(new String[] {url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl}); - File generatedReport = generateReport(mojo, testPom); + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -382,10 +413,39 @@ public void testInvalidFormat() throws Exception { try { File testPom = new File(getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml"); - AbstractPmdReport mojo = createReportMojo("pmd", testPom); + AbstractPmdReport mojo1 = lookupMojo("pmd", testPom); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; setVariableValueToObject( mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots()); - generateReport(mojo, testPom); + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; fail("Must nest MavenReportException."); } catch (MojoExecutionException e) { @@ -673,11 +733,40 @@ public void testPmdReportResolveRulesets() throws Exception { File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml"); - PmdReport mojo = (PmdReport) createReportMojo("pmd", testPom); + AbstractPmdReport mojo1 = lookupMojo("pmd", testPom); + assertNotNull("Mojo not found.", mojo1); + + SessionScope sessionScope = lookup(SessionScope.class); + MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + sessionScope.seed(MavenSession.class, mavenSession); + + DefaultRepositorySystemSession repositorySession = + (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + + List reactorProjects = + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + setVariableValueToObject( + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + setVariableValueToObject( + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + PmdReport mojo = (PmdReport) mojo1; mojo.rulesets[3] = sonarExportRulesetUrl; mojo.rulesets[4] = myRulesetUrl; mojo.rulesets[5] = notAInternalRulesetUrl; - generateReport(mojo, testPom); + mojo.execute(); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; // these are the rulesets, that have been copied to target/pmd/rulesets File generatedFile = new File( @@ -726,13 +815,8 @@ protected void tearDown() throws Exception { */ protected File generateReport(String goal, String pluginXml) throws Exception { File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); - AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); - return generateReport(mojo, pluginXmlFile); - } - - protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { - AbstractPmdReport mojo = lookupMojo(goal, pluginXmlFile); - assertNotNull("Mojo not found.", mojo); + AbstractPmdReport mojo1 = lookupMojo(goal, pluginXmlFile); + assertNotNull("Mojo not found.", mojo1); SessionScope sessionScope = lookup(SessionScope.class); MavenSession mavenSession = newMavenSession(new MavenProjectStub()); @@ -744,20 +828,17 @@ protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) th .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); List reactorProjects = - mojo.getReactorProjects() != null ? mojo.getReactorProjects() : Collections.emptyList(); + mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo, "session", mavenSession); - setVariableValueToObject(mojo, "repoSession", repositorySession); - setVariableValueToObject(mojo, "reactorProjects", reactorProjects); + setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + setVariableValueToObject(mojo1, "session", mavenSession); + setVariableValueToObject(mojo1, "repoSession", repositorySession); + setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); setVariableValueToObject( - mojo, "remoteProjectRepositories", mojo.getProject().getRemoteProjectRepositories()); + mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); setVariableValueToObject( - mojo, "siteDirectory", new File(mojo.getProject().getBasedir(), "src/site")); - return mojo; - } - - protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { + mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + AbstractPmdReport mojo = mojo1; mojo.execute(); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java index 8f2194be..aa0321fe 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java @@ -19,8 +19,6 @@ package org.apache.maven.plugins.pmd; 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; @@ -194,7 +192,7 @@ protected void tearDown() throws Exception { protected File generateReport(String goal, String pluginXml) throws Exception { File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); AbstractPmdReport mojo = createReportMojo(goal, pluginXmlFile); - return generateReport(mojo, pluginXmlFile); + return generateReport(mojo); } protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) throws Exception { @@ -224,7 +222,7 @@ protected AbstractPmdReport createReportMojo(String goal, File pluginXmlFile) th return mojo; } - protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws Exception { + protected File generateReport(AbstractPmdReport mojo) throws Exception { mojo.execute(); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); @@ -236,13 +234,6 @@ protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws return new File(outputDir, filename); } - /** - * Read the contents of the specified file into a string. - */ - protected String readFile(File file) throws IOException { - return new String(Files.readAllBytes(file.toPath())); - } - private MojoExecution getMockMojoExecution() { MojoDescriptor mojoDescriptor = new MojoDescriptor(); mojoDescriptor.setGoal("check"); From f71621119bf58136db6cbab0e3fd14dee8980b86 Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Mon, 10 Nov 2025 13:22:43 +0100 Subject: [PATCH 4/8] refactor: remove unused code Signed-off-by: Sandra Parsick --- .../maven/plugins/pmd/CpdReportTest.java | 77 ++++++++----------- .../maven/plugins/pmd/PmdReportTest.java | 16 +--- 2 files changed, 38 insertions(+), 55 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index c737567f..10d5e3f7 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -91,7 +91,9 @@ protected void setUp() throws Exception { * Test CPDReport given the default configuration */ public void testDefaultConfiguration() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), + "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -124,8 +126,7 @@ public void testDefaultConfiguration() throws Exception { File outputDir = mojo.getReportOutputDirectory(); String filename = mojo.getOutputPath() + ".html"; - File generatedReport = - new File(outputDir, filename); + File generatedReport = new File(outputDir, filename); assertTrue(new File(generatedReport.getAbsolutePath()).exists()); // check if the CPD files were generated @@ -144,7 +145,9 @@ public void testDefaultConfiguration() throws Exception { * Test CPDReport with the text renderer given as "format=txt" */ public void testTxtFormat() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "custom-configuration/cpd-txt-format-configuration-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), + "src/test/resources/unit/" + "custom-configuration/cpd-txt-format-configuration-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -174,9 +177,6 @@ public void testTxtFormat() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // check if the CPD files were generated File xmlFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml"); assertTrue(new File(xmlFile.getAbsolutePath()).exists()); @@ -195,7 +195,9 @@ public void testTxtFormat() throws Exception { * Test CpdReport using custom configuration */ public void testCustomConfiguration() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "custom-configuration/cpd-custom-configuration-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), + "src/test/resources/unit/" + "custom-configuration/cpd-custom-configuration-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -228,8 +230,7 @@ public void testCustomConfiguration() throws Exception { File outputDir = mojo.getReportOutputDirectory(); String filename = mojo.getOutputPath() + ".html"; - File generatedReport = - new File(outputDir, filename); + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the CPD files were generated @@ -284,9 +285,6 @@ public void testInvalidFormat() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // TODO this should be a more specific subclass fail("RuntimeException must be thrown"); } catch (RuntimeException e) { @@ -295,7 +293,9 @@ public void testInvalidFormat() throws Exception { } public void testWriteNonHtml() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), + "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -325,9 +325,6 @@ public void testWriteNonHtml() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // check if the CPD files were generated File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -349,7 +346,9 @@ public void testWriteNonHtml() throws Exception { * @throws Exception */ public void testIncludeXmlInReports() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-report-include-xml-in-reports-config.xml"); + File pluginXmlFile = new File( + getBasedir(), + "src/test/resources/unit/" + "default-configuration/cpd-report-include-xml-in-reports-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -379,9 +378,6 @@ public void testIncludeXmlInReports() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -401,7 +397,8 @@ public void testIncludeXmlInReports() throws Exception { public void testSkipEmptyReportConfiguration() throws Exception { // verify the generated files do not exist because PMD was skipped - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-skip-empty-report-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-skip-empty-report-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -440,7 +437,8 @@ public void testSkipEmptyReportConfiguration() throws Exception { public void testEmptyReportConfiguration() throws Exception { // verify the generated files do exist, even if there are no violations - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-empty-report-plugin-config.xml"); + File pluginXmlFile = + new File(getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-empty-report-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -488,7 +486,9 @@ public void testCpdEncodingConfiguration() throws Exception { try { System.setProperty("file.encoding", "UTF-16"); - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), + "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -518,9 +518,6 @@ public void testCpdEncodingConfiguration() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // check if the CPD files were generated File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -532,7 +529,8 @@ public void testCpdEncodingConfiguration() throws Exception { } public void testCpdJavascriptConfiguration() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-javascript-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-javascript-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -562,9 +560,6 @@ public void testCpdJavascriptConfiguration() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // verify the generated file exists and violations are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -574,7 +569,8 @@ public void testCpdJavascriptConfiguration() throws Exception { } public void testCpdJspConfiguration() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-jsp-plugin-config.xml"); + File pluginXmlFile = + new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-jsp-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -604,9 +600,6 @@ public void testCpdJspConfiguration() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // verify the generated file exists and violations are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -616,7 +609,10 @@ public void testCpdJspConfiguration() throws Exception { } public void testExclusionsConfiguration() throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml"); + File pluginXmlFile = new File( + getBasedir(), + "src/test/resources/unit/" + + "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -646,9 +642,6 @@ public void testExclusionsConfiguration() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // verify the generated file exists and no duplications are reported File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); assertTrue(generatedFile.exists()); @@ -658,7 +651,8 @@ public void testExclusionsConfiguration() throws Exception { public void testWithCpdErrors() throws Exception { try { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + "CpdReportTest/with-cpd-errors/pom.xml"); + File pluginXmlFile = + new File(getBasedir(), "src/test/resources/unit/" + "CpdReportTest/with-cpd-errors/pom.xml"); AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); assertNotNull("Mojo not found.", mojo1); @@ -688,9 +682,6 @@ public void testWithCpdErrors() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - fail("MojoExecutionException must be thrown"); } catch (MojoExecutionException e) { assertMavenReportException("There was 1 error while executing CPD", 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 7ad37c92..e4ef73bc 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -144,8 +144,8 @@ public void testDefaultConfigurationNotRenderRuleViolationPriority() throws Exce new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), new File(getBasedir(), "target/test/unit/default-configuration/target/site")); - File generatedReport = generateReport( - "pmd", "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml"); + File generatedReport = + generateReport("pmd", "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml"); assertTrue(generatedReport.exists()); String str = readFile(generatedReport); @@ -444,9 +444,6 @@ public void testInvalidFormat() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - fail("Must nest MavenReportException."); } catch (MojoExecutionException e) { assertTrue(e.getCause() instanceof MavenReportException); @@ -504,8 +501,7 @@ public void testLocationTemp() throws Exception { * Verify that suppressMarker works. */ public void testSuppressMarkerConfiguration() throws Exception { - File generatedReport = - generateReport("pmd", "default-configuration/pmd-with-suppressMarker-plugin-config.xml"); + File generatedReport = generateReport("pmd", "default-configuration/pmd-with-suppressMarker-plugin-config.xml"); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -596,8 +592,7 @@ public void testPMDProcessingError() throws Exception { } public void testPMDProcessingErrorWithDetailsSkipped() throws Exception { - File generatedReport = - generateReport("pmd", "processing-error/pmd-processing-error-skip-plugin-config.xml"); + File generatedReport = generateReport("pmd", "processing-error/pmd-processing-error-skip-plugin-config.xml"); assertTrue(generatedReport.exists()); String output = CapturingPrintStream.getOutput(); @@ -765,9 +760,6 @@ public void testPmdReportResolveRulesets() throws Exception { ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - // these are the rulesets, that have been copied to target/pmd/rulesets File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-custom-rules.xml"); From b3fa399b789baeec933a8d7194624298157ea0ca Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Tue, 11 Nov 2025 11:31:29 +0100 Subject: [PATCH 5/8] chore: migrate junit 3 to junit 5 Signed-off-by: Sandra Parsick --- pom.xml | 11 +- .../maven/plugins/pmd/CpdReportTest.java | 1196 ++++++++--------- .../maven/plugins/pmd/exec/ExecutorTest.java | 10 +- 3 files changed, 591 insertions(+), 626 deletions(-) diff --git a/pom.xml b/pom.xml index 294d598b..7807737e 100644 --- a/pom.xml +++ b/pom.xml @@ -231,12 +231,6 @@ under the License. - - junit - junit - 4.13.2 - test - org.apache.maven.plugin-testing maven-plugin-testing-harness @@ -285,6 +279,11 @@ under the License. 2.20.0 test + + org.junit.jupiter + junit-jupiter-api + test + org.slf4j slf4j-simple diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index 10d5e3f7..1387bf6f 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -18,8 +18,7 @@ */ package org.apache.maven.plugins.pmd; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; +import javax.inject.Inject; import java.io.File; import java.io.IOException; @@ -27,37 +26,35 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Collections; -import java.util.List; import java.util.Locale; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.StringUtils; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoTest; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; -import org.apache.maven.plugin.LegacySupport; import org.apache.maven.plugin.MojoExecution; -import org.apache.maven.plugin.MojoExecutionException; 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; -import org.apache.maven.project.DefaultProjectBuildingRequest; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.reporting.MavenReportException; -import org.apache.maven.session.scope.internal.SessionScope; -import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; -import org.eclipse.aether.repository.LocalRepository; -import org.w3c.dom.Document; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Maria Odea Ching * @version $Id$ */ -public class CpdReportTest extends AbstractMojoTestCase { +@MojoTest +public class CpdReportTest { + + @Inject + private MavenSession mavenSession; + private ArtifactStubFactory artifactStubFactory; /** @@ -75,53 +72,48 @@ public static boolean lowerCaseContains(String text, String contains) { /** * {@inheritDoc} */ - @Override - protected void setUp() throws Exception { - super.setUp(); - CapturingPrintStream.init(true); - - artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); - artifactStubFactory.getWorkingDir().mkdirs(); - SessionScope sessionScope = lookup(SessionScope.class); - sessionScope.enter(); - FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit")); + @BeforeEach + public void setUp() throws Exception { + // CapturingPrintStream.init(true); + // + // artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); + // artifactStubFactory.getWorkingDir().mkdirs(); + // SessionScope sessionScope = lookup(SessionScope.class); + // sessionScope.enter(); + // FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit")); } /** * Test CPDReport given the default configuration */ - public void testDefaultConfiguration() throws Exception { - File pluginXmlFile = new File( - getBasedir(), - "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + @InjectMojo( + goal = "cpd", + pom = "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testDefaultConfiguration(CpdReport mojo) throws Exception { + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // mojo.execute(); + + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); File outputDir = mojo.getReportOutputDirectory(); String filename = mojo.getOutputPath() + ".html"; @@ -142,560 +134,533 @@ public void testDefaultConfiguration() throws Exception { } /** - * Test CPDReport with the text renderer given as "format=txt" - */ - public void testTxtFormat() throws Exception { - File pluginXmlFile = new File( - getBasedir(), - "src/test/resources/unit/" + "custom-configuration/cpd-txt-format-configuration-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - // check if the CPD files were generated - 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(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 - assertTrue(lowerCaseContains(str, "public void duplicateMethod( int i )")); - } - - /** - * Test CpdReport using custom configuration - */ - public void testCustomConfiguration() throws Exception { - File pluginXmlFile = new File( - getBasedir(), - "src/test/resources/unit/" + "custom-configuration/cpd-custom-configuration-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - - File generatedReport = new File(outputDir, filename); - assertTrue(generatedReport.exists()); - - // check if the CPD files were generated - File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv"); - assertTrue(generatedFile.exists()); - - String str = readFile(generatedReport); - // Contents that should NOT be in the report - assertFalse(lowerCaseContains(str, "/Sample.java")); - assertFalse(lowerCaseContains(str, "public void duplicateMethod( int i )")); - // Contents that should be in the report - assertTrue(lowerCaseContains(str, "AnotherSample.java")); - assertTrue(lowerCaseContains(str, "public static void main( String[] args )")); - assertTrue(lowerCaseContains(str, "private String unusedMethod(")); - } - - /** - * Test CPDReport with invalid format - */ - public void testInvalidFormat() throws Exception { - try { - File testPom = new File( - getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", testPom); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - setVariableValueToObject( - mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots()); - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - // TODO this should be a more specific subclass - fail("RuntimeException must be thrown"); - } catch (RuntimeException e) { - assertMavenReportException("Can't find CPD custom format xhtml", e); - } - } - - public void testWriteNonHtml() throws Exception { - File pluginXmlFile = new File( - getBasedir(), - "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - // check if the CPD files were generated - File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - assertTrue(generatedFile.exists()); - - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document pmdCpdDocument = builder.parse(generatedFile); - assertNotNull(pmdCpdDocument); - - String str = readFile(generatedFile); - assertTrue(lowerCaseContains(str, "AppSample.java")); - assertTrue(lowerCaseContains(str, "App.java")); - assertTrue(lowerCaseContains(str, "public String dup( String str )")); - assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);")); - } - - /** - * verify the cpd.xml file is included in the reports when requested. - * - * @throws Exception - */ - public void testIncludeXmlInReports() throws Exception { - File pluginXmlFile = new File( - getBasedir(), - "src/test/resources/unit/" + "default-configuration/cpd-report-include-xml-in-reports-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - assertTrue(generatedFile.exists()); - - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document pmdCpdDocument = builder.parse(generatedFile); - assertNotNull(pmdCpdDocument); - - String str = readFile(generatedFile); - assertTrue(str.contains("")); - - File siteReport = new File(getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml"); - assertTrue(new File(siteReport.getAbsolutePath()).exists()); - String siteReportContent = readFile(siteReport); - assertTrue(siteReportContent.contains("")); - assertEquals(str, siteReportContent); - } - - public void testSkipEmptyReportConfiguration() throws Exception { - // verify the generated files do not exist because PMD was skipped - File pluginXmlFile = new File( - getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-skip-empty-report-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - - File generatedReport = new File(outputDir, filename); - assertFalse(new File(generatedReport.getAbsolutePath()).exists()); - } - - public void testEmptyReportConfiguration() throws Exception { - // verify the generated files do exist, even if there are no violations - File pluginXmlFile = - new File(getBasedir(), "src/test/resources/unit/" + "empty-report/cpd-empty-report-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - - File generatedReport = new File(outputDir, filename); - assertTrue( - generatedReport.getAbsolutePath() + " does not exist", - new File(generatedReport.getAbsolutePath()).exists()); - - String str = readFile(generatedReport); - assertFalse(lowerCaseContains(str, "Hello.java")); - assertTrue(str.contains("CPD found no problems in your source code.")); - } - - public void testCpdEncodingConfiguration() throws Exception { - String originalEncoding = System.getProperty("file.encoding"); - try { - System.setProperty("file.encoding", "UTF-16"); - - File pluginXmlFile = new File( - getBasedir(), - "src/test/resources/unit/" + "default-configuration/cpd-default-configuration-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - // check if the CPD files were generated - File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - assertTrue(generatedFile.exists()); - String str = readFile(generatedFile); - assertTrue(lowerCaseContains(str, "AppSample.java")); - } finally { - System.setProperty("file.encoding", originalEncoding); - } - } - - public void testCpdJavascriptConfiguration() throws Exception { - File pluginXmlFile = new File( - getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-javascript-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - // verify the generated file exists and violations are reported - File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - assertTrue(generatedFile.exists()); - String str = readFile(generatedFile); - assertTrue(lowerCaseContains(str, "Sample.js")); - assertTrue(lowerCaseContains(str, "SampleDup.js")); - } - - public void testCpdJspConfiguration() throws Exception { - File pluginXmlFile = - new File(getBasedir(), "src/test/resources/unit/" + "default-configuration/cpd-jsp-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - // verify the generated file exists and violations are reported - File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - assertTrue(generatedFile.exists()); - String str = readFile(generatedFile); - assertTrue(lowerCaseContains(str, "sample.jsp")); - assertTrue(lowerCaseContains(str, "sampleDup.jsp")); - } - - public void testExclusionsConfiguration() throws Exception { - File pluginXmlFile = new File( - getBasedir(), - "src/test/resources/unit/" - + "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("cpd", pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - // verify the generated file exists and no duplications are reported - File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - assertTrue(generatedFile.exists()); - String str = readFile(generatedFile); - assertEquals(0, StringUtils.countMatches(str, " reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - fail("MojoExecutionException must be thrown"); - } catch (MojoExecutionException e) { - assertMavenReportException("There was 1 error while executing CPD", e); - assertReportContains("Lexical error in file"); - assertReportContains("BadFile.java"); - } - } + * // * Test CPDReport with the text renderer given as "format=txt" + * // */ + // @Test + // public void testTxtFormat(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // // check if the CPD files were generated + // 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(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 + // assertTrue(lowerCaseContains(str, "public void duplicateMethod( int i )")); + // } + // + // /** + // * Test CpdReport using custom configuration + // */ + // @Test + // public void testCustomConfiguration(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // File outputDir = mojo.getReportOutputDirectory(); + // String filename = mojo.getOutputPath() + ".html"; + // + // File generatedReport = new File(outputDir, filename); + // assertTrue(generatedReport.exists()); + // + // // check if the CPD files were generated + // File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv"); + // assertTrue(generatedFile.exists()); + // + // String str = readFile(generatedReport); + // // Contents that should NOT be in the report + // assertFalse(lowerCaseContains(str, "/Sample.java")); + // assertFalse(lowerCaseContains(str, "public void duplicateMethod( int i )")); + // // Contents that should be in the report + // assertTrue(lowerCaseContains(str, "AnotherSample.java")); + // assertTrue(lowerCaseContains(str, "public static void main( String[] args )")); + // assertTrue(lowerCaseContains(str, "private String unusedMethod(")); + // } + // + // /** + // * Test CPDReport with invalid format + // */ + // @Test + // public void testInvalidFormat() throws Exception { + // try { + // File testPom = new File( + // getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml"); + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // // TODO this should be a more specific subclass + // fail("RuntimeException must be thrown"); + // } catch (RuntimeException e) { + // assertMavenReportException("Can't find CPD custom format xhtml", e); + // } + // } + // + // @Test + // public void testWriteNonHtml(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // // check if the CPD files were generated + // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + // assertTrue(generatedFile.exists()); + // + // DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + // Document pmdCpdDocument = builder.parse(generatedFile); + // assertNotNull(pmdCpdDocument); + // + // String str = readFile(generatedFile); + // assertTrue(lowerCaseContains(str, "AppSample.java")); + // assertTrue(lowerCaseContains(str, "App.java")); + // assertTrue(lowerCaseContains(str, "public String dup( String str )")); + // assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);")); + // } + // + // /** + // * verify the cpd.xml file is included in the reports when requested. + // * + // * @throws Exception + // */ + // @Test + // public void testIncludeXmlInReports(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + // assertTrue(generatedFile.exists()); + // + // DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + // Document pmdCpdDocument = builder.parse(generatedFile); + // assertNotNull(pmdCpdDocument); + // + // String str = readFile(generatedFile); + // assertTrue(str.contains("")); + // + // File siteReport = new File(getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml"); + // assertTrue(new File(siteReport.getAbsolutePath()).exists()); + // String siteReportContent = readFile(siteReport); + // assertTrue(siteReportContent.contains("")); + // assertEquals(str, siteReportContent); + // } + // + // @Test + // public void testSkipEmptyReportConfiguration(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // File outputDir = mojo.getReportOutputDirectory(); + // String filename = mojo.getOutputPath() + ".html"; + // + // File generatedReport = new File(outputDir, filename); + // assertFalse(new File(generatedReport.getAbsolutePath()).exists()); + // } + // + // @Test + // public void testEmptyReportConfiguration(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // File outputDir = mojo.getReportOutputDirectory(); + // String filename = mojo.getOutputPath() + ".html"; + // + // File generatedReport = new File(outputDir, filename); + // assertTrue( + // 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 { + // System.setProperty("file.encoding", "UTF-16"); + // + // File pluginXmlFile = new File( + // getBasedir(), + // "src/test/resources/unit/" + + // "default-configuration/cpd-default-configuration-plugin-config.xml"); + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // // check if the CPD files were generated + // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + // assertTrue(generatedFile.exists()); + // String str = readFile(generatedFile); + // assertTrue(lowerCaseContains(str, "AppSample.java")); + // } finally { + // System.setProperty("file.encoding", originalEncoding); + // } + // } + // + // @Test + // public void testCpdJavascriptConfiguration(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // // verify the generated file exists and violations are reported + // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + // assertTrue(generatedFile.exists()); + // String str = readFile(generatedFile); + // assertTrue(lowerCaseContains(str, "Sample.js")); + // assertTrue(lowerCaseContains(str, "SampleDup.js")); + // } + // + // @Test + // public void testCpdJspConfiguration(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // // verify the generated file exists and violations are reported + // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + // assertTrue(generatedFile.exists()); + // String str = readFile(generatedFile); + // assertTrue(lowerCaseContains(str, "sample.jsp")); + // assertTrue(lowerCaseContains(str, "sampleDup.jsp")); + // } + // + // @Test + // public void testExclusionsConfiguration(AbstractPmdReport mojo1) throws Exception { + // assertNotNull(mojo1, "Mojo not found."); + // + // SessionScope sessionScope = lookup(SessionScope.class); + // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); + // sessionScope.seed(MavenSession.class, mavenSession); + // + // DefaultRepositorySystemSession repositorySession = + // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); + // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() + // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); + // + // List reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // // verify the generated file exists and no duplications are reported + // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + // assertTrue(generatedFile.exists()); + // String str = readFile(generatedFile); + // assertEquals(0, StringUtils.countMatches(str, " reactorProjects = + // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); + // + // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); + // setVariableValueToObject(mojo1, "session", mavenSession); + // setVariableValueToObject(mojo1, "repoSession", repositorySession); + // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); + // setVariableValueToObject( + // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); + // setVariableValueToObject( + // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); + // AbstractPmdReport mojo = mojo1; + // mojo.execute(); + // + // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + // + // fail("MojoExecutionException must be thrown"); + // } catch (MojoExecutionException e) { + // assertMavenReportException("There was 1 error while executing CPD", e); + // assertReportContains("Lexical error in file"); + // assertReportContains("BadFile.java"); + // } + // } private static void assertMavenReportException(String expectedMessage, Exception 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 { @@ -703,14 +668,13 @@ private static void assertReportContains(String expectedMessage) throws IOExcept 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 - protected void tearDown() throws Exception { - SessionScope lookup = lookup(SessionScope.class); - lookup.exit(); - super.tearDown(); + @AfterEach + public void tearDown() throws Exception { + // SessionScope lookup = lookup(SessionScope.class); + // lookup.exit(); } /** diff --git a/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java b/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java index 3aaaf4a1..90397895 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/exec/ExecutorTest.java @@ -23,11 +23,13 @@ import java.net.URL; import java.net.URLClassLoader; -import junit.framework.TestCase; import org.apache.commons.lang3.SystemUtils; -import org.junit.Assert; +import org.junit.jupiter.api.Test; -public class ExecutorTest extends TestCase { +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ExecutorTest { + @Test public void testBuildClasspath() throws MalformedURLException { String basename = "home/test/dir with space/mylib.jar"; String pathname = new File("/", basename).getPath(); @@ -39,6 +41,6 @@ public void testBuildClasspath() throws MalformedURLException { StringBuilder classpath = new StringBuilder(); Executor.buildClasspath(classpath, mockedClassLoader); - Assert.assertEquals(pathname + File.pathSeparator, classpath.toString()); + assertEquals(pathname + File.pathSeparator, classpath.toString()); } } From 98a9e09e9a02c6a8c4a99e97ca34926f7aec6213 Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Wed, 12 Nov 2025 16:13:41 +0100 Subject: [PATCH 6/8] chore: migrate to mojotest - CpdReportTest Signed-off-by: Sandra Parsick --- pom.xml | 17 +- .../maven/plugins/pmd/CpdReportTest.java | 860 ++++++------------ .../CpdReportTest/with-cpd-errors/pom.xml | 2 +- ...cpd-custom-configuration-plugin-config.xml | 2 +- ...txt-format-configuration-plugin-config.xml | 2 +- ...pd-default-configuration-plugin-config.xml | 2 +- ...d-encoding-configuration-plugin-config.xml | 2 +- .../cpd-javascript-plugin-config.xml | 2 +- .../cpd-jsp-plugin-config.xml | 2 +- ...exclusions-configuration-plugin-config.xml | 4 +- ...d-report-include-xml-in-reports-config.xml | 2 +- .../excludes/my-exclude-cpd.properties | 19 + .../excludes/pmd_exclude.properties | 21 + .../cpd-skip-empty-report-plugin-config.xml | 6 +- .../cpd-invalid-format-plugin-config.xml | 1 + 15 files changed, 347 insertions(+), 597 deletions(-) create mode 100644 src/test/resources/unit/default-configuration/excludes/my-exclude-cpd.properties create mode 100644 src/test/resources/unit/default-configuration/excludes/pmd_exclude.properties diff --git a/pom.xml b/pom.xml index 7807737e..e9b63915 100644 --- a/pom.xml +++ b/pom.xml @@ -234,7 +234,7 @@ under the License. org.apache.maven.plugin-testing maven-plugin-testing-harness - 4.0.0-alpha-2 + 3.4.0 test @@ -279,11 +279,16 @@ under the License. 2.20.0 test - - org.junit.jupiter - junit-jupiter-api - test - + + org.junit.vintage + junit-vintage-engine + test + + + org.junit.jupiter + junit-jupiter-api + test + org.slf4j slf4j-simple diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index 1387bf6f..9cebfbd7 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -19,6 +19,8 @@ package org.apache.maven.plugins.pmd; import javax.inject.Inject; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; import java.io.IOException; @@ -26,24 +28,38 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Collections; import java.util.Locale; +import org.apache.commons.lang3.StringUtils; +import org.apache.maven.api.plugin.testing.Basedir; import org.apache.maven.api.plugin.testing.InjectMojo; import org.apache.maven.api.plugin.testing.MojoParameter; import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenSession; +import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory; import org.apache.maven.model.Plugin; 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.ArtifactStubFactory; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; import org.apache.maven.reporting.MavenReportException; -import org.junit.jupiter.api.AfterEach; +import org.codehaus.plexus.testing.PlexusExtension; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.repository.RemoteRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.w3c.dom.Document; import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir; +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 Maria Odea Ching @@ -55,7 +71,14 @@ public class CpdReportTest { @Inject private MavenSession mavenSession; - private ArtifactStubFactory artifactStubFactory; + @Inject + private DefaultRepositorySystemSessionFactory repoSessionFactory; + + @Inject + private MavenProject testMavenProject; + + @Inject + private MojoExecution mojoExecution; /** * Checks whether the string contained is contained in @@ -73,47 +96,34 @@ public static boolean lowerCaseContains(String text, String contains) { * {@inheritDoc} */ @BeforeEach - public void setUp() throws Exception { - // CapturingPrintStream.init(true); - // - // artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); - // artifactStubFactory.getWorkingDir().mkdirs(); - // SessionScope sessionScope = lookup(SessionScope.class); - // sessionScope.enter(); - // FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit")); + public void setUp() { + ArtifactRepository localRepo = Mockito.mock(ArtifactRepository.class); + Mockito.when(localRepo.getBasedir()) + .thenReturn(new File(PlexusExtension.getBasedir(), "target/local-repo").getAbsolutePath()); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setLocalRepository(localRepo); + + RemoteRepository centralRepo = + new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build(); + + DefaultRepositorySystemSession systemSession = repoSessionFactory.newRepositorySession(request); + Mockito.when(mavenSession.getRepositorySession()).thenReturn(systemSession); + Mockito.when(testMavenProject.getRemoteProjectRepositories()) + .thenReturn(Collections.singletonList(centralRepo)); + + Mockito.when(mojoExecution.getPlugin()).thenReturn(new Plugin()); } /** * Test CPDReport given the default configuration */ - @InjectMojo( - goal = "cpd", - pom = "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml") + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-default-configuration-plugin-config.xml") @MojoParameter(name = "siteDirectory", value = "src/site") @Test public void testDefaultConfiguration(CpdReport mojo) throws Exception { - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // mojo.execute(); - - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + mojo.execute(); File outputDir = mojo.getReportOutputDirectory(); String filename = mojo.getOutputPath() + ".html"; @@ -136,524 +146,240 @@ public void testDefaultConfiguration(CpdReport mojo) throws Exception { /** * // * Test CPDReport with the text renderer given as "format=txt" * // */ - // @Test - // public void testTxtFormat(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // // check if the CPD files were generated - // 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(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 - // assertTrue(lowerCaseContains(str, "public void duplicateMethod( int i )")); - // } - // - // /** - // * Test CpdReport using custom configuration - // */ - // @Test - // public void testCustomConfiguration(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // File outputDir = mojo.getReportOutputDirectory(); - // String filename = mojo.getOutputPath() + ".html"; - // - // File generatedReport = new File(outputDir, filename); - // assertTrue(generatedReport.exists()); - // - // // check if the CPD files were generated - // File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv"); - // assertTrue(generatedFile.exists()); - // - // String str = readFile(generatedReport); - // // Contents that should NOT be in the report - // assertFalse(lowerCaseContains(str, "/Sample.java")); - // assertFalse(lowerCaseContains(str, "public void duplicateMethod( int i )")); - // // Contents that should be in the report - // assertTrue(lowerCaseContains(str, "AnotherSample.java")); - // assertTrue(lowerCaseContains(str, "public static void main( String[] args )")); - // assertTrue(lowerCaseContains(str, "private String unusedMethod(")); - // } - // - // /** - // * Test CPDReport with invalid format - // */ - // @Test - // public void testInvalidFormat() throws Exception { - // try { - // File testPom = new File( - // getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml"); - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // // TODO this should be a more specific subclass - // fail("RuntimeException must be thrown"); - // } catch (RuntimeException e) { - // assertMavenReportException("Can't find CPD custom format xhtml", e); - // } - // } - // - // @Test - // public void testWriteNonHtml(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // // check if the CPD files were generated - // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - // assertTrue(generatedFile.exists()); - // - // DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - // Document pmdCpdDocument = builder.parse(generatedFile); - // assertNotNull(pmdCpdDocument); - // - // String str = readFile(generatedFile); - // assertTrue(lowerCaseContains(str, "AppSample.java")); - // assertTrue(lowerCaseContains(str, "App.java")); - // assertTrue(lowerCaseContains(str, "public String dup( String str )")); - // assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);")); - // } - // - // /** - // * verify the cpd.xml file is included in the reports when requested. - // * - // * @throws Exception - // */ - // @Test - // public void testIncludeXmlInReports(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - // assertTrue(generatedFile.exists()); - // - // DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - // Document pmdCpdDocument = builder.parse(generatedFile); - // assertNotNull(pmdCpdDocument); - // - // String str = readFile(generatedFile); - // assertTrue(str.contains("")); - // - // File siteReport = new File(getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml"); - // assertTrue(new File(siteReport.getAbsolutePath()).exists()); - // String siteReportContent = readFile(siteReport); - // assertTrue(siteReportContent.contains("")); - // assertEquals(str, siteReportContent); - // } - // - // @Test - // public void testSkipEmptyReportConfiguration(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // File outputDir = mojo.getReportOutputDirectory(); - // String filename = mojo.getOutputPath() + ".html"; - // - // File generatedReport = new File(outputDir, filename); - // assertFalse(new File(generatedReport.getAbsolutePath()).exists()); - // } - // - // @Test - // public void testEmptyReportConfiguration(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // File outputDir = mojo.getReportOutputDirectory(); - // String filename = mojo.getOutputPath() + ".html"; - // - // File generatedReport = new File(outputDir, filename); - // assertTrue( - // 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 { - // System.setProperty("file.encoding", "UTF-16"); - // - // File pluginXmlFile = new File( - // getBasedir(), - // "src/test/resources/unit/" + - // "default-configuration/cpd-default-configuration-plugin-config.xml"); - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // // check if the CPD files were generated - // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - // assertTrue(generatedFile.exists()); - // String str = readFile(generatedFile); - // assertTrue(lowerCaseContains(str, "AppSample.java")); - // } finally { - // System.setProperty("file.encoding", originalEncoding); - // } - // } - // - // @Test - // public void testCpdJavascriptConfiguration(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // // verify the generated file exists and violations are reported - // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - // assertTrue(generatedFile.exists()); - // String str = readFile(generatedFile); - // assertTrue(lowerCaseContains(str, "Sample.js")); - // assertTrue(lowerCaseContains(str, "SampleDup.js")); - // } - // - // @Test - // public void testCpdJspConfiguration(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // // verify the generated file exists and violations are reported - // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - // assertTrue(generatedFile.exists()); - // String str = readFile(generatedFile); - // assertTrue(lowerCaseContains(str, "sample.jsp")); - // assertTrue(lowerCaseContains(str, "sampleDup.jsp")); - // } - // - // @Test - // public void testExclusionsConfiguration(AbstractPmdReport mojo1) throws Exception { - // assertNotNull(mojo1, "Mojo not found."); - // - // SessionScope sessionScope = lookup(SessionScope.class); - // MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - // sessionScope.seed(MavenSession.class, mavenSession); - // - // DefaultRepositorySystemSession repositorySession = - // (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - // repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - // .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - // - // List reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // // verify the generated file exists and no duplications are reported - // File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); - // assertTrue(generatedFile.exists()); - // String str = readFile(generatedFile); - // assertEquals(0, StringUtils.countMatches(str, " reactorProjects = - // mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - // - // setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - // setVariableValueToObject(mojo1, "session", mavenSession); - // setVariableValueToObject(mojo1, "repoSession", repositorySession); - // setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - // setVariableValueToObject( - // mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - // setVariableValueToObject( - // mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - // AbstractPmdReport mojo = mojo1; - // mojo.execute(); - // - // ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - // buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // - // fail("MojoExecutionException must be thrown"); - // } catch (MojoExecutionException e) { - // assertMavenReportException("There was 1 error while executing CPD", e); - // assertReportContains("Lexical error in file"); - // assertReportContains("BadFile.java"); - // } - // } + @Basedir("/unit/custom-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-txt-format-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testTxtFormat(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + // check if the CPD files were generated + 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(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 + assertTrue(lowerCaseContains(str, "public void duplicateMethod( int i )")); + } + + /** + * Test CpdReport using custom configuration + */ + @Basedir("/unit/custom-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-custom-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testCustomConfiguration(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); + assertTrue(generatedReport.exists()); + + // check if the CPD files were generated + File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv"); + assertTrue(generatedFile.exists()); + + String str = readFile(generatedReport); + // Contents that should NOT be in the report + assertFalse(lowerCaseContains(str, "/Sample.java")); + assertFalse(lowerCaseContains(str, "public void duplicateMethod( int i )")); + // Contents that should be in the report + assertTrue(lowerCaseContains(str, "AnotherSample.java")); + assertTrue(lowerCaseContains(str, "public static void main( String[] args )")); + assertTrue(lowerCaseContains(str, "private String unusedMethod(")); + } + + /** + * Test CPDReport with invalid format + */ + @Basedir("/unit/invalid-format") + @InjectMojo(goal = "cpd", pom = "cpd-invalid-format-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testInvalidFormat(AbstractPmdReport mojo) { + try { + mojo.execute(); + + // TODO should have more specific exception + fail("RuntimeException must be thrown"); + } catch (RuntimeException e) { + assertMavenReportException("Can't find CPD custom format xhtml", e); + } + } + + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-default-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testWriteNonHtml(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + // check if the CPD files were generated + File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + assertTrue(generatedFile.exists()); + + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document pmdCpdDocument = builder.parse(generatedFile); + assertNotNull(pmdCpdDocument); + + String str = readFile(generatedFile); + assertTrue(lowerCaseContains(str, "AppSample.java")); + assertTrue(lowerCaseContains(str, "App.java")); + assertTrue(lowerCaseContains(str, "public String dup( String str )")); + assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);")); + } + + /** + * verify the cpd.xml file is included in the reports when requested. + * + * @throws Exception + */ + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-report-include-xml-in-reports-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testIncludeXmlInReports(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + assertTrue(generatedFile.exists()); + + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document pmdCpdDocument = builder.parse(generatedFile); + assertNotNull(pmdCpdDocument); + + String str = readFile(generatedFile); + assertTrue(str.contains("")); + + File siteReport = new File(getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml"); + assertTrue(new File(siteReport.getAbsolutePath()).exists()); + String siteReportContent = readFile(siteReport); + assertTrue(siteReportContent.contains("")); + assertEquals(str, siteReportContent); + } + + @Basedir("/unit/empty-report") + @InjectMojo(goal = "cpd", pom = "cpd-skip-empty-report-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testSkipEmptyReportConfiguration(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); + assertFalse(new File(generatedReport.getAbsolutePath()).exists()); + } + + @Basedir("/unit/empty-report") + @InjectMojo(goal = "cpd", pom = "cpd-empty-report-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testEmptyReportConfiguration(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); + assertTrue( + 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.")); + } + + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-default-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testCpdEncodingConfiguration(AbstractPmdReport mojo) throws Exception { + String originalEncoding = System.getProperty("file.encoding"); + try { + System.setProperty("file.encoding", "UTF-16"); + + mojo.execute(); + + // check if the CPD files were generated + File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + assertTrue(generatedFile.exists()); + String str = readFile(generatedFile); + assertTrue(lowerCaseContains(str, "AppSample.java")); + } finally { + System.setProperty("file.encoding", originalEncoding); + } + } + + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-javascript-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testCpdJavascriptConfiguration(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + // verify the generated file exists and violations are reported + File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + assertTrue(generatedFile.exists()); + String str = readFile(generatedFile); + assertTrue(lowerCaseContains(str, "Sample.js")); + assertTrue(lowerCaseContains(str, "SampleDup.js")); + } + + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-jsp-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testCpdJspConfiguration(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + // verify the generated file exists and violations are reported + File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + assertTrue(generatedFile.exists()); + String str = readFile(generatedFile); + assertTrue(lowerCaseContains(str, "sample.jsp")); + assertTrue(lowerCaseContains(str, "sampleDup.jsp")); + } + + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "cpd", pom = "cpd-report-cpd-exclusions-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testExclusionsConfiguration(AbstractPmdReport mojo) throws Exception { + mojo.execute(); + + // verify the generated file exists and no duplications are reported + File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"); + assertTrue(generatedFile.exists()); + String str = readFile(generatedFile); + assertEquals(0, StringUtils.countMatches(str, "${basedir}/target/test/unit/CpdReportTest/with-cpd-errors/target/site/xref 100 - ${basedir}/src/test/resources/unit/CpdReportTest/with-cpd-errors/src/main/java + ${basedir} UTF-8 diff --git a/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml b/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml index 6fd8a79e..cc96f99d 100644 --- a/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml +++ b/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml @@ -44,7 +44,7 @@ under the License. **/Sample.java - ${basedir}/src/test/resources/unit/custom-configuration/ + ${basedir} UTF-8 diff --git a/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml index ffc29032..9135b2a2 100644 --- a/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml +++ b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml @@ -40,7 +40,7 @@ under the License. false 30 - ${basedir}/src/test/resources/unit/custom-configuration/ + ${basedir} UTF-8 diff --git a/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml index a201c3ee..2d53555f 100644 --- a/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml @@ -41,7 +41,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref 100 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} UTF-8 diff --git a/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml index 66b45328..c3b5bba6 100644 --- a/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/cpd-encoding-configuration-plugin-config.xml @@ -41,7 +41,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref 100 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} UTF-8 diff --git a/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml index 089fb484..92cfee2a 100644 --- a/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml @@ -44,7 +44,7 @@ under the License. **/*.js - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} UTF-8 diff --git a/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml index 3cb44b30..0e0442cb 100644 --- a/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml @@ -44,7 +44,7 @@ under the License. **/*.jsp - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} UTF-8 diff --git a/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml index 76efc020..9e1a6869 100644 --- a/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml @@ -41,10 +41,10 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref 100 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} UTF-8 - ${basedir}/src/test/resources/unit/excludes/my-exclude-cpd.properties + ${basedir}/excludes/my-exclude-cpd.properties diff --git a/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml b/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml index afe562b1..c80008e7 100644 --- a/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml +++ b/src/test/resources/unit/default-configuration/cpd-report-include-xml-in-reports-config.xml @@ -41,7 +41,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref 100 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} UTF-8 true diff --git a/src/test/resources/unit/default-configuration/excludes/my-exclude-cpd.properties b/src/test/resources/unit/default-configuration/excludes/my-exclude-cpd.properties new file mode 100644 index 00000000..c58713bc --- /dev/null +++ b/src/test/resources/unit/default-configuration/excludes/my-exclude-cpd.properties @@ -0,0 +1,19 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +def.configuration.App,def.configuration.AppSample diff --git a/src/test/resources/unit/default-configuration/excludes/pmd_exclude.properties b/src/test/resources/unit/default-configuration/excludes/pmd_exclude.properties new file mode 100644 index 00000000..07b5d528 --- /dev/null +++ b/src/test/resources/unit/default-configuration/excludes/pmd_exclude.properties @@ -0,0 +1,21 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +def.configuration.App=UnusedPrivateField,EmptyCatchBlock,UselessParentheses +def.configuration.App2=UnnecessaryImport +def.configuration.AppSample=UnusedPrivateField,UnusedFormalParameter,UnusedPrivateMethod,UselessParentheses diff --git a/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml b/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml index 9109cda4..2a977e47 100644 --- a/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml +++ b/src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml @@ -34,10 +34,10 @@ under the License. maven-pmd-plugin - ${basedir}/target/test/unit/empty-report/target/site - ${basedir}/target/test/unit/empty-report/target + ${basedir}/target/test/unit/skip-empty-report/target/site + ${basedir}/target/test/unit/skip-empty-report/target - ${basedir}/src/test/resources/unit/empty-report/java/ + ${basedir} UTF-8 100 diff --git a/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml b/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml index 8393c5e2..5fff1c91 100644 --- a/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml +++ b/src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml @@ -39,6 +39,7 @@ under the License. xhtml false ${basedir}/target/test/unit/invalid-format/target/site/xref + ${basedir} 25 From 4d61e743f5c0959f16085be014c4bcf4813359d8 Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Wed, 12 Nov 2025 16:13:57 +0100 Subject: [PATCH 7/8] chore: migrate to mojotest - PmdReportTest Signed-off-by: Sandra Parsick --- .../maven/plugins/pmd/PmdReportTest.java | 554 +++++++++--------- .../custom-configuration-plugin-config.xml | 4 +- .../skip-plugin-config.xml | 3 + .../default-configuration-plugin-config.xml | 2 +- ...javascript-configuration-plugin-config.xml | 2 +- .../jsp-configuration-plugin-config.xml | 2 +- ...ck-default-configuration-plugin-config.xml | 3 + ...ck-failandwarnonpriority-plugin-config.xml | 3 + ...d-check-failmaxviolation-plugin-config.xml | 3 + ...pmd-check-failonpriority-plugin-config.xml | 3 + ...heck-notfailmaxviolation-plugin-config.xml | 3 + ...check-notfailonviolation-plugin-config.xml | 3 + ...exclusions-configuration-plugin-config.xml | 3 + .../pmd-report-custom-rules.xml | 8 +- ...d-report-include-xml-in-reports-config.xml | 3 + ...eport-no-render-violations-by-priority.xml | 2 +- ...not-render-rule-priority-plugin-config.xml | 2 +- ...exclusions-configuration-plugin-config.xml | 4 +- .../pmd-report-resolve-rulesets.xml | 4 +- .../pmd-with-analysis-cache-plugin-config.xml | 2 +- ...suppressMarker-no-render-plugin-config.xml | 2 +- .../pmd-with-suppressMarker-plugin-config.xml | 2 +- .../skip-empty-report-plugin-config.xml | 8 +- .../pmd-exclude-roots-plugin-config.xml | 12 +- .../invalid-format-plugin-config.xml | 3 + ...ocessing-error-no-report-plugin-config.xml | 2 +- .../pmd-processing-error-plugin-config.xml | 2 +- ...md-processing-error-skip-plugin-config.xml | 2 +- 28 files changed, 344 insertions(+), 302 deletions(-) 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 e4ef73bc..66a61741 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -18,6 +18,8 @@ */ package org.apache.maven.plugins.pmd; +import javax.inject.Inject; + import java.io.File; import java.io.IOException; import java.net.ServerSocket; @@ -25,7 +27,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Collections; -import java.util.List; import java.util.Locale; import com.github.tomakehurst.wiremock.WireMockServer; @@ -33,34 +34,53 @@ import net.sourceforge.pmd.renderers.Renderer; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.api.plugin.testing.Basedir; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoParameter; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenSession; +import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory; import org.apache.maven.model.Plugin; -import org.apache.maven.plugin.LegacySupport; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; -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; import org.apache.maven.plugins.pmd.exec.PmdExecutor; -import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.reporting.MavenReportException; -import org.apache.maven.session.scope.internal.SessionScope; -import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.testing.PlexusExtension; import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; -import org.eclipse.aether.repository.LocalRepository; +import org.eclipse.aether.repository.RemoteRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir; +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 Maria Odea Ching * @version $Id$ */ -public class PmdReportTest extends AbstractMojoTestCase { +@MojoTest +public class PmdReportTest { + + @Inject + private MavenSession mavenSession; + + @Inject + private DefaultRepositorySystemSessionFactory repoSessionFactory; - private ArtifactStubFactory artifactStubFactory; + @Inject + private MavenProject testMavenProject; + + @Inject + private MojoExecution mojoExecution; /** * Checks whether the string contained is contained in @@ -77,24 +97,42 @@ public static boolean lowerCaseContains(String text, String contains) { /** * {@inheritDoc} */ - @Override - protected void setUp() throws Exception { - super.setUp(); + @BeforeEach + public void setUp() throws Exception { CapturingPrintStream.init(true); + ArtifactRepository localRepo = Mockito.mock(ArtifactRepository.class); + Mockito.when(localRepo.getBasedir()) + .thenReturn(new File(PlexusExtension.getBasedir(), "target/local-repo").getAbsolutePath()); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setLocalRepository(localRepo); - artifactStubFactory = new DependencyArtifactStubFactory(getTestFile("target"), true, false); - artifactStubFactory.getWorkingDir().mkdirs(); - SessionScope sessionScope = lookup(SessionScope.class); - sessionScope.enter(); - org.apache.commons.io.FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit")); + RemoteRepository centralRepo = + new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build(); + + DefaultRepositorySystemSession systemSession = repoSessionFactory.newRepositorySession(request); + Mockito.when(mavenSession.getRepositorySession()).thenReturn(systemSession); + Mockito.when(mavenSession.getRequest()).thenReturn(request); + Mockito.when(testMavenProject.getRemoteProjectRepositories()) + .thenReturn(Collections.singletonList(centralRepo)); + + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-pmd-plugin"); + Mockito.when(mojoExecution.getPlugin()).thenReturn(plugin); } - public void testDefaultConfiguration() throws Exception { - FileUtils.copyDirectoryStructure( - new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), - new File(getBasedir(), "target/test/unit/default-configuration/target/site")); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "default-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testDefaultConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; - File generatedReport = generateReport("pmd", "default-configuration/default-configuration-plugin-config.xml"); + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -139,13 +177,17 @@ public void testDefaultConfiguration() throws Exception { assertTrue(output.contains("PMD version: " + AbstractPmdReport.getPmdVersion())); } - public void testDefaultConfigurationNotRenderRuleViolationPriority() throws Exception { - FileUtils.copyDirectoryStructure( - new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), - new File(getBasedir(), "target/test/unit/default-configuration/target/site")); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-report-not-render-rule-priority-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testDefaultConfigurationNotRenderRuleViolationPriority(PmdReport mojo) throws Exception { + mojo.execute(); - File generatedReport = - generateReport("pmd", "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml"); + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); String str = readFile(generatedReport); @@ -154,13 +196,17 @@ public void testDefaultConfigurationNotRenderRuleViolationPriority() throws Exce assertFalse(str.contains("Priority")); } - public void testDefaultConfigurationNoRenderViolationsByPriority() throws Exception { - FileUtils.copyDirectoryStructure( - new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), - new File(getBasedir(), "target/test/unit/default-configuration/target/site")); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-report-no-render-violations-by-priority.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testDefaultConfigurationNoRenderViolationsByPriority(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; - File generatedReport = - generateReport("pmd", "default-configuration/pmd-report-no-render-violations-by-priority.xml"); + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); String str = readFile(generatedReport); @@ -173,12 +219,12 @@ public void testDefaultConfigurationNoRenderViolationsByPriority() throws Except assertEquals(1, StringUtils.countMatches(str, "def/configuration/App.java")); } - public void testDefaultConfigurationWithAnalysisCache() throws Exception { - FileUtils.copyDirectoryStructure( - new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), - new File(getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site")); - - generateReport("pmd", "default-configuration/pmd-with-analysis-cache-plugin-config.xml"); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-with-analysis-cache-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testDefaultConfigurationWithAnalysisCache(PmdReport mojo) throws Exception { + mojo.execute(); // check if the PMD analysis cache file has been generated File cacheFile = @@ -186,9 +232,17 @@ public void testDefaultConfigurationWithAnalysisCache() throws Exception { assertTrue(cacheFile.exists()); } - public void testJavascriptConfiguration() throws Exception { - File generatedReport = - generateReport("pmd", "default-configuration/javascript-configuration-plugin-config.xml"); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "javascript-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testJavascriptConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -212,37 +266,11 @@ public void testJavascriptConfiguration() throws Exception { assertTrue(str.contains("Avoid using global variables")); } - public void testFileURL() throws Exception { - FileUtils.copyDirectoryStructure( - new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), - new File(getBasedir(), "target/test/unit/default-configuration/target/site")); - - File testPom = new File( - getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("pmd", testPom); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - PmdReport mojo = (PmdReport) mojo1; + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "default-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testFileURL(PmdReport mojo) throws Exception { // Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174). int port = determineFreePort(); @@ -285,9 +313,6 @@ public void testFileURL() throws Exception { mojo.execute(); - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - File outputDir = mojo.getReportOutputDirectory(); String filename = mojo.getOutputPath() + ".html"; @@ -337,8 +362,17 @@ private int determineFreePort() throws IOException { * * @throws Exception */ - public void testCustomConfiguration() throws Exception { - File generatedReport = generateReport("pmd", "custom-configuration/custom-configuration-plugin-config.xml"); + @Basedir("/unit/custom-configuration") + @InjectMojo(goal = "pmd", pom = "custom-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testCustomConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check the generated files @@ -360,8 +394,8 @@ public void testCustomConfiguration() throws Exception { assertFalse(lowerCaseContains(str, "Avoid using if...else statements without curly braces")); assertFalse( - "unnecessary constructor should not be triggered because of low priority", - lowerCaseContains(str, "Avoid unnecessary constructors - the compiler will generate these for you")); + lowerCaseContains(str, "Avoid unnecessary constructors - the compiler will generate these for you"), + "unnecessary constructor should not be triggered because of low priority"); // veryLongVariableNameWithViolation is really too long assertTrue(lowerCaseContains(str, "veryLongVariableNameWithViolation")); @@ -374,8 +408,17 @@ public void testCustomConfiguration() throws Exception { * * @throws Exception */ - public void testSkipConfiguration() throws Exception { - File generatedReport = generateReport("pmd", "custom-configuration/skip-plugin-config.xml"); + @Basedir("/unit/custom-configuration") + @InjectMojo(goal = "pmd", pom = "skip-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testSkipConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertFalse(generatedReport.exists()); // verify the generated files do not exist because PMD was skipped @@ -390,14 +433,31 @@ public void testSkipConfiguration() throws Exception { assertTrue(output.contains("Skipping org.apache.maven.plugins:maven-pmd-plugin")); } - public void testSkipEmptyReportConfiguration() throws Exception { - // verify the generated files do not exist because PMD was skipped - File generatedReport = generateReport("pmd", "empty-report/skip-empty-report-plugin-config.xml"); + @Basedir("/unit/empty-report") + @InjectMojo(goal = "pmd", pom = "skip-empty-report-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testSkipEmptyReportConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertFalse(generatedReport.exists()); } - public void testEmptyReportConfiguration() throws Exception { - File generatedReport = generateReport("pmd", "empty-report/empty-report-plugin-config.xml"); + @Basedir("/unit/empty-report") + @InjectMojo(goal = "pmd", pom = "empty-report-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testEmptyReportConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // verify the generated files do exist, even if there are no violations @@ -409,52 +469,30 @@ public void testEmptyReportConfiguration() throws Exception { assertFalse(str.contains("Violations By Priority")); } - public void testInvalidFormat() throws Exception { + @Basedir("/unit/invalid-format") + @InjectMojo(goal = "pmd", pom = "invalid-format-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testInvalidFormat(PmdReport mojo) { try { - File testPom = - new File(getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml"); - AbstractPmdReport mojo1 = lookupMojo("pmd", testPom); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - setVariableValueToObject( - mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots()); - mojo.execute(); - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); + mojo.execute(); - fail("Must nest MavenReportException."); + fail("Must nested MavenReportException."); } catch (MojoExecutionException e) { assertTrue(e.getCause() instanceof MavenReportException); } } - public void testInvalidTargetJdk() throws Exception { + @Basedir("/unit/invalid-format") + @InjectMojo(goal = "pmd", pom = "invalid-target-jdk-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testInvalidTargetJdk(PmdReport mojo) throws Exception { try { - generateReport("pmd", "invalid-format/invalid-target-jdk-plugin-config.xml"); + mojo.execute(); - fail("Must nest MavenReportException."); + fail("Must nested MavenReportException."); } catch (MojoExecutionException e) { assertTrue(e.getCause() instanceof MavenReportException); } @@ -463,9 +501,17 @@ public void testInvalidTargetJdk() throws Exception { /** * Verify the pmd.xml file is included in the reports when requested. */ - public void testIncludeXmlInReports() throws Exception { - File generatedReport = - generateReport("pmd", "default-configuration/pmd-report-include-xml-in-reports-config.xml"); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-report-include-xml-in-reports-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testIncludeXmlInReports(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // verify the pmd file is included in site @@ -483,25 +529,34 @@ public void testIncludeXmlInReports() throws Exception { /** * Verify the correct working of the locationTemp method. */ - public void testLocationTemp() throws Exception { - - File testPom = new File( - getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml"); - PmdReport mojo = (PmdReport) lookupMojo("pmd", testPom); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "default-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testLocationTemp(PmdReport mojo) { assertEquals( - "locationTemp is not correctly encoding filename", "001-export_format_pmd_language_java_name_some_2520name.xml", mojo.getLocationTemp( "http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name", - 1)); + 1), + "locationTemp is not correctly encoding filename"); } /** * Verify that suppressMarker works. */ - public void testSuppressMarkerConfiguration() throws Exception { - File generatedReport = generateReport("pmd", "default-configuration/pmd-with-suppressMarker-plugin-config.xml"); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-with-suppressMarker-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testSuppressMarkerConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -523,9 +578,17 @@ public void testSuppressMarkerConfiguration() throws Exception { assertTrue(report.contains("Avoid unused private fields such as 'unusedVar2'.")); } - public void testSuppressMarkerConfigurationWithoutRendering() throws Exception { - File generatedReport = - generateReport("pmd", "default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml"); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-with-suppressMarker-no-render-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testSuppressMarkerConfigurationWithoutRendering(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -547,8 +610,17 @@ public void testSuppressMarkerConfigurationWithoutRendering() throws Exception { assertFalse(report.contains("Avoid unused private fields such as 'unusedVar2'.")); } - public void testJspConfiguration() throws Exception { - File generatedReport = generateReport("pmd", "default-configuration/jsp-configuration-plugin-config.xml"); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "jsp-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testJspConfiguration(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); // check if the PMD files were generated @@ -582,21 +654,35 @@ public void testJspConfiguration() throws Exception { assertTrue(str.contains("Avoid having style information in JSP files.")); } - public void testPMDProcessingError() throws Exception { + @Basedir("/unit/processing-error") + @InjectMojo(goal = "pmd", pom = "pmd-processing-error-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testPMDProcessingError(PmdReport mojo) { try { - generateReport("pmd", "processing-error/pmd-processing-error-plugin-config.xml"); + mojo.execute(); + fail("Expected exception"); } catch (MojoExecutionException e) { assertTrue(e.getCause().getMessage().endsWith("Found 1 PMD processing error")); } } - public void testPMDProcessingErrorWithDetailsSkipped() throws Exception { - File generatedReport = generateReport("pmd", "processing-error/pmd-processing-error-skip-plugin-config.xml"); + @Basedir("/unit/processing-error") + @InjectMojo(goal = "pmd", pom = "pmd-processing-error-skip-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testPMDProcessingErrorWithDetailsSkipped(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); String output = CapturingPrintStream.getOutput(); - assertTrue(output, output.contains("There is 1 PMD processing error:")); + assertTrue(output.contains("There is 1 PMD processing error:"), output); File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml"); assertTrue(generatedFile.exists()); @@ -612,13 +698,21 @@ public void testPMDProcessingErrorWithDetailsSkipped() throws Exception { assertTrue(html.contains("at line 23, column 5: Encountered")); } - public void testPMDProcessingErrorWithDetailsNoReport() throws Exception { - File generatedReport = - generateReport("pmd", "processing-error/pmd-processing-error-no-report-plugin-config.xml"); + @Basedir("/unit/processing-error") + @InjectMojo(goal = "pmd", pom = "pmd-processing-error-no-report-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testPMDProcessingErrorWithDetailsNoReport(PmdReport mojo) throws Exception { + mojo.execute(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputPath() + ".html"; + + File generatedReport = new File(outputDir, filename); assertTrue(generatedReport.exists()); String output = CapturingPrintStream.getOutput(); - assertTrue(output, output.contains("There is 1 PMD processing error:")); + assertTrue(output.contains("There is 1 PMD processing error:"), output); File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml"); assertTrue(generatedFile.exists()); @@ -634,23 +728,31 @@ public void testPMDProcessingErrorWithDetailsNoReport() throws Exception { assertFalse(html.contains("at line 23, column 5: Encountered")); } - public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception { - generateReport("pmd", "exclude-roots/pmd-exclude-roots-plugin-config.xml"); + @Basedir("/unit/exclude-roots") + @InjectMojo(goal = "pmd", pom = "pmd-exclude-roots-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testPMDExcludeRootsShouldExcludeSubdirectories(PmdReport mojo) throws Exception { + mojo.execute(); File generatedFile = new File(getBasedir(), "target/test/unit/exclude-roots/target/pmd.xml"); assertTrue(generatedFile.exists()); String str = readFile(generatedFile); - assertTrue("Seems like all directories are excluded now", str.contains("ForLoopShouldBeWhileLoop")); + assertTrue(str.contains("ForLoopShouldBeWhileLoop"), "Seems like all directories are excluded now"); assertFalse( - "Exclusion of an exact source directory not working", str.contains("OverrideBothEqualsAndHashcode")); + str.contains("OverrideBothEqualsAndHashcode"), "Exclusion of an exact source directory not working"); assertFalse( - "Exclusion of base directory with subdirectories not working (MPMD-178)", - str.contains("JumbledIncrementer")); + str.contains("JumbledIncrementer"), + "Exclusion of base directory with subdirectories not working (MPMD-178)"); } - public void testViolationExclusion() throws Exception { - generateReport("pmd", "default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml"); + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-report-pmd-exclusions-configuration-plugin-config.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testViolationExclusion(PmdReport mojo) throws Exception { + mojo.execute(); File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml"); assertTrue(generatedFile.exists()); @@ -659,23 +761,30 @@ public void testViolationExclusion() throws Exception { assertFalse(str.contains("UnusedPrivateField")); } - public void testPmdReportResolveRulesets() throws Exception { + @Basedir("/unit/default-configuration") + @InjectMojo(goal = "pmd", pom = "pmd-report-resolve-rulesets.xml") + @MojoParameter(name = "siteDirectory", value = "src/site") + @Test + public void testPmdReportResolveRulesets(PmdReport mojo) throws Exception { int port = determineFreePort(); WireMockServer mockServer = new WireMockServer(port); mockServer.start(); @@ -722,44 +835,11 @@ public void testPmdReportResolveRulesets() throws Exception { .withHeader("Content-Type", "text/xml") .withBody(sonarRuleset))); - FileUtils.copyDirectoryStructure( - new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"), - new File(getBasedir(), "target/test/unit/default-configuration/target/site")); - - File testPom = - new File(getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml"); - AbstractPmdReport mojo1 = lookupMojo("pmd", testPom); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - PmdReport mojo = (PmdReport) mojo1; mojo.rulesets[3] = sonarExportRulesetUrl; mojo.rulesets[4] = myRulesetUrl; mojo.rulesets[5] = notAInternalRulesetUrl; mojo.execute(); - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - // these are the rulesets, that have been copied to target/pmd/rulesets File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-custom-rules.xml"); @@ -790,78 +870,10 @@ public void testPmdReportResolveRulesets() throws Exception { mockServer.stop(); } - @Override - protected void tearDown() throws Exception { - SessionScope lookup = lookup(SessionScope.class); - lookup.exit(); - super.tearDown(); - } - - /** - * Generate the report and return the generated file. - * - * @param goal the mojo goal - * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/" - * @return the generated HTML file - * @throws Exception if any - */ - protected File generateReport(String goal, String pluginXml) throws Exception { - File pluginXmlFile = new File(getBasedir(), "src/test/resources/unit/" + pluginXml); - AbstractPmdReport mojo1 = lookupMojo(goal, pluginXmlFile); - assertNotNull("Mojo not found.", mojo1); - - SessionScope sessionScope = lookup(SessionScope.class); - MavenSession mavenSession = newMavenSession(new MavenProjectStub()); - sessionScope.seed(MavenSession.class, mavenSession); - - DefaultRepositorySystemSession repositorySession = - (DefaultRepositorySystemSession) mavenSession.getRepositorySession(); - repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory() - .newInstance(repositorySession, new LocalRepository(artifactStubFactory.getWorkingDir()))); - - List reactorProjects = - mojo1.getReactorProjects() != null ? mojo1.getReactorProjects() : Collections.emptyList(); - - setVariableValueToObject(mojo1, "mojoExecution", getMockMojoExecution()); - setVariableValueToObject(mojo1, "session", mavenSession); - setVariableValueToObject(mojo1, "repoSession", repositorySession); - setVariableValueToObject(mojo1, "reactorProjects", reactorProjects); - setVariableValueToObject( - mojo1, "remoteProjectRepositories", mojo1.getProject().getRemoteProjectRepositories()); - setVariableValueToObject( - mojo1, "siteDirectory", new File(mojo1.getProject().getBasedir(), "src/site")); - AbstractPmdReport mojo = mojo1; - mojo.execute(); - - ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); - buildingRequest.setRepositorySession(lookup(LegacySupport.class).getRepositorySession()); - - File outputDir = mojo.getReportOutputDirectory(); - String filename = mojo.getOutputPath() + ".html"; - - return new File(outputDir, filename); - } - /** * Read the contents of the specified file into a string. */ protected String readFile(File file) throws IOException { return new String(Files.readAllBytes(file.toPath())); } - - private MojoExecution getMockMojoExecution() { - MojoDescriptor mojoDescriptor = new MojoDescriptor(); - mojoDescriptor.setGoal("pmd"); - - MojoExecution execution = new MojoExecution(mojoDescriptor); - - PluginDescriptor pluginDescriptor = new PluginDescriptor(); - Plugin plugin = new Plugin(); - plugin.setGroupId("org.apache.maven.plugins"); - plugin.setArtifactId("maven-pmd-plugin"); - pluginDescriptor.setPlugin(plugin); - mojoDescriptor.setPluginDescriptor(pluginDescriptor); - - return execution; - } } diff --git a/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml b/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml index d1ff3f43..86fb5e92 100644 --- a/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml +++ b/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml @@ -43,7 +43,7 @@ under the License. ISO-8859-1 rulesets/java/maven-pmd-plugin-default.xml - ${basedir}/src/test/resources/unit/custom-configuration/resources/rulesets/custom.xml + ${basedir}/resources/rulesets/custom.xml 1.4 4 @@ -52,7 +52,7 @@ under the License. **/AnotherSample.java - ${basedir}/src/test/resources/unit/custom-configuration/ + ${basedir} diff --git a/src/test/resources/unit/custom-configuration/skip-plugin-config.xml b/src/test/resources/unit/custom-configuration/skip-plugin-config.xml index 6f6f159e..db661418 100644 --- a/src/test/resources/unit/custom-configuration/skip-plugin-config.xml +++ b/src/test/resources/unit/custom-configuration/skip-plugin-config.xml @@ -44,6 +44,9 @@ under the License. 1.4 4 true + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml index fb99dbf1..16bc1d3f 100644 --- a/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml @@ -42,7 +42,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref UTF-8 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} diff --git a/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml index 791540f1..99dd156a 100644 --- a/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml @@ -49,7 +49,7 @@ under the License. **/*.js - ${basedir}/src/test/resources/unit/default-configuration/js + ${basedir}/js diff --git a/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml index a0fa7f44..98849624 100644 --- a/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml @@ -51,7 +51,7 @@ under the License. **/*.jsp - ${basedir}/src/test/resources/unit/default-configuration/jsp + ${basedir}/jsp diff --git a/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml index d715f865..2c777086 100644 --- a/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml @@ -37,6 +37,9 @@ under the License. ${basedir}/target/test/unit/default-configuration/target true true + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml index 65ab4361..9ee1351a 100644 --- a/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-check-failandwarnonpriority-plugin-config.xml @@ -38,6 +38,9 @@ under the License. true 3 true + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml index e7a1ddc3..03c510ea 100644 --- a/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-check-failmaxviolation-plugin-config.xml @@ -39,6 +39,9 @@ under the License. 3 true 3 + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml index debd26c3..a696288b 100644 --- a/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml @@ -38,6 +38,9 @@ under the License. true 1 false + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml index 118fb50e..3918e903 100644 --- a/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml @@ -39,6 +39,9 @@ under the License. 3 true 5 + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml index 48733374..b3df097b 100644 --- a/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml @@ -37,6 +37,9 @@ under the License. ${basedir}/target/test/unit/default-configuration/target false false + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml index 6c2e6b51..482d98d5 100644 --- a/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml @@ -40,6 +40,9 @@ under the License. false true ${basedir}/src/test/resources/unit/excludes/pmd_exclude.properties + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml b/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml index c737e21c..0e20e429 100644 --- a/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml +++ b/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml @@ -36,16 +36,16 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site ${basedir}/target/test/unit/default-configuration/target - ${basedir}/target/test/unit/default-configuration/target/pmd/rulesets + ${basedir}/target/pmd/rulesets - ${basedir}/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml + ${basedir}/rulesets/custom-rules.xml xml true - ${basedir}/target/test/unit/default-configuration/target/site/xref + ${basedir}/target/site/xref UTF-8 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} diff --git a/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml b/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml index 88e269bf..f1cb23f2 100644 --- a/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-report-include-xml-in-reports-config.xml @@ -40,6 +40,9 @@ under the License. xml UTF-8 true + + ${basedir} + diff --git a/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml b/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml index 9194f6f9..83428190 100644 --- a/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml +++ b/src/test/resources/unit/default-configuration/pmd-report-no-render-violations-by-priority.xml @@ -42,7 +42,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref UTF-8 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} false diff --git a/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml index 572a5e1e..8f0456f8 100644 --- a/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml @@ -42,7 +42,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref UTF-8 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} false diff --git a/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml index 3387d3ed..47710e66 100644 --- a/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml @@ -42,9 +42,9 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref UTF-8 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} - ${basedir}/src/test/resources/unit/excludes/pmd_exclude.properties + ${basedir}/excludes/pmd_exclude.properties diff --git a/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml b/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml index b03474bb..dc537126 100644 --- a/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml +++ b/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml @@ -38,7 +38,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target ${basedir}/target/test/unit/default-configuration/target/pmd/rulesets - ${basedir}/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml + ${basedir}/rulesets/custom-rules.xml category/java/bestpractices.xml category/java/design.xml @@ -51,7 +51,7 @@ under the License. ${basedir}/target/test/unit/default-configuration/target/site/xref UTF-8 - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} diff --git a/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml index 642f124e..031f7716 100644 --- a/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml @@ -44,7 +44,7 @@ under the License. true ${basedir}/target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} diff --git a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml index d3889696..371ca5ef 100644 --- a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml @@ -44,7 +44,7 @@ under the License. SUPPRESSME false - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} diff --git a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml index efd85233..2e93506c 100644 --- a/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml +++ b/src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml @@ -43,7 +43,7 @@ under the License. UTF-8 SUPPRESSME - ${basedir}/src/test/resources/unit/default-configuration/ + ${basedir} diff --git a/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml b/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml index 2aeff05f..c0d4d4d2 100644 --- a/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml +++ b/src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml @@ -34,11 +34,11 @@ under the License. maven-pmd-plugin - ${basedir}/target/test/unit/empty-report/target/site - ${basedir}/target/test/unit/empty-report/target - ${basedir}/target/test/unit/empty-report/target/pmd/rulesets + ${basedir}/target/test/unit/skip-empty-report/target/site + ${basedir}/target/test/unit/skip-empty-report/target + ${basedir}/target/test/unit/skip-empty-report/target/pmd/rulesets - ${basedir}/src/test/resources/unit/empty-report/java/ + ${basedir}/java UTF-8 true diff --git a/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml b/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml index bec142fe..23e1e7e7 100644 --- a/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml +++ b/src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml @@ -40,14 +40,14 @@ under the License. xml UTF-8 - ${basedir}/src/test/resources/unit/exclude-roots/src - ${basedir}/src/test/resources/unit/exclude-roots/baseroot/src1 - ${basedir}/src/test/resources/unit/exclude-roots/baseroot/src2 - ${basedir}/src/test/resources/unit/exclude-roots/othersrc + ${basedir}/src + ${basedir}/baseroot/src1 + ${basedir}/baseroot/src2 + ${basedir}/othersrc - ${basedir}/src/test/resources/unit/exclude-roots/baseroot - ${basedir}/src/test/resources/unit/exclude-roots/othersrc + ${basedir}/baseroot + ${basedir}/othersrc diff --git a/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml b/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml index e6ccad46..4b47ed43 100644 --- a/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml +++ b/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml @@ -41,6 +41,9 @@ under the License. false ${basedir}/target/test/unit/invalid-format/target/site/xref ISO-8859-1 + + ${basedir} + diff --git a/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml b/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml index 40f087f2..2b53aa56 100644 --- a/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml +++ b/src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml @@ -42,7 +42,7 @@ under the License. true false - ${basedir}/src/test/resources/unit/processing-error/src + ${basedir}/src diff --git a/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml b/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml index f08cee46..0c3e7d78 100644 --- a/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml +++ b/src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml @@ -41,7 +41,7 @@ under the License. UTF-8 false - ${basedir}/src/test/resources/unit/processing-error/src + ${basedir}/src diff --git a/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml b/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml index 55c8a4d9..5a91ffc2 100644 --- a/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml +++ b/src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml @@ -41,7 +41,7 @@ under the License. UTF-8 true - ${basedir}/src/test/resources/unit/processing-error/src + ${basedir}/src From 267488345194b746e4866e84e294dd49997a2178 Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Wed, 12 Nov 2025 16:15:18 +0100 Subject: [PATCH 8/8] fix: compiler Signed-off-by: Sandra Parsick --- src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index 9cebfbd7..0f5fab6c 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -204,7 +204,7 @@ public void testCustomConfiguration(AbstractPmdReport mojo) throws Exception { @InjectMojo(goal = "cpd", pom = "cpd-invalid-format-plugin-config.xml") @MojoParameter(name = "siteDirectory", value = "src/site") @Test - public void testInvalidFormat(AbstractPmdReport mojo) { + public void testInvalidFormat(AbstractPmdReport mojo) throws MojoExecutionException { try { mojo.execute();