Skip to content

Commit 12f6330

Browse files
committed
Improve code coverage of PropertyPlaceholderHelper
See gh-26268
1 parent fc0ea46 commit 12f6330

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

spring-core/src/test/java/org/springframework/util/PropertyPlaceholderHelperTests.java

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,29 @@
1717
package org.springframework.util;
1818

1919
import java.util.Properties;
20+
import java.util.stream.Stream;
2021

22+
import org.junit.jupiter.api.Disabled;
23+
import org.junit.jupiter.api.Nested;
2124
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.Arguments;
27+
import org.junit.jupiter.params.provider.MethodSource;
2228

2329
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
2430

2531
import static org.assertj.core.api.Assertions.assertThat;
2632
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
33+
import static org.mockito.BDDMockito.given;
34+
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.verify;
36+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2737

2838
/**
39+
* Tests for {@link PropertyPlaceholderHelper}.
40+
*
2941
* @author Rob Harrop
42+
* @author Stephane Nicoll
3043
*/
3144
class PropertyPlaceholderHelperTests {
3245

@@ -106,6 +119,57 @@ void unresolvedPlaceholderAsError() {
106119
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, false);
107120
assertThatIllegalArgumentException().isThrownBy(() ->
108121
helper.replacePlaceholders(text, props));
122+
123+
}
124+
125+
@Nested
126+
class DefaultValueTests {
127+
128+
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", true);
129+
130+
@ParameterizedTest(name = "{0} -> {1}")
131+
@MethodSource("defaultValues")
132+
void defaultValueIsApplied(String text, String value) {
133+
Properties properties = new Properties();
134+
properties.setProperty("one", "1");
135+
properties.setProperty("two", "2");
136+
assertThat(this.helper.replacePlaceholders(text, properties)).isEqualTo(value);
137+
}
138+
139+
@Test
140+
@Disabled("gh-26268")
141+
void defaultValueIsNotEvaluatedEarly() {
142+
PlaceholderResolver resolver = mockPlaceholderResolver("one", "1");
143+
assertThat(this.helper.replacePlaceholders("This is ${one:or${two}}",resolver)).isEqualTo("This is 1");
144+
verify(resolver).resolvePlaceholder("one");
145+
verifyNoMoreInteractions(resolver);
146+
}
147+
148+
static Stream<Arguments> defaultValues() {
149+
return Stream.of(
150+
Arguments.of("${invalid:test}", "test"),
151+
Arguments.of("${invalid:${one}}", "1"),
152+
Arguments.of("${invalid:${one}${two}}", "12"),
153+
Arguments.of("${invalid:${one}:${two}}", "1:2"),
154+
Arguments.of("${invalid:${also_invalid:test}}", "test"),
155+
Arguments.of("${invalid:${also_invalid:${one}}}", "1")
156+
);
157+
}
158+
159+
}
160+
161+
PlaceholderResolver mockPlaceholderResolver(String... pairs) {
162+
if (pairs.length % 2 == 1) {
163+
throw new IllegalArgumentException("size must be even, it is a set of key=value pairs");
164+
}
165+
PlaceholderResolver resolver = mock(PlaceholderResolver.class);
166+
for (int i = 0; i < pairs.length; i += 2) {
167+
String key = pairs[i];
168+
String value = pairs[i + 1];
169+
given(resolver.resolvePlaceholder(key)).willReturn(value);
170+
}
171+
return resolver;
109172
}
110173

174+
111175
}

0 commit comments

Comments
 (0)