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<PropertyDefinition> getAll() {
return definitions.values();
}
+ public String validKey(String key) {
+ return StringUtils.defaultString(deprecatedKeys.get(key), key);
+ }
+
static enum PropertyDefinitionFilter {
GLOBAL {
@Override
}
public String getCategory(String key) {
- return categories.get(key);
+ return categories.get(validKey(key));
}
public String getCategory(Property prop) {
* 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;
}
}
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 {
}
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;
}
}
@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");
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);