Polish PropertySourcesPropertyResolverTests
Backport Bot / build (push) Waiting to run Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Waiting to run Details
Build and Deploy Snapshot / Verify (push) Blocked by required conditions Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:23], map[id:ubuntu-latest name:Linux]) (push) Waiting to run Details
Deploy Docs / Dispatch docs deployment (push) Waiting to run Details

This commit is contained in:
Sam Brannen 2025-05-09 16:43:04 +02:00
parent 348b4cd067
commit 5a2cbc1ab3
1 changed files with 45 additions and 69 deletions

View File

@ -39,18 +39,15 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*/ */
class PropertySourcesPropertyResolverTests { class PropertySourcesPropertyResolverTests {
private Properties testProperties; private final Properties testProperties = new Properties();
private MutablePropertySources propertySources; private final MutablePropertySources propertySources = new MutablePropertySources();
private PropertySourcesPropertyResolver propertyResolver; private final PropertySourcesPropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
@BeforeEach @BeforeEach
void setUp() { void setUp() {
propertySources = new MutablePropertySources();
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
testProperties = new Properties();
propertySources.addFirst(new PropertiesPropertySource("testProperties", testProperties)); propertySources.addFirst(new PropertiesPropertySource("testProperties", testProperties));
} }
@ -78,14 +75,12 @@ class PropertySourcesPropertyResolverTests {
@Test @Test
void getProperty_propertySourceSearchOrderIsFIFO() { void getProperty_propertySourceSearchOrderIsFIFO() {
MutablePropertySources sources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource("ps1").withProperty("pName", "ps1Value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(sources); assertThat(propertyResolver.getProperty("pName")).isEqualTo("ps1Value");
sources.addFirst(new MockPropertySource("ps1").withProperty("pName", "ps1Value")); propertySources.addFirst(new MockPropertySource("ps2").withProperty("pName", "ps2Value"));
assertThat(resolver.getProperty("pName")).isEqualTo("ps1Value"); assertThat(propertyResolver.getProperty("pName")).isEqualTo("ps2Value");
sources.addFirst(new MockPropertySource("ps2").withProperty("pName", "ps2Value")); propertySources.addFirst(new MockPropertySource("ps3").withProperty("pName", "ps3Value"));
assertThat(resolver.getProperty("pName")).isEqualTo("ps2Value"); assertThat(propertyResolver.getProperty("pName")).isEqualTo("ps3Value");
sources.addFirst(new MockPropertySource("ps3").withProperty("pName", "ps3Value"));
assertThat(resolver.getProperty("pName")).isEqualTo("ps3Value");
} }
@Test @Test
@ -116,8 +111,8 @@ class PropertySourcesPropertyResolverTests {
class TestType { } class TestType { }
assertThatExceptionOfType(ConverterNotFoundException.class).isThrownBy(() -> assertThatExceptionOfType(ConverterNotFoundException.class)
propertyResolver.getProperty("foo", TestType.class)); .isThrownBy(() -> propertyResolver.getProperty("foo", TestType.class));
} }
@Test @Test
@ -128,7 +123,6 @@ class PropertySourcesPropertyResolverTests {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put(key, value1); // before construction map.put(key, value1); // before construction
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MapPropertySource("testProperties", map)); propertySources.addFirst(new MapPropertySource("testProperties", map));
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
assertThat(propertyResolver.getProperty(key)).isEqualTo(value1); assertThat(propertyResolver.getProperty(key)).isEqualTo(value1);
@ -139,7 +133,6 @@ class PropertySourcesPropertyResolverTests {
@Test @Test
void getProperty_doesNotCache_addNewKeyPostConstruction() { void getProperty_doesNotCache_addNewKeyPostConstruction() {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MapPropertySource("testProperties", map)); propertySources.addFirst(new MapPropertySource("testProperties", map));
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
assertThat(propertyResolver.getProperty("foo")).isNull(); assertThat(propertyResolver.getProperty("foo")).isNull();
@ -149,10 +142,9 @@ class PropertySourcesPropertyResolverTests {
@Test @Test
void getPropertySources_replacePropertySource() { void getPropertySources_replacePropertySource() {
propertySources = new MutablePropertySources();
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue")); propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue"));
propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue")); propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue"));
assertThat(propertySources).hasSize(3);
// 'local' was added first so has precedence // 'local' was added first so has precedence
assertThat(propertyResolver.getProperty("foo")).isEqualTo("localValue"); assertThat(propertyResolver.getProperty("foo")).isEqualTo("localValue");
@ -163,7 +155,7 @@ class PropertySourcesPropertyResolverTests {
// 'system' now has precedence // 'system' now has precedence
assertThat(propertyResolver.getProperty("foo")).isEqualTo("newValue"); assertThat(propertyResolver.getProperty("foo")).isEqualTo("newValue");
assertThat(propertySources).hasSize(2); assertThat(propertySources).hasSize(3);
} }
@Test @Test
@ -171,81 +163,65 @@ class PropertySourcesPropertyResolverTests {
testProperties.put("exists", "xyz"); testProperties.put("exists", "xyz");
assertThat(propertyResolver.getRequiredProperty("exists")).isEqualTo("xyz"); assertThat(propertyResolver.getRequiredProperty("exists")).isEqualTo("xyz");
assertThatIllegalStateException().isThrownBy(() -> assertThatIllegalStateException().isThrownBy(() -> propertyResolver.getRequiredProperty("bogus"));
propertyResolver.getRequiredProperty("bogus"));
} }
@Test @Test
void getRequiredProperty_withStringArrayConversion() { void getRequiredProperty_withStringArrayConversion() {
testProperties.put("exists", "abc,123"); testProperties.put("exists", "abc,123");
assertThat(propertyResolver.getRequiredProperty("exists", String[].class)).isEqualTo(new String[] { "abc", "123" }); assertThat(propertyResolver.getRequiredProperty("exists", String[].class)).containsExactly("abc", "123");
assertThatIllegalStateException().isThrownBy(() -> assertThatIllegalStateException().isThrownBy(() -> propertyResolver.getRequiredProperty("bogus", String[].class));
propertyResolver.getRequiredProperty("bogus", String[].class));
} }
@Test @Test
void resolvePlaceholders() { void resolvePlaceholders() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.resolvePlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
assertThat(resolver.resolvePlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
} }
@Test @Test
void resolvePlaceholders_withUnresolvable() { void resolvePlaceholders_withUnresolvable() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.resolvePlaceholders("Replace this ${key} plus ${unknown}"))
assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown}"))
.isEqualTo("Replace this value plus ${unknown}"); .isEqualTo("Replace this value plus ${unknown}");
} }
@Test @Test
void resolvePlaceholders_withDefaultValue() { void resolvePlaceholders_withDefaultValue() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.resolvePlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
.isEqualTo("Replace this value plus defaultValue"); .isEqualTo("Replace this value plus defaultValue");
} }
@Test @Test
void resolvePlaceholders_withNullInput() { void resolvePlaceholders_withNullInput() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() -> propertyResolver.resolvePlaceholders(null));
new PropertySourcesPropertyResolver(new MutablePropertySources()).resolvePlaceholders(null));
} }
@Test @Test
void resolveRequiredPlaceholders() { void resolveRequiredPlaceholders() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.resolveRequiredPlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
} }
@Test @Test
void resolveRequiredPlaceholders_withUnresolvable() { void resolveRequiredPlaceholders_withUnresolvable() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThatExceptionOfType(PlaceholderResolutionException.class)
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() -> .isThrownBy(() -> propertyResolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}"));
resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}"));
} }
@Test @Test
void resolveRequiredPlaceholders_withDefaultValue() { void resolveRequiredPlaceholders_withDefaultValue() {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
.isEqualTo("Replace this value plus defaultValue"); .isEqualTo("Replace this value plus defaultValue");
} }
@Test @Test
void resolveRequiredPlaceholders_withNullInput() { void resolveRequiredPlaceholders_withNullInput() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() -> propertyResolver.resolveRequiredPlaceholders(null));
new PropertySourcesPropertyResolver(new MutablePropertySources()).resolveRequiredPlaceholders(null));
} }
@Test @Test
@ -257,17 +233,17 @@ class PropertySourcesPropertyResolverTests {
propertyResolver.setRequiredProperties("foo", "bar"); propertyResolver.setRequiredProperties("foo", "bar");
// neither foo nor bar properties are present -> validating should throw // neither foo nor bar properties are present -> validating should throw
assertThatExceptionOfType(MissingRequiredPropertiesException.class).isThrownBy( assertThatExceptionOfType(MissingRequiredPropertiesException.class)
propertyResolver::validateRequiredProperties) .isThrownBy(propertyResolver::validateRequiredProperties)
.withMessage("The following properties were declared as required " + .withMessage("The following properties were declared as required " +
"but could not be resolved: [foo, bar]"); "but could not be resolved: [foo, bar]");
// add foo property -> validation should fail only on missing 'bar' property // add foo property -> validation should fail only on missing 'bar' property
testProperties.put("foo", "fooValue"); testProperties.put("foo", "fooValue");
assertThatExceptionOfType(MissingRequiredPropertiesException.class).isThrownBy( assertThatExceptionOfType(MissingRequiredPropertiesException.class)
propertyResolver::validateRequiredProperties) .isThrownBy(propertyResolver::validateRequiredProperties)
.withMessage("The following properties were declared as required " + .withMessage("The following properties were declared as required " +
"but could not be resolved: [bar]"); "but could not be resolved: [bar]");
// add bar property -> validation should pass, even with an empty string value // add bar property -> validation should pass, even with an empty string value
testProperties.put("bar", ""); testProperties.put("bar", "");
@ -292,13 +268,13 @@ class PropertySourcesPropertyResolverTests {
assertThat(pr.getProperty("p2")).isEqualTo("v2"); assertThat(pr.getProperty("p2")).isEqualTo("v2");
assertThat(pr.getProperty("p3")).isEqualTo("v1:v2"); assertThat(pr.getProperty("p3")).isEqualTo("v1:v2");
assertThat(pr.getProperty("p4")).isEqualTo("v1:v2"); assertThat(pr.getProperty("p4")).isEqualTo("v1:v2");
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() -> assertThatExceptionOfType(PlaceholderResolutionException.class)
pr.getProperty("p5")) .isThrownBy(() -> pr.getProperty("p5"))
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""); .withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
assertThat(pr.getProperty("p6")).isEqualTo("v1:v2:def"); assertThat(pr.getProperty("p6")).isEqualTo("v1:v2:def");
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() -> assertThatExceptionOfType(PlaceholderResolutionException.class)
pr.getProperty("pL")) .isThrownBy(() -> pr.getProperty("pL"))
.withMessageContaining("Circular"); .withMessageContaining("Circular");
} }
@Test @Test
@ -350,9 +326,9 @@ class PropertySourcesPropertyResolverTests {
// placeholders nested within the value of "p4" are unresolvable and cause an // placeholders nested within the value of "p4" are unresolvable and cause an
// exception by default // exception by default
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() -> assertThatExceptionOfType(PlaceholderResolutionException.class)
pr.getProperty("p4")) .isThrownBy(() -> pr.getProperty("p4"))
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""); .withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
// relax the treatment of unresolvable nested placeholders // relax the treatment of unresolvable nested placeholders
pr.setIgnoreUnresolvableNestedPlaceholders(true); pr.setIgnoreUnresolvableNestedPlaceholders(true);
@ -362,9 +338,9 @@ class PropertySourcesPropertyResolverTests {
// resolve[Nested]Placeholders methods behave as usual regardless the value of // resolve[Nested]Placeholders methods behave as usual regardless the value of
// ignoreUnresolvableNestedPlaceholders // ignoreUnresolvableNestedPlaceholders
assertThat(pr.resolvePlaceholders("${p1}:${p2}:${bogus}")).isEqualTo("v1:v2:${bogus}"); assertThat(pr.resolvePlaceholders("${p1}:${p2}:${bogus}")).isEqualTo("v1:v2:${bogus}");
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() -> assertThatExceptionOfType(PlaceholderResolutionException.class)
pr.resolveRequiredPlaceholders("${p1}:${p2}:${bogus}")) .isThrownBy(() -> pr.resolveRequiredPlaceholders("${p1}:${p2}:${bogus}"))
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""); .withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
} }