]> source.dussan.org Git - sonarqube.git/commitdiff
Update PropertyDefinition API : rename Builder.build() to Builder.builder(), remove...
authorJulien Lancelot <julien.lancelot@gmail.com>
Tue, 23 Apr 2013 13:39:06 +0000 (15:39 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Tue, 23 Apr 2013 13:39:06 +0000 (15:39 +0200)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/CpdPlugin.java
sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java
sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java

index 965f4149bc9d69bf6fa8a82d5a7821beeccdb0eb..5c3979cb886bb49b9a3845650fd69f4721a174c3 100644 (file)
@@ -35,30 +35,28 @@ public final class CpdPlugin extends SonarPlugin {
 
   public List getExtensions() {
     return ImmutableList.of(
-        PropertyDefinition.build(CoreProperties.CPD_CROSS_RPOJECT)
+        PropertyDefinition.builder(CoreProperties.CPD_CROSS_RPOJECT)
             .defaultValue(CoreProperties.CPD_CROSS_RPOJECT_DEFAULT_VALUE + "")
             .name("Cross project duplication detection")
             .description("SonarQube supports the detection of cross project duplications. Activating this property will slightly increase each Sonar analysis time.")
-            .qualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
-            .global(true)
+            .onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
             .category(CoreProperties.CATEGORY_DUPLICATIONS)
             .type(PropertyType.BOOLEAN)
             .build(),
-        PropertyDefinition.build(CoreProperties.CPD_SKIP_PROPERTY)
+        PropertyDefinition.builder(CoreProperties.CPD_SKIP_PROPERTY)
             .defaultValue("false")
             .name("Skip")
             .description("Disable detection of duplications")
-            .global(false)
+            .hidden()
             .category(CoreProperties.CATEGORY_DUPLICATIONS)
             .type(PropertyType.BOOLEAN)
             .build(),
-        PropertyDefinition.build(CoreProperties.CPD_EXCLUSIONS)
+        PropertyDefinition.builder(CoreProperties.CPD_EXCLUSIONS)
             .defaultValue("")
             .name("Duplication exclusions")
             .description("Patterns used to exclude some source files from the duplication detection mechanism. " +
                 "See the \"Exclusions\" category to know how to use wildcards to specify this property.")
-            .qualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
-            .global(true)
+            .onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
             .category(CoreProperties.CATEGORY_DUPLICATIONS)
             .multiValues(true)
             .build(),
index 7a894739d8c8f8ef2edc38ae78287c81f5fff21f..0c55c02fb3b447b74cd3d89b24a8b50e5d47e2cb 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.api.config;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.math.NumberUtils;
 import org.sonar.api.BatchExtension;
@@ -32,6 +33,7 @@ import org.sonar.api.resources.Qualifiers;
 
 import javax.annotation.Nullable;
 
+import java.util.Arrays;
 import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -85,7 +87,7 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
   /**
    * @since 3.6
    */
-  public static Builder build(String key) {
+  public static Builder builder(String key) {
     return new Builder(key);
   }
 
@@ -93,6 +95,18 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
    * @since 3.6
    */
   static PropertyDefinition create(Property annotation) {
+    Builder builder = PropertyDefinition.builder(annotation.key())
+      .name(annotation.name())
+      .defaultValue(annotation.defaultValue())
+      .description(annotation.description())
+      .category(annotation.category())
+      .type(annotation.type())
+      .options(Arrays.asList(annotation.options()))
+      .multiValues(annotation.multiValues())
+      .propertySetKey(annotation.propertySetKey())
+      .fields(PropertyFieldDefinition.create(annotation.fields()))
+      .deprecatedKey(annotation.deprecatedKey())
+      .index(annotation.index());
     List<String> qualifiers = newArrayList();
     if (annotation.project()) {
       qualifiers.add(Qualifiers.PROJECT);
@@ -100,21 +114,12 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
     if (annotation.module()) {
       qualifiers.add(Qualifiers.MODULE);
     }
-    return PropertyDefinition.build(annotation.key())
-        .name(annotation.name())
-        .defaultValue(annotation.defaultValue())
-        .description(annotation.description())
-        .global(annotation.global())
-        .qualifiers(qualifiers)
-        .category(annotation.category())
-        .type(annotation.type())
-        .options(annotation.options())
-        .multiValues(annotation.multiValues())
-        .propertySetKey(annotation.propertySetKey())
-        .fields(PropertyFieldDefinition.create(annotation.fields()))
-        .deprecatedKey(annotation.deprecatedKey())
-        .index(annotation.index())
-        .build();
+    if (annotation.global()) {
+      builder.onQualifiers(qualifiers);
+    } else {
+      builder.onlyOnQualifiers(qualifiers);
+    }
+    return builder.build();
   }
 
   public static Result validate(PropertyType type, @Nullable String value, List<String> options) {
@@ -291,6 +296,7 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
     private String propertySetKey;
     private List<PropertyFieldDefinition> fields;
     private String deprecatedKey;
+    private boolean hidden;
     private int index;
 
     private Builder(String key) {
@@ -307,6 +313,7 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
       this.qualifiers = newArrayList();
       this.options = newArrayList();
       this.fields = newArrayList();
+      this.hidden = false;
       this.index = 999;
     }
 
@@ -335,18 +342,27 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
       return this;
     }
 
-    public Builder qualifiers(String... qualifiers) {
-      this.qualifiers.addAll(newArrayList(qualifiers));
+    public Builder onQualifiers(String first, String... rest) {
+      this.qualifiers = Lists.asList(first, rest);
+      this.global = true;
+      return this;
+    }
+
+    public Builder onQualifiers(List<String> qualifiers) {
+      this.qualifiers = ImmutableList.copyOf(qualifiers);
+      this.global = true;
       return this;
     }
 
-    public Builder qualifiers(List<String> qualifiers) {
-      this.qualifiers.addAll(qualifiers);
+    public Builder onlyOnQualifiers(String first, String... rest) {
+      this.qualifiers = Lists.asList(first, rest);
+      this.global = false;
       return this;
     }
 
-    public Builder global(boolean global) {
-      this.global = global;
+    public Builder onlyOnQualifiers(List<String> qualifiers) {
+      this.qualifiers = ImmutableList.copyOf(qualifiers);
+      this.global = false;
       return this;
     }
 
@@ -355,8 +371,8 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
       return this;
     }
 
-    public Builder options(String... options) {
-      this.options.addAll(ImmutableList.copyOf(options));
+    public Builder options(String first, String... rest) {
+      this.options.addAll(Lists.asList(first, rest));
       return this;
     }
 
@@ -375,8 +391,8 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
       return this;
     }
 
-    public Builder fields(PropertyFieldDefinition... fields) {
-      this.fields.addAll(ImmutableList.copyOf(fields));
+    public Builder fields(PropertyFieldDefinition first, PropertyFieldDefinition... rest) {
+      this.fields.addAll(Lists.asList(first, rest));
       return this;
     }
 
@@ -390,6 +406,11 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
       return this;
     }
 
+    public Builder hidden() {
+      this.hidden = true;
+      return this;
+    }
+
     public Builder index(int index) {
       this.index = index;
       return this;
@@ -398,6 +419,10 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension
     public PropertyDefinition build() {
       Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Key must be set");
       fixType(key, type);
+      Preconditions.checkArgument(!hidden || qualifiers.isEmpty(), "Cannot be hidden and defining qualifiers on which to display");
+      if (hidden) {
+        global = false;
+      }
       return new PropertyDefinition(this);
     }
 
index b923e6674fd89c0b9ba6666af6df2685214b157a..35ee091cdaaa1fa2e9d6a07e3baa9bc631e4eaae 100644 (file)
@@ -33,15 +33,14 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_create_property() {
-    PropertyDefinition def = PropertyDefinition.build("hello")
+    PropertyDefinition def = PropertyDefinition.builder("hello")
         .name("Hello")
         .defaultValue("world")
         .category("categ")
         .options("de", "en")
         .description("desc")
         .type(PropertyType.FLOAT)
-        .global(false)
-        .qualifiers(Qualifiers.FILE, Qualifiers.CLASS)
+        .onlyOnQualifiers(Qualifiers.FILE, Qualifiers.CLASS)
         .multiValues(true)
         .propertySetKey("set")
         .build();
@@ -81,9 +80,21 @@ public class PropertyDefinitionTest {
     assertThat(def.fields()).isEmpty();
   }
 
+  @Test
+  public void should_create_hidden_property() {
+    PropertyDefinition def = PropertyDefinition.builder("hello")
+      .name("Hello")
+      .hidden()
+      .build();
+
+    assertThat(def.key()).isEqualTo("hello");
+    assertThat(def.qualifiers()).isEmpty();
+    assertThat(def.global()).isFalse();
+  }
+
   @Test
   public void should_create_property_with_default_values() {
-    PropertyDefinition def = PropertyDefinition.build("hello")
+    PropertyDefinition def = PropertyDefinition.builder("hello")
         .name("Hello")
         .build();
 
@@ -124,7 +135,7 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_support_property_sets() {
-    PropertyDefinition def = PropertyDefinition.build("hello")
+    PropertyDefinition def = PropertyDefinition.builder("hello")
         .name("Hello")
         .fields(
             PropertyFieldDefinition.build("first").name("First").description("Description").options("A", "B").build(),
@@ -169,7 +180,7 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_validate_string() {
-    PropertyDefinition def = PropertyDefinition.build("foo").name("foo").type(PropertyType.STRING).build();
+    PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.STRING).build();
 
     assertThat(def.validate(null).isValid()).isTrue();
     assertThat(def.validate("").isValid()).isTrue();
@@ -179,7 +190,7 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_validate_boolean() {
-    PropertyDefinition def = PropertyDefinition.build("foo").name("foo").type(PropertyType.BOOLEAN).build();
+    PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.BOOLEAN).build();
 
     assertThat(def.validate(null).isValid()).isTrue();
     assertThat(def.validate("").isValid()).isTrue();
@@ -193,7 +204,7 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_validate_integer() {
-    PropertyDefinition def = PropertyDefinition.build("foo").name("foo").type(PropertyType.INTEGER).build();
+    PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.INTEGER).build();
 
     assertThat(def.validate(null).isValid()).isTrue();
     assertThat(def.validate("").isValid()).isTrue();
@@ -206,7 +217,7 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_validate_float() {
-    PropertyDefinition def = PropertyDefinition.build("foo").name("foo").type(PropertyType.FLOAT).build();
+    PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.FLOAT).build();
 
     assertThat(def.validate(null).isValid()).isTrue();
     assertThat(def.validate("").isValid()).isTrue();
@@ -220,7 +231,7 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_validate_single_select_list() {
-    PropertyDefinition def = PropertyDefinition.build("foo").name("foo").type(PropertyType.SINGLE_SELECT_LIST).options("de", "en").build();
+    PropertyDefinition def = PropertyDefinition.builder("foo").name("foo").type(PropertyType.SINGLE_SELECT_LIST).options("de", "en").build();
 
     assertThat(def.validate(null).isValid()).isTrue();
     assertThat(def.validate("").isValid()).isTrue();
@@ -234,7 +245,7 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_auto_detect_password_type() {
-    PropertyDefinition def = PropertyDefinition.build("scm.password.secured").name("SCM password").build();
+    PropertyDefinition def = PropertyDefinition.builder("scm.password.secured").name("SCM password").build();
 
     assertThat(def.key()).isEqualTo("scm.password.secured");
     assertThat(def.type()).isEqualTo(PropertyType.PASSWORD);
@@ -242,12 +253,22 @@ public class PropertyDefinitionTest {
 
   @Test
   public void should_auto_detect_license_type() {
-    PropertyDefinition def = PropertyDefinition.build("views.license.secured").name("Views license").build();
+    PropertyDefinition def = PropertyDefinition.builder("views.license.secured").name("Views license").build();
 
     assertThat(def.key()).isEqualTo("views.license.secured");
     assertThat(def.type()).isEqualTo(PropertyType.LICENSE);
   }
 
+  @Test(expected = IllegalArgumentException.class)
+  public void should_not_define_qualifier_and_hidden() {
+    PropertyDefinition.builder("foo").name("foo").onQualifiers(Qualifiers.FILE).hidden().build();
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void should_not_authorise_empty_key() {
+    PropertyDefinition.builder(null).build();
+  }
+
   @Properties(@Property(key = "hello", name = "Hello", defaultValue = "world", description = "desc",
       options = {"de", "en"}, category = "categ", type = PropertyType.FLOAT, global = false, project = true, module = true, multiValues = true, propertySetKey = "set"))
   static class Init {
index 9fe56a5fb396804531a7ce9b0bdcb2d17078717b..f21fb61120649388d8825f0c2652913db29d15e0 100644 (file)
@@ -31,9 +31,9 @@ public class PropertyDefinitionsTest {
   @Test
   public void should_inspect_plugin_objects() {
     PropertyDefinitions def = new PropertyDefinitions(
-        PropertyDefinition.build("foo").name("Foo").build(),
-        PropertyDefinition.build("one").name("One").build(),
-        PropertyDefinition.build("two").name("Two").defaultValue("2").build()
+        PropertyDefinition.builder("foo").name("Foo").build(),
+        PropertyDefinition.builder("one").name("One").build(),
+        PropertyDefinition.builder("two").name("Two").defaultValue("2").build()
         );
 
     assertProperties(def);
@@ -56,8 +56,8 @@ public class PropertyDefinitionsTest {
   @Test
   public void test_categories() {
     PropertyDefinitions def = new PropertyDefinitions(
-        PropertyDefinition.build("inCateg").name("In Categ").category("categ").build(),
-        PropertyDefinition.build("noCateg").name("No categ").build()
+        PropertyDefinition.builder("inCateg").name("In Categ").category("categ").build(),
+        PropertyDefinition.builder("noCateg").name("No categ").build()
         );
 
     assertThat(def.getCategory("inCateg")).isEqualTo("categ");
@@ -75,8 +75,8 @@ public class PropertyDefinitionsTest {
   @Test
   public void test_default_category() {
     PropertyDefinitions def = new PropertyDefinitions();
-    def.addComponent(PropertyDefinition.build("inCateg").name("In Categ").category("categ").build(), "default");
-    def.addComponent(PropertyDefinition.build("noCateg").name("No categ").build(), "default");
+    def.addComponent(PropertyDefinition.builder("inCateg").name("In Categ").category("categ").build(), "default");
+    def.addComponent(PropertyDefinition.builder("noCateg").name("No categ").build(), "default");
 
     assertThat(def.getCategory("inCateg")).isEqualTo("categ");
     assertThat(def.getCategory("noCateg")).isEqualTo("default");
@@ -93,12 +93,12 @@ public class PropertyDefinitionsTest {
   @Test
   public void should_group_by_category() {
     PropertyDefinitions def = new PropertyDefinitions(
-        PropertyDefinition.build("global1").name("Global1").category("catGlobal1").global(true).build(),
-        PropertyDefinition.build("global2").name("Global2").category("catGlobal1").global(true).build(),
-        PropertyDefinition.build("global3").name("Global3").category("catGlobal2").global(true).build(),
-        PropertyDefinition.build("project").name("Project").category("catProject").global(false).qualifiers(Qualifiers.PROJECT).build(),
-        PropertyDefinition.build("module").name("Module").category("catModule").global(false).qualifiers(Qualifiers.MODULE).build(),
-        PropertyDefinition.build("view").name("View").category("catView").global(false).qualifiers(Qualifiers.VIEW).build()
+        PropertyDefinition.builder("global1").name("Global1").category("catGlobal1").build(),
+        PropertyDefinition.builder("global2").name("Global2").category("catGlobal1").build(),
+        PropertyDefinition.builder("global3").name("Global3").category("catGlobal2").build(),
+        PropertyDefinition.builder("project").name("Project").category("catProject").onlyOnQualifiers(Qualifiers.PROJECT).build(),
+        PropertyDefinition.builder("module").name("Module").category("catModule").onlyOnQualifiers(Qualifiers.MODULE).build(),
+        PropertyDefinition.builder("view").name("View").category("catView").onlyOnQualifiers(Qualifiers.VIEW).build()
         );
 
     assertThat(def.getPropertiesByCategory(null).keySet()).containsOnly("catGlobal1", "catGlobal2");
@@ -111,10 +111,10 @@ public class PropertyDefinitionsTest {
   @Test
   public void should_group_by_subcategory() {
     PropertyDefinitions def = new PropertyDefinitions(
-        PropertyDefinition.build("global1").name("Global1").category("catGlobal1").subcategory("sub1").global(true).build(),
-        PropertyDefinition.build("global2").name("Global2").category("catGlobal1").subcategory("sub2").global(true).build(),
-        PropertyDefinition.build("global3").name("Global3").category("catGlobal1").global(true).build(),
-        PropertyDefinition.build("global4").name("Global4").category("catGlobal2").global(true).build()
+        PropertyDefinition.builder("global1").name("Global1").category("catGlobal1").subcategory("sub1").build(),
+        PropertyDefinition.builder("global2").name("Global2").category("catGlobal1").subcategory("sub2").build(),
+        PropertyDefinition.builder("global3").name("Global3").category("catGlobal1").build(),
+        PropertyDefinition.builder("global4").name("Global4").category("catGlobal2").build()
         );
 
     assertThat(def.getPropertiesByCategory(null).get("catGlobal1").keySet()).containsOnly("default", "sub1", "sub2");