]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3343 Improve AnnotationRuleParser for better consistency
authorFabrice Bellingard <bellingard@gmail.com>
Wed, 21 Mar 2012 13:43:43 +0000 (14:43 +0100)
committerFabrice Bellingard <bellingard@gmail.com>
Wed, 21 Mar 2012 13:43:43 +0000 (14:43 +0100)
When a rule is annotated, its name and its description fields
should be saved as NULL if they are not not provided.

sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java

index 98554ffbeb8694ae9b99436cf079f0c0619461d3..7a5ba3aea8ab0de7599b195fdf413956a8a53656 100644 (file)
  */
 package org.sonar.api.rules;
 
-import com.google.common.collect.Lists;
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.List;
+
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -27,9 +30,7 @@ import org.sonar.api.ServerComponent;
 import org.sonar.api.utils.AnnotationUtils;
 import org.sonar.check.Check;
 
-import java.lang.reflect.Field;
-import java.util.Collection;
-import java.util.List;
+import com.google.common.collect.Lists;
 
 /**
  * @since 2.3
@@ -61,9 +62,10 @@ 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());
-    String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), ruleKey);
+    String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
+    String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
     Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
-    rule.setDescription(ruleAnnotation.description());
+    rule.setDescription(description);
     rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
     rule.setCardinality(ruleAnnotation.cardinality());
 
index eaf349b04aa72bc0aafd0259cc47854511f9d081..ab71282c48e7f07ea040e9a4c999feb494190c45 100644 (file)
  */
 package org.sonar.api.rules;
 
-import org.junit.Test;
-import org.sonar.check.IsoCategory;
-import org.sonar.check.Priority;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
 
 import java.util.Collections;
 import java.util.List;
 
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import org.junit.Test;
+import org.sonar.check.IsoCategory;
+import org.sonar.check.Priority;
 
 public class AnnotationRuleParserTest {
 
@@ -38,6 +39,7 @@ public class AnnotationRuleParserTest {
     Rule rule = rules.get(0);
     assertThat(rule.getKey(), is("foo"));
     assertThat(rule.getName(), is("bar"));
+    assertThat(rule.getDescription(), is("Foo Bar"));
     assertThat(rule.getSeverity(), is(RulePriority.BLOCKER));
     assertThat(rule.getParams().size(), is(1));
     RuleParam prop = rule.getParam("property");
@@ -47,13 +49,14 @@ public class AnnotationRuleParserTest {
   }
 
   @Test
-  public void ruleWithoutName() {
-    List<Rule> rules = parseAnnotatedClass(RuleWithoutName.class);
+  public void ruleWithoutNameNorDescription() {
+    List<Rule> rules = parseAnnotatedClass(RuleWithoutNameNorDescription.class);
     assertThat(rules.size(), is(1));
     Rule rule = rules.get(0);
     assertThat(rule.getKey(), is("foo"));
-    assertThat(rule.getName(), is("foo"));
     assertThat(rule.getSeverity(), is(RulePriority.MAJOR));
+    assertThat(rule.getName(), is(nullValue()));
+    assertThat(rule.getDescription(), is(nullValue()));
   }
 
   @Test
@@ -63,6 +66,7 @@ public class AnnotationRuleParserTest {
     Rule rule = rules.get(0);
     assertThat(rule.getKey(), is(RuleWithoutKey.class.getCanonicalName()));
     assertThat(rule.getName(), is("foo"));
+    assertThat(rule.getDescription(), is(nullValue()));
     assertThat(rule.getSeverity(), is(RulePriority.MAJOR));
   }
 
@@ -86,10 +90,10 @@ public class AnnotationRuleParserTest {
   }
 
   @org.sonar.check.Rule(key = "foo")
-  private class RuleWithoutName {
+  private class RuleWithoutNameNorDescription {
   }
 
-  @org.sonar.check.Rule(key = "foo", name = "bar", priority = Priority.BLOCKER)
+  @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", priority = Priority.BLOCKER)
   private class RuleWithProperty {
     @org.sonar.check.RuleProperty(description = "Ignore ?", defaultValue = "false")
     String property;