This is an automated email from the ASF dual-hosted git repository. adangel pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git
commit 0bc759bd1e2cc210ed8c0da2fc1dfd7734c02697 Author: Andreas Dangel <adan...@apache.org> AuthorDate: Thu Mar 25 12:53:47 2021 +0100 [MPMD-312] Remove deprecated usage of RuleSetReferenceId --- .../org/apache/maven/plugins/pmd/PmdReport.java | 37 ++++++++++-- .../apache/maven/plugins/pmd/PmdReportTest.java | 54 +++++++++++++++++ .../pmd-report-resolve-rulesets.xml | 67 ++++++++++++++++++++++ 3 files changed, 154 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java index ba06cf8..52585a2 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java +++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java @@ -54,7 +54,6 @@ import org.codehaus.plexus.resource.loader.ResourceNotFoundException; import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.StringUtils; -import net.sourceforge.pmd.RuleSetReferenceId; import net.sourceforge.pmd.renderers.Renderer; /** @@ -118,7 +117,7 @@ public class PmdReport * (<code>/rulesets/java/maven-pmd-plugin-default.xml</code>). */ @Parameter - private String[] rulesets = new String[] { "/rulesets/java/maven-pmd-plugin-default.xml" }; + String[] rulesets = new String[] { "/rulesets/java/maven-pmd-plugin-default.xml" }; /** * Controls whether the project's compile/test classpath should be passed to PMD to enable its type resolution @@ -438,8 +437,8 @@ public class PmdReport { String set = rulesets[idx]; getLog().debug( "Preparing ruleset: " + set ); - RuleSetReferenceId id = new RuleSetReferenceId( set ); - File ruleset = locator.getResourceAsFile( id.getRuleSetFileName(), getLocationTemp( set ) ); + String rulesetFilename = determineRulesetFilename( set ); + File ruleset = locator.getResourceAsFile( rulesetFilename, getLocationTemp( set ) ); if ( null == ruleset ) { throw new MavenReportException( "Could not resolve " + set ); @@ -454,6 +453,36 @@ public class PmdReport return StringUtils.join( sets, "," ); } + private String determineRulesetFilename( String ruleset ) + { + String result = ruleset.trim(); + String lowercase = result.toLowerCase( Locale.ROOT ); + if ( lowercase.endsWith( ".xml" ) ) + { + return result; + } + + // assume last part is a single rule, e.g. myruleset.xml/SingleRule + if ( result.indexOf( '/' ) > -1 ) + { + String rulesetFilename = result.substring( 0, result.lastIndexOf( '/' ) ); + if ( rulesetFilename.toLowerCase( Locale.ROOT ).endsWith( ".xml" ) ) + { + return rulesetFilename; + } + } + // maybe a built-in ruleset name, e.g. java-design -> rulesets/java/design.xml + int dashIndex = lowercase.indexOf( '-' ); + if ( dashIndex > -1 && lowercase.indexOf( '-', dashIndex + 1 ) == -1 ) + { + String language = result.substring( 0, dashIndex ); + String rulesetName = result.substring( dashIndex + 1 ); + return "rulesets/" + language + "/" + rulesetName + ".xml"; + } + // fallback - no change of the given ruleset specifier + return result; + } + private void generateMavenSiteReport( Locale locale ) throws MavenReportException { 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 8a4fc0b..7e175a9 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -690,4 +690,58 @@ public class PmdReportTest assertEquals( 4, StringUtils.countMatches( str, "\">UnusedPrivateField</a></td>" ) ); } + public void testPmdReportResolveRulesets() + throws Exception + { + int port = determineFreePort(); + WireMockServer mockServer = new WireMockServer( port ); + mockServer.start(); + + String sonarRuleset = + IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-way-ruleset.xml" ), + StandardCharsets.UTF_8 ); + + final String sonarProfileUrl = "/profiles/export?format=pmd&language=java&name=Sonar%2520way"; + final String sonarExportRulesetUrl = "http://localhost:" + mockServer.port() + sonarProfileUrl; + final String myRulesetBaseUrl = "/config/my-ruleset.xml"; + final String myRulesetUrl = "http://localhost:" + mockServer.port() + myRulesetBaseUrl; + + mockServer.stubFor( WireMock.get( WireMock.urlEqualTo( sonarProfileUrl ) ) + .willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type", + "text/xml" ).withBody( sonarRuleset ) ) ); + mockServer.stubFor( WireMock.get( WireMock.urlEqualTo( myRulesetBaseUrl ) ) + .willReturn( WireMock.aResponse().withStatus( 200 ).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" ); + PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); + mojo.rulesets[3] = sonarExportRulesetUrl; + mojo.rulesets[4] = myRulesetUrl; + mojo.execute(); + + // 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/custom-rules.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/java-design.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/my-ruleset.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + mockServer.stop(); + } + } 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 new file mode 100644 index 0000000..5d4ea4b --- /dev/null +++ b/src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml @@ -0,0 +1,67 @@ +<!-- +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. +--> + +<project> + <modelVersion>4.0.0</modelVersion> + <groupId>def.configuration</groupId> + <artifactId>default-configuration</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <inceptionYear>2006</inceptionYear> + <name>Maven PMD Plugin Default Configuration Test</name> + <url>http://maven.apache.org</url> + <build> + <finalName>default-configuration</finalName> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <configuration> + <project implementation="org.apache.maven.plugins.pmd.stubs.DefaultConfigurationMavenProjectStub"/> + <outputDirectory>${basedir}/target/test/unit/default-configuration/target/site</outputDirectory> + <targetDirectory>${basedir}/target/test/unit/default-configuration/target</targetDirectory> + <rulesetsTargetDirectory>${basedir}/target/test/unit/default-configuration/target/pmd/rulesets</rulesetsTargetDirectory> + <rulesets> + <ruleset>${basedir}/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml</ruleset> + <ruleset>category/java/bestpractices.xml</ruleset> + <ruleset>java-design</ruleset> + <!-- note, the port of the http urls will be replaced by the unit test --> + <ruleset>http://localhost:12345/profiles/export?format=pmd&language=java&name=Sonar%2520way</ruleset> + <ruleset>http://localhost:12345/config/my-ruleset.xml</ruleset> + </rulesets> + <format>xml</format> + <linkXRef>true</linkXRef> + <xrefLocation>${basedir}/target/test/unit/default-configuration/target/site/xref</xrefLocation> + <sourceEncoding>UTF-8</sourceEncoding> + <compileSourceRoots> + <compileSourceRoot>${basedir}/src/test/resources/unit/default-configuration/</compileSourceRoot> + </compileSourceRoots> + </configuration> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + </plugin> + </plugins> + </reporting> +</project>