|
54 | 54 | *
|
55 | 55 | * @author Christian Dupuis
|
56 | 56 | * @author Phillip Webb
|
| 57 | + * @author Stephane Nicoll |
57 | 58 | */
|
58 | 59 | public class ConfigurationPropertiesBindingPostProcessorTests {
|
59 | 60 |
|
@@ -133,6 +134,39 @@ public void testInitializersSeeBoundProperties() {
|
133 | 134 | this.context.refresh();
|
134 | 135 | }
|
135 | 136 |
|
| 137 | + @Test |
| 138 | + public void testValidationWithCustomValidator() { |
| 139 | + this.context = new AnnotationConfigApplicationContext(); |
| 140 | + this.context.register(TestConfigurationWithCustomValidator.class); |
| 141 | + try { |
| 142 | + this.context.refresh(); |
| 143 | + fail("Expected exception"); |
| 144 | + } |
| 145 | + catch (BeanCreationException ex) { |
| 146 | + BindException bex = (BindException) ex.getRootCause(); |
| 147 | + assertEquals(1, bex.getErrorCount()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testValidationWithCustomValidatorNotSupported() { |
| 153 | + MockEnvironment env = new MockEnvironment(); |
| 154 | + env.setProperty("test.foo", "bar"); |
| 155 | + this.context = new AnnotationConfigApplicationContext(); |
| 156 | + this.context.setEnvironment(env); |
| 157 | + this.context.register(TestConfigurationWithCustomValidator.class, |
| 158 | + PropertyWithValidatingSetter.class); |
| 159 | + try { |
| 160 | + // PropertyWithValidatingSetter should not use validator |
| 161 | + this.context.refresh(); |
| 162 | + fail("Expected exception"); |
| 163 | + } |
| 164 | + catch (BeanCreationException ex) { |
| 165 | + BindException bex = (BindException) ex.getRootCause(); |
| 166 | + assertEquals(1, bex.getErrorCount()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
136 | 170 | @Test
|
137 | 171 | public void testPropertyWithEnum() throws Exception {
|
138 | 172 | doEnumTest("test.theValue:foo");
|
@@ -373,6 +407,50 @@ public String getBar() {
|
373 | 407 |
|
374 | 408 | }
|
375 | 409 |
|
| 410 | + @Configuration |
| 411 | + @EnableConfigurationProperties |
| 412 | + public static class TestConfigurationWithCustomValidator { |
| 413 | + |
| 414 | + @Bean |
| 415 | + public PropertyWithCustomValidator propertyWithCustomValidator() { |
| 416 | + return new PropertyWithCustomValidator(); |
| 417 | + } |
| 418 | + |
| 419 | + @Bean |
| 420 | + public Validator configurationPropertiesValidator() { |
| 421 | + return new CustomPropertyValidator(); |
| 422 | + } |
| 423 | + |
| 424 | + } |
| 425 | + |
| 426 | + @ConfigurationProperties(prefix = "custom") |
| 427 | + public static class PropertyWithCustomValidator { |
| 428 | + |
| 429 | + private String foo; |
| 430 | + |
| 431 | + public String getFoo() { |
| 432 | + return foo; |
| 433 | + } |
| 434 | + |
| 435 | + public void setFoo(String foo) { |
| 436 | + this.foo = foo; |
| 437 | + } |
| 438 | + } |
| 439 | + |
| 440 | + public static class CustomPropertyValidator implements Validator { |
| 441 | + |
| 442 | + @Override |
| 443 | + public boolean supports(Class<?> aClass) { |
| 444 | + return aClass == PropertyWithCustomValidator.class; |
| 445 | + } |
| 446 | + |
| 447 | + @Override |
| 448 | + public void validate(Object o, Errors errors) { |
| 449 | + ValidationUtils.rejectIfEmpty(errors, "foo", "TEST1"); |
| 450 | + } |
| 451 | + |
| 452 | + } |
| 453 | + |
376 | 454 | @Configuration
|
377 | 455 | @EnableConfigurationProperties
|
378 | 456 | @ConfigurationProperties(prefix = "test", ignoreUnknownFields = false)
|
|
0 commit comments