+
pmd-unit-tests
JUnit4SuitesShouldUseSuiteAnnotation
@@ -3832,7 +3833,7 @@
min
-
+
pmd-unit-tests
JUnitSpelling
@@ -3910,7 +3911,7 @@
min
-
+
diff --git a/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd-kotlin.properties b/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd-kotlin.properties
new file mode 100644
index 00000000..33f8b94c
--- /dev/null
+++ b/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd-kotlin.properties
@@ -0,0 +1 @@
+rule.pmd-kotlin.FunctionNameTooShort.name=Function name too short
diff --git a/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd/rules/pmd-kotlin/FunctionNameTooShort.html b/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd/rules/pmd-kotlin/FunctionNameTooShort.html
new file mode 100644
index 00000000..92a9cafb
--- /dev/null
+++ b/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd/rules/pmd-kotlin/FunctionNameTooShort.html
@@ -0,0 +1,10 @@
+
+Function names should be easy to understand and describe the intention. Makes developers happy.
+Example:
+
+ fun b() = 1
+
+
+
+This is a simple rule to test Kotlin in PMD.
+
diff --git a/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules-kotlin.xml b/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules-kotlin.xml
new file mode 100644
index 00000000..d01d33ff
--- /dev/null
+++ b/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules-kotlin.xml
@@ -0,0 +1,8 @@
+
+
+
+ MAJOR
+ category/kotlin/bestpractices.xml/FunctionNameTooShort
+
+
+
diff --git a/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules-unit-tests.xml b/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules-unit-tests.xml
index 620eba3c..27ad0fae 100644
--- a/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules-unit-tests.xml
+++ b/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules-unit-tests.xml
@@ -24,7 +24,8 @@
MINOR
-
+
MAJOR
diff --git a/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml b/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml
index 4c0451c3..61d61839 100644
--- a/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml
+++ b/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml
@@ -6,10 +6,10 @@
DEPRECATED
-
- MAJOR
- category/java/bestpractices.xml/GuardLogStatement
-
+
+
+
+
MAJOR
@@ -66,11 +66,11 @@
category/java/errorprone.xml/CallSuperLast
-
- MAJOR
- category/java/errorprone.xml/EmptyInitializer
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -108,20 +108,21 @@
DEPRECATED
-
- MINOR
- category/java/errorprone.xml/ReturnEmptyArrayRatherThanNull
- DEPRECATED
-
+
+
+
+
+
-
- MINOR
- category/java/performance.xml/TooFewBranchesForASwitchStatement
-
- 3
-
- DEPRECATED
-
+
+
+
+
+
+
+
+
+
MAJOR
@@ -151,9 +152,9 @@
MAJOR
size
category/java/design.xml/NPathComplexity
-
- 200
-
+
+
+
@@ -168,17 +169,19 @@
DEPRECATED
-
- MAJOR
- category/java/bestpractices.xml/SwitchStmtsShouldHaveDefault
- DEPRECATED
-
+
+
+
+
+
+
-
- MAJOR
- category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt
- DEPRECATED
-
+
+
+
+
+
+
MAJOR
@@ -186,10 +189,10 @@
DEPRECATED
-
- MAJOR
- category/java/codestyle.xml/AvoidFinalLocalVariable
-
+
+
+
+
MINOR
@@ -204,26 +207,26 @@
DEPRECATED
-
- INFO
- category/java/bestpractices.xml/UnusedImports
- DEPRECATED
-
+
+
+
+
+
MINOR
category/java/codestyle.xml/LocalVariableCouldBeFinal
-
- MAJOR
- naming
- category/java/codestyle.xml/AbstractNaming
-
- true
-
- DEPRECATED
-
+
+
+
+
+
+
+
+
+
MAJOR
@@ -237,20 +240,20 @@
DEPRECATED
-
- MAJOR
- category/java/performance.xml/ByteInstantiation
-
+
+
+
+
-
- MAJOR
- category/java/performance.xml/ShortInstantiation
-
+
+
+
+
-
- MAJOR
- category/java/performance.xml/LongInstantiation
-
+
+
+
+
CRITICAL
@@ -339,10 +342,10 @@
DEPRECATED
-
- INFO
- category/java/codestyle.xml/UnnecessaryModifier
-
+
+
+
+
MAJOR
@@ -377,21 +380,21 @@
category/java/design.xml/SingularField
-
- MINOR
- category/java/codestyle.xml/DefaultPackage
-
+
+
+
+
-
- MAJOR
- category/java/errorprone.xml/DataflowAnomalyAnalysis
-
- 100
-
-
- 1000
-
-
+
+
+
+
+
+
+
+
+
+
MAJOR
@@ -411,11 +414,11 @@
DEPRECATED
-
- MAJOR
- category/java/bestpractices.xml/LooseCoupling
- DEPRECATED
-
+
+
+
+
+
MINOR
@@ -434,10 +437,10 @@
DEPRECATED
-
- MINOR
- category/java/performance.xml/SimplifyStartsWith
-
+
+
+
+
MAJOR
@@ -454,11 +457,11 @@
category/java/performance.xml/AvoidArrayLoops
-
- MAJOR
- category/java/performance.xml/UnnecessaryWrapperObjectCreation
- DEPRECATED
-
+
+
+
+
+
CRITICAL
@@ -475,36 +478,36 @@
DEPRECATED
-
- CRITICAL
- category/java/errorprone.xml/EmptyIfStmt
- DEPRECATED
-
+
+
+
+
+
-
- CRITICAL
- category/java/errorprone.xml/EmptyWhileStmt
- DEPRECATED
-
+
+
+
+
+
-
- MAJOR
- category/java/errorprone.xml/EmptyTryBlock
- DEPRECATED
-
+
+
+
+
+
-
- CRITICAL
- error-handling
- category/java/errorprone.xml/EmptyFinallyBlock
- DEPRECATED
-
+
+
+
+
+
+
-
- MAJOR
- category/java/errorprone.xml/EmptySwitchStatements
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -530,22 +533,22 @@
DEPRECATED
-
- CRITICAL
- category/java/errorprone.xml/EmptySynchronizedBlock
- DEPRECATED
-
+
+
+
+
+
MINOR
category/java/codestyle.xml/UnnecessaryReturn
-
- MAJOR
- category/java/errorprone.xml/EmptyInitializer
- DEPRECATED
-
+
+
+
+
+
CRITICAL
@@ -553,21 +556,21 @@
DEPRECATED
-
- MAJOR
- category/java/errorprone.xml/EmptyStatementNotInLoop
- DEPRECATED
-
+
+
+
+
+
-
- MAJOR
- category/java/performance.xml/BooleanInstantiation
-
+
+
+
+
-
- INFO
- category/java/codestyle.xml/UnnecessaryModifier
-
+
+
+
+
MINOR
@@ -700,21 +703,22 @@
DEPRECATED
-
- MAJOR
- category/java/errorprone.xml/NonCaseLabelInSwitchStatement
- DEPRECATED
-
+
+
+
+
+
+
MAJOR
category/java/performance.xml/OptimizableToArrayCall
-
- MAJOR
- category/java/errorprone.xml/BadComparison
-
+
+
+
+
CRITICAL
@@ -777,11 +781,11 @@
category/java/multithreading.xml/AvoidSynchronizedAtMethodLevel
-
- CRITICAL
- category/java/errorprone.xml/MissingBreakInSwitch
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -812,11 +816,11 @@
DEPRECATED
-
- MAJOR
- category/java/bestpractices.xml/PositionLiteralsFirstInComparisons
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -860,12 +864,12 @@
DEPRECATED
-
- MAJOR
- multithreading
- category/java/multithreading.xml/UnsynchronizedStaticDateFormatter
- DEPRECATED
-
+
+
+
+
+
+
MAJOR
@@ -942,13 +946,13 @@
DEPRECATED
-
- MAJOR
- category/java/errorprone.xml/BeanMembersShouldSerialize
-
-
-
-
+
+
+
+
+
+
+
MAJOR
@@ -956,11 +960,11 @@
DEPRECATED
-
- MAJOR
- category/java/errorprone.xml/CloneMethodMustImplementCloneable
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -971,15 +975,6 @@
false
-
-
-
-
- ,
-
-
-
-
3
@@ -1053,15 +1048,15 @@
DEPRECATED
-
- MAJOR
- size
- category/java/design.xml/ExcessiveMethodLength
-
- 100
-
- DEPRECATED
-
+
+
+
+
+
+
+
+
+
MAJOR
@@ -1073,15 +1068,15 @@
DEPRECATED
-
- MAJOR
- size
- category/java/design.xml/ExcessiveClassLength
-
- 1000
-
- DEPRECATED
-
+
+
+
+
+
+
+
+
+
MAJOR
@@ -1102,59 +1097,59 @@
-
- MAJOR
- size
- category/java/design.xml/NcssMethodCount
-
- 100
-
- DEPRECATED
-
-
-
- MAJOR
- size
- category/java/design.xml/NcssTypeCount
-
- 1500
-
- DEPRECATED
-
-
-
- MAJOR
- size
- category/java/design.xml/NcssConstructorCount
-
- 100
-
- DEPRECATED
-
-
-
- MINOR
- category/java/codestyle.xml/DuplicateImports
- DEPRECATED
-
-
-
- MINOR
- category/java/codestyle.xml/DontImportJavaLang
- DEPRECATED
-
-
-
- MINOR
- category/java/errorprone.xml/ImportFromSamePackage
- DEPRECATED
-
-
-
- MAJOR
- category/java/errorprone.xml/CloneThrowsCloneNotSupportedException
- DEPRECATED
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
MAJOR
@@ -1200,24 +1195,24 @@
DEPRECATED
-
- MAJOR
- naming
- category/java/codestyle.xml/VariableNamingConventions
-
-
-
-
-
-
-
-
-
-
-
-
- DEPRECATED
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
MAJOR
@@ -1251,11 +1246,11 @@
DEPRECATED
-
- MAJOR
- category/java/codestyle.xml/SuspiciousConstantFieldName
- DEPRECATED
-
+
+
+
+
+
CRITICAL
@@ -1275,11 +1270,11 @@
DEPRECATED
-
- MAJOR
- category/java/codestyle.xml/MIsLeadingVariableName
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -1311,10 +1306,10 @@
DEPRECATED
-
- MAJOR
- category/java/performance.xml/IntegerInstantiation
-
+
+
+
+
MAJOR
@@ -1322,11 +1317,11 @@
DEPRECATED
-
- MAJOR
- category/java/errorprone.xml/LoggerIsNotStaticFinal
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -1341,36 +1336,36 @@
DEPRECATED
-
- MAJOR
- category/java/codestyle.xml/IfStmtsMustUseBraces
- DEPRECATED
-
+
+
+
+
+
-
- MAJOR
- category/java/codestyle.xml/WhileLoopsMustUseBraces
- DEPRECATED
-
+
+
+
+
+
-
- MAJOR
- category/java/codestyle.xml/IfElseStmtsMustUseBraces
- DEPRECATED
-
+
+
+
+
+
-
- MAJOR
- category/java/codestyle.xml/ForLoopsMustUseBraces
- DEPRECATED
-
+
+
+
+
+
MAJOR
category/java/bestpractices.xml/AvoidUsingHardCodedIP
-
- IPv4|IPv6|IPv4 mapped IPv6
-
+
+
+
DEPRECATED
@@ -1379,10 +1374,10 @@
category/java/bestpractices.xml/CheckResultSet
-
- MAJOR
- category/java/performance.xml/AvoidUsingShortType
-
+
+
+
+
MAJOR
@@ -1412,11 +1407,11 @@
-
- MAJOR
- category/java/errorprone.xml/DoNotCallSystemExit
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -1531,11 +1526,11 @@
-
- MAJOR
- category/java/codestyle.xml/AvoidPrefixingMethodParameters
- DEPRECATED
-
+
+
+
+
+
MAJOR
@@ -1589,32 +1584,32 @@
DEPRECATED
-
- MAJOR
- category/java/bestpractices.xml/PositionLiteralsFirstInCaseInsensitiveComparisons
- DEPRECATED
-
+
+
+
+
+
MAJOR
category/java/bestpractices.xml/UseVarargs
-
- MAJOR
- category/java/errorprone.xml/EmptyStatementBlock
- DEPRECATED
-
+
+
+
+
+
MAJOR
category/java/codestyle.xml/UnnecessaryFullyQualifiedName
-
- MAJOR
- category/java/bestpractices.xml/GuardLogStatement
-
+
+
+
+
MAJOR
@@ -1658,20 +1653,20 @@
DEPRECATED
-
- MAJOR
- category/java/design.xml/SignatureDeclareThrowsException
-
- false
-
- DEPRECATED
-
+
+
+
+
+
+
+
+
-
- INFO
- category/java/bestpractices.xml/UnusedImports
- DEPRECATED
-
+
+
+
+
+
INFO
@@ -1679,41 +1674,41 @@
DEPRECATED
-
- MAJOR
- size
- category/java/design.xml/StdCyclomaticComplexity
-
- 10
-
-
- true
-
-
- true
-
- DEPRECATED
-
-
-
- MAJOR
- size
- category/java/design.xml/ModifiedCyclomaticComplexity
-
- 10
-
-
- true
-
-
- true
-
- DEPRECATED
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
MAJOR
- net.sourceforge.pmd.lang.rule.XPathRule
+ net.sourceforge.pmd.lang.rule.xpath.XPathRule
MULTIPLE
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java
index 8eba7b62..44600e7f 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,7 +19,7 @@
*/
package org.sonar.plugins.pmd;
-import net.sourceforge.pmd.Report;
+import net.sourceforge.pmd.reporting.Report;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@@ -40,7 +40,7 @@
class PmdConfigurationTest {
- private static final Pattern PMD_XML_PATTERN = Pattern.compile("^<\\?xml version=\"1\\.0\" encoding=\"UTF-8\"\\?>$");
+ private static final Pattern PMD_XML_PATTERN = Pattern.compile("^<\\?xml version=\"1\\.0\" encoding=\"UTF-8\"\\?>$");
private static final File WORK_DIR = new File("test-work-dir");
private final FileSystem fs = mock(FileSystem.class);
@@ -97,7 +97,7 @@ void should_dump_xml_report() throws IOException {
when(fs.workDir()).thenReturn(WORK_DIR);
settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true);
- Path reportFile = configuration.dumpXmlReport(new Report());
+ Path reportFile = configuration.dumpXmlReport(Report.buildReport(v -> {}));
assertThat(reportFile.toFile()).isEqualTo(new File(WORK_DIR, "pmd-result.xml"));
String pmdResultXML = String.join("", Files.readAllLines(reportFile, StandardCharsets.UTF_8));
@@ -112,7 +112,7 @@ void should_fail_to_dump_xml_report() {
settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true);
- final Throwable thrown = catchThrowable(() -> configuration.dumpXmlReport(new Report()));
+ final Throwable thrown = catchThrowable(() -> configuration.dumpXmlReport(Report.buildReport(v -> {})));
assertThat(thrown)
.isInstanceOf(IllegalStateException.class)
@@ -121,7 +121,7 @@ void should_fail_to_dump_xml_report() {
@Test
void should_ignore_xml_report_when_property_is_not_set() {
- Path reportFile = configuration.dumpXmlReport(new Report());
+ Path reportFile = configuration.dumpXmlReport(Report.buildReport(v -> {}));
assertThat(reportFile).isNull();
verifyNoMoreInteractions(fs);
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConstantsTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConstantsTest.java
index b525e2b0..79dd8bc0 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConstantsTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConstantsTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -29,15 +29,16 @@ class PmdConstantsTest {
void checkDefinedKeys() {
assertThat(PmdConstants.PLUGIN_NAME).isEqualTo("PMD");
assertThat(PmdConstants.PLUGIN_KEY).isEqualTo("pmd");
- assertThat(PmdConstants.REPOSITORY_KEY).isEqualTo("pmd");
+ assertThat(PmdConstants.MAIN_JAVA_REPOSITORY_KEY).isEqualTo("pmd");
+ assertThat(PmdConstants.MAIN_KOTLIN_REPOSITORY_KEY).isEqualTo("pmd-kotlin");
assertThat(PmdConstants.REPOSITORY_NAME).isEqualTo("PMD");
- assertThat(PmdConstants.TEST_REPOSITORY_KEY).isEqualTo("pmd-unit-tests");
+ assertThat(PmdConstants.TEST_JAVA_REPOSITORY_KEY).isEqualTo("pmd-unit-tests");
assertThat(PmdConstants.TEST_REPOSITORY_NAME).isEqualTo("PMD Unit Tests");
- assertThat(PmdConstants.XPATH_CLASS).isEqualTo("net.sourceforge.pmd.lang.rule.XPathRule");
+ assertThat(PmdConstants.XPATH_CLASS).isEqualTo("net.sourceforge.pmd.lang.rule.xpath.XPathRule");
assertThat(PmdConstants.XPATH_EXPRESSION_PARAM).isEqualTo("xpath");
assertThat(PmdConstants.XPATH_MESSAGE_PARAM).isEqualTo("message");
assertThat(PmdConstants.JAVA_SOURCE_VERSION).isEqualTo("sonar.java.source");
assertThat(PmdConstants.JAVA_SOURCE_VERSION_DEFAULT_VALUE).isEqualTo("11");
- assertThat(PmdConstants.LANGUAGE_KEY).isEqualTo("java");
+ assertThat(PmdConstants.LANGUAGE_JAVA_KEY).isEqualTo("java");
}
}
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java
index 5a8ab370..69305b98 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,8 +19,9 @@
*/
package org.sonar.plugins.pmd;
-import com.google.common.collect.ImmutableList;
-import net.sourceforge.pmd.*;
+import net.sourceforge.pmd.lang.rule.RuleSet;
+import net.sourceforge.pmd.lang.rule.RuleSetLoadException;
+import net.sourceforge.pmd.reporting.Report;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -40,6 +41,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
@@ -67,7 +69,14 @@ class PmdExecutorTest {
private static DefaultInputFile file(String path, Type type) {
return TestInputFileBuilder.create("sonar-pmd-test", path)
.setType(type)
- .setLanguage(PmdConstants.LANGUAGE_KEY)
+ .setLanguage(PmdConstants.LANGUAGE_JAVA_KEY)
+ .build();
+ }
+
+ private static DefaultInputFile fileKotlin(String path, Type type) {
+ return TestInputFileBuilder.create("", path)
+ .setType(type)
+ .setLanguage(PmdConstants.LANGUAGE_KOTLIN_KEY)
.build();
}
@@ -96,8 +105,8 @@ void whenNoFilesToAnalyzeThenExecutionSucceedsWithBlankReport() {
void should_execute_pmd_on_source_files_and_test_files() {
DefaultInputFile srcFile = file("src/Class.java", Type.MAIN);
DefaultInputFile tstFile = file("test/ClassTest.java", Type.TEST);
- setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml");
- setupPmdRuleSet(PmdConstants.TEST_REPOSITORY_KEY, "junit.xml");
+ setupPmdRuleSet(PmdConstants.MAIN_JAVA_REPOSITORY_KEY, "simple.xml");
+ setupPmdRuleSet(PmdConstants.TEST_JAVA_REPOSITORY_KEY, "junit.xml");
fileSystem.add(srcFile);
fileSystem.add(tstFile);
@@ -118,7 +127,7 @@ void should_execute_pmd_on_source_files_and_test_files() {
void should_ignore_empty_test_dir() {
DefaultInputFile srcFile = file("src/Class.java", Type.MAIN);
doReturn(pmdTemplate).when(pmdExecutor).createPmdTemplate(any(URLClassLoader.class));
- setupPmdRuleSet(PmdConstants.REPOSITORY_KEY, "simple.xml");
+ setupPmdRuleSet(PmdConstants.MAIN_JAVA_REPOSITORY_KEY, "simple.xml");
fileSystem.add(srcFile);
pmdExecutor.execute();
@@ -129,7 +138,7 @@ void should_ignore_empty_test_dir() {
@Test
void should_build_project_classloader_from_javaresourcelocator() throws Exception {
File file = new File("x");
- when(javaResourceLocator.classpath()).thenReturn(ImmutableList.of(file));
+ when(javaResourceLocator.classpath()).thenReturn(List.of(file));
pmdExecutor.execute();
ArgumentCaptor classLoaderArgument = ArgumentCaptor.forClass(URLClassLoader.class);
verify(pmdExecutor).createPmdTemplate(classLoaderArgument.capture());
@@ -142,7 +151,7 @@ void should_build_project_classloader_from_javaresourcelocator() throws Exceptio
void invalid_classpath_element() {
File invalidFile = mock(File.class);
when(invalidFile.toURI()).thenReturn(URI.create("x://xxx"));
- when(javaResourceLocator.classpath()).thenReturn(ImmutableList.of(invalidFile));
+ when(javaResourceLocator.classpath()).thenReturn(List.of(invalidFile));
final Throwable thrown = catchThrowable(() -> pmdExecutor.execute());
@@ -153,7 +162,7 @@ void invalid_classpath_element() {
@Test
void unknown_pmd_ruleset() {
- when(pmdConfiguration.dumpXmlRuleSet(eq(PmdConstants.REPOSITORY_KEY), anyString())).thenReturn(new File("unknown"));
+ when(pmdConfiguration.dumpXmlRuleSet(eq(PmdConstants.MAIN_JAVA_REPOSITORY_KEY), anyString())).thenReturn(new File("unknown"));
DefaultInputFile srcFile = file("src/Class.java", Type.MAIN);
fileSystem.add(srcFile);
@@ -165,6 +174,23 @@ void unknown_pmd_ruleset() {
.hasCauseInstanceOf(RuleSetLoadException.class);
}
+ @Test
+ void should_execute_pmd_on_kotlin_source_files() {
+
+ DefaultInputFile srcFile = fileKotlin("src/test/kotlin/TestKotlin.kt", Type.MAIN);
+
+ setupPmdRuleSet(PmdConstants.MAIN_KOTLIN_REPOSITORY_KEY, "simple-kotlin.xml");
+ fileSystem.add(srcFile);
+
+ Report report = pmdExecutor.execute();
+
+ assertThat(report).isNotNull();
+ assertThat(report.getViolations()).hasSize(1);
+ assertThat(report.getProcessingErrors()).isEmpty();
+ verify(pmdConfiguration).dumpXmlReport(report);
+
+ }
+
private void setupPmdRuleSet(String repositoryKey, String profileFileName) {
final Path sourcePath = Paths.get("src/test/resources/org/sonar/plugins/pmd/").resolve(profileFileName);
when(pmdConfiguration.dumpXmlRuleSet(eq(repositoryKey), anyString())).thenReturn(sourcePath.toFile());
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdKotlinRulesDefinitionTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdKotlinRulesDefinitionTest.java
new file mode 100644
index 00000000..70517217
--- /dev/null
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdKotlinRulesDefinitionTest.java
@@ -0,0 +1,87 @@
+/*
+ * SonarQube PMD7 Plugin
+ * Copyright (C) 2012-2021 SonarSource SA and others
+ * mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.plugins.pmd;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.api.server.rule.RulesDefinition;
+import org.sonar.api.server.rule.RulesDefinition.Param;
+import org.sonar.api.server.rule.RulesDefinition.Rule;
+import org.sonar.plugins.pmd.rule.PmdKotlinRulesDefinition;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class PmdKotlinRulesDefinitionTest {
+
+ @Test
+ void test() {
+ PmdKotlinRulesDefinition definition = new PmdKotlinRulesDefinition();
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ definition.define(context);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.MAIN_KOTLIN_REPOSITORY_KEY);
+
+ assertThat(repository).withFailMessage("repository is null, does key exist").isNotNull();
+ assertThat(repository.name()).isEqualTo(PmdConstants.REPOSITORY_KOTLIN_NAME);
+ assertThat(repository.language()).isEqualTo(PmdConstants.LANGUAGE_KOTLIN_KEY);
+
+ List rules = repository.rules();
+ assertThat(rules).hasSize(1);
+
+ for (Rule rule : rules) {
+ assertThat(rule.key()).isNotNull();
+ assertThat(rule.internalKey()).isNotNull();
+ assertThat(rule.name()).isNotNull();
+ assertThat(rule.htmlDescription()).isNotNull();
+ assertThat(rule.severity()).isNotNull();
+
+ for (Param param : rule.params()) {
+ assertThat(param.name()).isNotNull();
+ assertThat(param.description())
+ .overridingErrorMessage("Description is not set for parameter '" + param.name() + "' of rule '" + rule.key())
+ .isNotNull();
+ }
+ }
+ }
+
+ @Test
+ void should_exclude_java_rules() {
+ PmdKotlinRulesDefinition definition = new PmdKotlinRulesDefinition();
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ definition.define(context);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.MAIN_KOTLIN_REPOSITORY_KEY);
+
+ for (Rule rule : repository.rules()) {
+ assertThat(rule.key()).doesNotContain("AbstractClassWithoutAbstractMethod");
+ }
+ }
+
+ @Test
+ void should_exclude_junit_rules() {
+ PmdKotlinRulesDefinition definition = new PmdKotlinRulesDefinition();
+ RulesDefinition.Context context = new RulesDefinition.Context();
+ definition.define(context);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.MAIN_KOTLIN_REPOSITORY_KEY);
+
+ for (Rule rule : repository.rules()) {
+ assertThat(rule.key()).doesNotContain("JUnitStaticSuite");
+ }
+ }
+}
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPluginTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPluginTest.java
index 7d26a4cb..f31fc199 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPluginTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPluginTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -28,6 +28,7 @@
import org.sonar.api.utils.Version;
import org.sonar.plugins.pmd.profile.PmdProfileExporter;
import org.sonar.plugins.pmd.profile.PmdProfileImporter;
+import org.sonar.plugins.pmd.rule.PmdKotlinRulesDefinition;
import org.sonar.plugins.pmd.rule.PmdRulesDefinition;
import org.sonar.plugins.pmd.rule.PmdUnitTestsRulesDefinition;
@@ -53,13 +54,14 @@ void testPluginConfiguration() {
// then
final List extensions = context.getExtensions();
assertThat(extensions)
- .hasSize(9)
+ .hasSize(10)
.contains(
PmdSensor.class,
PmdConfiguration.class,
PmdExecutor.class,
PmdRulesDefinition.class,
PmdUnitTestsRulesDefinition.class,
+ PmdKotlinRulesDefinition.class,
PmdProfileExporter.class,
PmdProfileImporter.class,
PmdViolationRecorder.class
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPrioritiesTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPrioritiesTest.java
index 5ce029a3..53c40c43 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPrioritiesTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdPrioritiesTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -20,6 +20,7 @@
package org.sonar.plugins.pmd;
import org.junit.jupiter.api.Test;
+import org.sonar.api.rule.Severity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.rules.RulePriority.BLOCKER;
@@ -29,6 +30,7 @@
import static org.sonar.api.rules.RulePriority.MINOR;
class PmdPrioritiesTest {
+
@Test
void should_get_priority_from_level() {
assertThat(PmdPriorities.toSonarPrio(1)).isSameAs(BLOCKER);
@@ -40,6 +42,17 @@ void should_get_priority_from_level() {
assertThat(PmdPriorities.toSonarPrio(null)).isNull();
}
+ @Test
+ void should_get_priority_from_level_severity() {
+ assertThat(PmdPriorities.toSonarSeverity(1)).isSameAs(Severity.BLOCKER);
+ assertThat(PmdPriorities.toSonarSeverity(2)).isSameAs(Severity.CRITICAL);
+ assertThat(PmdPriorities.toSonarSeverity(3)).isSameAs(Severity.MAJOR);
+ assertThat(PmdPriorities.toSonarSeverity(4)).isSameAs(Severity.MINOR);
+ assertThat(PmdPriorities.toSonarSeverity(5)).isSameAs(Severity.INFO);
+ assertThat(PmdPriorities.toSonarSeverity(-1)).isNull();
+ assertThat(PmdPriorities.toSonarSeverity(null)).isNull();
+ }
+
@Test
void should_get_level_from_priority() {
assertThat(PmdPriorities.fromSonarPrio(BLOCKER)).isEqualTo(1);
@@ -48,4 +61,13 @@ void should_get_level_from_priority() {
assertThat(PmdPriorities.fromSonarPrio(MINOR)).isEqualTo(4);
assertThat(PmdPriorities.fromSonarPrio(INFO)).isEqualTo(5);
}
+
+ @Test
+ void should_get_level_from_priority_severity() {
+ assertThat(PmdPriorities.fromSonarSeverity(Severity.BLOCKER)).isEqualTo(1);
+ assertThat(PmdPriorities.fromSonarSeverity(Severity.CRITICAL)).isEqualTo(2);
+ assertThat(PmdPriorities.fromSonarSeverity(Severity.MAJOR)).isEqualTo(3);
+ assertThat(PmdPriorities.fromSonarSeverity(Severity.MINOR)).isEqualTo(4);
+ assertThat(PmdPriorities.fromSonarSeverity(Severity.INFO)).isEqualTo(5);
+ }
}
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesDefinitionTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesDefinitionTest.java
index a7fa6148..021369c4 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesDefinitionTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesDefinitionTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,8 +19,6 @@
*/
package org.sonar.plugins.pmd;
-import java.util.List;
-
import com.google.common.collect.Iterables;
import org.junit.jupiter.api.Test;
import org.sonar.api.PropertyType;
@@ -29,6 +27,8 @@
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.plugins.pmd.rule.PmdRulesDefinition;
+import java.util.List;
+
import static org.assertj.core.api.Assertions.assertThat;
class PmdRulesDefinitionTest {
@@ -38,13 +38,14 @@ void test() {
PmdRulesDefinition definition = new PmdRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
- RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.MAIN_JAVA_REPOSITORY_KEY);
assertThat(repository.name()).isEqualTo(PmdConstants.REPOSITORY_NAME);
- assertThat(repository.language()).isEqualTo(PmdConstants.LANGUAGE_KEY);
+ assertThat(repository.language()).isEqualTo(PmdConstants.LANGUAGE_JAVA_KEY);
List rules = repository.rules();
- assertThat(rules).hasSize(268);
+ // PMD-7-MIGRATION: check number of rules is correct from PMD 7.x (was 228 in PMD 6.x)
+ assertThat(rules).hasSize(205);
for (Rule rule : rules) {
assertThat(rule.key()).isNotNull();
@@ -67,7 +68,7 @@ void should_exclude_junit_rules() {
PmdRulesDefinition definition = new PmdRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
- RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.MAIN_JAVA_REPOSITORY_KEY);
for (Rule rule : repository.rules()) {
assertThat(rule.key()).doesNotContain("JUnitStaticSuite");
@@ -79,7 +80,9 @@ void should_use_text_parameter_for_xpath_rule() {
PmdRulesDefinition definition = new PmdRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
- RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.MAIN_JAVA_REPOSITORY_KEY);
+ List rules = repository.rules();
+ System.out.println("rules: " + rules);
Rule xpathRule = Iterables.find(repository.rules(), rule -> rule.key().equals("XPathRule"));
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java
index b3264fa9..ff3d25a7 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdSensorTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,11 +19,9 @@
*/
package org.sonar.plugins.pmd;
-import java.io.File;
-import java.util.Arrays;
-
-import net.sourceforge.pmd.Report;
-import net.sourceforge.pmd.RuleViolation;
+import net.sourceforge.pmd.reporting.FileAnalysisListener;
+import net.sourceforge.pmd.reporting.Report;
+import net.sourceforge.pmd.reporting.RuleViolation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sonar.api.batch.fs.InputFile.Type;
@@ -33,18 +31,13 @@
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
+import java.io.File;
+import java.util.function.Consumer;
+
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.atLeastOnce;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
class PmdSensorTest {
@@ -107,8 +100,8 @@ void should_not_execute_on_project_without_active_rules() {
addOneJavaFile(Type.MAIN);
addOneJavaFile(Type.TEST);
- when(profile.findByRepository(PmdConstants.REPOSITORY_KEY).isEmpty()).thenReturn(true);
- when(profile.findByRepository(PmdConstants.TEST_REPOSITORY_KEY).isEmpty()).thenReturn(true);
+ when(profile.findByRepository(PmdConstants.MAIN_JAVA_REPOSITORY_KEY).isEmpty()).thenReturn(true);
+ when(profile.findByRepository(PmdConstants.TEST_JAVA_REPOSITORY_KEY).isEmpty()).thenReturn(true);
// when
pmdSensor.execute(sensorContext);
@@ -189,13 +182,13 @@ void whenDescribeCalledThenSensorDescriptionIsWritten() {
// given
final SensorDescriptor mockDescriptor = mock(SensorDescriptor.class);
- when(mockDescriptor.onlyOnLanguage(anyString())).thenReturn(mockDescriptor);
+ when(mockDescriptor.onlyOnLanguages(anyString(), anyString())).thenReturn(mockDescriptor);
// when
pmdSensor.describe(mockDescriptor);
// then
- verify(mockDescriptor).onlyOnLanguage(PmdConstants.LANGUAGE_KEY);
+ verify(mockDescriptor).onlyOnLanguages(PmdConstants.LANGUAGE_JAVA_KEY, PmdConstants.LANGUAGE_KOTLIN_KEY);
verify(mockDescriptor).name("PmdSensor");
}
@@ -204,9 +197,14 @@ private static RuleViolation violation() {
}
private void mockExecutorResult(RuleViolation... violations) {
- final Report report = new Report();
- Arrays.stream(violations)
- .forEach(report::addRuleViolation);
+
+ Consumer fileAnalysisListenerConsumer = fal -> {
+ for (RuleViolation violation : violations) {
+ fal.onRuleViolation(violation);
+ }
+ };
+
+ final Report report = Report.buildReport(fileAnalysisListenerConsumer);
when(executor.execute())
.thenReturn(report);
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java
index 3c1f8706..02bdebc1 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTemplateTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,7 +19,7 @@
*/
package org.sonar.plugins.pmd;
-import net.sourceforge.pmd.lang.LanguageVersionHandler;
+import net.sourceforge.pmd.lang.Language;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -34,14 +34,14 @@ class PmdTemplateTest {
@ParameterizedTest
@ValueSource(strings = {
- "6", "7", "8", "9", "1.9", "10", "1.10", "11", "1.11", "12", "13", "14", "15", "16", "17", "18", "19", "19-preview","20", "20-preview"
+ "6", "7", "8", "9", "1.9", "10", "1.10", "11", "1.11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
})
void verifyCanHandleJavaLanguageVersion(String javaVersion) {
- final LanguageVersionHandler languageVersionHandler = PmdTemplate
+ final Language language = PmdTemplate
.languageVersion(javaVersion)
- .getLanguageVersionHandler();
+ .getLanguage();
- assertThat(languageVersionHandler).isNotNull();
+ assertThat(language).isNotNull();
}
@Test
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTestUtils.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTestUtils.java
index 49712370..d58c32e4 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTestUtils.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdTestUtils.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdUnitTestsRulesDefinitionTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdUnitTestsRulesDefinitionTest.java
index eeefd98f..525af340 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdUnitTestsRulesDefinitionTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdUnitTestsRulesDefinitionTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,14 +19,14 @@
*/
package org.sonar.plugins.pmd;
-import java.util.List;
-
import org.junit.jupiter.api.Test;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.server.rule.RulesDefinition.Param;
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.plugins.pmd.rule.PmdUnitTestsRulesDefinition;
+import java.util.List;
+
import static org.assertj.core.api.Assertions.assertThat;
class PmdUnitTestsRulesDefinitionTest {
@@ -37,15 +37,16 @@ void test() {
PmdUnitTestsRulesDefinition definition = new PmdUnitTestsRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
- RulesDefinition.Repository repository = context.repository(PmdConstants.TEST_REPOSITORY_KEY);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.TEST_JAVA_REPOSITORY_KEY);
assertThat(repository)
.isNotNull()
.hasFieldOrPropertyWithValue("name", PmdConstants.TEST_REPOSITORY_NAME)
- .hasFieldOrPropertyWithValue("language", PmdConstants.LANGUAGE_KEY);
+ .hasFieldOrPropertyWithValue("language", PmdConstants.LANGUAGE_JAVA_KEY);
List rules = repository.rules();
- assertThat(rules).hasSize(17);
+ //assertThat(rules).hasSize(17); NOTE: 5 rules have been removed in pmd-7
+ assertThat(rules).hasSize(12);
for (Rule rule : rules) {
assertThat(rule.key()).isNotNull();
@@ -56,11 +57,11 @@ void test() {
"JUnitTestsShouldIncludeAssert",
"TestClassWithoutTestCases",
"UnnecessaryBooleanAssertion",
- "UseAssertEqualsInsteadOfAssertTrue",
- "UseAssertSameInsteadOfAssertTrue",
- "UseAssertNullInsteadOfAssertTrue",
- "SimplifyBooleanAssertion",
- "UseAssertTrueInsteadOfAssertEquals",
+ //"UseAssertEqualsInsteadOfAssertTrue", // 5 rules have been removed in pmd-7
+ //"UseAssertSameInsteadOfAssertTrue",
+ //"UseAssertNullInsteadOfAssertTrue",
+ //"SimplifyBooleanAssertion",
+ //"UseAssertTrueInsteadOfAssertEquals",
"JUnitTestContainsTooManyAsserts",
"JUnit4SuitesShouldUseSuiteAnnotation",
"JUnit4TestShouldUseAfterAnnotation",
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationRecorderTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationRecorderTest.java
index 6854f115..288a4313 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationRecorderTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationRecorderTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,8 +19,9 @@
*/
package org.sonar.plugins.pmd;
-import net.sourceforge.pmd.Rule;
-import net.sourceforge.pmd.RuleViolation;
+import net.sourceforge.pmd.lang.document.FileId;
+import net.sourceforge.pmd.lang.rule.Rule;
+import net.sourceforge.pmd.reporting.RuleViolation;
import org.junit.jupiter.api.Test;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.TextRange;
@@ -37,11 +38,7 @@
import java.io.File;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
class PmdViolationRecorderTest {
@@ -102,8 +99,8 @@ void should_ignore_violation_on_unknown_rule() {
addToFileSystem(file1);
final String ruleName = "UNKNOWN";
final RuleViolation pmdViolation = createPmdViolation(file1, ruleName);
- final RuleKey expectedRuleKey1 = RuleKey.of(PmdConstants.REPOSITORY_KEY, ruleName);
- final RuleKey expectedRuleKey2 = RuleKey.of(PmdConstants.REPOSITORY_KEY, ruleName);
+ final RuleKey expectedRuleKey1 = RuleKey.of(PmdConstants.MAIN_JAVA_REPOSITORY_KEY, ruleName);
+ final RuleKey expectedRuleKey2 = RuleKey.of(PmdConstants.MAIN_JAVA_REPOSITORY_KEY, ruleName);
// when
subject.saveViolation(pmdViolation, mockContext);
@@ -137,7 +134,7 @@ private RuleViolation createPmdViolation(File file, String ruleName) {
final RuleViolation pmdViolation = mock(RuleViolation.class);
when(rule.getName()).thenReturn(ruleName);
- when(pmdViolation.getFilename()).thenReturn(file.getAbsolutePath());
+ when(pmdViolation.getFileId()).thenReturn(FileId.fromPath(file.toPath()));
when(pmdViolation.getBeginLine()).thenReturn(2);
when(pmdViolation.getDescription()).thenReturn("Description");
when(pmdViolation.getRule()).thenReturn(rule);
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/TextRangeCalculatorTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/TextRangeCalculatorTest.java
index 96837ae0..c4b2ea15 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/TextRangeCalculatorTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/TextRangeCalculatorTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,7 +19,7 @@
*/
package org.sonar.plugins.pmd;
-import net.sourceforge.pmd.RuleViolation;
+import net.sourceforge.pmd.reporting.RuleViolation;
import org.junit.jupiter.api.Test;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileExporterTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileExporterTest.java
index 30228f9b..d38c17be 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileExporterTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileExporterTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -19,13 +19,6 @@
*/
package org.sonar.plugins.pmd.profile;
-import java.io.IOException;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.List;
-
import com.google.common.base.CharMatcher;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
@@ -42,13 +35,18 @@
import org.sonar.plugins.pmd.PmdTestUtils;
import org.sonar.plugins.pmd.rule.PmdRulesDefinition;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
class PmdProfileExporterTest {
@@ -60,7 +58,7 @@ private static RulesProfile importProfile(String configuration) {
PmdRulesDefinition definition = new PmdRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
- RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);
+ RulesDefinition.Repository repository = context.repository(PmdConstants.MAIN_JAVA_REPOSITORY_KEY);
RuleFinder ruleFinder = createRuleFinder(repository.rules());
PmdProfileImporter importer = new PmdProfileImporter(ruleFinder);
@@ -197,9 +195,9 @@ void should_export_empty_configuration_as_xml() {
@Test
void should_export_xPath_rule() {
- Rule rule = Rule.create(PmdConstants.REPOSITORY_KEY, "MyOwnRule", "This is my own xpath rule.")
+ Rule rule = Rule.create(PmdConstants.MAIN_JAVA_REPOSITORY_KEY, "MyOwnRule", "This is my own xpath rule.")
.setConfigKey(PmdConstants.XPATH_CLASS)
- .setRepositoryKey(PmdConstants.REPOSITORY_KEY);
+ .setRepositoryKey(PmdConstants.MAIN_JAVA_REPOSITORY_KEY);
rule.createParameter(PmdConstants.XPATH_EXPRESSION_PARAM);
rule.createParameter(PmdConstants.XPATH_MESSAGE_PARAM);
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileImporterTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileImporterTest.java
index 8a79f273..73f4e02a 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileImporterTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/profile/PmdProfileImporterTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
@@ -55,8 +55,8 @@ private static RuleFinder createRuleFinder() {
String configKey = query.getConfigKey();
String key = configKey.substring(configKey.lastIndexOf('/') + 1);
Rule rule = Rule.create(query.getRepositoryKey(), key, "").setConfigKey(configKey).setSeverity(RulePriority.BLOCKER);
- if (rule.getConfigKey().equals("rulesets/java/coupling.xml/ExcessiveImports")) {
- rule.createParameter("minimum");
+ if (rule.getConfigKey().equals("category/java/bestpractices.xml/ForLoopVariableCount")) {
+ rule.createParameter("maximumVariables");
}
return rule;
});
@@ -76,8 +76,8 @@ void should_import_simple_profile() {
RulesProfile profile = importer.importProfile(reader, messages);
assertThat(profile.getActiveRules()).hasSize(3);
- assertThat(profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports")).isNotNull();
- assertThat(profile.getActiveRuleByConfigKey("pmd", "rulesets/java/design.xml/UseNotifyAllInsteadOfNotify")).isNotNull();
+ assertThat(profile.getActiveRuleByConfigKey("pmd", "category/java/errorprone.xml/AvoidLiteralsInIfCondition")).isNotNull();
+ assertThat(profile.getActiveRuleByConfigKey("pmd", "category/java/multithreading.xml/DoubleCheckedLocking")).isNotNull();
assertThat(messages.hasErrors()).isFalse();
}
@@ -96,9 +96,9 @@ void should_import_parameter() {
Reader reader = read("/org/sonar/plugins/pmd/simple.xml");
RulesProfile profile = importer.importProfile(reader, messages);
- ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports");
+ ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "category/java/bestpractices.xml/ForLoopVariableCount");
- assertThat(activeRule.getParameter("minimum")).isEqualTo("30");
+ assertThat(activeRule.getParameter("maximumVariables")).isEqualTo("5");
}
@Test
@@ -106,7 +106,7 @@ void should_import_default_priority() {
Reader reader = read("/org/sonar/plugins/pmd/simple.xml");
RulesProfile profile = importer.importProfile(reader, messages);
- ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports");
+ ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "category/java/multithreading.xml/DoubleCheckedLocking");
assertThat(activeRule.getSeverity()).isSameAs(RulePriority.BLOCKER);
}
@@ -117,11 +117,11 @@ void should_import_priority() {
RulesProfile profile = importer.importProfile(reader, messages);
- ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/design.xml/UseNotifyAllInsteadOfNotify");
- assertThat(activeRule.getSeverity()).isSameAs(RulePriority.MINOR);
-
- activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/CouplingBetweenObjects");
+ ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "category/java/errorprone.xml/AvoidLiteralsInIfCondition");
assertThat(activeRule.getSeverity()).isSameAs(RulePriority.CRITICAL);
+
+ activeRule = profile.getActiveRuleByConfigKey("pmd", "category/java/bestpractices.xml/ForLoopVariableCount");
+ assertThat(activeRule.getSeverity()).isSameAs(RulePriority.MINOR);
}
@Test
@@ -138,8 +138,10 @@ void should_deal_with_unsupported_property() {
Reader reader = read("/org/sonar/plugins/pmd/simple.xml");
RulesProfile profile = importer.importProfile(reader, messages);
- ActiveRule check = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/CouplingBetweenObjects");
+ ActiveRule check = profile.getActiveRuleByConfigKey("pmd", "category/java/bestpractices.xml/ForLoopVariableCount");
+ // PMD7-MIGRATION what is meaning of this check? The list of parameters is empty (but expected maximumVariables?)
+ // in the errors: The property 'maximumVariables' is not supported in the pmd rule: category/java/bestpractices.xml/ForLoopVariableCount
assertThat(check.getParameter("threshold")).isNull();
assertThat(messages.getWarnings()).hasSize(2);
}
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/rule/ExternalDescriptionLoaderTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/rule/ExternalDescriptionLoaderTest.java
index 92054c02..0c00f808 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/rule/ExternalDescriptionLoaderTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/rule/ExternalDescriptionLoaderTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
diff --git a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/xml/PmdRuleSetsTest.java b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/xml/PmdRuleSetsTest.java
index 96970021..6f30c237 100644
--- a/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/xml/PmdRuleSetsTest.java
+++ b/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/xml/PmdRuleSetsTest.java
@@ -1,5 +1,5 @@
/*
- * SonarQube PMD Plugin
+ * SonarQube PMD7 Plugin
* Copyright (C) 2012-2021 SonarSource SA and others
* mailto:jborgers AT jpinpoint DOT com; peter.paul.bakker AT stokpop DOT nl
*
diff --git a/sonar-pmd-plugin/src/test/kotlin/TestKotlin.kt b/sonar-pmd-plugin/src/test/kotlin/TestKotlin.kt
new file mode 100644
index 00000000..fcc3598f
--- /dev/null
+++ b/sonar-pmd-plugin/src/test/kotlin/TestKotlin.kt
@@ -0,0 +1,3 @@
+fun a(): Int { 1 }
+
+fun abc(): Int { 2 }
\ No newline at end of file
diff --git a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_rule_with_all_params_empty.xml b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_rule_with_all_params_empty.xml
index 07f8b46b..bb8d24e2 100644
--- a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_rule_with_all_params_empty.xml
+++ b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_rule_with_all_params_empty.xml
@@ -1,5 +1,6 @@
-
+
+ Sonar Profile: pmd
2
diff --git a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_xpath_rules.xml b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_xpath_rules.xml
index ab7cc906..c7ecd079 100644
--- a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_xpath_rules.xml
+++ b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/export_xpath_rules.xml
@@ -1,7 +1,7 @@
Sonar Profile: pmd
-
+
3
diff --git a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/junit.xml b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/junit.xml
index 09653f46..3dd6ec36 100644
--- a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/junit.xml
+++ b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/junit.xml
@@ -1,7 +1,9 @@
-
+
Sonar PMD Unit Tests rules
-
- 2
-
+
+
+
+
diff --git a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/simple-kotlin.xml b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/simple-kotlin.xml
new file mode 100644
index 00000000..ca69da70
--- /dev/null
+++ b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/simple-kotlin.xml
@@ -0,0 +1,7 @@
+
+
+ Sonar PMD kotlin rules
+
+ 2
+
+
diff --git a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/simple.xml b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/simple.xml
index 86acb5cf..5e72b60d 100644
--- a/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/simple.xml
+++ b/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/simple.xml
@@ -1,21 +1,21 @@
-
+
Sonar PMD rules
-
+
2
-
+
-
-
-
-
+
-
+
4
+
+
+
-
+
3