[java] Add simple rule test for latest java version

This fixes some parsing errors in java files under
src/test/resources
This commit is contained in:
Andreas Dangel
2021-07-30 10:32:01 +02:00
parent 4a81ec4357
commit d641b3a506
9 changed files with 57 additions and 19 deletions

View File

@ -23,6 +23,9 @@
<exclude-pattern>.*/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java10/LocalVariableTypeInference_varAsEnumName.java</exclude-pattern>
<exclude-pattern>.*/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java10/LocalVariableTypeInference_varAsTypeIdentifier.java</exclude-pattern>
<!-- this file contains are parse error explicitly -->
<exclude-pattern>.*/net/sourceforge/pmd/lang/java/ast/InfiniteLoopInLookahead.java</exclude-pattern>
<rule ref="category/java/bestpractices.xml" />
<rule ref="category/java/codestyle.xml" />
<rule ref="category/java/design.xml" />

View File

@ -11,35 +11,51 @@ import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardErrorStreamLog;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TemporaryFolder;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
public class PMDCoverageTest {
@Rule
public StandardOutputStreamLog output = new StandardOutputStreamLog();
public SystemOutRule output = new SystemOutRule().muteForSuccessfulTests().enableLog();
@Rule
public StandardErrorStreamLog errorStream = new StandardErrorStreamLog();
public SystemErrRule errorStream = new SystemErrRule().muteForSuccessfulTests().enableLog();
@Rule
public TemporaryFolder folder = new TemporaryFolder();
/**
* Test some of the PMD command line options
*/
@Test
public void testPmdOptions() {
runPmd("-d src/main/java/net/sourceforge/pmd/lang/java/rule/design -f text -R rulesets/internal/all-java.xml -language java -stress -benchmark");
runPmd("-d src/main/java/net/sourceforge/pmd/lang/java/rule/design -f text -R rulesets/internal/all-java.xml -stress -benchmark");
}
@Test
public void runAllJavaPmdOnSourceTree() {
runPmd("-d src/main/java -f text -R rulesets/internal/all-java.xml");
}
@Test
public void runAllJavaPmdOnTestResourcesWithLatestJavaVersion() {
List<LanguageVersion> versions = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersions();
LanguageVersion latest = versions.get(versions.size() - 1);
runPmd("-d src/test/resources -f text -R rulesets/internal/all-java.xml -language java -version " + latest.getVersion());
}
/**
@ -49,8 +65,10 @@ public class PMDCoverageTest {
*/
private void runPmd(String commandLine) {
String[] args = commandLine.split("\\s");
String report = "missing report";
try {
File f = folder.newFile();
args = ArrayUtils.addAll(
args,
@ -60,27 +78,26 @@ public class PMDCoverageTest {
String.valueOf(Runtime.getRuntime().availableProcessors())
);
System.err.println("Running PMD with: " + Arrays.toString(args));
PMD.runPmd(args);
report = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
assertEquals("Nothing should be output to stdout", 0, output.getLog().length());
assertEquals("No exceptions expected", 0, StringUtils.countMatches(errorStream.getLog(), "Exception applying rule"));
assertFalse("Wrong configuration? Ruleset not found", errorStream.getLog().contains("Ruleset not found"));
assertEquals("No usage of deprecated XPath attributes expected", 0, StringUtils.countMatches(errorStream.getLog(), "Use of deprecated attribute"));
String report = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
assertEquals("No processing errors expected", 0, StringUtils.countMatches(report, "Error while processing"));
// we might have explicit examples of parsing errors, so these are maybe false positives
assertEquals("No parsing error expected", 0, StringUtils.countMatches(report, "Error while parsing"));
} catch (IOException ioe) {
fail("Problem creating temporary file: " + ioe.getLocalizedMessage());
} catch (AssertionError ae) {
System.out.println("\nReport:\n");
System.out.println(report);
throw ae;
}
}
@Test
public void runAllJavaPmdOnSourceTree() {
runPmd("-d src/main/java -f text -R rulesets/internal/all-java.xml -language java");
}
}

View File

@ -2,7 +2,7 @@ public class Foo {
public void bar() {
System.out.println("hello");
System.out.println("hello");
int i = 5
int i = 5;
System.out.print("hello");
}

View File

@ -10,4 +10,4 @@ package foo.bar.baz;
public class Foo {}
@SuppressWarnings({"ugh","CPD-END"})
class Other {}

View File

@ -10,4 +10,9 @@ L12
["CPD-END"] 26 34
[}] 35 35
[)] 36 36
L13
[class] 1 5
[Other] 7 11
[{] 13 13
[}] 14 14
EOF

View File

@ -1,2 +1,7 @@
[Image] or [Truncated image[ Bcol Ecol
L13
[class] 1 5
[Other] 7 11
[{] 13 13
[}] 14 14
EOF

View File

@ -1 +1,3 @@
class tabWidth {
int i = 0;
}

View File

@ -1,7 +1,13 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[class] 1 5
[tabWidth] 7 14
[{] 16 16
L2
[int] 2 4
[i] 6 6
[=] 8 8
[0] 10 10
L3
[}] 1 1
EOF

View File

@ -377,7 +377,7 @@ public class CovariantReturnType extends AbstractClass {
</test-code>
<test-code>
<description>Avoid false positives when in ambiguous situation</description>
<description>Avoid false positives when in ambiguous situation - AmbiguousOverload</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.missingoverride;
@ -403,7 +403,7 @@ public class AmbiguousOverload implements Comparator<StringBuilder> {
</test-code>
<test-code>
<description>Avoid false positives when in ambiguous situation</description>
<description>Avoid false positives when in ambiguous situation - HierarchyWithSeveralBridges</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.missingoverride;