Skip to content

Commit

Permalink
[MPMD-307] - NPE when using custom rule
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Oct 22, 2020
1 parent aff4499 commit 563654b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/java/org/apache/maven/plugins/pmd/PmdReportGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,28 @@ private void endFileSection( int level )
sink.section_( level );
}

private void addRuleName( Violation ruleViolation )
{
boolean hasUrl = StringUtils.isNotBlank( ruleViolation.getExternalInfoUrl() );

if ( hasUrl )
{
sink.link( ruleViolation.getExternalInfoUrl() );
}

sink.text( ruleViolation.getRule() );

if ( hasUrl )
{
sink.link_();
}
}

private void processSingleRuleViolation( Violation ruleViolation, PmdFileInfo fileInfo )
{
sink.tableRow();
sink.tableCell();
sink.link( ruleViolation.getExternalInfoUrl() );
sink.text( ruleViolation.getRule() );
sink.link_();
addRuleName( ruleViolation );
sink.tableCell_();
sink.tableCell();
sink.text( ruleViolation.getText() );
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -664,4 +664,30 @@ public void testCodeClimateRenderer() throws MavenReportException
final Renderer renderer = PmdExecutor.createRenderer( "net.sourceforge.pmd.renderers.CodeClimateRenderer", "UTF-8" );
assertNotNull(renderer);
}

public void testPmdReportCustomRulesNoExternalInfoUrl()
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/pmd-report-custom-rules.xml" );
PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
mojo.execute();

File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
renderer( mojo, generatedFile );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );

String str = readFile( generatedFile );

// custom rule without link
assertEquals( 2, StringUtils.countMatches( str, "<td>CustomRule</td>" ) );
// standard rule with link
assertEquals( 4, StringUtils.countMatches( str, "\">UnusedPrivateField</a></td>" ) );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!--
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>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<ruleset name="Custom PMD Rule Ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

<description>
A ruleset with a custom rule without "externalInfoUrl".
</description>

<rule name="CustomRule"
language="java"
message="custom rule test"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>custom xpath rule test</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@SimpleName = 'App']
]]>
</value>
</property>
</properties>
</rule>

<rule ref="category/java/bestpractices.xml/UnusedPrivateField" />
</ruleset>

0 comments on commit 563654b

Please sign in to comment.