diff options
-rw-r--r-- | sonar-check-api/src/main/java/org/sonar/check/Rule.java | 2 | ||||
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java | 13 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java (renamed from sonar-check-api/src/main/java/org/sonar/check/Status.java) | 28 | ||||
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java | 2 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java | 21 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/rules/XMLRuleParserTest.java | 7 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java | 5 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java | 10 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java | 7 |
9 files changed, 61 insertions, 34 deletions
diff --git a/sonar-check-api/src/main/java/org/sonar/check/Rule.java b/sonar-check-api/src/main/java/org/sonar/check/Rule.java index 391ca5a4043..d754f488e4a 100644 --- a/sonar-check-api/src/main/java/org/sonar/check/Rule.java +++ b/sonar-check-api/src/main/java/org/sonar/check/Rule.java @@ -65,5 +65,5 @@ public @interface Rule { * The rule status. Can be READY, BETA or DEPRECATED * @since 3.6 */ - Status status() default Status.READY; + String status() default "READY"; } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java index 08938291178..6cf50c12a44 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java @@ -19,10 +19,19 @@ */ package org.sonar.core.rule; +import java.util.EnumSet; + public enum RuleStatus { READY, BETA, DEPRECATED, REMOVED; - public static RuleStatus defaultValue() { - return RuleStatus.READY; + public static String defaultValue() { + return RuleStatus.READY.name(); } + + public static final EnumSet<RuleStatus> STATUS_FOR_PLUGIN = EnumSet.range(READY, DEPRECATED); + + public final boolean isAvailableForPlugin() { + return STATUS_FOR_PLUGIN.contains(this); + } + } diff --git a/sonar-check-api/src/main/java/org/sonar/check/Status.java b/sonar-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java index 2f74e8b1b22..f42bb8563f7 100644 --- a/sonar-check-api/src/main/java/org/sonar/check/Status.java +++ b/sonar-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java @@ -17,11 +17,27 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ -package org.sonar.check; -/** - * @since 3.6 - */ -public enum Status { - READY, BETA, DEPRECATED; +package org.sonar.core.rule; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class RuleStatusTest { + + @Test + public void should_validate_status_for_user() { + assertThat(RuleStatus.READY.isAvailableForPlugin()).isTrue(); + assertThat(RuleStatus.BETA.isAvailableForPlugin()).isTrue(); + assertThat(RuleStatus.DEPRECATED.isAvailableForPlugin()).isTrue(); + + assertThat(RuleStatus.REMOVED.isAvailableForPlugin()).isFalse(); + } + + @Test + public void should_return_ready_as_default_value() { + assertThat(RuleStatus.defaultValue()).isEqualTo("READY"); + } + } 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 38f15ea804d..440d00a2001 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 @@ -69,7 +69,7 @@ public final class AnnotationRuleParser implements ServerComponent { rule.setDescription(description); rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority())); rule.setCardinality(ruleAnnotation.cardinality()); - rule.setStatus(ruleAnnotation.status().name()); + rule.setStatus(ruleAnnotation.status()); List<Field> fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { 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 8fe75c018ee..367ffb730df 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 @@ -31,11 +31,12 @@ import java.util.List; import static org.fest.assertions.Assertions.assertThat; public class AnnotationRuleParserTest { + @org.junit.Rule public final ExpectedException exception = ExpectedException.none(); @Test - public void ruleWithProperty() { + public void rule_with_property() { List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); @@ -43,7 +44,9 @@ public class AnnotationRuleParserTest { assertThat(rule.getName()).isEqualTo("bar"); assertThat(rule.getDescription()).isEqualTo("Foo Bar"); assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER); + assertThat(rule.getStatus()).isEqualTo("READY"); assertThat(rule.getParams()).hasSize(1); + RuleParam prop = rule.getParam("property"); assertThat(prop.getKey()).isEqualTo("property"); assertThat(prop.getDescription()).isEqualTo("Ignore ?"); @@ -52,7 +55,7 @@ public class AnnotationRuleParserTest { } @Test - public void ruleWithIntegerProperty() { + public void rule_with_integer_property() { List<Rule> rules = parseAnnotatedClass(RuleWithIntegerProperty.class); RuleParam prop = rules.get(0).getParam("property"); @@ -62,7 +65,7 @@ public class AnnotationRuleParserTest { } @Test - public void ruleWithTextProperty() { + public void rule_with_text_property() { List<Rule> rules = parseAnnotatedClass(RuleWithTextProperty.class); RuleParam prop = rules.get(0).getParam("property"); @@ -92,7 +95,7 @@ public class AnnotationRuleParserTest { } @Test - public void ruleWithoutNameNorDescription() { + public void rule_without_name_nor_description() { List<Rule> rules = parseAnnotatedClass(RuleWithoutNameNorDescription.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); @@ -103,7 +106,7 @@ public class AnnotationRuleParserTest { } @Test - public void ruleWithoutKey() { + public void rule_without_key() { List<Rule> rules = parseAnnotatedClass(RuleWithoutKey.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); @@ -137,7 +140,7 @@ public class AnnotationRuleParserTest { static class RuleWithoutNameNorDescription { } - @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", priority = Priority.BLOCKER) + @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = "READY", priority = Priority.BLOCKER) static class RuleWithProperty { @org.sonar.check.RuleProperty(description = "Ignore ?", defaultValue = "false") private String property; @@ -149,19 +152,19 @@ public class AnnotationRuleParserTest { private String additionalProperty; } - @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", priority = Priority.BLOCKER) + @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = "READY", priority = Priority.BLOCKER) static class RuleWithIntegerProperty { @org.sonar.check.RuleProperty(description = "Max", defaultValue = "12") private Integer property; } - @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", priority = Priority.BLOCKER) + @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = "READY", priority = Priority.BLOCKER) static class RuleWithTextProperty { @org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "TEXT") protected String property; } - @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", priority = Priority.BLOCKER) + @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = "READY", priority = Priority.BLOCKER) static class RuleWithInvalidPropertyType { @org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "INVALID") public String property; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rules/XMLRuleParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rules/XMLRuleParserTest.java index 59bd9bf54bd..918a5e3e1b2 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/rules/XMLRuleParserTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/XMLRuleParserTest.java @@ -24,7 +24,6 @@ import org.junit.Test; import org.sonar.api.PropertyType; import org.sonar.api.utils.SonarException; import org.sonar.check.Cardinality; -import org.sonar.check.Status; import java.io.File; import java.io.StringReader; @@ -131,8 +130,8 @@ public class XMLRuleParserTest { "<rule><key>foo</key><status>BETA</status></rule>"+ "<rule><key>foo</key><status>DEPRECATED</status></rule>"+ "</rules>")); - assertThat(rules.get(0).getStatus(), is(Status.READY.name())); - assertThat(rules.get(1).getStatus(), is(Status.BETA.name())); - assertThat(rules.get(2).getStatus(), is(Status.DEPRECATED.name())); + assertThat(rules.get(0).getStatus(), is("READY")); + assertThat(rules.get(1).getStatus(), is("BETA")); + assertThat(rules.get(2).getStatus(), is("DEPRECATED")); } } diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java b/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java index 446f2f36b66..fd014534e44 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java @@ -31,7 +31,6 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleParam; import org.sonar.api.rules.RulePriority; -import org.sonar.check.Status; import org.sonar.core.rule.RuleStatus; import org.sonar.jpa.dao.RulesDao; @@ -123,12 +122,12 @@ public class RulesBackup implements Backupable { matchingRuleInDb.setDescription(rule.getDescription()); matchingRuleInDb.setSeverity(rule.getSeverity()); matchingRuleInDb.setParams(rule.getParams()); - matchingRuleInDb.setStatus(Status.READY.name()); + matchingRuleInDb.setStatus(RuleStatus.defaultValue()); matchingRuleInDb.setLanguage(rule.getLanguage()); matchingRuleInDb.setUpdatedAt(new Date()); session.save(matchingRuleInDb); } else { - rule.setStatus(Status.READY.name()); + rule.setStatus(RuleStatus.defaultValue()); rule.setCreatedAt(new Date()); session.save(rule); } diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java index ca89fe12818..c55103c8a06 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java @@ -37,7 +37,6 @@ import org.sonar.api.rules.RuleParam; import org.sonar.api.rules.RuleRepository; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.TimeProfiler; -import org.sonar.check.Status; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.core.rule.RuleStatus; import org.sonar.jpa.session.DatabaseSessionFactory; @@ -111,7 +110,7 @@ public final class RegisterRules { for (Rule rule : repository.createRules()) { rule.setRepositoryKey(repository.getKey()); rule.setLanguage(repository.getLanguage()); - rule.setStatus(!Strings.isNullOrEmpty(rule.getStatus()) ? rule.getStatus() : RuleStatus.defaultValue().name()); + rule.setStatus(!Strings.isNullOrEmpty(rule.getStatus()) ? rule.getStatus() : RuleStatus.defaultValue()); validateRule(rule, repository.getKey()); ruleByKey.put(rule.getKey(), rule); registeredRules.add(rule); @@ -156,9 +155,12 @@ public final class RegisterRules { private void validateStatus(Rule rule) { try { - Status.valueOf(rule.getStatus()); + RuleStatus ruleStatus = RuleStatus.valueOf(rule.getStatus()); + if (!ruleStatus.isAvailableForPlugin()) { + throw new IllegalArgumentException(); + } } catch (IllegalArgumentException e) { - throw new SonarException("The status of a rule can only contains : " + Joiner.on(", ").join(Status.values()), e); + throw new SonarException("The status of a rule can only contains : " + Joiner.on(", ").join(RuleStatus.STATUS_FOR_PLUGIN), e); } } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java index 283e662813c..9ab9526eeeb 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java @@ -29,7 +29,6 @@ import org.sonar.api.rules.RuleParam; import org.sonar.api.rules.RulePriority; import org.sonar.api.rules.RuleRepository; import org.sonar.api.utils.SonarException; -import org.sonar.check.Status; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.core.rule.RuleStatus; import org.sonar.jpa.test.AbstractDbUnitTestCase; @@ -80,7 +79,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase { assertThat(first.getRepositoryKey(), is("fake")); assertThat(first.isEnabled(), is(true)); assertThat(first.getCreatedAt(), notNullValue()); - assertThat(first.getStatus(), is(Status.READY.name())); + assertThat(first.getStatus(), is(RuleStatus.READY.name())); assertThat(first.getLanguage(), is("java")); assertThat(first.getParams().size(), is(2)); } @@ -183,7 +182,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase { assertThat(rule1.getUpdatedAt(), notNullValue()); Rule rule2 = getSession().getSingleResult(Rule.class, "id", 2); - assertThat(rule2.getStatus(), is(Status.DEPRECATED.name())); + assertThat(rule2.getStatus(), is(RuleStatus.DEPRECATED.name())); assertThat(rule2.getUpdatedAt(), notNullValue()); } @@ -356,7 +355,7 @@ class FakeRepository extends RuleRepository { Rule rule2 = Rule.create("fake", "rule2", "Two"); rule2.setDescription("Description of Two"); rule2.setSeverity(RulePriority.INFO); - rule2.setStatus(Status.DEPRECATED.name()); + rule2.setStatus(RuleStatus.DEPRECATED.name()); return Arrays.asList(rule1, rule2); } |