EnvironmentConstructor extends Constructor { Object getValue(String value) { Matcher matcher = pattern.matcher(value); if (environment != null && matcher.find()) { String variableName = matcher.group(1); String defaultValue = null; if (variableName.contains(":")) { String[] variableSplit = variableName.split(":",2); defaultValue = variableSplit[1]; variableName = variableSplit[0]; } String result = environment.get(variableName); if (result != null) { return result; } else if (defaultValue != null) { return defaultValue; } } return value; } EnvironmentConstructor(Map<String, String> environment);   }
@Test public void testBasicString() { Map<String, String> environment = new HashMap<>(); environment.put("VALUE", "SomeString"); EnvironmentConstructor env = new EnvironmentConstructor(environment); assertThat( env.getValue("${env.VALUE}") ).isEqualTo("SomeString"); }
@Test public void testUseDefaultValue() { Map<String, String> environment = new HashMap<>(); EnvironmentConstructor env = new EnvironmentConstructor(environment); assertThat( env.getValue("${env.VALUE:http: }
@Test public void testUseEnvWhenDefaultExistsValue() { Map<String, String> environment = new HashMap<>(); environment.put("VALUE", "SomeString"); EnvironmentConstructor env = new EnvironmentConstructor(environment); assertThat( env.getValue("${env.VALUE:other}") ).isEqualTo("SomeString"); }