Skip to content

Commit

Permalink
Fix tests for PMD 7 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Feb 17, 2024
1 parent e6d26e9 commit e8e1bc6
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 37 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Expand Up @@ -83,7 +83,8 @@ under the License.
<properties>
<mavenVersion>3.2.5</mavenVersion>
<javaVersion>8</javaVersion>
<pmdVersion>6.55.0</pmdVersion>
<pmdVersion>7.0.0-SNAPSHOT</pmdVersion>
<!-- <pmdVersion>6.55.0</pmdVersion>-->
<slf4jVersion>1.7.36</slf4jVersion>
<aetherVersion>1.0.0.v20140518</aetherVersion>
<doxiaVersion>1.12.0</doxiaVersion>
Expand Down Expand Up @@ -161,6 +162,11 @@ under the License.
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-compat6</artifactId>
<version>${pmdVersion}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/it/MPMD-219-pmd-processing-error/verify.groovy
Expand Up @@ -20,5 +20,5 @@
File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
assert buildLog.text.contains( "PMD processing errors" )
assert buildLog.text.contains( "Error while parsing" )
assert buildLog.text.contains( "ParseException: Parse exception in file" )
assert buildLog.text.contains( "BrokenFile.java" )
2 changes: 1 addition & 1 deletion src/it/MPMD-243-excludeFromFailureFile/config/ruleset.xml
Expand Up @@ -26,5 +26,5 @@ under the License.
MPMD-243
</description>

<rule ref="rulesets/java/imports.xml/TooManyStaticImports"/>
<rule ref="category/java/codestyle.xml/TooManyStaticImports"/>
</ruleset>
14 changes: 10 additions & 4 deletions src/it/MPMD-244-logging/verify.groovy
Expand Up @@ -20,12 +20,18 @@
File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
assert buildLog.text.contains( "PMD processing errors" )
assert buildLog.text.contains( "Error while parsing" )
assert buildLog.text.contains( "ParseException: Parse exception" )
assert buildLog.text.contains( "at line 24, column 5: Encountered" )

String disabledPath = new File( basedir, 'logging-disabled/src/main/java/BrokenFile.java' ).getCanonicalPath()
String enabledPath = new File( basedir, 'logging-enabled/src/main/java/BrokenFile.java' ).getCanonicalPath()

// logging disabled: the pmd exception is only output through the processing error reporting (since MPMD-246)
assert 1 == buildLog.text.count( "net.sourceforge.pmd.PMDException: Error while parsing ${disabledPath}" )
// logging enabled: the pmd exception is output twice: through the processing error reporting (since MPMD-246) and through PMD's own logging
assert 2 == buildLog.text.count( "net.sourceforge.pmd.PMDException: Error while parsing ${enabledPath}" )
assert 1 == buildLog.text.count( "${disabledPath}: ParseException: Parse exception in" )
// logging enabled: the pmd exception is still output only once: through the processing error reporting (since MPMD-246) - PMD 7 doesn't log the processing error additionally
assert 1 == buildLog.text.count( "${enabledPath}: ParseException: Parse exception in" )

// build.log contains the logging from the two PMD executions
// only one execution has logging enabled, so we expect only one log output
// TODO assert 1 == buildLog.text.count( "[DEBUG] Rules loaded from" )
// TODO logging is always enabled and can't be disabled, because PMD 7 switched to slf4j
2 changes: 1 addition & 1 deletion src/it/MPMD-258-multiple-executions/invoker.properties
Expand Up @@ -15,5 +15,5 @@
# specific language governing permissions and limitations
# under the License.

invoker.goals = clean compile pmd:pmd
invoker.goals = -X clean compile pmd:pmd
invoker.maven.version = 3.1.0+
2 changes: 2 additions & 0 deletions src/it/MPMD-258-multiple-executions/verify.groovy
Expand Up @@ -21,7 +21,9 @@ File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

// we have 2 modules and for each module this should be output once
// note: this is only logged in debug mode by net.sourceforge.pmd.cache.FileAnalysisCache
assert 2 == buildLog.text.count( "Analysis cache created" )

// since we are running clean pmd:pmd, the cache is always created, never updated
// note: this is only logged in debug mode by net.sourceforge.pmd.cache.FileAnalysisCache
assert 0 == buildLog.text.count( "Analysis cache updated" )
2 changes: 1 addition & 1 deletion src/it/MPMD-268-deprecated-rules/verify.groovy
Expand Up @@ -19,4 +19,4 @@

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()
assert buildLog.text.contains( "[WARNING] Use Rule name category/java/codestyle.xml/ControlStatementBraces instead of the deprecated" )
assert buildLog.text.contains( "Use Rule name category/java/codestyle.xml/ControlStatementBraces instead of the deprecated Rule name" )
4 changes: 2 additions & 2 deletions src/it/MPMD-304-toolchain-support/verify.groovy
Expand Up @@ -29,8 +29,8 @@ assert buildLog.text.contains( '[INFO] You have 1 CPD duplication' )

File pmdReport = new File( basedir, 'target/pmd.xml' )
assert pmdReport.exists()
assert pmdReport.text.contains( '<violation beginline="24" endline="24" begincolumn="29" endcolumn="34" rule="ExtendsObject"' )
assert pmdReport.text.contains( '<violation beginline="36" endline="36" begincolumn="9" endcolumn="31" rule="DontCallThreadRun"' )
assert pmdReport.text.contains( '<violation beginline="24" endline="24" begincolumn="29" endcolumn="35" rule="ExtendsObject"' )
assert pmdReport.text.contains( '<violation beginline="36" endline="36" begincolumn="9" endcolumn="32" rule="DontCallThreadRun"' )

File pmdSite = new File( basedir, 'target/site/pmd.html' )
assert pmdSite.exists()
Expand Down
2 changes: 1 addition & 1 deletion src/it/MPMD-89-232-typeresolution/src/main/pmd/ruleset.xml
Expand Up @@ -24,5 +24,5 @@ under the License.
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>Custom Ruleset for test case MPMD-89 and MPMP-232</description>
<rule ref="rulesets/java/junit.xml/TestClassWithoutTestCases"/>
<rule ref="category/java/errorprone.xml/TestClassWithoutTestCases"/>
</ruleset>
4 changes: 2 additions & 2 deletions src/it/mpmd-138/verify.groovy
Expand Up @@ -23,7 +23,7 @@ assert buildLog.exists()

// Module 1
assert 1 == buildLog.getText().count('[INFO] PMD Failure: test.MyClass:27 Rule:UnnecessarySemicolon Priority:3 Unnecessary semicolon')
assert 1 == buildLog.getText().count('[INFO] PMD Failure: test.MyClass:28 Rule:UnnecessaryReturn Priority:3 Avoid unnecessary return statements')
assert 1 == buildLog.getText().count('[INFO] PMD Failure: test.MyClass:28 Rule:UnnecessaryReturn Priority:3 Unnecessary return statement')
assert 1 == buildLog.getText().count('[INFO] You have 2 PMD violations. For more details see:')

// Module 2
Expand All @@ -34,4 +34,4 @@ assert 1 == buildLog.getText().count('[INFO] You have 1 PMD violation. For more
assert 1 == buildLog.getText().count('[INFO] You have 1 CPD duplication. For more details see:')

// Module 4
assert 1 == buildLog.getText().count('[INFO] You have 2 CPD duplications. For more details see:')
assert 1 == buildLog.getText().count('[INFO] You have 2 CPD duplications. For more details see:')
38 changes: 21 additions & 17 deletions src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java
Expand Up @@ -216,9 +216,9 @@ public void testFileURL() throws Exception {
.withHeader("Content-Type", "text/xml")
.withBody(sonarRuleset)));

URL url = getClass().getClassLoader().getResource("rulesets/java/basic.xml");
URL url2 = getClass().getClassLoader().getResource("rulesets/java/unusedcode.xml");
URL url3 = getClass().getClassLoader().getResource("rulesets/java/imports.xml");
URL url = getClass().getClassLoader().getResource("category/java/bestpractices.xml");
URL url2 = getClass().getClassLoader().getResource("category/java/codestyle.xml");
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);
Expand All @@ -229,16 +229,16 @@ public void testFileURL() throws Exception {
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

// the resolved and extracted rulesets
generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-basic.xml");
generatedFile = new File(
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/001-bestpractices.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/002-unusedcode.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/002-codestyle.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-imports.xml");
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-errorprone.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(
Expand Down Expand Up @@ -424,7 +424,8 @@ public void testSuppressMarkerConfiguration() throws Exception {
// check that there is no violation reported for "unusedVar2" - as it is suppressed
assertFalse(str.contains("Avoid unused private fields such as 'unusedVar2'.\n </violation>"));
// but it appears as suppressed
assertTrue(str.contains("suppressiontype=\"nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\""));
assertTrue(
str.contains("suppressiontype=\"//nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\""));

// check if there's a link to the JXR files
str = readFile(generatedReport);
Expand All @@ -448,7 +449,8 @@ public void testSuppressMarkerConfigurationWithoutRendering() throws Exception {
// check that there is no violation reported for "unusedVar2" - as it is suppressed
assertFalse(str.contains("Avoid unused private fields such as 'unusedVar2'.\n </violation>"));
// but it appears as suppressed
assertTrue(str.contains("suppressiontype=\"nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\""));
assertTrue(
str.contains("suppressiontype=\"//nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\""));

// check if there's a link to the JXR files
str = readFile(generatedReport);
Expand Down Expand Up @@ -512,13 +514,14 @@ public void testPMDProcessingErrorWithDetailsSkipped() throws Exception {
File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
String str = readFile(generatedFile);
assertTrue(str.contains("Error while parsing"));
assertTrue(str.contains("ParseException:"));
// The parse exception must be in the XML report
assertTrue(str.contains("ParseException: Encountered \"\" at line 23, column 5."));
assertTrue(str.contains("at line 23, column 5: Encountered"));

str = readFile(generatedReport);
// The parse exception must also be in the HTML report
assertTrue(str.contains("ParseException: Encountered \"\" at line 23, column 5."));
assertTrue(str.contains("ParseException:"));
assertTrue(str.contains("at line 23, column 5: Encountered"));
}

public void testPMDProcessingErrorWithDetailsNoReport() throws Exception {
Expand All @@ -532,13 +535,14 @@ public void testPMDProcessingErrorWithDetailsNoReport() throws Exception {
File generatedFile = new File(getBasedir(), "target/test/unit/parse-error/target/pmd.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
String str = readFile(generatedFile);
assertTrue(str.contains("Error while parsing"));
// The parse exception must be in the XML report
assertTrue(str.contains("ParseException: Encountered \"\" at line 23, column 5."));
assertTrue(str.contains("ParseException:"));
assertTrue(str.contains("at line 23, column 5: Encountered"));

str = readFile(generatedReport);
// The parse exception must NOT be in the HTML report, since reportProcessingErrors is false
assertFalse(str.contains("ParseException: Encountered \"\" at line 23, column 5."));
assertFalse(str.contains("ParseException:"));
assertFalse(str.contains("at line 23, column 5: Encountered"));
}

public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception {
Expand Down Expand Up @@ -650,8 +654,8 @@ public void testPmdReportResolveRulesets() throws Exception {
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/002-bestpractices.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(
getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-java-design.xml");
generatedFile =
new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/003-design.xml");
assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));

generatedFile = new File(
Expand Down
Expand Up @@ -77,7 +77,7 @@ public String dup( String str )

for( int i = 0; i < str.length(); i++ )
{
if ( i != ( str.length() -1 ) )
if ( i != ( ( str.length() -1 ) ) )
{
tmp = tmp + str.substring( i, i + 1);
}
Expand All @@ -96,4 +96,4 @@ public String dup( String str )
return tmp;
}

}
}
Expand Up @@ -55,7 +55,7 @@ public String dup( String str )

for( int i = 0; i < str.length(); i++ )
{
if ( i != ( str.length() -1 ) )
if ( i != ( ( str.length() -1 ) ) )
{
tmp = tmp + str.substring( i, i + 1);
}
Expand All @@ -74,4 +74,4 @@ public String dup( String str )
return tmp;
}

}
}
Expand Up @@ -41,7 +41,7 @@ under the License.
<rulesets>
<ruleset>${basedir}/src/test/resources/unit/default-configuration/rulesets/custom-rules.xml</ruleset>
<ruleset>category/java/bestpractices.xml</ruleset>
<ruleset>java-design</ruleset>
<ruleset>category/java/design.xml</ruleset>
<!-- note, the port of the http urls will be replaced by the unit test -->
<ruleset>http://localhost:12345/profiles/export?format=pmd&amp;language=java&amp;name=Sonar%2520way</ruleset>
<ruleset>http://localhost:12345/config/my-ruleset.xml</ruleset>
Expand Down
Expand Up @@ -36,7 +36,7 @@ under the License.
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@SimpleName = 'App']
//ClassDeclaration[@SimpleName = 'App']
]]>
</value>
</property>
Expand Down

0 comments on commit e8e1bc6

Please sign in to comment.