Search in sources :

Example 1 with Rule

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);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) Rule(org.sonar.check.Rule) File(java.io.File) Test(org.junit.Test)

Example 2 with Rule

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);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) Rule(org.sonar.check.Rule) File(java.io.File) Test(org.junit.Test)

Example 3 with Rule

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");
            }
        }
    }
}
Also used : BeforeClass(org.junit.BeforeClass) FileReader(java.io.FileReader) Rule(org.sonar.check.Rule) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Rule

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();
        }
    }
}
Also used : HashMap(java.util.HashMap) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) BeforeClass(org.junit.BeforeClass) Rule(org.sonar.check.Rule) Test(org.junit.Test)

Example 5 with Rule

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;
}
Also used : RspecKey(org.sonar.java.RspecKey) Rule(org.sonar.check.Rule)

Aggregations

Rule (org.sonar.check.Rule)8 File (java.io.File)6 Test (org.junit.Test)6 AnalyzerMessage (org.sonar.java.AnalyzerMessage)5 BeforeClass (org.junit.BeforeClass)2 FileReader (java.io.FileReader)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FilterableIssue (org.sonar.api.scan.issue.filter.FilterableIssue)1 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)1 RspecKey (org.sonar.java.RspecKey)1 VisitorsBridgeForTests (org.sonar.java.model.VisitorsBridgeForTests)1 JavaCheck (org.sonar.plugins.java.api.JavaCheck)1