Skip to content

Commit 5a2cbc1

Browse files
committed
Polish PropertySourcesPropertyResolverTests
1 parent 348b4cd commit 5a2cbc1

File tree

1 file changed

+45
-69
lines changed

1 file changed

+45
-69
lines changed

spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,15 @@
3939
*/
4040
class PropertySourcesPropertyResolverTests {
4141

42-
private Properties testProperties;
42+
private final Properties testProperties = new Properties();
4343

44-
private MutablePropertySources propertySources;
44+
private final MutablePropertySources propertySources = new MutablePropertySources();
4545

46-
private PropertySourcesPropertyResolver propertyResolver;
46+
private final PropertySourcesPropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
4747

4848

4949
@BeforeEach
5050
void setUp() {
51-
propertySources = new MutablePropertySources();
52-
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
53-
testProperties = new Properties();
5451
propertySources.addFirst(new PropertiesPropertySource("testProperties", testProperties));
5552
}
5653

@@ -78,14 +75,12 @@ void getProperty_withDefaultValue() {
7875

7976
@Test
8077
void getProperty_propertySourceSearchOrderIsFIFO() {
81-
MutablePropertySources sources = new MutablePropertySources();
82-
PropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
83-
sources.addFirst(new MockPropertySource("ps1").withProperty("pName", "ps1Value"));
84-
assertThat(resolver.getProperty("pName")).isEqualTo("ps1Value");
85-
sources.addFirst(new MockPropertySource("ps2").withProperty("pName", "ps2Value"));
86-
assertThat(resolver.getProperty("pName")).isEqualTo("ps2Value");
87-
sources.addFirst(new MockPropertySource("ps3").withProperty("pName", "ps3Value"));
88-
assertThat(resolver.getProperty("pName")).isEqualTo("ps3Value");
78+
propertySources.addFirst(new MockPropertySource("ps1").withProperty("pName", "ps1Value"));
79+
assertThat(propertyResolver.getProperty("pName")).isEqualTo("ps1Value");
80+
propertySources.addFirst(new MockPropertySource("ps2").withProperty("pName", "ps2Value"));
81+
assertThat(propertyResolver.getProperty("pName")).isEqualTo("ps2Value");
82+
propertySources.addFirst(new MockPropertySource("ps3").withProperty("pName", "ps3Value"));
83+
assertThat(propertyResolver.getProperty("pName")).isEqualTo("ps3Value");
8984
}
9085

9186
@Test
@@ -116,8 +111,8 @@ void getProperty_withNonConvertibleTargetType() {
116111

117112
class TestType { }
118113

119-
assertThatExceptionOfType(ConverterNotFoundException.class).isThrownBy(() ->
120-
propertyResolver.getProperty("foo", TestType.class));
114+
assertThatExceptionOfType(ConverterNotFoundException.class)
115+
.isThrownBy(() -> propertyResolver.getProperty("foo", TestType.class));
121116
}
122117

123118
@Test
@@ -128,7 +123,6 @@ void getProperty_doesNotCache_replaceExistingKeyPostConstruction() {
128123

129124
HashMap<String, Object> map = new HashMap<>();
130125
map.put(key, value1); // before construction
131-
MutablePropertySources propertySources = new MutablePropertySources();
132126
propertySources.addFirst(new MapPropertySource("testProperties", map));
133127
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
134128
assertThat(propertyResolver.getProperty(key)).isEqualTo(value1);
@@ -139,7 +133,6 @@ void getProperty_doesNotCache_replaceExistingKeyPostConstruction() {
139133
@Test
140134
void getProperty_doesNotCache_addNewKeyPostConstruction() {
141135
HashMap<String, Object> map = new HashMap<>();
142-
MutablePropertySources propertySources = new MutablePropertySources();
143136
propertySources.addFirst(new MapPropertySource("testProperties", map));
144137
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
145138
assertThat(propertyResolver.getProperty("foo")).isNull();
@@ -149,10 +142,9 @@ void getProperty_doesNotCache_addNewKeyPostConstruction() {
149142

150143
@Test
151144
void getPropertySources_replacePropertySource() {
152-
propertySources = new MutablePropertySources();
153-
propertyResolver = new PropertySourcesPropertyResolver(propertySources);
154145
propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue"));
155146
propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue"));
147+
assertThat(propertySources).hasSize(3);
156148

157149
// 'local' was added first so has precedence
158150
assertThat(propertyResolver.getProperty("foo")).isEqualTo("localValue");
@@ -163,89 +155,73 @@ void getPropertySources_replacePropertySource() {
163155
// 'system' now has precedence
164156
assertThat(propertyResolver.getProperty("foo")).isEqualTo("newValue");
165157

166-
assertThat(propertySources).hasSize(2);
158+
assertThat(propertySources).hasSize(3);
167159
}
168160

169161
@Test
170162
void getRequiredProperty() {
171163
testProperties.put("exists", "xyz");
172164
assertThat(propertyResolver.getRequiredProperty("exists")).isEqualTo("xyz");
173165

174-
assertThatIllegalStateException().isThrownBy(() ->
175-
propertyResolver.getRequiredProperty("bogus"));
166+
assertThatIllegalStateException().isThrownBy(() -> propertyResolver.getRequiredProperty("bogus"));
176167
}
177168

178169
@Test
179170
void getRequiredProperty_withStringArrayConversion() {
180171
testProperties.put("exists", "abc,123");
181-
assertThat(propertyResolver.getRequiredProperty("exists", String[].class)).isEqualTo(new String[] { "abc", "123" });
172+
assertThat(propertyResolver.getRequiredProperty("exists", String[].class)).containsExactly("abc", "123");
182173

183-
assertThatIllegalStateException().isThrownBy(() ->
184-
propertyResolver.getRequiredProperty("bogus", String[].class));
174+
assertThatIllegalStateException().isThrownBy(() -> propertyResolver.getRequiredProperty("bogus", String[].class));
185175
}
186176

187177
@Test
188178
void resolvePlaceholders() {
189-
MutablePropertySources propertySources = new MutablePropertySources();
190179
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
191-
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
192-
assertThat(resolver.resolvePlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
180+
assertThat(propertyResolver.resolvePlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
193181
}
194182

195183
@Test
196184
void resolvePlaceholders_withUnresolvable() {
197-
MutablePropertySources propertySources = new MutablePropertySources();
198185
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
199-
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
200-
assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown}"))
186+
assertThat(propertyResolver.resolvePlaceholders("Replace this ${key} plus ${unknown}"))
201187
.isEqualTo("Replace this value plus ${unknown}");
202188
}
203189

204190
@Test
205191
void resolvePlaceholders_withDefaultValue() {
206-
MutablePropertySources propertySources = new MutablePropertySources();
207192
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
208-
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
209-
assertThat(resolver.resolvePlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
193+
assertThat(propertyResolver.resolvePlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
210194
.isEqualTo("Replace this value plus defaultValue");
211195
}
212196

213197
@Test
214198
void resolvePlaceholders_withNullInput() {
215-
assertThatIllegalArgumentException().isThrownBy(() ->
216-
new PropertySourcesPropertyResolver(new MutablePropertySources()).resolvePlaceholders(null));
199+
assertThatIllegalArgumentException().isThrownBy(() -> propertyResolver.resolvePlaceholders(null));
217200
}
218201

219202
@Test
220203
void resolveRequiredPlaceholders() {
221-
MutablePropertySources propertySources = new MutablePropertySources();
222204
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
223-
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
224-
assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
205+
assertThat(propertyResolver.resolveRequiredPlaceholders("Replace this ${key}")).isEqualTo("Replace this value");
225206
}
226207

227208
@Test
228209
void resolveRequiredPlaceholders_withUnresolvable() {
229-
MutablePropertySources propertySources = new MutablePropertySources();
230210
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
231-
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
232-
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
233-
resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}"));
211+
assertThatExceptionOfType(PlaceholderResolutionException.class)
212+
.isThrownBy(() -> propertyResolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}"));
234213
}
235214

236215
@Test
237216
void resolveRequiredPlaceholders_withDefaultValue() {
238-
MutablePropertySources propertySources = new MutablePropertySources();
239217
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
240-
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
241-
assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
218+
assertThat(propertyResolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown:defaultValue}"))
242219
.isEqualTo("Replace this value plus defaultValue");
243220
}
244221

245222
@Test
246223
void resolveRequiredPlaceholders_withNullInput() {
247-
assertThatIllegalArgumentException().isThrownBy(() ->
248-
new PropertySourcesPropertyResolver(new MutablePropertySources()).resolveRequiredPlaceholders(null));
224+
assertThatIllegalArgumentException().isThrownBy(() -> propertyResolver.resolveRequiredPlaceholders(null));
249225
}
250226

251227
@Test
@@ -257,17 +233,17 @@ void setRequiredProperties_andValidateRequiredProperties() {
257233
propertyResolver.setRequiredProperties("foo", "bar");
258234

259235
// neither foo nor bar properties are present -> validating should throw
260-
assertThatExceptionOfType(MissingRequiredPropertiesException.class).isThrownBy(
261-
propertyResolver::validateRequiredProperties)
262-
.withMessage("The following properties were declared as required " +
263-
"but could not be resolved: [foo, bar]");
236+
assertThatExceptionOfType(MissingRequiredPropertiesException.class)
237+
.isThrownBy(propertyResolver::validateRequiredProperties)
238+
.withMessage("The following properties were declared as required " +
239+
"but could not be resolved: [foo, bar]");
264240

265241
// add foo property -> validation should fail only on missing 'bar' property
266242
testProperties.put("foo", "fooValue");
267-
assertThatExceptionOfType(MissingRequiredPropertiesException.class).isThrownBy(
268-
propertyResolver::validateRequiredProperties)
269-
.withMessage("The following properties were declared as required " +
270-
"but could not be resolved: [bar]");
243+
assertThatExceptionOfType(MissingRequiredPropertiesException.class)
244+
.isThrownBy(propertyResolver::validateRequiredProperties)
245+
.withMessage("The following properties were declared as required " +
246+
"but could not be resolved: [bar]");
271247

272248
// add bar property -> validation should pass, even with an empty string value
273249
testProperties.put("bar", "");
@@ -292,13 +268,13 @@ void resolveNestedPropertyPlaceholders() {
292268
assertThat(pr.getProperty("p2")).isEqualTo("v2");
293269
assertThat(pr.getProperty("p3")).isEqualTo("v1:v2");
294270
assertThat(pr.getProperty("p4")).isEqualTo("v1:v2");
295-
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
296-
pr.getProperty("p5"))
297-
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
271+
assertThatExceptionOfType(PlaceholderResolutionException.class)
272+
.isThrownBy(() -> pr.getProperty("p5"))
273+
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
298274
assertThat(pr.getProperty("p6")).isEqualTo("v1:v2:def");
299-
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
300-
pr.getProperty("pL"))
301-
.withMessageContaining("Circular");
275+
assertThatExceptionOfType(PlaceholderResolutionException.class)
276+
.isThrownBy(() -> pr.getProperty("pL"))
277+
.withMessageContaining("Circular");
302278
}
303279

304280
@Test
@@ -350,9 +326,9 @@ void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
350326

351327
// placeholders nested within the value of "p4" are unresolvable and cause an
352328
// exception by default
353-
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
354-
pr.getProperty("p4"))
355-
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
329+
assertThatExceptionOfType(PlaceholderResolutionException.class)
330+
.isThrownBy(() -> pr.getProperty("p4"))
331+
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
356332

357333
// relax the treatment of unresolvable nested placeholders
358334
pr.setIgnoreUnresolvableNestedPlaceholders(true);
@@ -362,9 +338,9 @@ void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
362338
// resolve[Nested]Placeholders methods behave as usual regardless the value of
363339
// ignoreUnresolvableNestedPlaceholders
364340
assertThat(pr.resolvePlaceholders("${p1}:${p2}:${bogus}")).isEqualTo("v1:v2:${bogus}");
365-
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
366-
pr.resolveRequiredPlaceholders("${p1}:${p2}:${bogus}"))
367-
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
341+
assertThatExceptionOfType(PlaceholderResolutionException.class)
342+
.isThrownBy(() -> pr.resolveRequiredPlaceholders("${p1}:${p2}:${bogus}"))
343+
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
368344
}
369345

370346

0 commit comments

Comments
 (0)