]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4908 add RuleParamType#ofValues()
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 13 Jan 2014 17:28:50 +0000 (18:28 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 13 Jan 2014 17:28:56 +0000 (18:28 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java
sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java

index 5b9599db9890789a9e7a5d35a6e0df1bbdeb895d..7b7d5285f3f85008c3e5ff1ccfd432d1327c2434 100644 (file)
  */
 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
index 722c672696fc9fe8302efe4cb7662619be89da82..f78bdba7ffa69444d9470cf51b0eb98ea874c044 100644 (file)
@@ -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\",");
   }
 }