Skip to content

Commit 9d78f17

Browse files
committed
Adapted PropertySourcesPropertyResolverTests for different error message format
1 parent 245d311 commit 9d78f17

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

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

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import java.util.Map;
2121
import java.util.Properties;
2222

23-
import org.hamcrest.Matchers;
2423
import org.junit.Before;
2524
import org.junit.Test;
2625

2726
import org.springframework.core.convert.ConversionException;
2827
import org.springframework.mock.env.MockPropertySource;
2928

30-
import static org.hamcrest.CoreMatchers.*;
29+
import static org.hamcrest.Matchers.*;
3130
import static org.junit.Assert.*;
3231

3332
/**
@@ -37,10 +36,14 @@
3736
* @since 3.1
3837
*/
3938
public class PropertySourcesPropertyResolverTests {
39+
4040
private Properties testProperties;
41+
4142
private MutablePropertySources propertySources;
43+
4244
private ConfigurablePropertyResolver propertyResolver;
4345

46+
4447
@Before
4548
public void setUp() {
4649
propertySources = new MutablePropertySources();
@@ -49,6 +52,7 @@ public void setUp() {
4952
propertySources.addFirst(new PropertiesPropertySource("testProperties", testProperties));
5053
}
5154

55+
5256
@Test
5357
public void containsProperty() {
5458
assertThat(propertyResolver.containsProperty("foo"), is(false));
@@ -104,7 +108,6 @@ public void getProperty_withStringArrayConversion() {
104108
assertThat(propertyResolver.getProperty("foo", String[].class), equalTo(new String[] { "bar", "baz" }));
105109
}
106110

107-
108111
@Test
109112
public void getProperty_withNonConvertibleTargetType() {
110113
testProperties.put("foo", "bar");
@@ -114,7 +117,8 @@ class TestType { }
114117
try {
115118
propertyResolver.getProperty("foo", TestType.class);
116119
fail("Expected IllegalArgumentException due to non-convertible types");
117-
} catch (IllegalArgumentException ex) {
120+
}
121+
catch (IllegalArgumentException ex) {
118122
// expected
119123
}
120124
}
@@ -173,7 +177,8 @@ public void getRequiredProperty() {
173177
try {
174178
propertyResolver.getRequiredProperty("bogus");
175179
fail("expected IllegalStateException");
176-
} catch (IllegalStateException ex) {
180+
}
181+
catch (IllegalStateException ex) {
177182
// expected
178183
}
179184
}
@@ -186,7 +191,8 @@ public void getRequiredProperty_withStringArrayConversion() {
186191
try {
187192
propertyResolver.getRequiredProperty("bogus", String[].class);
188193
fail("expected IllegalStateException");
189-
} catch (IllegalStateException ex) {
194+
}
195+
catch (IllegalStateException ex) {
190196
// expected
191197
}
192198
}
@@ -328,21 +334,23 @@ public void setRequiredProperties_andValidateRequiredProperties() {
328334
try {
329335
propertyResolver.validateRequiredProperties();
330336
fail("expected validation exception");
331-
} catch (MissingRequiredPropertiesException ex) {
337+
}
338+
catch (MissingRequiredPropertiesException ex) {
332339
assertThat(ex.getMessage(), equalTo(
333340
"The following properties were declared as required " +
334-
"but could not be resolved: [foo, bar]"));
341+
"but could not be resolved: [foo, bar]"));
335342
}
336343

337344
// add foo property -> validation should fail only on missing 'bar' property
338345
testProperties.put("foo", "fooValue");
339346
try {
340347
propertyResolver.validateRequiredProperties();
341348
fail("expected validation exception");
342-
} catch (MissingRequiredPropertiesException ex) {
349+
}
350+
catch (MissingRequiredPropertiesException ex) {
343351
assertThat(ex.getMessage(), equalTo(
344352
"The following properties were declared as required " +
345-
"but could not be resolved: [bar]"));
353+
"but could not be resolved: [bar]"));
346354
}
347355

348356
// add bar property -> validation should pass, even with an empty string value
@@ -354,35 +362,41 @@ public void setRequiredProperties_andValidateRequiredProperties() {
354362
public void resolveNestedPropertyPlaceholders() {
355363
MutablePropertySources ps = new MutablePropertySources();
356364
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
365373
);
366-
PropertySourcesPropertyResolver pr = new PropertySourcesPropertyResolver(ps);
374+
ConfigurablePropertyResolver pr = new PropertySourcesPropertyResolver(ps);
367375
assertThat(pr.getProperty("p1"), equalTo("v1"));
368376
assertThat(pr.getProperty("p2"), equalTo("v2"));
369377
assertThat(pr.getProperty("p3"), equalTo("v1:v2"));
370378
assertThat(pr.getProperty("p4"), equalTo("v1:v2"));
371379
try {
372380
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}\""));
376385
}
377386
assertThat(pr.getProperty("p6"), equalTo("v1:v2:def"));
378387
try {
379388
pr.getProperty("pL");
380-
} catch (StackOverflowError ex) {
389+
}
390+
catch (StackOverflowError ex) {
381391
// no explicit handling for cyclic references for now
382392
}
383393
}
384394

385395

386-
static interface SomeType { }
387-
static class SpecificType implements SomeType { }
396+
interface SomeType {
397+
}
398+
399+
static class SpecificType implements SomeType {
400+
}
401+
388402
}

0 commit comments

Comments
 (0)