PropertyManager { public Optional<String> property(String key) { if (!properties.containsKey(key)) return Optional.empty(); String envVarName = envVarName(key); String value = System.getenv(envVarName); if (!Strings.isBlank(value)) { logger.info("found overridden property by env var {}, key={}, value={}", envVarName, key, maskValue(key, value)); return Optional.of(value); } value = System.getProperty(key); if (!Strings.isBlank(value)) { logger.info("found overridden property by system property -D{}, key={}, value={}", key, key, maskValue(key, value)); return Optional.of(value); } return properties.get(key); }  Optional<String> property(String key); String maskValue(String key, String value); final Properties properties; }
@Test void property() { System.setProperty("sys.cache.host", "overrideHost"); propertyManager.properties.set("sys.cache.host", "host"); assertThat(propertyManager.property("sys.cache.host")).hasValue("overrideHost"); System.clearProperty("sys.cache.host"); assertThat(propertyManager.property("sys.cache.host")).hasValue("host"); }
@Test void overridePropertyWithEmptyValue() { System.setProperty("sys.cache.host", "overrideHost"); propertyManager.properties.set("sys.cache.host", ""); assertThat(propertyManager.property("sys.cache.host")).hasValue("overrideHost"); }
@Test void ignoreEnvIfKeyNotDefinedInProperties() { System.setProperty("sys.cache.host", "overrideHost"); assertThat(propertyManager.property("sys.cache.host")).isEmpty(); }