aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
blob: 6f5f7c9c9bd3825f82d34858cce15489c4676cbd (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube 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.
 *
 * SonarQube 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.api.rules;

import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.PropertyType;
import org.sonar.api.utils.SonarException;
import org.sonar.check.Priority;

import static org.assertj.core.api.Assertions.assertThat;

public class AnnotationRuleParserTest {

  @org.junit.Rule
  public final ExpectedException exception = ExpectedException.none();

  @Test
  public void rule_with_property() {
    List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class);
    assertThat(rules).hasSize(1);
    Rule rule = rules.get(0);
    assertThat(rule.getKey()).isEqualTo("foo");
    assertThat(rule.getName()).isEqualTo("bar");
    assertThat(rule.getDescription()).isEqualTo("Foo Bar");
    assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
    assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY);
    assertThat(rule.getParams()).hasSize(1);

    RuleParam prop = rule.getParam("property");
    assertThat(prop.getKey()).isEqualTo("property");
    assertThat(prop.getDescription()).isEqualTo("Ignore ?");
    assertThat(prop.getDefaultValue()).isEqualTo("false");
    assertThat(prop.getType()).isEqualTo(PropertyType.STRING.name());
  }

  @Test
  public void rule_with_integer_property() {
    List<Rule> rules = parseAnnotatedClass(RuleWithIntegerProperty.class);

    RuleParam prop = rules.get(0).getParam("property");
    assertThat(prop.getDescription()).isEqualTo("Max");
    assertThat(prop.getDefaultValue()).isEqualTo("12");
    assertThat(prop.getType()).isEqualTo(PropertyType.INTEGER.name());
  }

  @Test
  public void rule_with_text_property() {
    List<Rule> rules = parseAnnotatedClass(RuleWithTextProperty.class);

    RuleParam prop = rules.get(0).getParam("property");
    assertThat(prop.getDescription()).isEqualTo("text");
    assertThat(prop.getDefaultValue()).isEqualTo("Long text");
    assertThat(prop.getType()).isEqualTo(PropertyType.TEXT.name());
  }

  @Test
  public void should_reject_invalid_property_types() {
    exception.expect(SonarException.class);
    exception.expectMessage("Invalid property type [INVALID]");

    parseAnnotatedClass(RuleWithInvalidPropertyType.class);
  }

  @Test
  public void should_recognize_type() {
    assertThat(AnnotationRuleParser.guessType(Integer.class)).isEqualTo(PropertyType.INTEGER);
    assertThat(AnnotationRuleParser.guessType(int.class)).isEqualTo(PropertyType.INTEGER);
    assertThat(AnnotationRuleParser.guessType(Float.class)).isEqualTo(PropertyType.FLOAT);
    assertThat(AnnotationRuleParser.guessType(float.class)).isEqualTo(PropertyType.FLOAT);
    assertThat(AnnotationRuleParser.guessType(Boolean.class)).isEqualTo(PropertyType.BOOLEAN);
    assertThat(AnnotationRuleParser.guessType(boolean.class)).isEqualTo(PropertyType.BOOLEAN);
    assertThat(AnnotationRuleParser.guessType(String.class)).isEqualTo(PropertyType.STRING);
    assertThat(AnnotationRuleParser.guessType(Object.class)).isEqualTo(PropertyType.STRING);
  }

  @Test
  public void rule_without_name_nor_description() {
    List<Rule> rules = parseAnnotatedClass(RuleWithoutNameNorDescription.class);
    assertThat(rules).hasSize(1);
    Rule rule = rules.get(0);
    assertThat(rule.getKey()).isEqualTo("foo");
    assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
    assertThat(rule.getName()).isNull();
    assertThat(rule.getDescription()).isNull();
  }

  @Test
  public void rule_without_key() {
    List<Rule> rules = parseAnnotatedClass(RuleWithoutKey.class);
    assertThat(rules).hasSize(1);
    Rule rule = rules.get(0);
    assertThat(rule.getKey()).isEqualTo(RuleWithoutKey.class.getCanonicalName());
    assertThat(rule.getName()).isEqualTo("foo");
    assertThat(rule.getDescription()).isNull();
    assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
  }

  @Test
  public void overridden_rule() {
    List<Rule> rules = parseAnnotatedClass(OverridingRule.class);
    assertThat(rules).hasSize(1);
    Rule rule = rules.get(0);
    assertThat(rule.getKey()).isEqualTo("overriding_foo");
    assertThat(rule.getName()).isEqualTo("Overriding Foo");
    assertThat(rule.getDescription()).isNull();
    assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
    assertThat(rule.getParams()).hasSize(2);
  }

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

  @org.sonar.check.Rule(name = "foo")
  static class RuleWithoutKey {
  }

  @org.sonar.check.Rule(key = "foo")
  static class RuleWithoutNameNorDescription {
  }

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

  @org.sonar.check.Rule(key = "overriding_foo", name = "Overriding Foo")
  static class OverridingRule extends RuleWithProperty {
    @org.sonar.check.RuleProperty
    private String additionalProperty;
  }

  @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
  static class RuleWithIntegerProperty {
    @org.sonar.check.RuleProperty(description = "Max", defaultValue = "12")
    private Integer property;
  }

  @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
  static class RuleWithTextProperty {
    @org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "TEXT")
    protected String property;
  }

  @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
  static class RuleWithInvalidPropertyType {
    @org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "INVALID")
    public String property;
  }
}