From 6756fb0c8258ae6fbe21681ffe744d48365cc1fd Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 7 Dec 2012 00:01:13 +0100 Subject: [PATCH] SONAR-3940 do not duplicate property in settings page --- .../sonar/api/config/PropertyDefinitions.java | 9 ++++-- .../java/org/sonar/api/config/Settings.java | 31 ++++++------------- .../org/sonar/api/config/SettingsTest.java | 11 ++++++- 3 files changed, 25 insertions(+), 26 deletions(-) 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 8aae3ed9289..58820d07115 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 @@ -93,20 +93,23 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen categories.put(definition.getKey(), StringUtils.defaultIfBlank(definition.getCategory(), defaultCategory)); if (!Strings.isNullOrEmpty(definition.getDeprecatedKey()) && !definition.getDeprecatedKey().equals(definition.getKey())) { deprecatedKeys.put(definition.getDeprecatedKey(), definition.getKey()); - definitions.put(definition.getDeprecatedKey(), definition); } } return this; } public PropertyDefinition get(String key) { - return definitions.get(key); + return definitions.get(validKey(key)); } public Collection getAll() { return definitions.values(); } + public String validKey(String key) { + return StringUtils.defaultString(deprecatedKeys.get(key), key); + } + static enum PropertyDefinitionFilter { GLOBAL { @Override @@ -172,7 +175,7 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen } public String getCategory(String key) { - return categories.get(key); + return categories.get(validKey(key)); } public String getCategory(Property prop) { 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 c3c6f46fc9e..74b1436aafb 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 @@ -107,9 +107,10 @@ public class Settings implements BatchComponent, ServerComponent { * Does not decrypt value. */ protected String getClearString(String key) { - String value = properties.get(key); + String validKey = definitions.validKey(key); + String value = properties.get(validKey); if (value == null) { - value = getDefaultValue(key); + value = getDefaultValue(validKey); } return value; } @@ -247,7 +248,7 @@ public class Settings implements BatchComponent, ServerComponent { } public final Settings appendProperty(String key, String value) { - String newValue = properties.get(key); + String newValue = properties.get(definitions.validKey(key)); if (StringUtils.isEmpty(newValue)) { newValue = StringUtils.trim(value); } else { @@ -280,27 +281,13 @@ public class Settings implements BatchComponent, ServerComponent { } public final Settings setProperty(String key, @Nullable String value) { - return setProperty(key, value, true); - } - - private Settings setProperty(String key, @Nullable String value, boolean recursive) { + String validKey = definitions.validKey(key); if (value == null) { - properties.remove(key); - doOnRemoveProperty(key); + properties.remove(validKey); + doOnRemoveProperty(validKey); } else { - properties.put(key, StringUtils.trim(value)); - doOnSetProperty(key, value); - } - if (recursive) { - String newKey = definitions.getNewKey(key); - if (newKey != null) { - setProperty(newKey, value, false); - } else { - String deprecatedKey = definitions.getDeprecatedKey(key); - if (deprecatedKey != null) { - setProperty(deprecatedKey, value, false); - } - } + properties.put(validKey, StringUtils.trim(value)); + doOnSetProperty(validKey, value); } return this; } 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 0993b09063f..745c28d0990 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 @@ -398,7 +398,7 @@ public class SettingsTest { } @Test - public void should_load_value_set_on_deprecated_key() { + public void should_load_value_of_deprecated_key() { // it's used for example when deprecated settings are set through command-line Settings settings = new Settings(definitions); settings.setProperty("oldKey", "value of oldKey"); @@ -407,6 +407,15 @@ public class SettingsTest { assertThat(settings.getString("oldKey")).isEqualTo("value of oldKey"); } + @Test + public void should_load_values_of_deprecated_key() { + Settings settings = new Settings(definitions); + settings.setProperty("oldKey", "a,b"); + + assertThat(settings.getStringArray("newKey")).containsOnly("a", "b"); + assertThat(settings.getStringArray("oldKey")).containsOnly("a", "b"); + } + @Test public void should_support_deprecated_props_with_multi_values() { Settings settings = new Settings(definitions); -- 2.39.5