From: Julien Lancelot Date: Wed, 9 Jul 2014 14:12:22 +0000 (+0200) Subject: SONAR-5437 Support colon character in rule keys X-Git-Tag: 4.4-RC3~24 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=46a655cc343aba370396d8af5fbc34d5da668a0a;p=sonarqube.git SONAR-5437 Support colon character in rule keys --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleKey.java b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleKey.java index a474347df74..9d316d5a250 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleKey.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleKey.java @@ -53,9 +53,11 @@ public class RuleKey implements Serializable { * if the format is not valid. */ public static RuleKey parse(String s) { - String[] split = s.split(":"); - Preconditions.checkArgument(split.length == 2, "Invalid rule key: " + s); - return RuleKey.of(split[0], split[1]); + int semiColonPos = s.indexOf(":"); + Preconditions.checkArgument(semiColonPos > 0, "Invalid rule key: " + s); + String key = s.substring(0, semiColonPos); + String repo = s.substring(semiColonPos + 1); + return RuleKey.of(key, repo); } /** diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java index 150ed5efac9..d5190d941ae 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java @@ -25,6 +25,7 @@ import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; public class RuleKeyTest { + @Test public void testOf() throws Exception { RuleKey key = RuleKey.of("squid", "NullDeref"); @@ -32,6 +33,13 @@ public class RuleKeyTest { assertThat(key.rule()).isEqualTo("NullDeref"); } + @Test + public void key_can_contain_colons() throws Exception { + RuleKey key = RuleKey.of("squid", "Key:With:Some::Colons"); + assertThat(key.repository()).isEqualTo("squid"); + assertThat(key.rule()).isEqualTo("Key:With:Some::Colons"); + } + @Test public void repository_must_not_be_null() throws Exception { try { @@ -73,7 +81,7 @@ public class RuleKeyTest { } @Test - public void should_encode_and_decode_string() throws Exception { + public void encode_and_decode_string() throws Exception { RuleKey key = RuleKey.of("squid", "NullDeref"); String serialized = key.toString(); assertThat(serialized).isEqualTo("squid:NullDeref"); @@ -84,7 +92,14 @@ public class RuleKeyTest { } @Test - public void should_not_accept_bad_format() throws Exception { + public void parse_key_with_colons() throws Exception { + RuleKey key = RuleKey.parse("squid:Key:With:Some::Colons"); + assertThat(key.repository()).isEqualTo("squid"); + assertThat(key.rule()).isEqualTo("Key:With:Some::Colons"); + } + + @Test + public void not_accept_bad_format() throws Exception { try { RuleKey.parse("foo"); fail();