@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();
}
*/
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
/**
* Creates a new {@link Builder}
+ *
* @param qualifier
*/
public Builder(String qualifier) {
/**
* 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) {
/**
* 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) {
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.
*/
/**
* Creates a new {@link Builder}
+ *
* @param qualifier
*/
public static Builder builder(String qualifier) {
/**
* Qualifier is the unique key.
- *
+ *
* @return the qualifier
*/
public String getQualifier() {
/**
* 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() {
/**
* 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
*/
/**
* 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
*/
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)
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();
+ }
}