From 3c972b4cb543743a3bc5e2bd3c7d1b581e66dd32 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 16 Jul 2013 15:05:53 +0200 Subject: SONAR-4501 Make properties (sub)categories case insensitives --- .../main/java/org/sonar/api/CoreProperties.java | 5 ++ .../main/java/org/sonar/api/config/Category.java | 73 ++++++++++++++++++++++ .../org/sonar/api/config/PropertyDefinitions.java | 51 +++++++++++++-- .../java/org/sonar/api/config/SubCategory.java | 35 +++++++++++ .../java/org/sonar/api/config/CategoryTest.java | 37 +++++++++++ .../sonar/api/config/PropertyDefinitionsTest.java | 64 +++++++++++++------ 6 files changed, 240 insertions(+), 25 deletions(-) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/config/Category.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/config/SubCategory.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/config/CategoryTest.java (limited to 'sonar-plugin-api/src') diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java index 084cabc0d63..027b5d9a4e8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/CoreProperties.java @@ -72,6 +72,11 @@ public interface CoreProperties { */ String CATEGORY_EXCLUSIONS = "exclusions"; + /** + * @since 3.7 + */ + String CATEGORY_LICENSES = "licenses"; + /* Global settings */ String SONAR_HOME = "SONAR_HOME"; String PROJECT_BRANCH_PROPERTY = "sonar.branch"; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Category.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Category.java new file mode 100644 index 00000000000..ddb8ff34367 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Category.java @@ -0,0 +1,73 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.config; + +import org.apache.commons.lang.StringUtils; + +import java.util.Locale; + +/** + * @since 3.7 + */ +public class Category { + + private final String originalKey; + private final boolean special; + + public Category(String originalKey) { + this(originalKey, false); + } + + Category(String originalKey, boolean special) { + this.originalKey = originalKey; + this.special = special; + } + + public String originalKey() { + return originalKey; + } + + public String key() { + return StringUtils.lowerCase(originalKey, Locale.ENGLISH); + } + + public boolean isSpecial() { + return special; + } + + @Override + public int hashCode() { + return key().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Category)) { + return false; + } + return StringUtils.equalsIgnoreCase(((Category) obj).originalKey, this.originalKey); + } + + @Override + public String toString() { + return this.originalKey; + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java index b729e1064af..94edae487ff 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java @@ -23,6 +23,7 @@ import com.google.common.base.Strings; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; +import org.sonar.api.CoreProperties; import org.sonar.api.Properties; import org.sonar.api.Property; import org.sonar.api.ServerComponent; @@ -44,8 +45,8 @@ import java.util.Map; public final class PropertyDefinitions implements BatchComponent, ServerComponent { private final Map definitions = Maps.newHashMap(); - private final Map categories = Maps.newHashMap(); - private final Map subcategories = Maps.newHashMap(); + private final Map categories = Maps.newHashMap(); + private final Map subcategories = Maps.newHashMap(); // deprecated key -> new key private final Map deprecatedKeys = Maps.newHashMap(); @@ -107,8 +108,9 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen if (!definitions.containsKey(definition.key())) { definitions.put(definition.key(), definition); String category = StringUtils.defaultIfBlank(definition.category(), defaultCategory); - categories.put(definition.key(), category); - subcategories.put(definition.key(), StringUtils.defaultIfBlank(definition.subCategory(), category)); + categories.put(definition.key(), new Category(category)); + String subcategory = StringUtils.defaultIfBlank(definition.subCategory(), category); + subcategories.put(definition.key(), new SubCategory(subcategory)); if (!Strings.isNullOrEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) { deprecatedKeys.put(definition.deprecatedKey(), definition.key()); } @@ -130,7 +132,9 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen /** * @since 3.6 + * @deprecated since 3.7 use {@link #propertiesByCategory(String)} */ + @Deprecated public Map>> getPropertiesByCategory(@Nullable String qualifier) { Map>> byCategory = new HashMap>>(); @@ -151,10 +155,45 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen return byCategory; } + /** + * @since 3.6 + * @deprecated since 3.7 use {@link #propertiesByCategory(String)} + */ + @Deprecated public Map>> getPropertiesByCategory() { return getPropertiesByCategory(null); } + /** + * @since 3.7 + */ + public Map>> propertiesByCategory(@Nullable String qualifier) { + Map>> byCategory = new HashMap>>(); + if (qualifier == null) { + // Special categories on global page + byCategory.put(new Category("email", true), new HashMap>()); + byCategory.put(new Category("encryption", true), new HashMap>()); + HashMap> licenseSubCategories = new HashMap>(); + licenseSubCategories.put(new SubCategory("server_id", true), new ArrayList()); + byCategory.put(new Category(CoreProperties.CATEGORY_LICENSES, false), licenseSubCategories); + } + for (PropertyDefinition definition : getAll()) { + if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) { + Category category = categories.get(definition.key()); + if (!byCategory.containsKey(category)) { + byCategory.put(category, new HashMap>()); + } + SubCategory subCategory = subcategories.get(definition.key()); + if (!byCategory.get(category).containsKey(subCategory)) { + byCategory.get(category).put(subCategory, new ArrayList()); + } + byCategory.get(category).get(subCategory).add(definition); + } + } + + return byCategory; + } + public String getDefaultValue(String key) { PropertyDefinition def = get(key); if (def == null) { @@ -164,11 +203,11 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen } public String getCategory(String key) { - return categories.get(validKey(key)); + return categories.get(validKey(key)).toString(); } public String getSubCategory(String key) { - return subcategories.get(validKey(key)); + return subcategories.get(validKey(key)).toString(); } public String getCategory(Property prop) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/SubCategory.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/SubCategory.java new file mode 100644 index 00000000000..cd90b20e2f0 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/SubCategory.java @@ -0,0 +1,35 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.config; + +/** + * @since 3.7 + */ +public class SubCategory extends Category { + + public SubCategory(String originalKey) { + super(originalKey); + } + + SubCategory(String originalKey, boolean special) { + super(originalKey, special); + } + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/CategoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/CategoryTest.java new file mode 100644 index 00000000000..e8ee207892f --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/CategoryTest.java @@ -0,0 +1,37 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.config; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class CategoryTest { + + @Test + public void category_key_is_case_insentive() { + assertThat(new Category("Licenses")).isEqualTo(new Category("licenses")); + } + + @Test + public void should_preserve_original_key() { + assertThat(new Category("Licenses").originalKey()).isEqualTo("Licenses"); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java index ed794b4dc8c..9bc2eace66b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java @@ -34,10 +34,10 @@ public class PropertyDefinitionsTest { @Test public void should_build_with_predefined_list_of_definitions() { List list = Arrays.asList( - PropertyDefinition.builder("foo").name("Foo").build(), - PropertyDefinition.builder("one").name("One").build(), - PropertyDefinition.builder("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() + ); PropertyDefinitions def = new PropertyDefinitions(list); assertProperties(def); @@ -46,9 +46,9 @@ public class PropertyDefinitionsTest { @Test public void should_inspect_plugin_objects() { PropertyDefinitions def = new PropertyDefinitions( - PropertyDefinition.builder("foo").name("Foo").build(), - PropertyDefinition.builder("one").name("One").build(), - PropertyDefinition.builder("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); @@ -71,8 +71,8 @@ public class PropertyDefinitionsTest { @Test public void test_categories() { PropertyDefinitions def = new PropertyDefinitions( - PropertyDefinition.builder("inCateg").name("In Categ").category("categ").build(), - PropertyDefinition.builder("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"); @@ -105,15 +105,25 @@ public class PropertyDefinitionsTest { assertThat(def.getCategory("noCateg")).isEqualTo("default"); } + @Test + public void should_return_special_categories() { + PropertyDefinitions def = new PropertyDefinitions(); + + assertThat(def.propertiesByCategory(null).keySet()).containsOnly(new Category("encryption"), new Category("email"), new Category("licenses")); + assertThat(def.propertiesByCategory(null).keySet().iterator().next().isSpecial()).isTrue(); + assertThat(def.propertiesByCategory(null).get(new Category("licenses")).keySet()).containsOnly(new SubCategory("server_id")); + assertThat(def.propertiesByCategory(null).get(new Category("licenses")).keySet().iterator().next().isSpecial()).isTrue(); + } + @Test public void should_group_by_category() { PropertyDefinitions def = new PropertyDefinitions( - 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() + 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"); @@ -121,19 +131,30 @@ public class PropertyDefinitionsTest { assertThat(def.getPropertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly("catModule"); assertThat(def.getPropertiesByCategory(Qualifiers.VIEW).keySet()).containsOnly("catView"); assertThat(def.getPropertiesByCategory("Unkown").keySet()).isEmpty(); + + assertThat(def.propertiesByCategory(null).keySet()).containsOnly(new Category("encryption"), new Category("email"), new Category("licenses"), new Category("catGlobal1"), + new Category("catGlobal2")); + assertThat(def.propertiesByCategory(Qualifiers.PROJECT).keySet()).containsOnly(new Category("catProject")); + assertThat(def.propertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly(new Category("catModule")); + assertThat(def.propertiesByCategory(Qualifiers.VIEW).keySet()).containsOnly(new Category("catView")); + assertThat(def.propertiesByCategory("Unkown").keySet()).isEmpty(); } @Test public void should_group_by_subcategory() { PropertyDefinitions def = new PropertyDefinitions( - 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() + 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("catGlobal1", "sub1", "sub2"); assertThat(def.getPropertiesByCategory(null).get("catGlobal2").keySet()).containsOnly("catGlobal2"); + + assertThat(def.propertiesByCategory(null).get(new Category("catGlobal1")).keySet()).containsOnly(new SubCategory("catGlobal1"), new SubCategory("sub1"), + new SubCategory("sub2")); + assertThat(def.propertiesByCategory(null).get(new Category("catGlobal2")).keySet()).containsOnly(new SubCategory("catGlobal2")); } @Test @@ -143,6 +164,11 @@ public class PropertyDefinitionsTest { assertThat(def.getPropertiesByCategory().keySet()).containsOnly("catGlobal1", "catGlobal2"); assertThat(def.getPropertiesByCategory(Qualifiers.PROJECT).keySet()).containsOnly("catProject"); assertThat(def.getPropertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly("catModule"); + + assertThat(def.propertiesByCategory(null).keySet()).containsOnly(new Category("encryption"), new Category("email"), new Category("licenses"), new Category("catglobal1"), + new Category("catglobal2")); + assertThat(def.propertiesByCategory(Qualifiers.PROJECT).keySet()).containsOnly(new Category("catproject")); + assertThat(def.propertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly(new Category("catmodule")); } private void assertProperties(PropertyDefinitions definitions) { -- cgit v1.2.3