aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
blob: da3ca48d4c1aa846f54fb317ac09bfb5b5f149f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.sonar.api.rules;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.sonar.check.IsoCategory;
import org.sonar.check.Priority;

public class AnnotationRuleParserTest {

  @Test
  public void ruleWithProperty() {
    List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class);
    assertThat(rules.size(), is(1));
    Rule rule = rules.get(0);
    assertThat(rule.getKey(), is("foo"));
    assertThat(rule.getName(), is("bar"));
    assertThat(rule.getPriority(), is(RulePriority.BLOCKER));
    assertThat(rule.getRulesCategory(), is(Iso9126RulesCategories.MAINTAINABILITY));
    assertThat(rule.getParams().size(), is(1));
    RuleParam prop = rule.getParam("property");
    assertThat(prop.getKey(), is("property"));
    assertThat(prop.getDescription(), is("Ignore ?"));
    assertThat(prop.getDefaultValue(), is("false"));
  }

  @Test
  public void ruleWithoutName() {
    List<Rule> rules = parseAnnotatedClass(RuleWithoutName.class);
    assertThat(rules.size(), is(1));
    Rule rule = rules.get(0);
    assertThat(rule.getKey(), is("foo"));
    assertThat(rule.getName(), is("foo"));
    assertThat(rule.getPriority(), is(RulePriority.MAJOR));
    assertThat(rule.getRulesCategory(), is(Iso9126RulesCategories.MAINTAINABILITY));
  }

  @Test
  public void ruleWithoutKey() {
    List<Rule> rules = parseAnnotatedClass(RuleWithoutKey.class);
    assertThat(rules.size(), is(1));
    Rule rule = rules.get(0);
    assertThat(rule.getKey(), is(RuleWithoutKey.class.getCanonicalName()));
    assertThat(rule.getName(), is("foo"));
    assertThat(rule.getPriority(), is(RulePriority.MAJOR));
    assertThat(rule.getRulesCategory(), is(Iso9126RulesCategories.MAINTAINABILITY));
  }

  @Test
  public void supportDeprecatedAnnotations() {
    List<Rule> rules = parseAnnotatedClass(Check.class);
    assertThat(rules.size(), is(1));
    Rule rule = rules.get(0);
    assertThat(rule.getKey(), is(Check.class.getCanonicalName()));
    assertThat(rule.getName(), is(Check.class.getCanonicalName()));
    assertThat(rule.getDescription(), is("Deprecated check"));
    assertThat(rule.getPriority(), is(RulePriority.BLOCKER));
    assertThat(rule.getRulesCategory(), is(Iso9126RulesCategories.MAINTAINABILITY));
  }

  private List<Rule> parseAnnotatedClass(Class annotatedClass) {
    return new AnnotationRuleParser().parse("repo", Collections.singleton(annotatedClass));
  }

  @org.sonar.check.Rule(name = "foo", isoCategory = IsoCategory.Maintainability)
  private class RuleWithoutKey {
  }

  @org.sonar.check.Rule(key = "foo", isoCategory = IsoCategory.Maintainability)
  private class RuleWithoutName {
  }

  @org.sonar.check.Rule(key = "foo", name = "bar", isoCategory = IsoCategory.Maintainability, priority = Priority.BLOCKER)
  private class RuleWithProperty {
    @org.sonar.check.RuleProperty(description = "Ignore ?", defaultValue = "false")
    String property;
  }

  @org.sonar.check.Check(description = "Deprecated check", isoCategory = IsoCategory.Maintainability, priority = Priority.BLOCKER)
  private class Check {
  }

}