From 2d6a169ec764e30415520ddb51d15506a0727501 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 12 Aug 2014 11:25:00 +0200 Subject: [PATCH] SONAR-5526 Support colon character in active rule keys --- .../core/qualityprofile/db/ActiveRuleKey.java | 10 +++-- .../qualityprofile/db/ActiveRuleKeyTest.java | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java index 4aa7bc2281c..8ecd3cb43e0 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java @@ -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)); } /** diff --git a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleKeyTest.java b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleKeyTest.java index 10026049cca..165ca07afbd 100644 --- a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleKeyTest.java +++ b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/ActiveRuleKeyTest.java @@ -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"); -- 2.39.5