20
20
import java .util .Map ;
21
21
import java .util .Properties ;
22
22
23
- import org .hamcrest .Matchers ;
24
23
import org .junit .Before ;
25
24
import org .junit .Test ;
26
25
27
26
import org .springframework .core .convert .ConversionException ;
28
27
import org .springframework .mock .env .MockPropertySource ;
29
28
30
- import static org .hamcrest .CoreMatchers .*;
29
+ import static org .hamcrest .Matchers .*;
31
30
import static org .junit .Assert .*;
32
31
33
32
/**
37
36
* @since 3.1
38
37
*/
39
38
public class PropertySourcesPropertyResolverTests {
39
+
40
40
private Properties testProperties ;
41
+
41
42
private MutablePropertySources propertySources ;
43
+
42
44
private ConfigurablePropertyResolver propertyResolver ;
43
45
46
+
44
47
@ Before
45
48
public void setUp () {
46
49
propertySources = new MutablePropertySources ();
@@ -49,6 +52,7 @@ public void setUp() {
49
52
propertySources .addFirst (new PropertiesPropertySource ("testProperties" , testProperties ));
50
53
}
51
54
55
+
52
56
@ Test
53
57
public void containsProperty () {
54
58
assertThat (propertyResolver .containsProperty ("foo" ), is (false ));
@@ -104,7 +108,6 @@ public void getProperty_withStringArrayConversion() {
104
108
assertThat (propertyResolver .getProperty ("foo" , String [].class ), equalTo (new String [] { "bar" , "baz" }));
105
109
}
106
110
107
-
108
111
@ Test
109
112
public void getProperty_withNonConvertibleTargetType () {
110
113
testProperties .put ("foo" , "bar" );
@@ -114,7 +117,8 @@ class TestType { }
114
117
try {
115
118
propertyResolver .getProperty ("foo" , TestType .class );
116
119
fail ("Expected IllegalArgumentException due to non-convertible types" );
117
- } catch (IllegalArgumentException ex ) {
120
+ }
121
+ catch (IllegalArgumentException ex ) {
118
122
// expected
119
123
}
120
124
}
@@ -173,7 +177,8 @@ public void getRequiredProperty() {
173
177
try {
174
178
propertyResolver .getRequiredProperty ("bogus" );
175
179
fail ("expected IllegalStateException" );
176
- } catch (IllegalStateException ex ) {
180
+ }
181
+ catch (IllegalStateException ex ) {
177
182
// expected
178
183
}
179
184
}
@@ -186,7 +191,8 @@ public void getRequiredProperty_withStringArrayConversion() {
186
191
try {
187
192
propertyResolver .getRequiredProperty ("bogus" , String [].class );
188
193
fail ("expected IllegalStateException" );
189
- } catch (IllegalStateException ex ) {
194
+ }
195
+ catch (IllegalStateException ex ) {
190
196
// expected
191
197
}
192
198
}
@@ -328,21 +334,23 @@ public void setRequiredProperties_andValidateRequiredProperties() {
328
334
try {
329
335
propertyResolver .validateRequiredProperties ();
330
336
fail ("expected validation exception" );
331
- } catch (MissingRequiredPropertiesException ex ) {
337
+ }
338
+ catch (MissingRequiredPropertiesException ex ) {
332
339
assertThat (ex .getMessage (), equalTo (
333
340
"The following properties were declared as required " +
334
- "but could not be resolved: [foo, bar]" ));
341
+ "but could not be resolved: [foo, bar]" ));
335
342
}
336
343
337
344
// add foo property -> validation should fail only on missing 'bar' property
338
345
testProperties .put ("foo" , "fooValue" );
339
346
try {
340
347
propertyResolver .validateRequiredProperties ();
341
348
fail ("expected validation exception" );
342
- } catch (MissingRequiredPropertiesException ex ) {
349
+ }
350
+ catch (MissingRequiredPropertiesException ex ) {
343
351
assertThat (ex .getMessage (), equalTo (
344
352
"The following properties were declared as required " +
345
- "but could not be resolved: [bar]" ));
353
+ "but could not be resolved: [bar]" ));
346
354
}
347
355
348
356
// add bar property -> validation should pass, even with an empty string value
@@ -354,35 +362,41 @@ public void setRequiredProperties_andValidateRequiredProperties() {
354
362
public void resolveNestedPropertyPlaceholders () {
355
363
MutablePropertySources ps = new MutablePropertySources ();
356
364
ps .addFirst (new MockPropertySource ()
357
- .withProperty ("p1" , "v1" )
358
- .withProperty ("p2" , "v2" )
359
- .withProperty ("p3" , "${p1}:${p2}" ) // nested placeholders
360
- .withProperty ("p4" , "${p3}" ) // deeply nested placeholders
361
- .withProperty ("p5" , "${p1}:${p2}:${bogus}" ) // unresolvable placeholder
362
- .withProperty ("p6" , "${p1}:${p2}:${bogus:def}" ) // unresolvable w/ default
363
- .withProperty ("pL" , "${pR}" ) // cyclic reference left
364
- .withProperty ("pR" , "${pL}" ) // cyclic reference right
365
+ .withProperty ("p1" , "v1" )
366
+ .withProperty ("p2" , "v2" )
367
+ .withProperty ("p3" , "${p1}:${p2}" ) // nested placeholders
368
+ .withProperty ("p4" , "${p3}" ) // deeply nested placeholders
369
+ .withProperty ("p5" , "${p1}:${p2}:${bogus}" ) // unresolvable placeholder
370
+ .withProperty ("p6" , "${p1}:${p2}:${bogus:def}" ) // unresolvable w/ default
371
+ .withProperty ("pL" , "${pR}" ) // cyclic reference left
372
+ .withProperty ("pR" , "${pL}" ) // cyclic reference right
365
373
);
366
- PropertySourcesPropertyResolver pr = new PropertySourcesPropertyResolver (ps );
374
+ ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver (ps );
367
375
assertThat (pr .getProperty ("p1" ), equalTo ("v1" ));
368
376
assertThat (pr .getProperty ("p2" ), equalTo ("v2" ));
369
377
assertThat (pr .getProperty ("p3" ), equalTo ("v1:v2" ));
370
378
assertThat (pr .getProperty ("p4" ), equalTo ("v1:v2" ));
371
379
try {
372
380
pr .getProperty ("p5" );
373
- } catch (IllegalArgumentException ex ) {
374
- assertThat (ex .getMessage (), Matchers .containsString (
375
- "Could not resolve placeholder 'bogus' in string value [${p1}:${p2}:${bogus}]" ));
381
+ }
382
+ catch (IllegalArgumentException ex ) {
383
+ assertThat (ex .getMessage (), containsString (
384
+ "Could not resolve placeholder 'bogus' in string value \" ${p1}:${p2}:${bogus}\" " ));
376
385
}
377
386
assertThat (pr .getProperty ("p6" ), equalTo ("v1:v2:def" ));
378
387
try {
379
388
pr .getProperty ("pL" );
380
- } catch (StackOverflowError ex ) {
389
+ }
390
+ catch (StackOverflowError ex ) {
381
391
// no explicit handling for cyclic references for now
382
392
}
383
393
}
384
394
385
395
386
- static interface SomeType { }
387
- static class SpecificType implements SomeType { }
396
+ interface SomeType {
397
+ }
398
+
399
+ static class SpecificType implements SomeType {
400
+ }
401
+
388
402
}
0 commit comments