]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3879 Replace rule status enumeration in the annotation by a string
authorJulien Lancelot <julien.lancelot@gmail.com>
Fri, 22 Mar 2013 09:49:35 +0000 (10:49 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Fri, 22 Mar 2013 09:49:35 +0000 (10:49 +0100)
sonar-check-api/src/main/java/org/sonar/check/Rule.java
sonar-check-api/src/main/java/org/sonar/check/Status.java [deleted file]
sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java
sonar-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/XMLRuleParserTest.java
sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java
sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java
sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java

index 391ca5a40436db07663e7b93c45dc9254ca7c9ff..d754f488e4ac353bc0e2d9a482efc470201151cb 100644 (file)
@@ -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-check-api/src/main/java/org/sonar/check/Status.java b/sonar-check-api/src/main/java/org/sonar/check/Status.java
deleted file mode 100644 (file)
index 2f74e8b..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * 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;
-}
index 08938291178b43c78d0ab2037f3c7295509334a2..6cf50c12a442a6d83a7022a055b2a71c5a6adbc1 100644 (file)
  */
 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-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java b/sonar-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java
new file mode 100644 (file)
index 0000000..f42bb85
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+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");
+  }
+
+}
index 38f15ea804dbf3eae6e4fbf7c9dfc13a8b40a9ff..440d00a200109d63c0c410bce1ceec5d18df641e 100644 (file)
@@ -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) {
index 8fe75c018ee5766f5a17676878fbbaa521d1efcd..367ffb730dfdce451eb4e60c3975dce3a6bcde8e 100644 (file)
@@ -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;
index 59bd9bf54bd04b28028761cb4589567ef8cee584..918a5e3e1b2d7ee288141da68409740198b60ea5 100644 (file)
@@ -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"));
   }
 }
index 446f2f36b662088c8953498f0da6abae36dd4635..fd014534e447cf397f305bd2465966dd8617ddc0 100644 (file)
@@ -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);
       }
index ca89fe12818b856777cf5fa8a3c9cd26c2dda9a7..c55103c8a0664029e2b20781c7ec0425ce51c207 100644 (file)
@@ -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);
     }
   }
 
index 283e662813c735eb8d4c8e7379b1da3f47eb238d..9ab9526eeebdc9dd2176bc08848b5f5840d9561b 100644 (file)
@@ -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);
   }