]> source.dussan.org Git - sonarqube.git/commitdiff
API: add ResourceType#setProperty(String,boolean) + remove some deprecated methods
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 6 Jul 2012 12:54:55 +0000 (14:54 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 6 Jul 2012 12:57:14 +0000 (14:57 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/DefaultResourceTypes.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceType.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypeTest.java

index cbd4aa570f250ab0d0610cc37b9ea560a511ca74..a05b3e65ef800842d5e1120ca5cacad43685071f 100644 (file)
@@ -30,33 +30,31 @@ import org.sonar.api.resources.ResourceTypeTree;
 @InstantiationStrategy(InstantiationStrategy.PER_BATCH)
 public final class DefaultResourceTypes extends ExtensionProvider implements BatchExtension, ServerExtension {
 
-  private static final String TRUE = "true";
-
   @Override
   public ResourceTypeTree provide() {
     return ResourceTypeTree.builder()
 
-        .addType(ResourceType.builder(Qualifiers.PROJECT)
-            .setProperty("deletable", TRUE)
-            .setProperty("modifiable_history", TRUE)
-            .setProperty("hasRolePolicy", TRUE)
-            .setProperty("updatable_key", TRUE)
-            .build())
-        .addType(ResourceType.builder(Qualifiers.MODULE)
-            .setProperty("updatable_key", TRUE)
-            .build())
-        .addType(ResourceType.builder(Qualifiers.DIRECTORY).build())
-        .addType(ResourceType.builder(Qualifiers.PACKAGE).build())
-        .addType(ResourceType.builder(Qualifiers.FILE).hasSourceCode().build())
-        .addType(ResourceType.builder(Qualifiers.CLASS).hasSourceCode().build())
-        .addType(ResourceType.builder(Qualifiers.UNIT_TEST_FILE).hasSourceCode().build())
-
-        .addRelations(Qualifiers.PROJECT, Qualifiers.MODULE)
-        .addRelations(Qualifiers.MODULE, Qualifiers.DIRECTORY, Qualifiers.PACKAGE)
-        .addRelations(Qualifiers.DIRECTORY, Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE)
-        .addRelations(Qualifiers.PACKAGE, Qualifiers.CLASS, Qualifiers.UNIT_TEST_FILE)
-
-        .build();
+      .addType(ResourceType.builder(Qualifiers.PROJECT)
+        .setProperty("deletable", true)
+        .setProperty("modifiable_history", true)
+        .setProperty("hasRolePolicy", true)
+        .setProperty("updatable_key", true)
+        .build())
+      .addType(ResourceType.builder(Qualifiers.MODULE)
+        .setProperty("updatable_key", true)
+        .build())
+      .addType(ResourceType.builder(Qualifiers.DIRECTORY).build())
+      .addType(ResourceType.builder(Qualifiers.PACKAGE).build())
+      .addType(ResourceType.builder(Qualifiers.FILE).hasSourceCode().build())
+      .addType(ResourceType.builder(Qualifiers.CLASS).hasSourceCode().build())
+      .addType(ResourceType.builder(Qualifiers.UNIT_TEST_FILE).hasSourceCode().build())
+
+      .addRelations(Qualifiers.PROJECT, Qualifiers.MODULE)
+      .addRelations(Qualifiers.MODULE, Qualifiers.DIRECTORY, Qualifiers.PACKAGE)
+      .addRelations(Qualifiers.DIRECTORY, Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE)
+      .addRelations(Qualifiers.PACKAGE, Qualifiers.CLASS, Qualifiers.UNIT_TEST_FILE)
+
+      .build();
 
   }
 
index 368e9f0e2493dc90200c411073651317597de0be..e5444ca5452b10cc668edf971df2877ad5082432 100644 (file)
  */
 package org.sonar.api.resources;
 
-import java.util.Map;
-
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.Immutable;
-
 import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import com.google.common.collect.Maps;
 
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.Immutable;
+import java.util.Map;
+
 /**
  * <p>Experimental extension to declare types of resources.</p>
  * <p>
  * Since 3.0, ResourceType object can declare properties that give information about the capabilities of the
- * resource type. Those properties may be used, of instance, to adapt the Web UI according to the type of 
+ * resource type. Those properties may be used, of instance, to adapt the Web UI according to the type of
  * the resource being displayed.
  * <br>
  * Currently, the following properties can be defined:
  * </p>
  * <ul>
- *   <li>"deletable": if set to "true", then this resource can be deleted/purged.</li>
- *   <li>"availableForFilters": if set to "true", then this resource can be displayed in the filters results</li>
- *   <li>"modifiable_history": if set to "true", then the history of this resource may be modified (deletion of snapshots, modification of events, ...)</li>
+ * <li>"deletable": if set to "true", then this resource can be deleted/purged.</li>
+ * <li>"availableForFilters": if set to "true", then this resource can be displayed in the filters results</li>
+ * <li>"modifiable_history": if set to "true", then the history of this resource may be modified (deletion of snapshots, modification of events, ...)</li>
  * </ul>
  *
  * @since 2.14
@@ -61,6 +60,7 @@ public final class ResourceType {
 
     /**
      * Creates a new {@link Builder}
+     *
      * @param qualifier
      */
     public Builder(String qualifier) {
@@ -69,7 +69,7 @@ public final class ResourceType {
 
     /**
      * Relative path of the icon used to represent the resource type.
-     * 
+     *
      * @param iconPath path to icon, relative to context of web-application (e.g. "/images/q/DIR.png")
      */
     public Builder setIconPath(@Nullable String iconPath) {
@@ -96,7 +96,7 @@ public final class ResourceType {
 
     /**
      * Sets a property on the resource type. See the description of {@link ResourceType} class for more information.
-     * 
+     *
      * @since 3.0
      */
     public Builder setProperty(String key, String value) {
@@ -106,6 +106,13 @@ public final class ResourceType {
       return this;
     }
 
+    /**
+     * @since 3.2
+     */
+    public Builder setProperty(String key, boolean value) {
+      return setProperty(key, String.valueOf(value));
+    }
+
     /**
      * Creates an instance of {@link ResourceType} based on all information given to the builder.
      */
@@ -119,6 +126,7 @@ public final class ResourceType {
 
   /**
    * Creates a new {@link Builder}
+   *
    * @param qualifier
    */
   public static Builder builder(String qualifier) {
@@ -141,7 +149,7 @@ public final class ResourceType {
 
   /**
    * Qualifier is the unique key.
-   * 
+   *
    * @return the qualifier
    */
   public String getQualifier() {
@@ -150,25 +158,16 @@ public final class ResourceType {
 
   /**
    * Returns the relative path of the icon used to represent the resource type
-   * 
+   *
    * @return the relative path.
    */
   public String getIconPath() {
     return iconPath;
   }
 
-  /**
-   * @deprecated since 3.0. Use {@link #getBooleanProperty(String)} with "availableForFilters".
-   */
-  @Deprecated
-  public boolean isAvailableForFilters() {
-    Boolean availableForFilters = getBooleanProperty("availableForFilters");
-    return availableForFilters == null ? false : availableForFilters.booleanValue();
-  }
-
   /**
    * Tells whether resources of this type has source code or not.
-   * 
+   *
    * @return true if the type has source code
    */
   public boolean hasSourceCode() {
@@ -177,7 +176,7 @@ public final class ResourceType {
 
   /**
    * Returns the value of the property for this resource type.
-   * 
+   *
    * @return the String value of the property, or NULL if the property hasn't been set.
    * @since 3.0
    */
@@ -188,13 +187,14 @@ public final class ResourceType {
 
   /**
    * Returns the value of the property for this resource type.
-   * 
+   *
    * @return the Boolean value of the property. If the property hasn't been set, False is returned.
    * @since 3.0
    */
-  public Boolean getBooleanProperty(String key) {
+  public boolean getBooleanProperty(String key) {
     Preconditions.checkNotNull(key);
-    return Boolean.valueOf(properties.get(key));
+    String value = properties.get(key);
+    return value != null && Boolean.parseBoolean(value);
   }
 
   @Override
index b7a040852a3061c1ab27e0b0bfa6dc3d05049653..998017b75f8a8049287e319dd8762cfe105654fe 100644 (file)
  */
 package org.sonar.api.resources;
 
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
 import org.junit.Test;
 
+import static org.fest.assertions.Assertions.assertThat;
+
+
 public class ResourceTypeTest {
 
   @Test
   public void shouldCreateWithDefaults() {
     ResourceType def = ResourceType.builder("qualifier")
-        .build();
-    assertThat(def.getQualifier(), is("qualifier"));
-    assertThat(def.getIconPath(), is("/images/q/qualifier.png"));
-    assertThat(def.hasSourceCode(), is(false));
+      .build();
+    assertThat(def.getQualifier()).isEqualTo("qualifier");
+    assertThat(def.getIconPath()).isEqualTo("/images/q/qualifier.png");
+    assertThat(def.hasSourceCode()).isFalse();
   }
 
   @Test
   public void shouldCreate() {
     ResourceType def = ResourceType.builder("qualifier")
-        .setIconPath("/custom-icon.png")
-        .hasSourceCode()
-        .setProperty("availableForFilters", "true")
-        .setProperty("anotherProperty", "foo")
-        .build();
-    assertThat(def.getQualifier(), is("qualifier"));
-    assertThat(def.getIconPath(), is("/custom-icon.png"));
-    assertThat(def.hasSourceCode(), is(true));
-    assertThat(def.getBooleanProperty("availableForFilters"), is(true));
-    assertThat(def.getStringProperty("anotherProperty"), is("foo"));
+      .setIconPath("/custom-icon.png")
+      .hasSourceCode()
+      .setProperty("availableForFilters", "true")
+      .setProperty("anotherProperty", "foo")
+      .build();
+    assertThat(def.getQualifier()).isEqualTo("qualifier");
+    assertThat(def.getIconPath()).isEqualTo("/custom-icon.png");
+    assertThat(def.hasSourceCode()).isTrue();
+    assertThat(def.getBooleanProperty("availableForFilters")).isTrue();
+    assertThat(def.getStringProperty("anotherProperty")).isEqualTo("foo");
   }
 
   @Test(expected = IllegalArgumentException.class)
@@ -61,26 +61,41 @@ public class ResourceTypeTest {
     ResourceType foo2 = ResourceType.builder("FOO").build();
     ResourceType bar = ResourceType.builder("BAR").build();
 
-    assertThat(foo1.equals(foo1), is(true));
-    assertThat(foo1.equals(foo2), is(true));
-    assertThat(foo1.equals(bar), is(false));
+    assertThat(foo1.equals(foo1)).isTrue();
+    assertThat(foo1.equals(foo2)).isTrue();
+    assertThat(foo1.equals(bar)).isFalse();
 
-    assertThat(foo1.hashCode(), is(foo1.hashCode()));
+    assertThat(foo1.hashCode()).isEqualTo(foo1.hashCode());
   }
 
   @Test
   public void testDeprecatedIsAvailableForFiltesCompatibility() {
-    // test getter
-    ResourceType def = ResourceType.builder("qualifier")
-        .setProperty("availableForFilters", "true")
-        .build();
-    assertThat(def.isAvailableForFilters(), is(true));
-
-    // test setter on Builder
-    def = ResourceType.builder("qualifier")
-        .availableForFilters()
-        .build();
-    assertThat(def.getBooleanProperty("availableForFilters"), is(true));
+    ResourceType def = ResourceType.builder("qualifier").build();
+    assertThat(def.getBooleanProperty("availableForFilters")).isFalse();
+
+    def = ResourceType.builder("qualifier").availableForFilters().build();
+    assertThat(def.getBooleanProperty("availableForFilters")).isTrue();
   }
 
+  @Test
+  public void getBooleanProperty_is_set() {
+    // set with boolean parameter
+    ResourceType def = ResourceType.builder("qualifier").setProperty("availableForFilters", true).build();
+    assertThat(def.getBooleanProperty("availableForFilters")).isTrue();
+
+    def = ResourceType.builder("qualifier").setProperty("availableForFilters", false).build();
+    assertThat(def.getBooleanProperty("availableForFilters")).isFalse();
+
+    def = ResourceType.builder("qualifier").setProperty("availableForFilters", "true").build();
+    assertThat(def.getBooleanProperty("availableForFilters")).isTrue();
+
+    def = ResourceType.builder("qualifier").setProperty("availableForFilters", "false").build();
+    assertThat(def.getBooleanProperty("availableForFilters")).isFalse();
+  }
+
+  @Test
+  public void getBooleanProperty_is_not_set() {
+    ResourceType def = ResourceType.builder("qualifier").build();
+    assertThat(def.getBooleanProperty("availableForFilters")).isFalse();
+  }
 }