]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1981: If name not specified for rule, then AnnotationRuleParser should set...
authorGodin <mandrikov@gmail.com>
Mon, 15 Nov 2010 20:49:44 +0000 (20:49 +0000)
committerGodin <mandrikov@gmail.com>
Mon, 15 Nov 2010 20:49:44 +0000 (20:49 +0000)
sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java [new file with mode: 0644]

index b9c25a2165b00bc864de625a8f30eee046bb8f34..ab89fdb1b0c8ba1733b61d96d8afae7324860b08 100644 (file)
@@ -61,7 +61,8 @@ public final class AnnotationRuleParser implements ServerComponent {
 
   private Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) {
     String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
-    Rule rule = Rule.create(repositoryKey, ruleKey, ruleAnnotation.name());
+    String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), ruleKey);
+    Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
     rule.setDescription(ruleAnnotation.description());
     rule.setRulesCategory(RulesCategory.fromIsoCategory(ruleAnnotation.isoCategory()));
     rule.setPriority(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
@@ -79,7 +80,8 @@ public final class AnnotationRuleParser implements ServerComponent {
 
   private Rule toRule(String repositoryKey, Class clazz, Check checkAnnotation) {
     String ruleKey = StringUtils.defaultIfEmpty(checkAnnotation.key(), clazz.getCanonicalName());
-    Rule rule = Rule.create(repositoryKey, ruleKey, checkAnnotation.title());
+    String ruleName = StringUtils.defaultIfEmpty(checkAnnotation.title(), ruleKey);
+    Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
     rule.setDescription(checkAnnotation.description());
     rule.setRulesCategory(RulesCategory.fromIsoCategory(checkAnnotation.isoCategory()));
     rule.setPriority(RulePriority.fromCheckPriority(checkAnnotation.priority()));
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
new file mode 100644 (file)
index 0000000..da3ca48
--- /dev/null
@@ -0,0 +1,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 {
+  }
+
+}