summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabrice Bellingard <bellingard@gmail.com>2012-03-21 14:43:43 +0100
committerFabrice Bellingard <bellingard@gmail.com>2012-03-21 14:43:43 +0100
commit89eddc1fcf964498e7d92ac309b62998cbdd607f (patch)
tree2b18fd4d30075354486413de5d1d26f14164d5e5
parent06a9f756793c6403b4bf67d5b61e86a7b57e404e (diff)
downloadsonarqube-89eddc1fcf964498e7d92ac309b62998cbdd607f.tar.gz
sonarqube-89eddc1fcf964498e7d92ac309b62998cbdd607f.zip
SONAR-3343 Improve AnnotationRuleParser for better consistency
When a rule is annotated, its name and its description fields should be saved as NULL if they are not not provided.
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java14
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java24
2 files changed, 22 insertions, 16 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java
index 98554ffbeb8..7a5ba3aea8a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java
@@ -19,7 +19,10 @@
*/
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());
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
index eaf349b04aa..ab71282c48e 100644
--- 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
@@ -19,15 +19,16 @@
*/
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;