Refactor property placeholder use of env. vars
This commit is a slight refactoring of the use of environment variables
in replacing property placeholders. In commit
115f983827
the constructor for
Settings.Builder was made package visible to provide a hook for tests to
mock obtaining environment variables. But we do not need to go that far
and can instead provide a small hook for this for tests without opening
up the constructor. Thus, in this commit we refactor
Settings.Builder#replacePropertyPlaceholders to a package-visible method
that accepts a function providing environment variables by names. The
public-visible method just delegates to this method passing in
System::getenv and tests can use the package-visible method to mock the
behavior they need without relying on external environment variables.
This commit is contained in:
parent
ca033b42a9
commit
ba14aca218
|
@ -602,8 +602,7 @@ public final class Settings implements ToXContent {
|
|||
|
||||
private final Map<String, String> map = new LinkedHashMap<>();
|
||||
|
||||
// visible for testing
|
||||
Builder() {
|
||||
private Builder() {
|
||||
|
||||
}
|
||||
|
||||
|
@ -961,11 +960,16 @@ public final class Settings implements ToXContent {
|
|||
* another setting already set on this builder.
|
||||
*/
|
||||
public Builder replacePropertyPlaceholders() {
|
||||
return replacePropertyPlaceholders(System::getenv);
|
||||
}
|
||||
|
||||
// visible for testing
|
||||
Builder replacePropertyPlaceholders(Function<String, String> getenv) {
|
||||
PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
|
||||
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new PropertyPlaceholder.PlaceholderResolver() {
|
||||
@Override
|
||||
public String resolvePlaceholder(String placeholderName) {
|
||||
final String value = getenv(placeholderName);
|
||||
final String value = getenv.apply(placeholderName);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
|
@ -1001,11 +1005,6 @@ public final class Settings implements ToXContent {
|
|||
return this;
|
||||
}
|
||||
|
||||
// visible for testing
|
||||
String getenv(String placeholderName) {
|
||||
return System.getenv(placeholderName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that all settings in the builder start with the specified prefix.
|
||||
*
|
||||
|
|
|
@ -63,15 +63,9 @@ public class SettingsTests extends ESTestCase {
|
|||
|
||||
public void testReplacePropertiesPlaceholderByEnvironmentVariables() {
|
||||
final String hostname = randomAsciiOfLength(16);
|
||||
final Settings.Builder builder = new Settings.Builder() {
|
||||
@Override
|
||||
protected String getenv(String placeholderName) {
|
||||
return "HOSTNAME".equals(placeholderName) ? hostname : null;
|
||||
}
|
||||
};
|
||||
final Settings implicitEnvSettings = builder
|
||||
final Settings implicitEnvSettings = Settings.builder()
|
||||
.put("setting1", "${HOSTNAME}")
|
||||
.replacePropertyPlaceholders()
|
||||
.replacePropertyPlaceholders(name -> "HOSTNAME".equals(name) ? hostname : null)
|
||||
.build();
|
||||
assertThat(implicitEnvSettings.get("setting1"), equalTo(hostname));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue