*/
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);
}
* 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));
}
/**
import org.sonar.api.rule.RuleKey;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
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");
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");