aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-03-01 21:27:20 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-03-02 10:40:13 +0100
commit13ea3c2d0b37a90f78d4a1639611ff9186bd9613 (patch)
tree36b1f9c7d4f4c3807549bb9cd3823d6d07f37593 /sonar-plugin-api
parente4dac55db3e3224be660cbd648022bf005194751 (diff)
downloadsonarqube-13ea3c2d0b37a90f78d4a1639611ff9186bd9613.tar.gz
sonarqube-13ea3c2d0b37a90f78d4a1639611ff9186bd9613.zip
SONAR-7267 Do not accept multiple rules with the same key in a repository
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java14
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java12
2 files changed, 13 insertions, 13 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
index d55538662e0..c4b0b275cab 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
@@ -20,6 +20,7 @@
package org.sonar.api.server.rule;
import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -46,7 +47,7 @@ import org.sonar.api.server.ServerSide;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.utils.log.Loggers;
-import static com.google.common.base.Objects.firstNonNull;
+import static com.google.common.base.Preconditions.*;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format;
@@ -444,7 +445,9 @@ public interface RulesDefinition {
interface NewExtendedRepository {
/**
- * Create a rule with specified key. Max length of key is 200 characters.
+ * Create a rule with specified key. Max length of key is 200 characters. Key must be unique
+ * among the repository
+ * @throws IllegalArgumentException is key is not unique. Note a warning was logged up to version 5.4 (included)
*/
NewRule createRule(String ruleKey);
@@ -490,12 +493,7 @@ public interface RulesDefinition {
@Override
public NewRule createRule(String ruleKey) {
- if (newRules.containsKey(ruleKey)) {
- // Should fail in a perfect world, but at the time being the Findbugs plugin
- // defines several times the rule EC_INCOMPATIBLE_ARRAY_COMPARE
- // See http://jira.sonarsource.com/browse/SONARJAVA-428
- Loggers.get(getClass()).warn(format("The rule '%s' of repository '%s' is declared several times", ruleKey, key));
- }
+ checkArgument(!newRules.containsKey(ruleKey), "The rule '%s' of repository '%s' is declared several times", ruleKey, key);
NewRule newRule = new NewRule(key, ruleKey);
newRules.put(ruleKey, newRule);
return newRule;
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
index c8b90f23eac..49b0bd9896c 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
@@ -22,11 +22,11 @@ package org.sonar.api.server.rule;
import java.net.URL;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.utils.log.LogTester;
-import org.sonar.api.utils.log.LoggerLevel;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@@ -38,6 +38,9 @@ public class RulesDefinitionTest {
@Rule
public LogTester logTester = new LogTester();
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
@Test
public void define_repositories() {
assertThat(context.repositories()).isEmpty();
@@ -257,13 +260,12 @@ public class RulesDefinitionTest {
}
@Test
- public void warning_if_duplicated_rule_keys() {
+ public void fail_if_duplicated_rule_keys_in_the_same_repository() {
+ expectedException.expect(IllegalArgumentException.class);
+
RulesDefinition.NewRepository findbugs = context.createRepository("findbugs", "java");
findbugs.createRule("NPE");
findbugs.createRule("NPE");
- // do not fail as long as http://jira.sonarsource.com/browse/SONARJAVA-428 is not fixed
- // and as common-rules are packaged within plugins (common-rules were integrated to core in v5.2)
- assertThat(logTester.logs(LoggerLevel.WARN)).hasSize(1);
}
@Test