aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2013-03-25 13:29:14 +0100
committerJulien Lancelot <julien.lancelot@gmail.com>2013-03-25 13:29:27 +0100
commit8a3488a1de7657410156b825c4cae18832d7b750 (patch)
treecb3fada4725b6ddcfc65374995affd96fb0e81be
parent481f9aa524a1c08ab99f79ee8ae9f87842b601e0 (diff)
downloadsonarqube-8a3488a1de7657410156b825c4cae18832d7b750.tar.gz
sonarqube-8a3488a1de7657410156b825c4cae18832d7b750.zip
SONAR-3879 Moved status constant to Rule. Removed Rule status enumeration.
-rw-r--r--sonar-check-api/src/main/java/org/sonar/check/Rule.java6
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java6
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java37
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/dao/RulesDao.java7
-rw-r--r--sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java5
-rw-r--r--sonar-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java43
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/AbstractLanguage.java2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Java.java14
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java42
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rules/AnnotationRuleParserTest.java10
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTest.java49
-rw-r--r--sonar-server/src/main/java/org/sonar/server/configuration/RulesBackup.java7
-rw-r--r--sonar-server/src/main/java/org/sonar/server/startup/RegisterRules.java42
-rw-r--r--sonar-server/src/test/java/org/sonar/server/startup/RegisterRulesTest.java24
14 files changed, 110 insertions, 184 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 ef62eaa66e4..5c01708bbfe 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
@@ -31,10 +31,6 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE)
public @interface Rule {
- public static final String STATUS_BETA = "BETA";
- public static final String STATUS_DEPRECATED = "DEPRECATED";
- public static final String STATUS_READY = "READY";
-
/**
* The default key is the class name.
*/
@@ -66,7 +62,7 @@ public @interface Rule {
Cardinality cardinality() default Cardinality.SINGLE;
/**
- * The rule status. Can be READY, BETA or DEPRECATED
+ * The status. Can be READY, BETA or DEPRECATED
* @since 3.6
*/
String status() default "READY";
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java
index f38d92cadde..f394dd2cfbe 100644
--- a/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java
+++ b/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java
@@ -50,7 +50,7 @@ public class DefaultRuleFinder implements RuleFinder {
return (Rule) session.getSingleResult(
session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.id=:id and r.status<>:status")
.setParameter("id", ruleId)
- .setParameter("status", RuleStatus.REMOVED.name()
+ .setParameter("status", Rule.STATUS_REMOVED
),
null);
}
@@ -65,7 +65,7 @@ public class DefaultRuleFinder implements RuleFinder {
session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.key=:key and r.pluginName=:pluginName and r.status<>:status")
.setParameter("key", key)
.setParameter("pluginName", repositoryKey)
- .setParameter("status", RuleStatus.REMOVED.name()
+ .setParameter("status", Rule.STATUS_REMOVED
),
null);
}
@@ -84,7 +84,7 @@ public class DefaultRuleFinder implements RuleFinder {
private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where status<>:status ");
Map<String, Object> params = new HashMap<String, Object>();
- params.put("status", RuleStatus.REMOVED.name());
+ params.put("status", Rule.STATUS_REMOVED);
if (StringUtils.isNotBlank(query.getRepositoryKey())) {
hql.append("AND pluginName=:repositoryKey ");
params.put("repositoryKey", query.getRepositoryKey());
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
deleted file mode 100644
index 6cf50c12a44..00000000000
--- a/sonar-core/src/main/java/org/sonar/core/rule/RuleStatus.java
+++ /dev/null
@@ -1,37 +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.core.rule;
-
-import java.util.EnumSet;
-
-public enum RuleStatus {
- READY, BETA, DEPRECATED, REMOVED;
-
- 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/main/java/org/sonar/jpa/dao/RulesDao.java b/sonar-core/src/main/java/org/sonar/jpa/dao/RulesDao.java
index 9915043d217..a994963048b 100644
--- a/sonar-core/src/main/java/org/sonar/jpa/dao/RulesDao.java
+++ b/sonar-core/src/main/java/org/sonar/jpa/dao/RulesDao.java
@@ -22,7 +22,6 @@ package org.sonar.jpa.dao;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleParam;
-import org.sonar.core.rule.RuleStatus;
import java.util.List;
@@ -34,14 +33,14 @@ public class RulesDao extends BaseDao {
public List<Rule> getRules() {
return getSession().createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.status<>:status")
- .setParameter("status", RuleStatus.REMOVED.name())
+ .setParameter("status", Rule.STATUS_REMOVED)
.getResultList();
}
public List<Rule> getRulesByRepository(String repositoryKey) {
return getSession().createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.pluginName=:pluginName and r.status<>:status")
.setParameter("pluginName", repositoryKey)
- .setParameter("status", RuleStatus.REMOVED.name())
+ .setParameter("status", Rule.STATUS_REMOVED)
.getResultList();
}
@@ -59,7 +58,7 @@ public class RulesDao extends BaseDao {
session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.key=:key and r.pluginName=:pluginName and r.status<>:status")
.setParameter("key", ruleKey)
.setParameter("pluginName", repositoryKey)
- .setParameter("status", RuleStatus.REMOVED.name()
+ .setParameter("status", Rule.STATUS_REMOVED
), null);
}
diff --git a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
index 682e4db4fac..64fe2a46344 100644
--- a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
@@ -22,6 +22,7 @@ package org.sonar.core.rule;
import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.rules.Rule;
import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.List;
@@ -47,7 +48,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
assertThat(ruleDto.getId(), Is.is(1L));
assertThat(ruleDto.getName(), Is.is("Avoid Null"));
assertThat(ruleDto.getDescription(), Is.is("Should avoid NULL"));
- assertThat(ruleDto.getStatus(), Is.is(RuleStatus.READY.name()));
+ assertThat(ruleDto.getStatus(), Is.is(Rule.STATUS_READY));
assertThat(ruleDto.getRepositoryKey(), Is.is("checkstyle"));
}
@@ -59,7 +60,7 @@ public class RuleDaoTest extends AbstractDaoTestCase {
assertThat(ruleDto.getId(), Is.is(2L));
assertThat(ruleDto.getName(), Is.is("Avoid Null"));
assertThat(ruleDto.getDescription(), Is.is("Should avoid NULL"));
- assertThat(ruleDto.getStatus(), Is.is(RuleStatus.READY.name()));
+ assertThat(ruleDto.getStatus(), Is.is(Rule.STATUS_READY));
assertThat(ruleDto.getRepositoryKey(), Is.is("checkstyle"));
}
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
deleted file mode 100644
index f42bb8563f7..00000000000
--- a/sonar-core/src/test/java/org/sonar/core/rule/RuleStatusTest.java
+++ /dev/null
@@ -1,43 +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.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/resources/AbstractLanguage.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/AbstractLanguage.java
index e23da992fa5..e56a920a195 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/AbstractLanguage.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/AbstractLanguage.java
@@ -79,7 +79,7 @@ public abstract class AbstractLanguage implements Language {
if (this == o) {
return true;
}
- if (o == null || getClass() != o.getClass()) {
+ if (!(o instanceof Language)) {
return false;
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Java.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Java.java
index 5c243f04dca..d29825bd639 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Java.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Java.java
@@ -76,18 +76,4 @@ public class Java extends AbstractLanguage {
return ArrayUtils.contains(SUFFIXES, suffix);
}
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- // We replace the test equality on classes by test on Language instance in order to keep backward compatibility between this deprecated class and the new one in sonar-java
- if (!(o instanceof Language)) {
- return false;
- }
-
- Language language = (Language) o;
- return !(getKey() != null ? !getKey().equals(language.getKey()) : language.getKey() != null);
- }
-
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java
index e91c724567c..27e9bb00350 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java
@@ -20,12 +20,15 @@
package org.sonar.api.rules;
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableSet;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.database.DatabaseProperties;
+import org.sonar.api.utils.SonarException;
import org.sonar.check.Cardinality;
import javax.persistence.Column;
@@ -45,11 +48,32 @@ import javax.persistence.TemporalType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
+import java.util.Set;
@Entity
@Table(name = "rules")
public final class Rule {
+ /**
+ * @since 3.6
+ */
+ public static final String STATUS_BETA = "BETA";
+ /**
+ * @since 3.6
+ */
+ public static final String STATUS_DEPRECATED = "DEPRECATED";
+ /**
+ * @since 3.6
+ */
+ public static final String STATUS_READY = "READY";
+
+ /**
+ * For internal use only.
+ * @since 3.6
+ */
+ public static final String STATUS_REMOVED = "REMOVED";
+
+
@Id
@Column(name = "id")
@GeneratedValue
@@ -85,7 +109,7 @@ public final class Rule {
private Cardinality cardinality = Cardinality.SINGLE;
@Column(name = "status", updatable = true, nullable = true)
- private String status = "READY";
+ private String status = STATUS_READY;
@Column(name = "language", updatable = true, nullable = true)
private String language;
@@ -253,11 +277,11 @@ public final class Rule {
}
/**
- * @deprecated in 3.6
+ * @deprecated in 3.6. Replaced by {@link #setStatus(String status)}.
*/
@Deprecated
public Rule setEnabled(Boolean enabled) {
- throw new UnsupportedOperationException("No more supported since version 3.6. Please use setStatus() instead.");
+ throw new UnsupportedOperationException("No more supported since version 3.6.");
}
public Boolean isEnabled() {
@@ -305,7 +329,7 @@ public final class Rule {
}
/**
- * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007
+ * @deprecated since 2.5. See http://jira.codehaus.org/browse/SONAR-2007
*/
@Deprecated
public Integer getCategoryId() {
@@ -392,6 +416,9 @@ public final class Rule {
* @since 3.6
*/
public Rule setStatus(String status) {
+ if (!getStatusList().contains(status)) {
+ throw new SonarException("The status of a rule can only contain : " + Joiner.on(", ").join(getStatusList()));
+ }
this.status = status;
return this;
}
@@ -509,4 +536,11 @@ public final class Rule {
public static Rule create(String repositoryKey, String key) {
return new Rule().setUniqueKey(repositoryKey, key);
}
+
+ /**
+ * @since 3.6
+ */
+ public static Set<String> getStatusList() {
+ return ImmutableSet.of(STATUS_READY, STATUS_BETA, STATUS_DEPRECATED, STATUS_REMOVED);
+ }
}
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 985fde99d01..a8465ee0081 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
@@ -44,7 +44,7 @@ public class AnnotationRuleParserTest {
assertThat(rule.getName()).isEqualTo("bar");
assertThat(rule.getDescription()).isEqualTo("Foo Bar");
assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
- assertThat(rule.getStatus()).isEqualTo(org.sonar.check.Rule.STATUS_READY);
+ assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY);
assertThat(rule.getParams()).hasSize(1);
RuleParam prop = rule.getParam("property");
@@ -140,7 +140,7 @@ public class AnnotationRuleParserTest {
static class RuleWithoutNameNorDescription {
}
- @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = org.sonar.check.Rule.STATUS_READY, priority = Priority.BLOCKER)
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.STATUS_READY, priority = Priority.BLOCKER)
static class RuleWithProperty {
@org.sonar.check.RuleProperty(description = "Ignore ?", defaultValue = "false")
private String property;
@@ -152,19 +152,19 @@ public class AnnotationRuleParserTest {
private String additionalProperty;
}
- @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = org.sonar.check.Rule.STATUS_READY, priority = Priority.BLOCKER)
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.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", status = org.sonar.check.Rule.STATUS_READY, priority = Priority.BLOCKER)
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.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", status = org.sonar.check.Rule.STATUS_READY, priority = Priority.BLOCKER)
+ @org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", status = Rule.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/RuleTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTest.java
index e17c6ca5514..d4e12fb4168 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/rules/RuleTest.java
@@ -19,35 +19,32 @@
*/
package org.sonar.api.rules;
-import org.hamcrest.core.Is;
import org.junit.Assert;
import org.junit.Test;
+import org.sonar.api.utils.SonarException;
import java.util.Arrays;
import java.util.List;
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
public class RuleTest {
@Test
public void description_should_be_cleaned() {
- Rule rule = new Rule();
- rule.setDescription(" my description ");
+ Rule rule = Rule.create().setDescription(" my description ");
Assert.assertEquals("my description", rule.getDescription());
rule.setDescription(null);
- assertNull(rule.getDescription());
+ assertThat(rule.getDescription()).isNull();
}
@Test
public void should_remove_new_line_characters_in_name_with_setter() {
- Rule rule = new Rule();
+ Rule rule = Rule.create();
for (String example : getExamplesContainingNewLineCharacter()) {
rule.setName(example);
- assertThat(rule.getName(), is("test"));
+ assertThat(rule.getName()).isEqualTo("test");
}
}
@@ -56,7 +53,7 @@ public class RuleTest {
Rule rule;
for (String example : getExamplesContainingNewLineCharacter()) {
rule = new Rule(null, null).setName(example);
- assertThat(rule.getName(), is("test"));
+ assertThat(rule.getName()).isEqualTo("test");
}
}
@@ -65,23 +62,43 @@ public class RuleTest {
Rule rule;
for (String example : getExamplesContainingNewLineCharacter()) {
rule = new Rule(null, null).setName(example);
- assertThat(rule.getName(), is("test"));
+ assertThat(rule.getName()).isEqualTo("test");
}
}
@Test
public void default_priority_is_major() {
- Rule rule = new Rule();
- assertThat(rule.getSeverity(), Is.is(RulePriority.MAJOR));
+ Rule rule = Rule.create();
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
rule = new Rule("name", "key");
- assertThat(rule.getSeverity(), Is.is(RulePriority.MAJOR));
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
rule.setSeverity(RulePriority.BLOCKER);
- assertThat(rule.getSeverity(), Is.is(RulePriority.BLOCKER));
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
rule.setSeverity(null);
- assertThat(rule.getSeverity(), Is.is(RulePriority.MAJOR));
+ assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
+ }
+
+ @Test(expected = SonarException.class)
+ public void should_not_authorize_unkown_status() {
+ Rule.create().setStatus("Unknown");
+ }
+
+ @Test
+ public void should_set_valid_status() {
+ Rule rule = Rule.create().setStatus(Rule.STATUS_DEPRECATED);
+ assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_DEPRECATED);
+
+ rule = Rule.create().setStatus(Rule.STATUS_REMOVED);
+ assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_REMOVED);
+
+ rule = Rule.create().setStatus(Rule.STATUS_BETA);
+ assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_BETA);
+
+ rule = Rule.create().setStatus(Rule.STATUS_READY);
+ assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY);
}
private List<String> getExamplesContainingNewLineCharacter() {
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 fd014534e44..2d7cfaffd48 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.core.rule.RuleStatus;
import org.sonar.jpa.dao.RulesDao;
import java.util.Collection;
@@ -85,7 +84,7 @@ public class RulesBackup implements Backupable {
private void disableUserRules() {
LoggerFactory.getLogger(getClass()).info("Disable rules created by user");
for (Rule rule : getUserRules()) {
- rule.setStatus(RuleStatus.REMOVED.name());
+ rule.setStatus(Rule.STATUS_REMOVED);
session.save(rule);
}
}
@@ -122,12 +121,12 @@ public class RulesBackup implements Backupable {
matchingRuleInDb.setDescription(rule.getDescription());
matchingRuleInDb.setSeverity(rule.getSeverity());
matchingRuleInDb.setParams(rule.getParams());
- matchingRuleInDb.setStatus(RuleStatus.defaultValue());
+ matchingRuleInDb.setStatus(Rule.STATUS_READY);
matchingRuleInDb.setLanguage(rule.getLanguage());
matchingRuleInDb.setUpdatedAt(new Date());
session.save(matchingRuleInDb);
} else {
- rule.setStatus(RuleStatus.defaultValue());
+ rule.setStatus(Rule.STATUS_READY);
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 c55103c8a06..75a7c2825c8 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
@@ -20,7 +20,6 @@
package org.sonar.server.startup;
-import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.ArrayListMultimap;
@@ -38,7 +37,6 @@ import org.sonar.api.rules.RuleRepository;
import org.sonar.api.utils.SonarException;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.core.i18n.RuleI18nManager;
-import org.sonar.core.rule.RuleStatus;
import org.sonar.jpa.session.DatabaseSessionFactory;
import org.sonar.server.configuration.ProfilesManager;
@@ -93,7 +91,7 @@ public final class RegisterRules {
.getResultList();
}
- private List<Rule> registerRules(RulesByRepository existingRules, TimeProfiler profiler, DatabaseSession session){
+ private List<Rule> registerRules(RulesByRepository existingRules, TimeProfiler profiler, DatabaseSession session) {
List<Rule> registeredRules = newArrayList();
for (RuleRepository repository : repositories) {
profiler.start("Register rules [" + repository.getKey() + "/" + StringUtils.defaultString(repository.getLanguage(), "-") + "]");
@@ -110,7 +108,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());
+ rule.setStatus(!Strings.isNullOrEmpty(rule.getStatus()) ? rule.getStatus() : Rule.STATUS_READY);
validateRule(rule, repository.getKey());
ruleByKey.put(rule.getKey(), rule);
registeredRules.add(rule);
@@ -132,7 +130,6 @@ public final class RegisterRules {
private void validateRule(Rule rule, String repositoryKey) {
validateRuleRepositoryName(rule, repositoryKey);
validateRuleDescription(rule, repositoryKey);
- validateStatus(rule);
}
private void validateRuleRepositoryName(Rule rule, String repositoryKey) {
@@ -153,17 +150,6 @@ public final class RegisterRules {
}
}
- private void validateStatus(Rule rule) {
- try {
- 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(RuleStatus.STATUS_FOR_PLUGIN), e);
- }
- }
-
private void updateRule(Rule persistedRule, Rule rule, DatabaseSession session) {
LOG.debug("Update rule " + rule);
@@ -221,7 +207,7 @@ public final class RegisterRules {
}
private void disableDeprecatedRules(RulesByRepository existingRules, List<Rule> registeredRules, DatabaseSession session) {
- for(Rule rule : existingRules.rules()) {
+ for (Rule rule : existingRules.rules()) {
if (!registeredRules.contains(rule)) {
disable(rule, existingRules, session);
}
@@ -229,7 +215,7 @@ public final class RegisterRules {
}
private void disableDeprecatedRepositories(RulesByRepository existingRules, DatabaseSession session) {
- for(final String repositoryKey : existingRules.repositories()) {
+ for (final String repositoryKey : existingRules.repositories()) {
if (!Iterables.any(repositories, new Predicate<RuleRepository>() {
public boolean apply(RuleRepository input) {
return input.getKey().equals(repositoryKey);
@@ -252,7 +238,7 @@ public final class RegisterRules {
deprecatedUserRuleIds.addAll(session.createQuery(
"SELECT r.id FROM " + Rule.class.getSimpleName() +
" r WHERE r.parent IS NOT NULL AND EXISTS(FROM " + Rule.class.getSimpleName() + " p WHERE r.parent=p and p.status=:status)")
- .setParameter("status", RuleStatus.REMOVED.name())
+ .setParameter("status", Rule.STATUS_REMOVED)
.getResultList());
for (Integer deprecatedUserRuleId : deprecatedUserRuleIds) {
@@ -262,10 +248,10 @@ public final class RegisterRules {
profiler.stop();
}
- private void disable(Rule rule, RulesByRepository existingRules, DatabaseSession session){
- if (!rule.getStatus().equals(RuleStatus.REMOVED.name())) {
+ private void disable(Rule rule, RulesByRepository existingRules, DatabaseSession session) {
+ if (!rule.getStatus().equals(Rule.STATUS_REMOVED)) {
LOG.debug("Disable rule " + rule);
- rule.setStatus(RuleStatus.REMOVED.name());
+ rule.setStatus(Rule.STATUS_REMOVED);
rule.setUpdatedAt(new Date());
session.saveWithoutFlush(rule);
existingRules.addRuleToRemove(rule);
@@ -287,14 +273,14 @@ public final class RegisterRules {
rulesToRemove = newArrayList();
}
- public RulesByRepository(List<Rule> rules){
+ public RulesByRepository(List<Rule> rules) {
this();
- for (Rule rule :rules) {
+ for (Rule rule : rules) {
ruleRepositoryList.put(rule.getRepositoryKey(), rule);
}
}
- public void add(Rule rule){
+ public void add(Rule rule) {
ruleRepositoryList.put(rule.getRepositoryKey(), rule);
}
@@ -302,15 +288,15 @@ public final class RegisterRules {
return ruleRepositoryList.get(repositoryKey);
}
- public Collection<String> repositories(){
+ public Collection<String> repositories() {
return ruleRepositoryList.keySet();
}
- public Collection<Rule> rules(){
+ public Collection<Rule> rules() {
return ruleRepositoryList.values();
}
- public void addRuleToRemove(Rule rule){
+ public void addRuleToRemove(Rule rule) {
rulesToRemove.add(rule);
}
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 9ab9526eeeb..841d4822cbd 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
@@ -30,7 +30,6 @@ import org.sonar.api.rules.RulePriority;
import org.sonar.api.rules.RuleRepository;
import org.sonar.api.utils.SonarException;
import org.sonar.core.i18n.RuleI18nManager;
-import org.sonar.core.rule.RuleStatus;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import org.sonar.server.configuration.ProfilesManager;
@@ -79,7 +78,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
assertThat(first.getRepositoryKey(), is("fake"));
assertThat(first.isEnabled(), is(true));
assertThat(first.getCreatedAt(), notNullValue());
- assertThat(first.getStatus(), is(RuleStatus.READY.name()));
+ assertThat(first.getStatus(), is(Rule.STATUS_READY));
assertThat(first.getLanguage(), is("java"));
assertThat(first.getParams().size(), is(2));
}
@@ -113,7 +112,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
task.start();
Rule rule = getSession().getSingleResult(Rule.class, "id", 1);
- assertThat(rule.getStatus(), is(RuleStatus.READY.name()));
+ assertThat(rule.getStatus(), is(Rule.STATUS_READY));
assertThat(rule.getUpdatedAt(), notNullValue());
}
@@ -123,7 +122,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
task.start();
Rule rule = getSession().getSingleResult(Rule.class, "id", 1);
- assertThat(rule.getStatus(), is(RuleStatus.REMOVED.name()));
+ assertThat(rule.getStatus(), is(Rule.STATUS_REMOVED));
assertThat(rule.getUpdatedAt(), nullValue());
}
@@ -182,7 +181,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
assertThat(rule1.getUpdatedAt(), notNullValue());
Rule rule2 = getSession().getSingleResult(Rule.class, "id", 2);
- assertThat(rule2.getStatus(), is(RuleStatus.DEPRECATED.name()));
+ assertThat(rule2.getStatus(), is(Rule.STATUS_DEPRECATED));
assertThat(rule2.getUpdatedAt(), notNullValue());
}
@@ -244,7 +243,7 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
setupData("shared");
task.start();
- List<Rule> result = getSession().getResults(Rule.class, "status", RuleStatus.READY.name());
+ List<Rule> result = getSession().getResults(Rule.class, "status", Rule.STATUS_READY);
assertThat(result.size(), is(VolumeRepository.SIZE));
}
@@ -325,17 +324,6 @@ public class RegisterRulesTest extends AbstractDbUnitTestCase {
}
}
- // http://jira.codehaus.org/browse/SONAR-3879
- @Test
- public void should_fail_with_rule_with_unknown_status() throws Exception {
- task = new RegisterRules(getSessionFactory(), new RuleRepository[] {new RuleWithUnkownStatusRepository()}, null, profilesManager);
- try {
- task.start();
- fail("Rule status must be unknown");
- } catch (SonarException e) {
- assertThat(e.getMessage(), containsString("The status of a rule can only contains : "));
- }
- }
}
class FakeRepository extends RuleRepository {
@@ -355,7 +343,7 @@ class FakeRepository extends RuleRepository {
Rule rule2 = Rule.create("fake", "rule2", "Two");
rule2.setDescription("Description of Two");
rule2.setSeverity(RulePriority.INFO);
- rule2.setStatus(RuleStatus.DEPRECATED.name());
+ rule2.setStatus(Rule.STATUS_DEPRECATED);
return Arrays.asList(rule1, rule2);
}