aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-11-07 17:30:33 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-11-07 17:30:33 +0100
commit95718a509257f37bbb498f36185adceb65537068 (patch)
treedc219ee7815e1e7017df956942c5daeacf7e0e34 /sonar-plugin-api
parent7264e00ff2f411e6dacd65e467968b1ff1134583 (diff)
downloadsonarqube-95718a509257f37bbb498f36185adceb65537068.tar.gz
sonarqube-95718a509257f37bbb498f36185adceb65537068.zip
SONAR-3940 property relocation
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/Property.java6
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java20
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java34
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/SettingsTest.java40
5 files changed, 97 insertions, 13 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java
index b0179ceedf3..4802b6fe251 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java
@@ -121,4 +121,10 @@ public @interface Property {
* @since 3.3
*/
PropertyField[] fields() default {};
+
+ /**
+ * Relocation of key.
+ * @since 3.4
+ */
+ String deprecatedKey() default "";
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java
index c829cd4ac17..083b598d4da 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java
@@ -71,6 +71,7 @@ public final class PropertyDefinition {
private final boolean isGlobal;
private final boolean multiValues;
private final String propertySetKey;
+ private final String deprecatedKey;
private final List<PropertyFieldDefinition> fields;
private PropertyDefinition(Property annotation) {
@@ -87,6 +88,7 @@ public final class PropertyDefinition {
this.multiValues = annotation.multiValues();
this.propertySetKey = annotation.propertySetKey();
this.fields = ImmutableList.copyOf(PropertyFieldDefinition.create(annotation.fields()));
+ this.deprecatedKey = annotation.deprecatedKey();
}
private PropertyDefinition(String key, PropertyType type, String[] options) {
@@ -103,6 +105,7 @@ public final class PropertyDefinition {
this.multiValues = false;
this.propertySetKey = null;
this.fields = null;
+ this.deprecatedKey = null;
}
private static PropertyType fixType(String key, PropertyType type) {
@@ -215,4 +218,11 @@ public final class PropertyDefinition {
public List<PropertyFieldDefinition> getFields() {
return fields;
}
+
+ /**
+ * @since 3.4
+ */
+ public String getDeprecatedKey() {
+ return deprecatedKey;
+ }
}
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 dfd1dfe5b55..8aae3ed9289 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
@@ -19,6 +19,7 @@
*/
package org.sonar.api.config;
+import com.google.common.base.Strings;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
@@ -43,6 +44,9 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen
private final Map<String, PropertyDefinition> definitions = Maps.newHashMap();
private final Map<String, String> categories = Maps.newHashMap();
+ // deprecated key -> new key
+ private final Map<String, String> deprecatedKeys = Maps.newHashMap();
+
public PropertyDefinitions(Object... components) {
if (components != null) {
addComponents(Arrays.asList(components));
@@ -87,6 +91,10 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen
if (!definitions.containsKey(definition.getKey())) {
definitions.put(definition.getKey(), definition);
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;
}
@@ -170,4 +178,16 @@ public final class PropertyDefinitions implements BatchComponent, ServerComponen
public String getCategory(Property prop) {
return getCategory(prop.key());
}
+
+ public String getNewKey(String deprecatedKey) {
+ return deprecatedKeys.get(deprecatedKey);
+ }
+
+ public String getDeprecatedKey(String key) {
+ PropertyDefinition def = get(key);
+ if (def == null) {
+ return null;
+ }
+ return StringUtils.defaultIfEmpty(def.getDeprecatedKey(), null);
+ }
}
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 8043a7e9e35..a3a087b4175 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
@@ -92,10 +92,8 @@ public class Settings implements BatchComponent, ServerComponent {
}
public final String getString(String key) {
- String value = properties.get(key);
- if (value == null) {
- value = getDefaultValue(key);
- } else if (encryption.isEncrypted(value)) {
+ String value = getClearString(key);
+ if (value != null && encryption.isEncrypted(value)) {
try {
value = encryption.decrypt(value);
} catch (Exception e) {
@@ -264,10 +262,8 @@ public class Settings implements BatchComponent, ServerComponent {
throw new IllegalStateException("Fail to set multiple values on a single value property " + key);
}
- if (values == null) {
- properties.remove(key);
- doOnRemoveProperty(key);
- } else {
+ String text = null;
+ if (values != null) {
List<String> escaped = Lists.newArrayList();
for (String value : values) {
if (null != value) {
@@ -278,13 +274,16 @@ public class Settings implements BatchComponent, ServerComponent {
}
String escapedValue = Joiner.on(',').join(escaped);
- properties.put(key, StringUtils.trim(escapedValue));
- doOnSetProperty(key, escapedValue);
+ text = StringUtils.trim(escapedValue);
}
- return this;
+ return setProperty(key, text);
}
public final Settings setProperty(String key, @Nullable String value) {
+ return setProperty(key, value, true);
+ }
+
+ private Settings setProperty(String key, @Nullable String value, boolean recursive) {
if (value == null) {
properties.remove(key);
doOnRemoveProperty(key);
@@ -292,6 +291,17 @@ public class Settings implements BatchComponent, ServerComponent {
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);
+ }
+ }
+ }
return this;
}
@@ -347,7 +357,7 @@ public class Settings implements BatchComponent, ServerComponent {
}
public final Settings setProperties(Settings s) {
- if (s.properties==null) {
+ if (s.properties == null) {
return clear();
}
return setProperties(Maps.newHashMap(s.properties));
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 8be0edeca61..0993b09063f 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
@@ -44,7 +44,10 @@ public class SettingsTest {
@Property(key = "integer", name = "Integer", defaultValue = "12345"),
@Property(key = "array", name = "Array", defaultValue = "one,two,three"),
@Property(key = "multi_values", name = "Array", defaultValue = "1,2,3", multiValues = true),
- @Property(key = "sonar.jira", name = "Jira Server", type = PropertyType.PROPERTY_SET, propertySetKey = "jira")
+ @Property(key = "sonar.jira", name = "Jira Server", type = PropertyType.PROPERTY_SET, propertySetKey = "jira"),
+ @Property(key = "newKey", name = "New key", deprecatedKey = "oldKey"),
+ @Property(key = "newKeyWithDefaultValue", name = "New key with default value", deprecatedKey = "oldKeyWithDefaultValue", defaultValue = "default_value"),
+ @Property(key = "new_multi_values", name = "New multi values", defaultValue = "1,2,3", multiValues = true, deprecatedKey = "old_multi_values")
})
static class Init {
}
@@ -376,4 +379,39 @@ public class SettingsTest {
assertThat(settings.getKeysStartingWith("sonar.jdbc")).containsOnly("sonar.jdbc.url", "sonar.jdbc.username");
assertThat(settings.getKeysStartingWith("other")).hasSize(0);
}
+
+ @Test
+ public void should_fallback_deprecated_key_to_default_value_of_new_key() {
+ Settings settings = new Settings(definitions);
+
+ assertThat(settings.getString("newKeyWithDefaultValue")).isEqualTo("default_value");
+ assertThat(settings.getString("oldKeyWithDefaultValue")).isEqualTo("default_value");
+ }
+
+ @Test
+ public void should_fallback_deprecated_key_to_new_key() {
+ Settings settings = new Settings(definitions);
+ settings.setProperty("newKey", "value of newKey");
+
+ assertThat(settings.getString("newKey")).isEqualTo("value of newKey");
+ assertThat(settings.getString("oldKey")).isEqualTo("value of newKey");
+ }
+
+ @Test
+ public void should_load_value_set_on_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");
+
+ assertThat(settings.getString("newKey")).isEqualTo("value of oldKey");
+ assertThat(settings.getString("oldKey")).isEqualTo("value of oldKey");
+ }
+
+ @Test
+ public void should_support_deprecated_props_with_multi_values() {
+ Settings settings = new Settings(definitions);
+ settings.setProperty("new_multi_values", new String[]{" A ", " B "});
+ assertThat(settings.getStringArray("new_multi_values")).isEqualTo(new String[]{"A", "B"});
+ assertThat(settings.getStringArray("old_multi_values")).isEqualTo(new String[]{"A", "B"});
+ }
}