diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-01-17 17:45:21 +0100 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-01-17 17:48:54 +0100 |
commit | 7e0378b34f77b959d7e1990db7ce02eb7db068e7 (patch) | |
tree | d563792065bf279f7323067d898f8a621ec39692 | |
parent | b2606fb5c1b0268ad2508a81652d73b0a32c1998 (diff) | |
download | sonarqube-7e0378b34f77b959d7e1990db7ce02eb7db068e7.tar.gz sonarqube-7e0378b34f77b959d7e1990db7ce02eb7db068e7.zip |
SONAR-4326 Use regex inspired from StackOverflow to validate tags
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleTagFormat.java | 6 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleTagFormatTest.java | 6 |
2 files changed, 11 insertions, 1 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleTagFormat.java b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleTagFormat.java index 68bdf27c0b4..998a31950d8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleTagFormat.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleTagFormat.java @@ -26,12 +26,16 @@ import org.apache.commons.lang.StringUtils; */ public class RuleTagFormat { + // Allowed characters are the same as those on StackOverflow + // see http://meta.stackoverflow.com/questions/22624/what-symbols-characters-are-not-allowed-in-tags + private static final String VALID_CHARACTERS_REGEX = "^[a-z0-9\\+#\\-\\.]+$"; + private RuleTagFormat() { // only static methods } public static boolean isValid(String tag) { - return StringUtils.isNotBlank(tag) && StringUtils.indexOf(tag, " ") < 0; + return StringUtils.isNotBlank(tag) && tag.matches(VALID_CHARACTERS_REGEX); } public static void validate(String tag) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleTagFormatTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleTagFormatTest.java index c4b9263588c..3197d4eca16 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleTagFormatTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleTagFormatTest.java @@ -31,8 +31,14 @@ public class RuleTagFormatTest { assertThat(RuleTagFormat.isValid("")).isFalse(); assertThat(RuleTagFormat.isValid(" ")).isFalse(); assertThat(RuleTagFormat.isValid("coding style")).isFalse(); + assertThat(RuleTagFormat.isValid("Style")).isFalse(); + assertThat(RuleTagFormat.isValid("sTyle")).isFalse(); + assertThat(RuleTagFormat.isValid("@style")).isFalse(); assertThat(RuleTagFormat.isValid("style")).isTrue(); + assertThat(RuleTagFormat.isValid("c++")).isTrue(); + assertThat(RuleTagFormat.isValid("f#")).isTrue(); + assertThat(RuleTagFormat.isValid("c++11")).isTrue(); } @Test |