From f295fab3f8650f33dd132fa6060d69f3c0306671 Mon Sep 17 00:00:00 2001 From: Godin Date: Mon, 15 Nov 2010 20:49:44 +0000 Subject: SONAR-1981: If name not specified for rule, then AnnotationRuleParser should set it equal to key --- .../sonar/api/rules/AnnotationRuleParserTest.java | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java (limited to 'sonar-plugin-api/src/test/java') 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 { + } + +} -- cgit v1.2.3