From 563654b474e031d7639429128b4d30eab5da3c02 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 22 Oct 2020 11:08:59 +0200 Subject: [PATCH] [MPMD-307] - NPE when using custom rule --- .../maven/plugins/pmd/PmdReportGenerator.java | 21 ++++++- .../maven/plugins/pmd/PmdReportTest.java | 26 ++++++++ .../pmd-report-custom-rules.xml | 62 +++++++++++++++++++ .../rulesets/custom-rules.xml | 47 ++++++++++++++ 4 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml create mode 100644 src/test/resources/unit/default-configuration/rulesets/custom-rules.xml diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReportGenerator.java b/src/main/java/org/apache/maven/plugins/pmd/PmdReportGenerator.java index f1dae2a1..a0943631 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/PmdReportGenerator.java +++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReportGenerator.java @@ -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() ); 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 4718d87f..8a4fc0bc 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -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, "CustomRule" ) ); + // standard rule with link + assertEquals( 4, StringUtils.countMatches( str, "\">UnusedPrivateField" ) ); + } + } 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 new file mode 100644 index 00000000..04c2c55f --- /dev/null +++ b/src/test/resources/unit/default-configuration/pmd-report-custom-rules.xml @@ -0,0 +1,62 @@ + + + + 4.0.0 + def.configuration + default-configuration + jar + 1.0-SNAPSHOT + 2006 + Maven PMD Plugin Default Configuration Test + http://maven.apache.org + + default-configuration + + + org.apache.maven.plugins + maven-pmd-plugin + + + ${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}/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml + + xml + true + ${basedir}/target/test/unit/default-configuration/target/site/xref + UTF-8 + + ${basedir}/src/test/resources/unit/default-configuration/ + + + + + + + + + org.apache.maven.plugins + maven-jxr-plugin + + + + diff --git a/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml b/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml new file mode 100644 index 00000000..4946c530 --- /dev/null +++ b/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml @@ -0,0 +1,47 @@ + + + + + + A ruleset with a custom rule without "externalInfoUrl". + + + + custom xpath rule test + 1 + + + + + + + + + + +