* 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);
}
/**
import static org.fest.assertions.Fail.fail;
public class RuleKeyTest {
+
@Test
public void testOf() throws Exception {
RuleKey key = RuleKey.of("squid", "NullDeref");
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 {
}
@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");
}
@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();