|
|
|
@ -17,13 +17,11 @@ |
|
|
|
package org.springframework.util; |
|
|
|
package org.springframework.util; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Nested; |
|
|
|
import org.junit.jupiter.api.Nested; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.params.ParameterizedTest; |
|
|
|
import org.junit.jupiter.params.ParameterizedTest; |
|
|
|
import org.junit.jupiter.params.provider.Arguments; |
|
|
|
import org.junit.jupiter.params.provider.CsvSource; |
|
|
|
import org.junit.jupiter.params.provider.MethodSource; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; |
|
|
|
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; |
|
|
|
|
|
|
|
|
|
|
|
@ -44,6 +42,7 @@ class PropertyPlaceholderHelperTests { |
|
|
|
|
|
|
|
|
|
|
|
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}"); |
|
|
|
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void withProperties() { |
|
|
|
void withProperties() { |
|
|
|
String text = "foo=${foo}"; |
|
|
|
String text = "foo=${foo}"; |
|
|
|
@ -116,8 +115,8 @@ class PropertyPlaceholderHelperTests { |
|
|
|
props.setProperty("foo", "bar"); |
|
|
|
props.setProperty("foo", "bar"); |
|
|
|
|
|
|
|
|
|
|
|
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, null, false); |
|
|
|
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, null, false); |
|
|
|
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(PlaceholderResolutionException.class) |
|
|
|
helper.replacePlaceholders(text, props)); |
|
|
|
.isThrownBy(() -> helper.replacePlaceholders(text, props)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Nested |
|
|
|
@Nested |
|
|
|
@ -126,7 +125,14 @@ class PropertyPlaceholderHelperTests { |
|
|
|
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", null, true); |
|
|
|
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", null, true); |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "{0} -> {1}") |
|
|
|
@ParameterizedTest(name = "{0} -> {1}") |
|
|
|
@MethodSource("defaultValues") |
|
|
|
@CsvSource(delimiterString = "->", textBlock = """ |
|
|
|
|
|
|
|
${invalid:test} -> test |
|
|
|
|
|
|
|
${invalid:${one}} -> 1 |
|
|
|
|
|
|
|
${invalid:${one}${two}} -> 12 |
|
|
|
|
|
|
|
${invalid:${one}:${two}} -> 1:2 |
|
|
|
|
|
|
|
${invalid:${also_invalid:test}} -> test |
|
|
|
|
|
|
|
${invalid:${also_invalid:${one}}} -> 1 |
|
|
|
|
|
|
|
""") |
|
|
|
void defaultValueIsApplied(String text, String value) { |
|
|
|
void defaultValueIsApplied(String text, String value) { |
|
|
|
Properties properties = new Properties(); |
|
|
|
Properties properties = new Properties(); |
|
|
|
properties.setProperty("one", "1"); |
|
|
|
properties.setProperty("one", "1"); |
|
|
|
@ -142,24 +148,26 @@ class PropertyPlaceholderHelperTests { |
|
|
|
verify(resolver, never()).resolvePlaceholder("two"); |
|
|
|
verify(resolver, never()).resolvePlaceholder("two"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Stream<Arguments> defaultValues() { |
|
|
|
@ParameterizedTest(name = "{0} -> {1}") |
|
|
|
return Stream.of( |
|
|
|
@CsvSource(delimiterString = "->", textBlock = """ |
|
|
|
Arguments.of("${invalid:test}", "test"), |
|
|
|
${prefix://my-service} -> example-service
|
|
|
|
Arguments.of("${invalid:${one}}", "1"), |
|
|
|
${p1} -> example-service |
|
|
|
Arguments.of("${invalid:${one}${two}}", "12"), |
|
|
|
""") |
|
|
|
Arguments.of("${invalid:${one}:${two}}", "1:2"), |
|
|
|
void placeholdersWithExactMatchAreConsidered(String text, String expected) { |
|
|
|
Arguments.of("${invalid:${also_invalid:test}}", "test"), |
|
|
|
Properties properties = new Properties(); |
|
|
|
Arguments.of("${invalid:${also_invalid:${one}}}", "1") |
|
|
|
properties.setProperty("prefix://my-service", "example-service"); |
|
|
|
); |
|
|
|
properties.setProperty("px", "prefix"); |
|
|
|
|
|
|
|
properties.setProperty("p1", "${prefix://my-service}"); |
|
|
|
|
|
|
|
assertThat(this.helper.replacePlaceholders(text, properties)).isEqualTo(expected); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
PlaceholderResolver mockPlaceholderResolver(String... pairs) { |
|
|
|
|
|
|
|
|
|
|
|
private static PlaceholderResolver mockPlaceholderResolver(String... pairs) { |
|
|
|
if (pairs.length % 2 == 1) { |
|
|
|
if (pairs.length % 2 == 1) { |
|
|
|
throw new IllegalArgumentException("size must be even, it is a set of key=value pairs"); |
|
|
|
throw new IllegalArgumentException("size must be even, it is a set of key=value pairs"); |
|
|
|
} |
|
|
|
} |
|
|
|
PlaceholderResolver resolver = mock(PlaceholderResolver.class); |
|
|
|
PlaceholderResolver resolver = mock(); |
|
|
|
for (int i = 0; i < pairs.length; i += 2) { |
|
|
|
for (int i = 0; i < pairs.length; i += 2) { |
|
|
|
String key = pairs[i]; |
|
|
|
String key = pairs[i]; |
|
|
|
String value = pairs[i + 1]; |
|
|
|
String value = pairs[i + 1]; |
|
|
|
@ -168,5 +176,4 @@ class PropertyPlaceholderHelperTests { |
|
|
|
return resolver; |
|
|
|
return resolver; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|