]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5526 Support colon character in active rule keys
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 12 Aug 2014 09:25:00 +0000 (11:25 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 12 Aug 2014 09:25:00 +0000 (11:25 +0200)
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java
sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleKeyTest.java

index 4aa7bc2281c2d13773e49270c58a0a6263974eb5..8ecd3cb43e0306d08938a2d14791a6839fb6a5ee 100644 (file)
@@ -43,7 +43,7 @@ public class ActiveRuleKey implements Serializable {
    */
   public static ActiveRuleKey of(String qualityProfileKey, RuleKey ruleKey) {
     Preconditions.checkNotNull(qualityProfileKey, "QProfile is missing");
-    Preconditions.checkNotNull(ruleKey, "RuleKey is missing key");
+    Preconditions.checkNotNull(ruleKey, "RuleKey is missing");
     return new ActiveRuleKey(qualityProfileKey, ruleKey);
   }
 
@@ -52,9 +52,11 @@ public class ActiveRuleKey implements Serializable {
    * if the format is not valid.
    */
   public static ActiveRuleKey parse(String s) {
-    String[] split = s.split(":");
-    Preconditions.checkArgument(split.length == 3, "Bad format of activeRule key: " + s);
-    return ActiveRuleKey.of(split[0], RuleKey.of(split[1], split[2]));
+    Preconditions.checkArgument(s.split(":").length >= 3, "Bad format of activeRule key: " + s);
+    int semiColonPos = s.indexOf(":");
+    String key = s.substring(0, semiColonPos);
+    String ruleKey = s.substring(semiColonPos + 1);
+    return ActiveRuleKey.of(key, RuleKey.parse(ruleKey));
   }
 
   /**
index 10026049cca695d31362dc5d1cf2b36ce1c0d5b4..165ca07afbdb3848925cf5fdf0690ee1070c64f7 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 
 import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
 
 public class ActiveRuleKeyTest {
 
@@ -35,6 +36,35 @@ public class ActiveRuleKeyTest {
     assertThat(key.toString()).isEqualTo("P1:xoo:R1");
   }
 
+  @Test
+  public void rule_key_can_contain_colons() throws Exception {
+    RuleKey ruleKey = RuleKey.of("squid", "Key:With:Some::Colons");
+    ActiveRuleKey key = ActiveRuleKey.of("P1", ruleKey);
+    assertThat(key.qProfile()).isEqualTo("P1");
+    assertThat(key.ruleKey()).isSameAs(ruleKey);
+    assertThat(key.toString()).isEqualTo("P1:squid:Key:With:Some::Colons");
+  }
+
+  @Test
+  public void profile_must_not_be_null() throws Exception {
+    try {
+      ActiveRuleKey.of(null, RuleKey.of("xoo", "R1"));
+      fail();
+    } catch (NullPointerException e) {
+      assertThat(e).hasMessage("QProfile is missing");
+    }
+  }
+
+  @Test
+  public void rule_key_must_not_be_null() throws Exception {
+    try {
+      ActiveRuleKey.of("P1", null);
+      fail();
+    } catch (NullPointerException e) {
+      assertThat(e).hasMessage("RuleKey is missing");
+    }
+  }
+
   @Test
   public void parse() throws Exception {
     ActiveRuleKey key = ActiveRuleKey.parse("P1:xoo:R1");
@@ -43,6 +73,16 @@ public class ActiveRuleKeyTest {
     assertThat(key.ruleKey().rule()).isEqualTo("R1");
   }
 
+  @Test
+  public void parse_fail_when_less_than_three_colons() throws Exception {
+    try {
+      ActiveRuleKey.parse("P1:xoo");
+      fail();
+    } catch (IllegalArgumentException e) {
+      assertThat(e).hasMessage("Bad format of activeRule key: P1:xoo");
+    }
+  }
+
   @Test
   public void equals_and_hashcode() throws Exception {
     ActiveRuleKey key1 = ActiveRuleKey.parse("P1:xoo:R1");