-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-4609 [Custom Rules] CheckRegistrar classes can register che…
…ck instances, default quality profile and AutoScan (#4470)
- Loading branch information
1 parent
1947bdb
commit 3d80c6c
Showing
29 changed files
with
912 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...hecks-testkit/src/main/java/org/sonar/java/checks/verifier/TestCheckRegistrarContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* SonarQube Java | ||
* Copyright (C) 2012-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* 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.java.checks.verifier; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.sonar.api.rule.RuleKey; | ||
import org.sonar.api.rules.RuleAnnotationUtils; | ||
import org.sonar.plugins.java.api.CheckRegistrar; | ||
import org.sonar.plugins.java.api.JavaCheck; | ||
|
||
public class TestCheckRegistrarContext extends CheckRegistrar.RegistrarContext { | ||
|
||
public final List<Class<? extends JavaCheck>> mainCheckClasses = new ArrayList<>(); | ||
public final List<JavaCheck> mainCheckInstances = new ArrayList<>(); | ||
public final List<RuleKey> mainRuleKeys = new ArrayList<>(); | ||
|
||
public final List<Class<? extends JavaCheck>> testCheckClasses = new ArrayList<>(); | ||
public final List<JavaCheck> testCheckInstances = new ArrayList<>(); | ||
public final List<RuleKey> testRuleKeys = new ArrayList<>(); | ||
|
||
public final Set<RuleKey> autoScanCompatibleRules = new HashSet<>(); | ||
|
||
@Override | ||
public void registerMainChecks(String repositoryKey, Collection<?> javaCheckClassesAndInstances) { | ||
validateAndRegisterChecks(repositoryKey, javaCheckClassesAndInstances, mainCheckClasses, mainCheckInstances, mainRuleKeys); | ||
} | ||
|
||
@Override | ||
public void registerTestChecks(String repositoryKey, Collection<?> javaCheckClassesAndInstances) { | ||
validateAndRegisterChecks(repositoryKey, javaCheckClassesAndInstances, testCheckClasses, testCheckInstances, testRuleKeys); | ||
} | ||
|
||
@Override | ||
public void registerMainSharedCheck(JavaCheck check, Collection<RuleKey> ruleKeys) { | ||
mainCheckClasses.add(check.getClass()); | ||
mainCheckInstances.add(check); | ||
mainRuleKeys.addAll(ruleKeys); | ||
} | ||
|
||
@Override | ||
public void registerTestSharedCheck(JavaCheck check, Collection<RuleKey> ruleKeys) { | ||
testCheckClasses.add(check.getClass()); | ||
testCheckInstances.add(check); | ||
testRuleKeys.addAll(ruleKeys); | ||
} | ||
|
||
@Override | ||
public void registerAutoScanCompatibleRules(Collection<RuleKey> ruleKeys) { | ||
autoScanCompatibleRules.addAll(ruleKeys); | ||
} | ||
|
||
private static void validateAndRegisterChecks(String repositoryKey, | ||
Collection<?> javaCheckClassesAndInstances, | ||
List<Class<? extends JavaCheck>> destCheckClasses, | ||
List<JavaCheck> destCheckInstances, | ||
List<RuleKey> destRuleKeys) { | ||
if (StringUtils.isBlank(repositoryKey)) { | ||
throw new IllegalArgumentException("Please specify a non blank repository key"); | ||
} | ||
for (Object javaCheckClassOrInstance : javaCheckClassesAndInstances) { | ||
Class<? extends JavaCheck> checkClass; | ||
JavaCheck check; | ||
try { | ||
if (javaCheckClassOrInstance instanceof Class) { | ||
checkClass = (Class<? extends JavaCheck>) javaCheckClassOrInstance; | ||
check = checkClass.getDeclaredConstructor().newInstance(); | ||
} else { | ||
check = (JavaCheck) javaCheckClassOrInstance; | ||
checkClass = check.getClass(); | ||
} | ||
} catch (ClassCastException | NoSuchMethodException | InstantiationException | IllegalAccessException | | ||
InvocationTargetException e) { | ||
throw new IllegalStateException(String.format("Fail to instantiate %s", javaCheckClassOrInstance), e); | ||
} | ||
RuleKey ruleKey = RuleKey.of(repositoryKey, RuleAnnotationUtils.getRuleKey(checkClass)); | ||
destCheckClasses.add(checkClass); | ||
destCheckInstances.add(check); | ||
destRuleKeys.add(ruleKey); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...cks-testkit/src/main/java/org/sonar/java/checks/verifier/TestProfileRegistrarContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* SonarQube Java | ||
* Copyright (C) 2012-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* 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.java.checks.verifier; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.sonar.api.rule.RuleKey; | ||
import org.sonar.plugins.java.api.ProfileRegistrar; | ||
|
||
public class TestProfileRegistrarContext implements ProfileRegistrar.RegistrarContext { | ||
|
||
public final Set<RuleKey> defaultQualityProfileRules = new HashSet<>(); | ||
|
||
@Override | ||
public void registerDefaultQualityProfileRules(Collection<RuleKey> ruleKeys) { | ||
defaultQualityProfileRules.addAll(ruleKeys); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...s-testkit/src/test/java/org/sonar/java/checks/verifier/TestCheckRegistrarContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* SonarQube Java | ||
* Copyright (C) 2012-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* 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.java.checks.verifier; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.sonar.api.rule.RuleKey; | ||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.java.api.JavaCheck; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class TestCheckRegistrarContextTest { | ||
|
||
@Rule(key = "X1") | ||
static class Rule1 implements JavaCheck { | ||
} | ||
@Rule(key = "X2") | ||
static class Rule2 implements JavaCheck { | ||
} | ||
@Rule(key = "X3") | ||
static class Rule3 implements JavaCheck { | ||
} | ||
@Rule(key = "X4") | ||
static class Rule4 implements JavaCheck { | ||
} | ||
@Rule(key = "X5") | ||
static class Rule5 implements JavaCheck { | ||
} | ||
@Rule(key = "X6") | ||
static class Rule6 implements JavaCheck { | ||
} | ||
static class Scanner implements JavaCheck { | ||
} | ||
|
||
@Test | ||
void store_registration() { | ||
TestCheckRegistrarContext context = new TestCheckRegistrarContext(); | ||
|
||
var rule4 = new Rule4(); | ||
|
||
context.registerClassesForRepository( | ||
"customRepo", | ||
List.of(Rule1.class, Rule2.class), | ||
List.of(Rule1.class, Rule3.class)); | ||
|
||
context.registerMainChecks("customRepo", List.of( | ||
rule4, | ||
Rule5.class)); | ||
context.registerTestChecks("customRepo", List.of( | ||
rule4, | ||
Rule6.class)); | ||
context.registerMainSharedCheck(new Scanner(), List.of( | ||
RuleKey.of("securityRepo", "R1"), | ||
RuleKey.of("securityRepo", "R2"))); | ||
context.registerTestSharedCheck(new Scanner(), List.of( | ||
RuleKey.of("securityRepo", "R3"), | ||
RuleKey.of("securityRepo", "R4"))); | ||
context.registerAutoScanCompatibleRules(List.of( | ||
RuleKey.of("customRepo", "X1"))); | ||
|
||
assertThat(context.mainRuleKeys).extracting(RuleKey::toString).containsExactly( | ||
"customRepo:X1", | ||
"customRepo:X2", | ||
"customRepo:X4", | ||
"customRepo:X5", | ||
"securityRepo:R1", | ||
"securityRepo:R2"); | ||
|
||
assertThat(context.mainCheckClasses).extracting(Class::getSimpleName).containsExactly( | ||
"Rule1", | ||
"Rule2", | ||
"Rule4", | ||
"Rule5", | ||
"Scanner"); | ||
|
||
assertThat(context.testRuleKeys).extracting(RuleKey::toString).containsExactly( | ||
"customRepo:X1", | ||
"customRepo:X3", | ||
"customRepo:X4", | ||
"customRepo:X6", | ||
"securityRepo:R3", | ||
"securityRepo:R4"); | ||
|
||
assertThat(context.testCheckClasses).extracting(Class::getSimpleName).containsExactly( | ||
"Rule1", | ||
"Rule3", | ||
"Rule4", | ||
"Rule6", | ||
"Scanner"); | ||
|
||
assertThat(context.autoScanCompatibleRules).containsExactly(RuleKey.of("customRepo", "X1")); | ||
} | ||
|
||
@Test | ||
void should_fail_if_repository_key_is_blank() { | ||
TestCheckRegistrarContext context = new TestCheckRegistrarContext(); | ||
List<?> checkClasses = List.of(Rule1.class); | ||
assertThatThrownBy(() -> context.registerMainChecks(" ", checkClasses)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Please specify a non blank repository key"); | ||
} | ||
|
||
@Test | ||
void should_fail_if_not_a_JavaCheck() { | ||
TestCheckRegistrarContext context = new TestCheckRegistrarContext(); | ||
List<?> checkClasses = List.of(Object.class); | ||
assertThatThrownBy(() -> context.registerMainChecks("repo", checkClasses)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Fail to instantiate class java.lang.Object"); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...testkit/src/test/java/org/sonar/java/checks/verifier/TestProfileRegistrarContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* SonarQube Java | ||
* Copyright (C) 2012-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* 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.java.checks.verifier; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.sonar.api.rule.RuleKey; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class TestProfileRegistrarContextTest { | ||
|
||
@Test | ||
void store_registration() { | ||
TestProfileRegistrarContext context = new TestProfileRegistrarContext(); | ||
context.registerDefaultQualityProfileRules(List.of(RuleKey.of("java", "S1234"))); | ||
assertThat(context.defaultQualityProfileRules).containsExactly(RuleKey.of("java", "S1234")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.