use of org.sonar.check.Rule in project sonar-java by SonarSource.
the class JavaCheckVerifierTest method rule_metadata_undefined_remediation_function.
@Test
public void rule_metadata_undefined_remediation_function() {
@Rule(key = "UndefinedRemediationFunc")
class WrongJson extends FakeVisitor {
}
WrongJson check = new WrongJson();
check.withPreciseIssue(new AnalyzerMessage(check, new File("a"), 1, "message", 0));
JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoCost.java", check);
}
use of org.sonar.check.Rule in project sonar-java by SonarSource.
the class JavaCheckVerifierTest method rule_metadata_remediation_function.
@Test
public void rule_metadata_remediation_function() {
@Rule(key = "DoesntExists")
class WrongJson extends FakeVisitor {
}
WrongJson check = new WrongJson();
check.withPreciseIssue(new AnalyzerMessage(check, new File("a"), 1, "message", 0));
JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoCost.java", check);
}
use of org.sonar.check.Rule in project sonar-java by SonarSource.
the class CheckListTest method rules_targeting_tests_should_have_tests_tag.
@Test
public void rules_targeting_tests_should_have_tests_tag() throws Exception {
Set<Class> testChecks = new HashSet<>(CheckList.getJavaTestChecks());
for (Class cls : CheckList.getChecks()) {
String key = getKey(cls, AnnotationUtils.getAnnotation(cls, Rule.class));
URL metadataURL = getClass().getResource("/org/sonar/l10n/java/rules/" + CheckList.REPOSITORY_KEY + "/" + key + "_java.json");
File metadataFile = new File(metadataURL.toURI());
assertThat(metadataFile).exists();
try (FileReader jsonReader = new FileReader(metadataFile)) {
DummyMetatada metatada = gson.fromJson(jsonReader, DummyMetatada.class);
if (testChecks.contains(cls)) {
assertThat(metatada.tags).as("Rule " + key + " is targeting tests sources and should contain the 'tests' tag.").contains("tests");
} else {
assertThat(metatada.tags).as("Rule " + key + " is targeting main sources and should not contain the 'tests' tag.").doesNotContain("tests");
}
}
}
}
use of org.sonar.check.Rule in project sonar-java by SonarSource.
the class CheckListTest method test.
/**
* Enforces that each check has test, name and description.
*/
@Test
public void test() {
List<Class> checks = CheckList.getChecks();
Map<String, String> keyMap = new HashMap<>();
for (Class cls : checks) {
String testName = '/' + cls.getName().replace('.', '/') + "Test.class";
String simpleName = cls.getSimpleName();
// Handle legacy keys.
Rule ruleAnnotation = AnnotationUtils.getAnnotation(cls, Rule.class);
String key = getKey(cls, ruleAnnotation);
keyMap.put(ruleAnnotation.key(), key);
if (SE_CHEKS.contains(simpleName)) {
continue;
}
assertThat(getClass().getResource(testName)).overridingErrorMessage("No test for " + simpleName).isNotNull();
}
Set<String> keys = Sets.newHashSet();
Set<String> names = Sets.newHashSet();
CustomRulesDefinition definition = new CustomRulesDefinition();
RulesDefinition.Context context = new RulesDefinition.Context();
definition.define(context);
List<RulesDefinition.Rule> rules = context.repository(CheckList.REPOSITORY_KEY).rules();
for (RulesDefinition.Rule rule : rules) {
assertThat(keys).as("Duplicate key " + rule.key()).doesNotContain(rule.key());
keys.add(rule.key());
names.add(rule.name());
assertThat(getClass().getResource("/org/sonar/l10n/java/rules/" + CheckList.REPOSITORY_KEY + "/" + keyMap.get(rule.key()) + "_java.html")).overridingErrorMessage("No description for " + rule.key() + " " + keyMap.get(rule.key())).isNotNull();
assertThat(getClass().getResource("/org/sonar/l10n/java/rules/" + CheckList.REPOSITORY_KEY + "/" + keyMap.get(rule.key()) + "_java.json")).overridingErrorMessage("No json metadata file for " + rule.key() + " " + keyMap.get(rule.key())).isNotNull();
assertThat(rule.htmlDescription()).isNull();
assertThat(rule.markdownDescription()).isEqualTo(ARTIFICIAL_DESCRIPTION);
for (RulesDefinition.Param param : rule.params()) {
assertThat(param.description()).overridingErrorMessage(rule.key() + " missing description for param " + param.key()).isNotEmpty();
}
}
}
use of org.sonar.check.Rule in project sonar-java by SonarSource.
the class CheckVerifier method ruleKey.
private static String ruleKey(AnalyzerMessage issue) {
String ruleKey;
RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(issue.getCheck().getClass(), RspecKey.class);
if (rspecKeyAnnotation != null) {
ruleKey = rspecKeyAnnotation.value();
} else {
Rule ruleAnnotation = AnnotationUtils.getAnnotation(issue.getCheck().getClass(), Rule.class);
if (ruleAnnotation != null) {
ruleKey = ruleAnnotation.key();
} else {
Fail.fail("Rules should be annotated with '@Rule(key = \"...\")' annotation (org.sonar.check.Rule).");
// unreachable
return null;
}
}
return ruleKey;
}
Aggregations