### Question:
ConfigUtils { Optional<String> expandVariables(Properties props, String key, String defaultValue) { String value = System.getProperty(getSystemPropertyName(key)); if (value == null) { value = System.getenv(getEnvVarName(key)); } if (value == null) { value = props.getProperty(key, defaultValue); } return value == null || value.trim().isEmpty() ? Optional.empty() : Optional.of(expandVariables(props, value)); }  List<String> getNormalizedPackages(String packages); String getStringValue(Properties props, String propertyName, String defaultValue); String getStringValue2(
      Properties props, String propertyName1, String propertyName2, String defaultValue); Optional<String> getStringValue(Properties props, String propertyName); boolean getBooleanValue(Properties props, String key, boolean defaultValue); int getIntValue(Properties props, String key, int defaultValue); String getEnvVarName(String propertyName); String getSystemPropertyName(String key); List<File> getCommaSeparatedFileValues(String uriValues);  }### Answer:
@Test public void should_expand_variables_with_braces() { System.setProperty(MY_PROP1, "XXX"); System.setProperty(MY_PROP2, "YYY"); String userVariableName; if (System.getProperty("os.name").startsWith(("Windows"))) { userVariableName = "$USERNAME"; } else { userVariableName = "$USER"; } Properties props = new Properties(); props.setProperty(MY_PROP1, "XXX_from_props"); props.setProperty(MY_PROP3, "ZZZ"); String actual = expandVariables( props, userVariableName + " ${" + MY_PROP1 + "} foo ${" + MY_PROP2 + "} bar ${" + MY_PROP3 + "}"); assertThat(actual, is(System.getProperty("user.name") + " XXX foo YYY bar ZZZ")); }
@Test public void should_expand_variables_without_braces() { System.setProperty(MY_PROP1, "XXX"); System.setProperty(MY_PROP2, "YYY"); String userVariableName; if (System.getProperty("os.name").startsWith(("Windows"))) { userVariableName = "$USERNAME"; } else { userVariableName = "$USER"; } Properties props = new Properties(); props.setProperty(MY_PROP1, "XXX_from_props"); props.setProperty(MY_PROP3, "ZZZ"); String actual = expandVariables( props, userVariableName + " $" + MY_PROP1 + " foo $" + MY_PROP2 + " bar $" + MY_PROP3 + "${user.name}baz"); assertThat( actual, is( System.getProperty("user.name") + " XXX foo YYY bar ZZZ" + System.getProperty("user.name") + "baz")); }
@Test public void should_detect_codekvast_system_properties() { System.setProperty(CODEKVAST_APP_VERSION, "sysprop-appVersion"); Properties props = new Properties(); props.setProperty("appVersion", "some-app-version"); assertThat( expandVariables(props, "appVersion", "default-app-version"), is(Optional.of("sysprop-appVersion"))); }
@Test public void should_handle_missing_expansions() { assertThat( expandVariables(new Properties(), "foo $missingProp1 bar ${missing.prop2} baz"), is("foo $missingProp1 bar ${missing.prop2} baz")); output.expect(containsString("Unrecognized variable: $missingProp1")); output.expect(containsString("Unrecognized variable: ${missing.prop2}")); }