diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-01-13 18:28:50 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-01-13 18:28:56 +0100 |
commit | 256c992dd006ded2b7e2a84e0a5a7dfb8be16fe7 (patch) | |
tree | d2224df51c12a622bfdf9f6f81e384fe82ce1129 /sonar-plugin-api | |
parent | 0c67addd5cbe173c2300fd022297fa4c5cb5f30b (diff) | |
download | sonarqube-256c992dd006ded2b7e2a84e0a5a7dfb8be16fe7.tar.gz sonarqube-256c992dd006ded2b7e2a84e0a5a7dfb8be16fe7.zip |
SONAR-4908 add RuleParamType#ofValues()
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java | 51 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java | 35 |
2 files changed, 81 insertions, 5 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java index 5b9599db989..7b7d5285f3f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java @@ -19,21 +19,66 @@ */ package org.sonar.api.rule; +import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.PropertyType; + /** * @since 4.2 */ public final class RuleParamType { + private static final String FIELD_SEPARATOR = "|"; + private static final String OPTION_SEPARATOR = ","; + public static final RuleParamType STRING = new RuleParamType("STRING"); public static final RuleParamType TEXT = new RuleParamType("TEXT"); public static final RuleParamType BOOLEAN = new RuleParamType("BOOLEAN"); public static final RuleParamType INTEGER = new RuleParamType("INTEGER"); - public static final RuleParamType REGULAR_EXPRESSION = new RuleParamType("REGULAR_EXPRESSION"); + private final String type; + private final String[] options; + + // format is "type|comma-separated list of options", for example "INTEGER" or "SINGLE_SELECT_LIST|foo=one,bar,baz=two" private final String key; - private RuleParamType(String key) { - this.key = key; + private RuleParamType(String type, String... options) { + this.type = type; + this.options = options; + StringBuilder sb = new StringBuilder(); + sb.append(type); + if (options.length > 0) { + sb.append(FIELD_SEPARATOR); + for (String option : options) { + sb.append(StringEscapeUtils.escapeCsv(option)); + sb.append(OPTION_SEPARATOR); + } + } + this.key = sb.toString(); + } + + public String type() { + return type; + } + + public String[] options() { + return options; + } + + public static RuleParamType ofValues(String... acceptedValues) { + // reuse the same type as plugin properties in order to + // benefit from shared helpers (validation, HTML component) + String type = PropertyType.SINGLE_SELECT_LIST.name(); + return new RuleParamType(type, acceptedValues); + } + + public static RuleParamType parse(String key) { + String format = StringUtils.substringBefore(key, FIELD_SEPARATOR); + String options = StringUtils.substringAfter(key, FIELD_SEPARATOR); + if (StringUtils.isBlank(options)) { + return new RuleParamType(format); + } + return new RuleParamType(format, options.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")); } @Override diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java index 722c672696f..f78bdba7ffa 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java @@ -24,12 +24,25 @@ import org.junit.Test; import static org.fest.assertions.Assertions.assertThat; public class RuleParamTypeTest { + @Test public void testEquals() throws Exception { + RuleParamType noOptions = RuleParamType.INTEGER; + RuleParamType withOptions1 = RuleParamType.ofValues("one", "two"); + RuleParamType withOptions2 = RuleParamType.ofValues("three", "four"); + assertThat(RuleParamType.INTEGER) .isEqualTo(RuleParamType.INTEGER) .isNotEqualTo(RuleParamType.STRING) .isNotEqualTo("INTEGER") + .isNotEqualTo(withOptions1) + .isNotEqualTo(null); + + assertThat(withOptions1) + .isEqualTo(withOptions1) + .isNotEqualTo(noOptions) + .isNotEqualTo(withOptions2) + .isNotEqualTo("SINGLE_SELECT_LIST|one,two,") .isNotEqualTo(null); } @@ -39,7 +52,25 @@ public class RuleParamTypeTest { } @Test - public void testToString() throws Exception { - assertThat(RuleParamType.INTEGER.toString()).isEqualTo("INTEGER"); + public void testInteger() throws Exception { + RuleParamType type = RuleParamType.INTEGER; + assertThat(type.toString()).isEqualTo("INTEGER"); + assertThat(RuleParamType.parse(type.toString()).type()).isEqualTo("INTEGER"); + assertThat(RuleParamType.parse(type.toString()).options()).isEmpty(); + assertThat(RuleParamType.parse(type.toString()).toString()).isEqualTo("INTEGER"); + } + + @Test + public void testListOfValues() throws Exception { + RuleParamType selectList = RuleParamType.parse("SINGLE_SELECT_LIST|foo,bar,"); + assertThat(selectList.type()).isEqualTo("SINGLE_SELECT_LIST"); + assertThat(selectList.options()).containsOnly("foo", "bar"); + assertThat(selectList.toString()).isEqualTo("SINGLE_SELECT_LIST|foo,bar,"); + + // escape values + selectList = RuleParamType.ofValues("foo", "one,two|three,four"); + assertThat(selectList.type()).isEqualTo("SINGLE_SELECT_LIST"); + assertThat(selectList.options()).containsOnly("foo", "one,two|three,four"); + assertThat(selectList.toString()).isEqualTo("SINGLE_SELECT_LIST|foo,\"one,two|three,four\","); } } |