From cf4c5fe91ce78cc4cba28c8fa342e1edf7e0cf76 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 25 Apr 2013 09:04:27 +0200 Subject: Revert Rename PropertyDefinitions to PropertyDefs and PropertyFieldDefinition to PropertyFieldDef --- .../java/org/sonar/plugins/cpd/CpdSensorTest.java | 4 +- .../plugins/dbcleaner/DefaultPurgeTaskTest.java | 6 +- .../org/sonar/batch/bootstrap/BatchSettings.java | 6 +- .../sonar/batch/bootstrap/BatchSettingsTest.java | 18 +-- .../org/sonar/batch/scan/ModuleSettingsTest.java | 4 +- .../java/org/sonar/api/config/PropertyDef.java | 12 +- .../org/sonar/api/config/PropertyDefinitions.java | 180 +++++++++++++++++++++ .../java/org/sonar/api/config/PropertyDefs.java | 180 --------------------- .../org/sonar/api/config/PropertyFieldDef.java | 154 ------------------ .../sonar/api/config/PropertyFieldDefinition.java | 154 ++++++++++++++++++ .../main/java/org/sonar/api/config/Settings.java | 10 +- .../org/sonar/api/platform/ComponentContainer.java | 12 +- .../java/org/sonar/api/config/PropertyDefTest.java | 4 +- .../sonar/api/config/PropertyDefinitionsTest.java | 172 ++++++++++++++++++++ .../org/sonar/api/config/PropertyDefsTest.java | 172 -------------------- .../java/org/sonar/api/config/SettingsTest.java | 4 +- .../sonar/api/platform/ComponentContainerTest.java | 16 +- .../condition/HasProjectPropertyConditionTest.java | 4 +- .../org/sonar/server/platform/ServerSettings.java | 6 +- .../startup/RenameDeprecatedPropertyKeys.java | 6 +- .../main/java/org/sonar/server/ui/JRubyFacade.java | 6 +- .../server/platform/PersistentSettingsTest.java | 4 +- .../sonar/server/platform/ServerSettingsTest.java | 14 +- .../startup/RenameDeprecatedPropertyKeysTest.java | 4 +- 24 files changed, 576 insertions(+), 576 deletions(-) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefs.java delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDef.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefsTest.java diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java index ed3038909ea..26d5637373f 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/CpdSensorTest.java @@ -22,7 +22,7 @@ package org.sonar.plugins.cpd; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Before; import org.junit.Test; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.api.resources.Java; import org.sonar.api.resources.Language; @@ -44,7 +44,7 @@ public class CpdSensorTest { IndexFactory indexFactory = mock(IndexFactory.class); sonarEngine = new SonarEngine(indexFactory, null, null, null); sonarBridgeEngine = new SonarBridgeEngine(indexFactory, null, null); - settings = new Settings(new PropertyDefs(CpdPlugin.class)); + settings = new Settings(new PropertyDefinitions(CpdPlugin.class)); sensor = new CpdSensor(sonarEngine, sonarBridgeEngine, settings); } diff --git a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java index 38443e3097a..1afad9051d8 100644 --- a/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java +++ b/plugins/sonar-dbcleaner-plugin/src/test/java/org/sonar/plugins/dbcleaner/DefaultPurgeTaskTest.java @@ -21,7 +21,7 @@ package org.sonar.plugins.dbcleaner; import org.junit.Test; import org.sonar.api.CoreProperties; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.api.resources.Scopes; import org.sonar.core.purge.PurgeDao; @@ -37,7 +37,7 @@ public class DefaultPurgeTaskTest { @Test public void shouldNotDeleteHistoricalDataOfDirectories() { PurgeDao purgeDao = mock(PurgeDao.class); - Settings settings = new Settings(new PropertyDefs(DefaultPurgeTask.class)); + Settings settings = new Settings(new PropertyDefinitions(DefaultPurgeTask.class)); settings.setProperty(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, "false"); DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class), mock(PurgeProfiler.class)); @@ -49,7 +49,7 @@ public class DefaultPurgeTaskTest { @Test public void shouldDeleteHistoricalDataOfDirectoriesByDefault() { PurgeDao purgeDao = mock(PurgeDao.class); - Settings settings = new Settings(new PropertyDefs(DefaultPurgeTask.class)); + Settings settings = new Settings(new PropertyDefinitions(DefaultPurgeTask.class)); DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, settings, mock(DefaultPeriodCleaner.class), mock(PurgeProfiler.class)); task.purge(1L); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java index 1bedd4b6b99..72d038aa58d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchSettings.java @@ -26,7 +26,7 @@ import org.json.simple.JSONValue; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import javax.annotation.Nullable; @@ -44,9 +44,9 @@ public class BatchSettings extends Settings { private final BootstrapSettings bootstrapSettings; private final ServerClient client; - public BatchSettings(BootstrapSettings bootstrapSettings, PropertyDefs propertyDefs, + public BatchSettings(BootstrapSettings bootstrapSettings, PropertyDefinitions propertyDefinitions, ServerClient client, Configuration deprecatedConfiguration) { - super(propertyDefs); + super(propertyDefinitions); this.bootstrapSettings = bootstrapSettings; this.client = client; this.deprecatedConfiguration = deprecatedConfiguration; diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java index 870a88002b2..7ac5b3ba19b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchSettingsTest.java @@ -23,7 +23,7 @@ import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; import org.junit.Test; import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import java.util.Collections; import java.util.Map; @@ -52,7 +52,7 @@ public class BatchSettingsTest { // Reconstruct bootstrap settings to get system property bootstrapSettings = new BootstrapSettings(new BootstrapProperties(Collections. emptyMap())); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getString("BatchSettingsTest.testSystemProp")).isEqualTo("system"); } @@ -63,7 +63,7 @@ public class BatchSettingsTest { when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); project.setProperty("project.prop", "project"); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); batchSettings.init(project); assertThat(batchSettings.getString("project.prop")).isEqualTo("project"); @@ -73,7 +73,7 @@ public class BatchSettingsTest { public void should_load_global_settings() { when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getBoolean("sonar.cpd.cross")).isTrue(); } @@ -83,7 +83,7 @@ public class BatchSettingsTest { when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); batchSettings.init(project); assertThat(batchSettings.getString("sonar.java.coveragePlugin")).isEqualTo("jacoco"); @@ -94,7 +94,7 @@ public class BatchSettingsTest { when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); batchSettings.init(project); Map moduleSettings = batchSettings.getModuleProperties("struts-core"); @@ -109,7 +109,7 @@ public class BatchSettingsTest { System.setProperty("BatchSettingsTest.testSystemProp", "system"); project.setProperty("BatchSettingsTest.testSystemProp", "build"); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getString("BatchSettingsTest.testSystemProp")).isEqualTo("system"); } @@ -119,7 +119,7 @@ public class BatchSettingsTest { when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); when(client.request("/batch_bootstrap/properties?project=struts")).thenReturn(REACTOR_JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); batchSettings.init(project); assertThat(deprecatedConf.getString("sonar.cpd.cross")).isEqualTo("true"); @@ -137,7 +137,7 @@ public class BatchSettingsTest { @Test public void project_should_be_optional() { when(client.request("/batch_bootstrap/properties")).thenReturn(JSON_RESPONSE); - BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefs(), client, deprecatedConf); + BatchSettings batchSettings = new BatchSettings(bootstrapSettings, new PropertyDefinitions(), client, deprecatedConf); assertThat(batchSettings.getProperties()).isNotEmpty(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java index 1207c93e7f8..e3487f6dd79 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ModuleSettingsTest.java @@ -24,7 +24,7 @@ import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Test; import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.batch.bootstrap.BatchSettings; import java.util.List; @@ -52,7 +52,7 @@ public class ModuleSettingsTest { @Test public void test_loading_of_module_settings() { BatchSettings batchSettings = mock(BatchSettings.class); - when(batchSettings.getDefinitions()).thenReturn(new PropertyDefs()); + when(batchSettings.getDefinitions()).thenReturn(new PropertyDefinitions()); when(batchSettings.getProperties()).thenReturn(ImmutableMap.of( "overridding", "batch", "on-batch", "true" diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDef.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDef.java index a8736d58661..4c8db9efb3b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDef.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDef.java @@ -72,7 +72,7 @@ public final class PropertyDef implements BatchExtension, ServerExtension { private boolean multiValues; private String propertySetKey; private String deprecatedKey; - private List fields; + private List fields; private String subcategory; private int index; @@ -109,7 +109,7 @@ public final class PropertyDef implements BatchExtension, ServerExtension { .options(Arrays.asList(annotation.options())) .multiValues(annotation.multiValues()) .propertySetKey(annotation.propertySetKey()) - .fields(PropertyFieldDef.create(annotation.fields())) + .fields(PropertyFieldDefinition.create(annotation.fields())) .deprecatedKey(annotation.deprecatedKey()); List qualifiers = newArrayList(); if (annotation.project()) { @@ -227,7 +227,7 @@ public final class PropertyDef implements BatchExtension, ServerExtension { return propertySetKey; } - public List fields() { + public List fields() { return fields; } @@ -280,7 +280,7 @@ public final class PropertyDef implements BatchExtension, ServerExtension { private List options = newArrayList(); private boolean multiValues = false; private String propertySetKey = ""; - private List fields = newArrayList(); + private List fields = newArrayList(); private String deprecatedKey = ""; private boolean hidden = false; private int index = 999; @@ -363,12 +363,12 @@ public final class PropertyDef implements BatchExtension, ServerExtension { return this; } - public Builder fields(PropertyFieldDef first, PropertyFieldDef... rest) { + public Builder fields(PropertyFieldDefinition first, PropertyFieldDefinition... rest) { this.fields.addAll(Lists.asList(first, rest)); return this; } - public Builder fields(List fields) { + public Builder fields(List fields) { this.fields.addAll(ImmutableList.copyOf(fields)); return this; } 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 new file mode 100644 index 00000000000..452ba25f82e --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java @@ -0,0 +1,180 @@ +/* + * 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 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.Properties; +import org.sonar.api.Property; +import org.sonar.api.ServerComponent; +import org.sonar.api.utils.AnnotationUtils; + +import javax.annotation.Nullable; + +import java.util.*; + +/** + * Metadata of all the properties declared by plugins + * + * @since 2.12 + */ +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(); + + // deprecated key -> new key + private final Map deprecatedKeys = Maps.newHashMap(); + + public PropertyDefinitions(Object... components) { + if (components != null) { + addComponents(Arrays.asList(components)); + } + } + + public PropertyDefinitions addComponents(Collection components) { + return addComponents(components, ""); + } + + public PropertyDefinitions addComponents(Collection components, String defaultCategory) { + for (Object component : components) { + addComponent(component, defaultCategory); + } + return this; + } + + public PropertyDefinitions addComponent(Object object) { + return addComponent(object, ""); + } + + public PropertyDefinitions addComponent(Object component, String defaultCategory) { + addComponentFromAnnotationProperty(component, defaultCategory); + if (component instanceof PropertyDef) { + PropertyDef propertyDefinition = (PropertyDef) component; + add(propertyDefinition, defaultCategory); + } + return this; + } + + private PropertyDefinitions addComponentFromAnnotationProperty(Object component, String defaultCategory) { + Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class); + if (annotations != null) { + for (Property property : annotations.value()) { + addProperty(property, defaultCategory); + } + } + Property annotation = AnnotationUtils.getAnnotation(component, Property.class); + if (annotation != null) { + addProperty(annotation, defaultCategory); + } + return this; + } + + private PropertyDefinitions addProperty(Property property, String defaultCategory) { + PropertyDef definition = PropertyDef.create(property); + return add(definition, defaultCategory); + } + + private PropertyDefinitions add(PropertyDef definition, String defaultCategory) { + if (!definitions.containsKey(definition.key())) { + definitions.put(definition.key(), definition); + categories.put(definition.key(), StringUtils.defaultIfBlank(definition.category(), defaultCategory)); + subcategories.put(definition.key(), StringUtils.defaultIfBlank(definition.subcategory(), "default")); + if (!Strings.isNullOrEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) { + deprecatedKeys.put(definition.deprecatedKey(), definition.key()); + } + } + return this; + } + + public PropertyDef get(String key) { + return definitions.get(validKey(key)); + } + + public Collection getAll() { + return definitions.values(); + } + + public String validKey(String key) { + return StringUtils.defaultString(deprecatedKeys.get(key), key); + } + + /** + * @since 3.6 + */ + public Map>> getPropertiesByCategory(@Nullable String qualifier) { + Map>> byCategory = new HashMap>>(); + + for (PropertyDef definition : getAll()) { + if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) { + String category = getCategory(definition.key()); + if (!byCategory.containsKey(category)) { + byCategory.put(category, new HashMap>()); + } + String subCategory = getSubCategory(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 Map>> getPropertiesByCategory() { + return getPropertiesByCategory(null); + } + + public String getDefaultValue(String key) { + PropertyDef def = get(key); + if (def == null) { + return null; + } + return StringUtils.defaultIfEmpty(def.defaultValue(), null); + } + + public String getCategory(String key) { + return categories.get(validKey(key)); + } + + public String getSubCategory(String key) { + return subcategories.get(validKey(key)); + } + + public String getCategory(Property prop) { + return getCategory(prop.key()); + } + + public String getNewKey(String deprecatedKey) { + return deprecatedKeys.get(deprecatedKey); + } + + public String getDeprecatedKey(String key) { + PropertyDef def = get(key); + if (def == null) { + return null; + } + return StringUtils.defaultIfEmpty(def.deprecatedKey(), null); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefs.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefs.java deleted file mode 100644 index a8a55ede663..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefs.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * 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 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.Properties; -import org.sonar.api.Property; -import org.sonar.api.ServerComponent; -import org.sonar.api.utils.AnnotationUtils; - -import javax.annotation.Nullable; - -import java.util.*; - -/** - * Metadata of all the properties declared by plugins - * - * @since 2.12 - */ -public final class PropertyDefs implements BatchComponent, ServerComponent { - - private final Map definitions = Maps.newHashMap(); - private final Map categories = Maps.newHashMap(); - private final Map subcategories = Maps.newHashMap(); - - // deprecated key -> new key - private final Map deprecatedKeys = Maps.newHashMap(); - - public PropertyDefs(Object... components) { - if (components != null) { - addComponents(Arrays.asList(components)); - } - } - - public PropertyDefs addComponents(Collection components) { - return addComponents(components, ""); - } - - public PropertyDefs addComponents(Collection components, String defaultCategory) { - for (Object component : components) { - addComponent(component, defaultCategory); - } - return this; - } - - public PropertyDefs addComponent(Object object) { - return addComponent(object, ""); - } - - public PropertyDefs addComponent(Object component, String defaultCategory) { - addComponentFromAnnotationProperty(component, defaultCategory); - if (component instanceof PropertyDef) { - PropertyDef propertyDefinition = (PropertyDef) component; - add(propertyDefinition, defaultCategory); - } - return this; - } - - private PropertyDefs addComponentFromAnnotationProperty(Object component, String defaultCategory) { - Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class); - if (annotations != null) { - for (Property property : annotations.value()) { - addProperty(property, defaultCategory); - } - } - Property annotation = AnnotationUtils.getAnnotation(component, Property.class); - if (annotation != null) { - addProperty(annotation, defaultCategory); - } - return this; - } - - private PropertyDefs addProperty(Property property, String defaultCategory) { - PropertyDef definition = PropertyDef.create(property); - return add(definition, defaultCategory); - } - - private PropertyDefs add(PropertyDef definition, String defaultCategory) { - if (!definitions.containsKey(definition.key())) { - definitions.put(definition.key(), definition); - categories.put(definition.key(), StringUtils.defaultIfBlank(definition.category(), defaultCategory)); - subcategories.put(definition.key(), StringUtils.defaultIfBlank(definition.subcategory(), "default")); - if (!Strings.isNullOrEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) { - deprecatedKeys.put(definition.deprecatedKey(), definition.key()); - } - } - return this; - } - - public PropertyDef get(String key) { - return definitions.get(validKey(key)); - } - - public Collection getAll() { - return definitions.values(); - } - - public String validKey(String key) { - return StringUtils.defaultString(deprecatedKeys.get(key), key); - } - - /** - * @since 3.6 - */ - public Map>> getPropertiesByCategory(@Nullable String qualifier) { - Map>> byCategory = new HashMap>>(); - - for (PropertyDef definition : getAll()) { - if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) { - String category = getCategory(definition.key()); - if (!byCategory.containsKey(category)) { - byCategory.put(category, new HashMap>()); - } - String subCategory = getSubCategory(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 Map>> getPropertiesByCategory() { - return getPropertiesByCategory(null); - } - - public String getDefaultValue(String key) { - PropertyDef def = get(key); - if (def == null) { - return null; - } - return StringUtils.defaultIfEmpty(def.defaultValue(), null); - } - - public String getCategory(String key) { - return categories.get(validKey(key)); - } - - public String getSubCategory(String key) { - return subcategories.get(validKey(key)); - } - - public String getCategory(Property prop) { - return getCategory(prop.key()); - } - - public String getNewKey(String deprecatedKey) { - return deprecatedKeys.get(deprecatedKey); - } - - public String getDeprecatedKey(String key) { - PropertyDef def = get(key); - if (def == null) { - return null; - } - return StringUtils.defaultIfEmpty(def.deprecatedKey(), null); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDef.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDef.java deleted file mode 100644 index f9928122df5..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDef.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * 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 com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.common.collect.ImmutableList; -import org.sonar.api.PropertyField; -import org.sonar.api.PropertyType; - -import javax.annotation.Nullable; - -import java.util.List; - -import static com.google.common.collect.Lists.newArrayList; - -/** - * @since 3.3 - */ -public final class PropertyFieldDef { - private final String key; - private final String name; - private final String description; - private final int indicativeSize; - private final PropertyType type; - private final List options; - - private PropertyFieldDef(Builder builder) { - this.key = builder.key; - this.name = builder.name; - this.description = builder.description; - this.indicativeSize = builder.indicativeSize; - this.type = builder.type; - this.options = builder.options; - } - - static List create(PropertyField[] fields) { - List definitions = newArrayList(); - for (PropertyField field : fields) { - definitions.add(PropertyFieldDef.build(field.key()) - .name(field.name()) - .description(field.description()) - .indicativeSize(field.indicativeSize()) - .type(field.type()) - .options(field.options()) - .build() - ); - } - return definitions; - } - - public static Builder build(String key) { - return new Builder(key); - } - - public String key() { - return key; - } - - public String name() { - return name; - } - - public String description() { - return description; - } - - public int indicativeSize() { - return indicativeSize; - } - - public PropertyType type() { - return type; - } - - public List options() { - return options; - } - - public PropertyDef.Result validate(@Nullable String value) { - return PropertyDef.validate(type, value, options); - } - - public static class Builder { - private String key; - private String name; - private String description; - private int indicativeSize; - private PropertyType type; - private List options; - - private Builder(String key) { - this.key = key; - this.name = ""; - this.description = ""; - this.indicativeSize = 20; - this.type = PropertyType.STRING; - this.options = newArrayList(); - } - - public Builder name(String name) { - this.name = name; - return this; - } - - public Builder description(String description) { - this.description = description; - return this; - } - - public Builder indicativeSize(int indicativeSize) { - this.indicativeSize = indicativeSize; - return this; - } - - public Builder type(PropertyType type) { - this.type = type; - return this; - } - - public Builder options(String... options) { - this.options.addAll(ImmutableList.copyOf(options)); - return this; - } - - public Builder options(List options) { - this.options.addAll(ImmutableList.copyOf(options)); - return this; - } - - public PropertyFieldDef build() { - Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Key must be set"); - Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Name must be set"); - return new PropertyFieldDef(this); - } - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java new file mode 100644 index 00000000000..3c3d4748ccd --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java @@ -0,0 +1,154 @@ +/* + * 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 com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; +import org.sonar.api.PropertyField; +import org.sonar.api.PropertyType; + +import javax.annotation.Nullable; + +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; + +/** + * @since 3.3 + */ +public final class PropertyFieldDefinition { + private final String key; + private final String name; + private final String description; + private final int indicativeSize; + private final PropertyType type; + private final List options; + + private PropertyFieldDefinition(Builder builder) { + this.key = builder.key; + this.name = builder.name; + this.description = builder.description; + this.indicativeSize = builder.indicativeSize; + this.type = builder.type; + this.options = builder.options; + } + + static List create(PropertyField[] fields) { + List definitions = newArrayList(); + for (PropertyField field : fields) { + definitions.add(PropertyFieldDefinition.build(field.key()) + .name(field.name()) + .description(field.description()) + .indicativeSize(field.indicativeSize()) + .type(field.type()) + .options(field.options()) + .build() + ); + } + return definitions; + } + + public static Builder build(String key) { + return new Builder(key); + } + + public String key() { + return key; + } + + public String name() { + return name; + } + + public String description() { + return description; + } + + public int indicativeSize() { + return indicativeSize; + } + + public PropertyType type() { + return type; + } + + public List options() { + return options; + } + + public PropertyDef.Result validate(@Nullable String value) { + return PropertyDef.validate(type, value, options); + } + + public static class Builder { + private String key; + private String name; + private String description; + private int indicativeSize; + private PropertyType type; + private List options; + + private Builder(String key) { + this.key = key; + this.name = ""; + this.description = ""; + this.indicativeSize = 20; + this.type = PropertyType.STRING; + this.options = newArrayList(); + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + public Builder indicativeSize(int indicativeSize) { + this.indicativeSize = indicativeSize; + return this; + } + + public Builder type(PropertyType type) { + this.type = type; + return this; + } + + public Builder options(String... options) { + this.options.addAll(ImmutableList.copyOf(options)); + return this; + } + + public Builder options(List options) { + this.options.addAll(ImmutableList.copyOf(options)); + return this; + } + + public PropertyFieldDefinition build() { + Preconditions.checkArgument(!Strings.isNullOrEmpty(key), "Key must be set"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "Name must be set"); + return new PropertyFieldDefinition(this); + } + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java index c6de094d007..f63934c6228 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java @@ -51,14 +51,14 @@ import java.util.Properties; public class Settings implements BatchComponent, ServerComponent { protected Map properties; - protected PropertyDefs definitions; + protected PropertyDefinitions definitions; private Encryption encryption; public Settings() { - this(new PropertyDefs()); + this(new PropertyDefinitions()); } - public Settings(PropertyDefs definitions) { + public Settings(PropertyDefinitions definitions) { this.properties = Maps.newHashMap(); this.definitions = definitions; this.encryption = new Encryption(this); @@ -364,7 +364,7 @@ public class Settings implements BatchComponent, ServerComponent { return ImmutableMap.copyOf(properties); } - public PropertyDefs getDefinitions() { + public PropertyDefinitions getDefinitions() { return definitions; } @@ -373,7 +373,7 @@ public class Settings implements BatchComponent, ServerComponent { * This method is usually used by unit tests. */ public static Settings createForComponent(Object component) { - return new Settings(new PropertyDefs(component)); + return new Settings(new PropertyDefinitions(component)); } protected void doOnSetProperty(String key, @Nullable String value) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java b/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java index 21774a6631c..860f297b032 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/platform/ComponentContainer.java @@ -29,7 +29,7 @@ import org.picocontainer.lifecycle.ReflectionLifecycleStrategy; import org.picocontainer.monitors.NullComponentMonitor; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import javax.annotation.Nullable; @@ -42,7 +42,7 @@ public class ComponentContainer implements BatchComponent, ServerComponent { ComponentContainer parent, child; // no need for multiple children MutablePicoContainer pico; - PropertyDefs propertyDefs; + PropertyDefinitions propertyDefinitions; /** * Create root container @@ -51,8 +51,8 @@ public class ComponentContainer implements BatchComponent, ServerComponent { this.parent = null; this.child = null; this.pico = createPicoContainer(); - propertyDefs = new PropertyDefs(); - addSingleton(propertyDefs); + propertyDefinitions = new PropertyDefinitions(); + addSingleton(propertyDefinitions); addSingleton(this); } @@ -63,7 +63,7 @@ public class ComponentContainer implements BatchComponent, ServerComponent { this.parent = parent; this.pico = parent.pico.makeChildContainer(); this.parent.child = this; - this.propertyDefs = parent.propertyDefs; + this.propertyDefinitions = parent.propertyDefinitions; addSingleton(this); } @@ -170,7 +170,7 @@ public class ComponentContainer implements BatchComponent, ServerComponent { } public void declareExtension(@Nullable PluginMetadata plugin, Object extension) { - propertyDefs.addComponent(extension, plugin != null ? plugin.getName() : ""); + propertyDefinitions.addComponent(extension, plugin != null ? plugin.getName() : ""); } public ComponentContainer addPicoAdapter(ComponentAdapter adapter) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefTest.java index 416ab35b4b7..4a284b97c30 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefTest.java @@ -139,8 +139,8 @@ public class PropertyDefTest { PropertyDef def = PropertyDef.builder("hello") .name("Hello") .fields( - PropertyFieldDef.build("first").name("First").description("Description").options("A", "B").build(), - PropertyFieldDef.build("second").name("Second").type(PropertyType.INTEGER).indicativeSize(5).build() + PropertyFieldDefinition.build("first").name("First").description("Description").options("A", "B").build(), + PropertyFieldDefinition.build("second").name("Second").type(PropertyType.INTEGER).indicativeSize(5).build() ) .build(); 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 new file mode 100644 index 00000000000..cdd9afc4f0e --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionsTest.java @@ -0,0 +1,172 @@ +/* + * 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 org.sonar.api.Properties; +import org.sonar.api.Property; +import org.sonar.api.resources.Qualifiers; + +import static org.fest.assertions.Assertions.assertThat; + +public class PropertyDefinitionsTest { + + @Test + public void should_inspect_plugin_objects() { + PropertyDefinitions def = new PropertyDefinitions( + PropertyDef.builder("foo").name("Foo").build(), + PropertyDef.builder("one").name("One").build(), + PropertyDef.builder("two").name("Two").defaultValue("2").build() + ); + + assertProperties(def); + } + + @Test + public void should_inspect_annotation_plugin_objects() { + PropertyDefinitions def = new PropertyDefinitions(new PluginWithProperty(), new PluginWithProperties()); + + assertProperties(def); + } + + @Test + public void should_inspect_plugin_classes() { + PropertyDefinitions def = new PropertyDefinitions(PluginWithProperty.class, PluginWithProperties.class); + + assertProperties(def); + } + + @Test + public void test_categories() { + PropertyDefinitions def = new PropertyDefinitions( + PropertyDef.builder("inCateg").name("In Categ").category("categ").build(), + PropertyDef.builder("noCateg").name("No categ").build() + ); + + assertThat(def.getCategory("inCateg")).isEqualTo("categ"); + assertThat(def.getCategory("noCateg")).isEmpty(); + } + + @Test + public void test_categories_on_annotation_plugin() { + PropertyDefinitions def = new PropertyDefinitions(Categories.class); + + assertThat(def.getCategory("inCateg")).isEqualTo("categ"); + assertThat(def.getCategory("noCateg")).isEqualTo(""); + } + + @Test + public void test_default_category() { + PropertyDefinitions def = new PropertyDefinitions(); + def.addComponent(PropertyDef.builder("inCateg").name("In Categ").category("categ").build(), "default"); + def.addComponent(PropertyDef.builder("noCateg").name("No categ").build(), "default"); + + assertThat(def.getCategory("inCateg")).isEqualTo("categ"); + assertThat(def.getCategory("noCateg")).isEqualTo("default"); + } + + @Test + public void test_default_category_on_annotation_plugin() { + PropertyDefinitions def = new PropertyDefinitions(); + def.addComponent(Categories.class, "default"); + assertThat(def.getCategory("inCateg")).isEqualTo("categ"); + assertThat(def.getCategory("noCateg")).isEqualTo("default"); + } + + @Test + public void should_group_by_category() { + PropertyDefinitions def = new PropertyDefinitions( + PropertyDef.builder("global1").name("Global1").category("catGlobal1").build(), + PropertyDef.builder("global2").name("Global2").category("catGlobal1").build(), + PropertyDef.builder("global3").name("Global3").category("catGlobal2").build(), + PropertyDef.builder("project").name("Project").category("catProject").onlyOnQualifiers(Qualifiers.PROJECT).build(), + PropertyDef.builder("module").name("Module").category("catModule").onlyOnQualifiers(Qualifiers.MODULE).build(), + PropertyDef.builder("view").name("View").category("catView").onlyOnQualifiers(Qualifiers.VIEW).build() + ); + + assertThat(def.getPropertiesByCategory(null).keySet()).containsOnly("catGlobal1", "catGlobal2"); + assertThat(def.getPropertiesByCategory(Qualifiers.PROJECT).keySet()).containsOnly("catProject"); + assertThat(def.getPropertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly("catModule"); + assertThat(def.getPropertiesByCategory(Qualifiers.VIEW).keySet()).containsOnly("catView"); + assertThat(def.getPropertiesByCategory("Unkown").keySet()).isEmpty(); + } + + @Test + public void should_group_by_subcategory() { + PropertyDefinitions def = new PropertyDefinitions( + PropertyDef.builder("global1").name("Global1").category("catGlobal1").subcategory("sub1").build(), + PropertyDef.builder("global2").name("Global2").category("catGlobal1").subcategory("sub2").build(), + PropertyDef.builder("global3").name("Global3").category("catGlobal1").build(), + PropertyDef.builder("global4").name("Global4").category("catGlobal2").build() + ); + + assertThat(def.getPropertiesByCategory(null).get("catGlobal1").keySet()).containsOnly("default", "sub1", "sub2"); + assertThat(def.getPropertiesByCategory(null).get("catGlobal2").keySet()).containsOnly("default"); + } + + @Test + public void should_group_by_category_on_annotation_plugin() { + PropertyDefinitions def = new PropertyDefinitions(ByCategory.class); + + assertThat(def.getPropertiesByCategory().keySet()).containsOnly("catGlobal1", "catGlobal2"); + assertThat(def.getPropertiesByCategory(Qualifiers.PROJECT).keySet()).containsOnly("catProject"); + assertThat(def.getPropertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly("catModule"); + } + + private void assertProperties(PropertyDefinitions definitions) { + assertThat(definitions.get("foo").name()).isEqualTo("Foo"); + assertThat(definitions.get("one").name()).isEqualTo("One"); + assertThat(definitions.get("two").name()).isEqualTo("Two"); + assertThat(definitions.get("unknown")).isNull(); + + assertThat(definitions.getDefaultValue("foo")).isNull(); + assertThat(definitions.getDefaultValue("two")).isEqualTo("2"); + + assertThat(definitions.getAll().size()).isEqualTo(3); + } + + @Property(key = "foo", name = "Foo") + static final class PluginWithProperty { + } + + @Properties({ + @Property(key = "one", name = "One"), + @Property(key = "two", name = "Two", defaultValue = "2") + }) + static final class PluginWithProperties { + } + + @Properties({ + @Property(key = "inCateg", name = "In Categ", category = "categ"), + @Property(key = "noCateg", name = "No categ") + }) + static final class Categories { + } + + @Properties({ + @Property(key = "global1", name = "Global1", category = "catGlobal1", global = true, project = false, module = false), + @Property(key = "global2", name = "Global2", category = "catGlobal1", global = true, project = false, module = false), + @Property(key = "global3", name = "Global3", category = "catGlobal2", global = true, project = false, module = false), + @Property(key = "project", name = "Project", category = "catProject", global = false, project = true, module = false), + @Property(key = "module", name = "Module", category = "catModule", global = false, project = false, module = true) + }) + static final class ByCategory { + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefsTest.java deleted file mode 100644 index 5765ec28b64..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefsTest.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * 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 org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.resources.Qualifiers; - -import static org.fest.assertions.Assertions.assertThat; - -public class PropertyDefsTest { - - @Test - public void should_inspect_plugin_objects() { - PropertyDefs def = new PropertyDefs( - PropertyDef.builder("foo").name("Foo").build(), - PropertyDef.builder("one").name("One").build(), - PropertyDef.builder("two").name("Two").defaultValue("2").build() - ); - - assertProperties(def); - } - - @Test - public void should_inspect_annotation_plugin_objects() { - PropertyDefs def = new PropertyDefs(new PluginWithProperty(), new PluginWithProperties()); - - assertProperties(def); - } - - @Test - public void should_inspect_plugin_classes() { - PropertyDefs def = new PropertyDefs(PluginWithProperty.class, PluginWithProperties.class); - - assertProperties(def); - } - - @Test - public void test_categories() { - PropertyDefs def = new PropertyDefs( - PropertyDef.builder("inCateg").name("In Categ").category("categ").build(), - PropertyDef.builder("noCateg").name("No categ").build() - ); - - assertThat(def.getCategory("inCateg")).isEqualTo("categ"); - assertThat(def.getCategory("noCateg")).isEmpty(); - } - - @Test - public void test_categories_on_annotation_plugin() { - PropertyDefs def = new PropertyDefs(Categories.class); - - assertThat(def.getCategory("inCateg")).isEqualTo("categ"); - assertThat(def.getCategory("noCateg")).isEqualTo(""); - } - - @Test - public void test_default_category() { - PropertyDefs def = new PropertyDefs(); - def.addComponent(PropertyDef.builder("inCateg").name("In Categ").category("categ").build(), "default"); - def.addComponent(PropertyDef.builder("noCateg").name("No categ").build(), "default"); - - assertThat(def.getCategory("inCateg")).isEqualTo("categ"); - assertThat(def.getCategory("noCateg")).isEqualTo("default"); - } - - @Test - public void test_default_category_on_annotation_plugin() { - PropertyDefs def = new PropertyDefs(); - def.addComponent(Categories.class, "default"); - assertThat(def.getCategory("inCateg")).isEqualTo("categ"); - assertThat(def.getCategory("noCateg")).isEqualTo("default"); - } - - @Test - public void should_group_by_category() { - PropertyDefs def = new PropertyDefs( - PropertyDef.builder("global1").name("Global1").category("catGlobal1").build(), - PropertyDef.builder("global2").name("Global2").category("catGlobal1").build(), - PropertyDef.builder("global3").name("Global3").category("catGlobal2").build(), - PropertyDef.builder("project").name("Project").category("catProject").onlyOnQualifiers(Qualifiers.PROJECT).build(), - PropertyDef.builder("module").name("Module").category("catModule").onlyOnQualifiers(Qualifiers.MODULE).build(), - PropertyDef.builder("view").name("View").category("catView").onlyOnQualifiers(Qualifiers.VIEW).build() - ); - - assertThat(def.getPropertiesByCategory(null).keySet()).containsOnly("catGlobal1", "catGlobal2"); - assertThat(def.getPropertiesByCategory(Qualifiers.PROJECT).keySet()).containsOnly("catProject"); - assertThat(def.getPropertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly("catModule"); - assertThat(def.getPropertiesByCategory(Qualifiers.VIEW).keySet()).containsOnly("catView"); - assertThat(def.getPropertiesByCategory("Unkown").keySet()).isEmpty(); - } - - @Test - public void should_group_by_subcategory() { - PropertyDefs def = new PropertyDefs( - PropertyDef.builder("global1").name("Global1").category("catGlobal1").subcategory("sub1").build(), - PropertyDef.builder("global2").name("Global2").category("catGlobal1").subcategory("sub2").build(), - PropertyDef.builder("global3").name("Global3").category("catGlobal1").build(), - PropertyDef.builder("global4").name("Global4").category("catGlobal2").build() - ); - - assertThat(def.getPropertiesByCategory(null).get("catGlobal1").keySet()).containsOnly("default", "sub1", "sub2"); - assertThat(def.getPropertiesByCategory(null).get("catGlobal2").keySet()).containsOnly("default"); - } - - @Test - public void should_group_by_category_on_annotation_plugin() { - PropertyDefs def = new PropertyDefs(ByCategory.class); - - assertThat(def.getPropertiesByCategory().keySet()).containsOnly("catGlobal1", "catGlobal2"); - assertThat(def.getPropertiesByCategory(Qualifiers.PROJECT).keySet()).containsOnly("catProject"); - assertThat(def.getPropertiesByCategory(Qualifiers.MODULE).keySet()).containsOnly("catModule"); - } - - private void assertProperties(PropertyDefs definitions) { - assertThat(definitions.get("foo").name()).isEqualTo("Foo"); - assertThat(definitions.get("one").name()).isEqualTo("One"); - assertThat(definitions.get("two").name()).isEqualTo("Two"); - assertThat(definitions.get("unknown")).isNull(); - - assertThat(definitions.getDefaultValue("foo")).isNull(); - assertThat(definitions.getDefaultValue("two")).isEqualTo("2"); - - assertThat(definitions.getAll().size()).isEqualTo(3); - } - - @Property(key = "foo", name = "Foo") - static final class PluginWithProperty { - } - - @Properties({ - @Property(key = "one", name = "One"), - @Property(key = "two", name = "Two", defaultValue = "2") - }) - static final class PluginWithProperties { - } - - @Properties({ - @Property(key = "inCateg", name = "In Categ", category = "categ"), - @Property(key = "noCateg", name = "No categ") - }) - static final class Categories { - } - - @Properties({ - @Property(key = "global1", name = "Global1", category = "catGlobal1", global = true, project = false, module = false), - @Property(key = "global2", name = "Global2", category = "catGlobal1", global = true, project = false, module = false), - @Property(key = "global3", name = "Global3", category = "catGlobal2", global = true, project = false, module = false), - @Property(key = "project", name = "Project", category = "catProject", global = false, project = true, module = false), - @Property(key = "module", name = "Module", category = "catModule", global = false, project = false, module = true) - }) - static final class ByCategory { - } -} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java index 91280833a91..228e0ee6e12 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java @@ -33,7 +33,7 @@ import static org.fest.assertions.Assertions.assertThat; public class SettingsTest { - private PropertyDefs definitions; + private PropertyDefinitions definitions; @Properties({ @Property(key = "hello", name = "Hello", defaultValue = "world"), @@ -57,7 +57,7 @@ public class SettingsTest { @Before public void init_definitions() { - definitions = new PropertyDefs(); + definitions = new PropertyDefinitions(); definitions.addComponent(Init.class); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java index 6a3bfa07658..6f1a766d303 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java @@ -24,7 +24,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.Property; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import java.util.Arrays; @@ -157,9 +157,9 @@ public class ComponentContainerTest { ComponentContainer container = new ComponentContainer(); container.addSingleton(ComponentWithProperty.class); - PropertyDefs propertyDefs = container.getComponentByType(PropertyDefs.class); - assertThat(propertyDefs.get("foo")).isNotNull(); - assertThat(propertyDefs.get("foo").defaultValue()).isEqualTo("bar"); + PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); + assertThat(propertyDefinitions.get("foo")).isNotNull(); + assertThat(propertyDefinitions.get("foo").defaultValue()).isEqualTo("bar"); } @Test @@ -168,8 +168,8 @@ public class ComponentContainerTest { PluginMetadata plugin = mock(PluginMetadata.class); container.declareExtension(plugin, ComponentWithProperty.class); - PropertyDefs propertyDefs = container.getComponentByType(PropertyDefs.class); - assertThat(propertyDefs.get("foo")).isNotNull(); + PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); + assertThat(propertyDefinitions.get("foo")).isNotNull(); assertThat(container.getComponentByType(ComponentWithProperty.class)).isNull(); } @@ -179,8 +179,8 @@ public class ComponentContainerTest { PluginMetadata plugin = mock(PluginMetadata.class); container.addExtension(plugin, ComponentWithProperty.class); - PropertyDefs propertyDefs = container.getComponentByType(PropertyDefs.class); - assertThat(propertyDefs.get("foo")).isNotNull(); + PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); + assertThat(propertyDefinitions.get("foo")).isNotNull(); assertThat(container.getComponentByType(ComponentWithProperty.class)).isNotNull(); assertThat(container.getComponentByKey(ComponentWithProperty.class)).isNotNull(); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasProjectPropertyConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasProjectPropertyConditionTest.java index 32a382cb6d3..37f9d9940c8 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasProjectPropertyConditionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/workflow/condition/HasProjectPropertyConditionTest.java @@ -22,7 +22,7 @@ package org.sonar.api.workflow.condition; import org.junit.Test; import org.sonar.api.Properties; import org.sonar.api.Property; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.api.workflow.internal.DefaultReview; import org.sonar.api.workflow.internal.DefaultWorkflowContext; @@ -50,7 +50,7 @@ public class HasProjectPropertyConditionTest { public void returnTrueIfDefaultValue() { HasProjectPropertyCondition condition = new HasProjectPropertyCondition("jira.url"); DefaultWorkflowContext context = new DefaultWorkflowContext(); - context.setSettings(new Settings(new PropertyDefs().addComponent(WithDefaultValue.class))); + context.setSettings(new Settings(new PropertyDefinitions().addComponent(WithDefaultValue.class))); assertThat(condition.doVerify(new DefaultReview(), context)).isTrue(); } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java index 115e010fff8..a357d077fe8 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java @@ -22,7 +22,7 @@ package org.sonar.server.platform; import com.google.common.annotations.VisibleForTesting; import org.apache.commons.configuration.Configuration; import org.sonar.api.CoreProperties; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.core.config.ConfigurationUtils; @@ -53,12 +53,12 @@ public class ServerSettings extends Settings { private File deployDir; private File sonarHome; - public ServerSettings(PropertyDefs definitions, Configuration deprecatedConfiguration, ServletContext servletContext) { + public ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, ServletContext servletContext) { this(definitions, deprecatedConfiguration, getDeployDir(servletContext), SonarHome.getHome()); } @VisibleForTesting - ServerSettings(PropertyDefs definitions, Configuration deprecatedConfiguration, File deployDir, File sonarHome) { + ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, File deployDir, File sonarHome) { super(definitions); this.deprecatedConfiguration = deprecatedConfiguration; this.deployDir = deployDir; diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java b/sonar-server/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java index ac4a2aace4d..93bcd99ca3e 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RenameDeprecatedPropertyKeys.java @@ -22,7 +22,7 @@ package org.sonar.server.startup; import com.google.common.base.Strings; import org.slf4j.LoggerFactory; import org.sonar.api.config.PropertyDef; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.core.properties.PropertiesDao; /** @@ -31,9 +31,9 @@ import org.sonar.core.properties.PropertiesDao; public class RenameDeprecatedPropertyKeys { private PropertiesDao dao; - private PropertyDefs definitions; + private PropertyDefinitions definitions; - public RenameDeprecatedPropertyKeys(PropertiesDao dao, PropertyDefs definitions) { + public RenameDeprecatedPropertyKeys(PropertiesDao dao, PropertyDefinitions definitions) { this.dao = dao; this.definitions = definitions; } diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index 5892a089b21..0e16d6329d7 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -23,7 +23,7 @@ import com.google.common.collect.ListMultimap; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.config.License; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.platform.NewUserHandler; @@ -185,8 +185,8 @@ public final class JRubyFacade { } // PLUGINS ------------------------------------------------------------------ - public PropertyDefs getPropertyDefinitions() { - return get(PropertyDefs.class); + public PropertyDefinitions getPropertyDefinitions() { + return get(PropertyDefinitions.class); } public boolean hasPlugin(String key) { diff --git a/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java index dc034d3807b..737cdb50c12 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/PersistentSettingsTest.java @@ -24,7 +24,7 @@ import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentMatcher; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.core.properties.PropertiesDao; import org.sonar.core.properties.PropertyDto; @@ -45,7 +45,7 @@ public class PersistentSettingsTest { public void init() throws URISyntaxException { dao = mock(PropertiesDao.class); settings = new ServerSettings( - new PropertyDefs(), + new PropertyDefinitions(), new PropertiesConfiguration(), new File("."), new File(PersistentSettingsTest.class.getResource("/org/sonar/server/platform/PersistentSettingsTest/").toURI())); diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java index 46ae543034f..a9b181ca343 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java @@ -22,7 +22,7 @@ package org.sonar.server.platform; import com.google.common.collect.ImmutableMap; import org.apache.commons.configuration.BaseConfiguration; import org.junit.Test; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import java.io.File; import java.net.URISyntaxException; @@ -36,7 +36,7 @@ public class ServerSettingsTest { @Test public void load_properties_file() { - ServerSettings settings = new ServerSettings(new PropertyDefs(), new BaseConfiguration(), new File("."), home); + ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home); assertThat(settings.getString("hello")).isEqualTo("world"); } @@ -44,7 +44,7 @@ public class ServerSettingsTest { @Test public void systemPropertiesShouldOverridePropertiesFile() { System.setProperty("ServerSettingsTestEnv", "in_env"); - ServerSettings settings = new ServerSettings(new PropertyDefs(), new BaseConfiguration(), new File("."), home); + ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home); assertThat(settings.getString("ServerSettingsTestEnv")).isEqualTo("in_env"); } @@ -52,12 +52,12 @@ public class ServerSettingsTest { @Test(expected = IllegalStateException.class) public void fail_if_properties_file_is_not_found() { File sonarHome = new File("unknown/path"); - new ServerSettings(new PropertyDefs(), new BaseConfiguration(), new File("."), sonarHome); + new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), sonarHome); } @Test public void activateDatabaseSettings() { - ServerSettings settings = new ServerSettings(new PropertyDefs(), new BaseConfiguration(), new File("."), home); + ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home); Map databaseProperties = ImmutableMap.of("in_db", "true"); settings.activateDatabaseSettings(databaseProperties); @@ -67,7 +67,7 @@ public class ServerSettingsTest { @Test public void file_settings_override_db_settings() { - ServerSettings settings = new ServerSettings(new PropertyDefs(), new BaseConfiguration(), new File("."), home); + ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home); assertThat(settings.getString("in_file")).isEqualTo("true"); Map databaseProperties = ImmutableMap.of("in_file", "false"); @@ -79,7 +79,7 @@ public class ServerSettingsTest { @Test public void synchronize_deprecated_commons_configuration() { BaseConfiguration deprecated = new BaseConfiguration(); - ServerSettings settings = new ServerSettings(new PropertyDefs(), deprecated, new File("."), home); + ServerSettings settings = new ServerSettings(new PropertyDefinitions(), deprecated, new File("."), home); assertThat(settings.getString("in_file")).isEqualTo("true"); assertThat(deprecated.getString("in_file")).isEqualTo("true"); diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RenameDeprecatedPropertyKeysTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RenameDeprecatedPropertyKeysTest.java index e08e5ecda91..eea101c7e38 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/RenameDeprecatedPropertyKeysTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/RenameDeprecatedPropertyKeysTest.java @@ -22,7 +22,7 @@ package org.sonar.server.startup; import org.junit.Test; import org.sonar.api.Properties; import org.sonar.api.Property; -import org.sonar.api.config.PropertyDefs; +import org.sonar.api.config.PropertyDefinitions; import org.sonar.core.properties.PropertiesDao; import static org.mockito.Mockito.*; @@ -31,7 +31,7 @@ public class RenameDeprecatedPropertyKeysTest { @Test public void should_rename_deprecated_keys() { PropertiesDao dao = mock(PropertiesDao.class); - PropertyDefs definitions = new PropertyDefs(FakeExtension.class); + PropertyDefinitions definitions = new PropertyDefinitions(FakeExtension.class); RenameDeprecatedPropertyKeys task = new RenameDeprecatedPropertyKeys(dao, definitions); task.start(); -- cgit v1.2.3