SystemEnvironmentPropertySource replaces hyphens in property names as well
Issue: SPR-13594
This commit is contained in:
parent
07fd7b905e
commit
95d62658ff
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -24,8 +24,8 @@ import org.springframework.util.Assert;
|
||||||
* Specialization of {@link MapPropertySource} designed for use with
|
* Specialization of {@link MapPropertySource} designed for use with
|
||||||
* {@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables}.
|
* {@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables}.
|
||||||
* Compensates for constraints in Bash and other shells that do not allow for variables
|
* Compensates for constraints in Bash and other shells that do not allow for variables
|
||||||
* containing the period character; also allows for uppercase variations on property
|
* containing the period character and/or hyphen character; also allows for uppercase
|
||||||
* names for more idiomatic shell use.
|
* variations on property names for more idiomatic shell use.
|
||||||
*
|
*
|
||||||
* <p>For example, a call to {@code getProperty("foo.bar")} will attempt to find a value
|
* <p>For example, a call to {@code getProperty("foo.bar")} will attempt to find a value
|
||||||
* for the original property or any 'equivalent' property, returning the first found:
|
* for the original property or any 'equivalent' property, returning the first found:
|
||||||
|
|
@ -35,8 +35,9 @@ import org.springframework.util.Assert;
|
||||||
* <li>{@code FOO.BAR} - original, with upper case</li>
|
* <li>{@code FOO.BAR} - original, with upper case</li>
|
||||||
* <li>{@code FOO_BAR} - with underscores and upper case</li>
|
* <li>{@code FOO_BAR} - with underscores and upper case</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
|
* Any hyphen variant of the above would work as well, or even mix dot/hyphen variants.
|
||||||
*
|
*
|
||||||
* The same applies for calls to {@link #containsProperty(String)}, which returns
|
* <p>The same applies for calls to {@link #containsProperty(String)}, which returns
|
||||||
* {@code true} if any of the above properties are present, otherwise {@code false}.
|
* {@code true} if any of the above properties are present, otherwise {@code false}.
|
||||||
*
|
*
|
||||||
* <p>This feature is particularly useful when specifying active or default profiles as
|
* <p>This feature is particularly useful when specifying active or default profiles as
|
||||||
|
|
@ -102,29 +103,42 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
|
||||||
*/
|
*/
|
||||||
private String resolvePropertyName(String name) {
|
private String resolvePropertyName(String name) {
|
||||||
Assert.notNull(name, "Property name must not be null");
|
Assert.notNull(name, "Property name must not be null");
|
||||||
|
String resolvedName = checkPropertyName(name);
|
||||||
|
if (resolvedName != null) {
|
||||||
|
return resolvedName;
|
||||||
|
}
|
||||||
|
String uppercasedName = name.toUpperCase();
|
||||||
|
if (!name.equals(uppercasedName)) {
|
||||||
|
resolvedName = checkPropertyName(uppercasedName);
|
||||||
|
if (resolvedName != null) {
|
||||||
|
return resolvedName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String checkPropertyName(String name) {
|
||||||
|
// Check name as-is
|
||||||
if (containsKey(name)) {
|
if (containsKey(name)) {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
// Check name with just dots replaced
|
||||||
String usName = name.replace('.', '_');
|
String noDotName = name.replace('.', '_');
|
||||||
if (!name.equals(usName) && containsKey(usName)) {
|
if (!name.equals(noDotName) && containsKey(noDotName)) {
|
||||||
return usName;
|
return noDotName;
|
||||||
}
|
}
|
||||||
|
// Check name with just hyphens replaced
|
||||||
String ucName = name.toUpperCase();
|
String noHyphenName = name.replace('-', '_');
|
||||||
if (!name.equals(ucName)) {
|
if (!name.equals(noHyphenName) && containsKey(noHyphenName)) {
|
||||||
if (containsKey(ucName)) {
|
return noHyphenName;
|
||||||
return ucName;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
String usUcName = ucName.replace('.', '_');
|
|
||||||
if (!ucName.equals(usUcName) && containsKey(usUcName)) {
|
|
||||||
return usUcName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// Check name with dots and hyphens replaced
|
||||||
return name;
|
String noDotNoHyphenName = noDotName.replace('-', '_');
|
||||||
|
if (!noDotName.equals(noDotNoHyphenName) && containsKey(noDotNoHyphenName)) {
|
||||||
|
return noDotNoHyphenName;
|
||||||
|
}
|
||||||
|
// Give up
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean containsKey(String name) {
|
private boolean containsKey(String name) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -93,16 +93,59 @@ public class SystemEnvironmentPropertySourceTests {
|
||||||
@Test
|
@Test
|
||||||
public void withUppercase() {
|
public void withUppercase() {
|
||||||
envMap.put("A_KEY", "a_value");
|
envMap.put("A_KEY", "a_value");
|
||||||
|
envMap.put("A_LONG_KEY", "a_long_value");
|
||||||
|
envMap.put("A_DOT.KEY", "a_dot_value");
|
||||||
|
envMap.put("A_HYPHEN-KEY", "a_hyphen_value");
|
||||||
|
|
||||||
assertThat(ps.containsProperty("A_KEY"), equalTo(true));
|
assertThat(ps.containsProperty("A_KEY"), equalTo(true));
|
||||||
assertThat(ps.containsProperty("A.KEY"), equalTo(true));
|
assertThat(ps.containsProperty("A.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A-KEY"), equalTo(true));
|
||||||
assertThat(ps.containsProperty("a_key"), equalTo(true));
|
assertThat(ps.containsProperty("a_key"), equalTo(true));
|
||||||
assertThat(ps.containsProperty("a.key"), equalTo(true));
|
assertThat(ps.containsProperty("a.key"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("a-key"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A_LONG_KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A.LONG.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A-LONG-KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A.LONG-KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A-LONG.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A_long_KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A.long.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A-long-KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A.long-KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A-long.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A_DOT.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A-DOT.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A_dot.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A-dot.KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A_HYPHEN-KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A.HYPHEN-KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A_hyphen-KEY"), equalTo(true));
|
||||||
|
assertThat(ps.containsProperty("A.hyphen-KEY"), equalTo(true));
|
||||||
|
|
||||||
assertThat(ps.getProperty("A_KEY"), equalTo((Object)"a_value"));
|
assertThat(ps.getProperty("A_KEY"), equalTo("a_value"));
|
||||||
assertThat(ps.getProperty("A.KEY"), equalTo((Object)"a_value"));
|
assertThat(ps.getProperty("A.KEY"), equalTo("a_value"));
|
||||||
assertThat(ps.getProperty("a_key"), equalTo((Object)"a_value"));
|
assertThat(ps.getProperty("A-KEY"), equalTo("a_value"));
|
||||||
assertThat(ps.getProperty("a.key"), equalTo((Object)"a_value"));
|
assertThat(ps.getProperty("a_key"), equalTo("a_value"));
|
||||||
|
assertThat(ps.getProperty("a.key"), equalTo("a_value"));
|
||||||
|
assertThat(ps.getProperty("a-key"), equalTo("a_value"));
|
||||||
|
assertThat(ps.getProperty("A_LONG_KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A.LONG.KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A-LONG-KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A.LONG-KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A-LONG.KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A_long_KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A.long.KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A-long-KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A.long-KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A-long.KEY"), equalTo("a_long_value"));
|
||||||
|
assertThat(ps.getProperty("A_DOT.KEY"), equalTo("a_dot_value"));
|
||||||
|
assertThat(ps.getProperty("A-DOT.KEY"), equalTo("a_dot_value"));
|
||||||
|
assertThat(ps.getProperty("A_dot.KEY"), equalTo("a_dot_value"));
|
||||||
|
assertThat(ps.getProperty("A-dot.KEY"), equalTo("a_dot_value"));
|
||||||
|
assertThat(ps.getProperty("A_HYPHEN-KEY"), equalTo("a_hyphen_value"));
|
||||||
|
assertThat(ps.getProperty("A.HYPHEN-KEY"), equalTo("a_hyphen_value"));
|
||||||
|
assertThat(ps.getProperty("A_hyphen-KEY"), equalTo("a_hyphen_value"));
|
||||||
|
assertThat(ps.getProperty("A.hyphen-KEY"), equalTo("a_hyphen_value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue