39
39
*/
40
40
class PropertySourcesPropertyResolverTests {
41
41
42
- private Properties testProperties ;
42
+ private final Properties testProperties = new Properties () ;
43
43
44
- private MutablePropertySources propertySources ;
44
+ private final MutablePropertySources propertySources = new MutablePropertySources () ;
45
45
46
- private PropertySourcesPropertyResolver propertyResolver ;
46
+ private final PropertySourcesPropertyResolver propertyResolver = new PropertySourcesPropertyResolver ( propertySources ) ;
47
47
48
48
49
49
@ BeforeEach
50
50
void setUp () {
51
- propertySources = new MutablePropertySources ();
52
- propertyResolver = new PropertySourcesPropertyResolver (propertySources );
53
- testProperties = new Properties ();
54
51
propertySources .addFirst (new PropertiesPropertySource ("testProperties" , testProperties ));
55
52
}
56
53
@@ -78,14 +75,12 @@ void getProperty_withDefaultValue() {
78
75
79
76
@ Test
80
77
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" );
89
84
}
90
85
91
86
@ Test
@@ -116,8 +111,8 @@ void getProperty_withNonConvertibleTargetType() {
116
111
117
112
class TestType { }
118
113
119
- assertThatExceptionOfType (ConverterNotFoundException .class ). isThrownBy (() ->
120
- propertyResolver .getProperty ("foo" , TestType .class ));
114
+ assertThatExceptionOfType (ConverterNotFoundException .class )
115
+ . isThrownBy (() -> propertyResolver .getProperty ("foo" , TestType .class ));
121
116
}
122
117
123
118
@ Test
@@ -128,7 +123,6 @@ void getProperty_doesNotCache_replaceExistingKeyPostConstruction() {
128
123
129
124
HashMap <String , Object > map = new HashMap <>();
130
125
map .put (key , value1 ); // before construction
131
- MutablePropertySources propertySources = new MutablePropertySources ();
132
126
propertySources .addFirst (new MapPropertySource ("testProperties" , map ));
133
127
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver (propertySources );
134
128
assertThat (propertyResolver .getProperty (key )).isEqualTo (value1 );
@@ -139,7 +133,6 @@ void getProperty_doesNotCache_replaceExistingKeyPostConstruction() {
139
133
@ Test
140
134
void getProperty_doesNotCache_addNewKeyPostConstruction () {
141
135
HashMap <String , Object > map = new HashMap <>();
142
- MutablePropertySources propertySources = new MutablePropertySources ();
143
136
propertySources .addFirst (new MapPropertySource ("testProperties" , map ));
144
137
PropertyResolver propertyResolver = new PropertySourcesPropertyResolver (propertySources );
145
138
assertThat (propertyResolver .getProperty ("foo" )).isNull ();
@@ -149,10 +142,9 @@ void getProperty_doesNotCache_addNewKeyPostConstruction() {
149
142
150
143
@ Test
151
144
void getPropertySources_replacePropertySource () {
152
- propertySources = new MutablePropertySources ();
153
- propertyResolver = new PropertySourcesPropertyResolver (propertySources );
154
145
propertySources .addLast (new MockPropertySource ("local" ).withProperty ("foo" , "localValue" ));
155
146
propertySources .addLast (new MockPropertySource ("system" ).withProperty ("foo" , "systemValue" ));
147
+ assertThat (propertySources ).hasSize (3 );
156
148
157
149
// 'local' was added first so has precedence
158
150
assertThat (propertyResolver .getProperty ("foo" )).isEqualTo ("localValue" );
@@ -163,89 +155,73 @@ void getPropertySources_replacePropertySource() {
163
155
// 'system' now has precedence
164
156
assertThat (propertyResolver .getProperty ("foo" )).isEqualTo ("newValue" );
165
157
166
- assertThat (propertySources ).hasSize (2 );
158
+ assertThat (propertySources ).hasSize (3 );
167
159
}
168
160
169
161
@ Test
170
162
void getRequiredProperty () {
171
163
testProperties .put ("exists" , "xyz" );
172
164
assertThat (propertyResolver .getRequiredProperty ("exists" )).isEqualTo ("xyz" );
173
165
174
- assertThatIllegalStateException ().isThrownBy (() ->
175
- propertyResolver .getRequiredProperty ("bogus" ));
166
+ assertThatIllegalStateException ().isThrownBy (() -> propertyResolver .getRequiredProperty ("bogus" ));
176
167
}
177
168
178
169
@ Test
179
170
void getRequiredProperty_withStringArrayConversion () {
180
171
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" );
182
173
183
- assertThatIllegalStateException ().isThrownBy (() ->
184
- propertyResolver .getRequiredProperty ("bogus" , String [].class ));
174
+ assertThatIllegalStateException ().isThrownBy (() -> propertyResolver .getRequiredProperty ("bogus" , String [].class ));
185
175
}
186
176
187
177
@ Test
188
178
void resolvePlaceholders () {
189
- MutablePropertySources propertySources = new MutablePropertySources ();
190
179
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" );
193
181
}
194
182
195
183
@ Test
196
184
void resolvePlaceholders_withUnresolvable () {
197
- MutablePropertySources propertySources = new MutablePropertySources ();
198
185
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}" ))
201
187
.isEqualTo ("Replace this value plus ${unknown}" );
202
188
}
203
189
204
190
@ Test
205
191
void resolvePlaceholders_withDefaultValue () {
206
- MutablePropertySources propertySources = new MutablePropertySources ();
207
192
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}" ))
210
194
.isEqualTo ("Replace this value plus defaultValue" );
211
195
}
212
196
213
197
@ Test
214
198
void resolvePlaceholders_withNullInput () {
215
- assertThatIllegalArgumentException ().isThrownBy (() ->
216
- new PropertySourcesPropertyResolver (new MutablePropertySources ()).resolvePlaceholders (null ));
199
+ assertThatIllegalArgumentException ().isThrownBy (() -> propertyResolver .resolvePlaceholders (null ));
217
200
}
218
201
219
202
@ Test
220
203
void resolveRequiredPlaceholders () {
221
- MutablePropertySources propertySources = new MutablePropertySources ();
222
204
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" );
225
206
}
226
207
227
208
@ Test
228
209
void resolveRequiredPlaceholders_withUnresolvable () {
229
- MutablePropertySources propertySources = new MutablePropertySources ();
230
210
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}" ));
234
213
}
235
214
236
215
@ Test
237
216
void resolveRequiredPlaceholders_withDefaultValue () {
238
- MutablePropertySources propertySources = new MutablePropertySources ();
239
217
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}" ))
242
219
.isEqualTo ("Replace this value plus defaultValue" );
243
220
}
244
221
245
222
@ Test
246
223
void resolveRequiredPlaceholders_withNullInput () {
247
- assertThatIllegalArgumentException ().isThrownBy (() ->
248
- new PropertySourcesPropertyResolver (new MutablePropertySources ()).resolveRequiredPlaceholders (null ));
224
+ assertThatIllegalArgumentException ().isThrownBy (() -> propertyResolver .resolveRequiredPlaceholders (null ));
249
225
}
250
226
251
227
@ Test
@@ -257,17 +233,17 @@ void setRequiredProperties_andValidateRequiredProperties() {
257
233
propertyResolver .setRequiredProperties ("foo" , "bar" );
258
234
259
235
// 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]" );
264
240
265
241
// add foo property -> validation should fail only on missing 'bar' property
266
242
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]" );
271
247
272
248
// add bar property -> validation should pass, even with an empty string value
273
249
testProperties .put ("bar" , "" );
@@ -292,13 +268,13 @@ void resolveNestedPropertyPlaceholders() {
292
268
assertThat (pr .getProperty ("p2" )).isEqualTo ("v2" );
293
269
assertThat (pr .getProperty ("p3" )).isEqualTo ("v1:v2" );
294
270
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}\" " );
298
274
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" );
302
278
}
303
279
304
280
@ Test
@@ -350,9 +326,9 @@ void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
350
326
351
327
// placeholders nested within the value of "p4" are unresolvable and cause an
352
328
// 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}\" " );
356
332
357
333
// relax the treatment of unresolvable nested placeholders
358
334
pr .setIgnoreUnresolvableNestedPlaceholders (true );
@@ -362,9 +338,9 @@ void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
362
338
// resolve[Nested]Placeholders methods behave as usual regardless the value of
363
339
// ignoreUnresolvableNestedPlaceholders
364
340
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}\" " );
368
344
}
369
345
370
346
0 commit comments