From f295fab3f8650f33dd132fa6060d69f3c0306671 Mon Sep 17 00:00:00 2001 From: Godin Date: Mon, 15 Nov 2010 20:49:44 +0000 Subject: [PATCH] SONAR-1981: If name not specified for rule, then AnnotationRuleParser should set it equal to key --- .../sonar/api/rules/AnnotationRuleParser.java | 6 +- .../api/rules/AnnotationRuleParserTest.java | 87 +++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java 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 b9c25a2165b..ab89fdb1b0c 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 @@ -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 index 00000000000..da3ca48d4c1 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java @@ -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 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 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 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 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 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 { + } + +} -- 2.39.5