Commit 954d1372 authored by Stephane Nicoll's avatar Stephane Nicoll

Reduce code duplication

parent 1173f771
...@@ -78,14 +78,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -78,14 +78,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.foo:spam"); EnvironmentTestUtils.addEnvironment(this.context, "test.foo:spam");
this.context.register(TestConfigurationWithValidatingSetter.class); this.context.register(TestConfigurationWithValidatingSetter.class);
try { assertBindingFailure(1);
this.context.refresh();
fail("Expected exception");
}
catch (BeanCreationException ex) {
BindException bex = (BindException) ex.getRootCause();
assertEquals(1, bex.getErrorCount());
}
} }
@Test @Test
...@@ -110,28 +103,14 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -110,28 +103,14 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
public void testValidationWithoutJSR303() { public void testValidationWithoutJSR303() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfigurationWithoutJSR303.class); this.context.register(TestConfigurationWithoutJSR303.class);
try { assertBindingFailure(1);
this.context.refresh();
fail("Expected exception");
}
catch (BeanCreationException ex) {
BindException bex = (BindException) ex.getRootCause();
assertEquals(1, bex.getErrorCount());
}
} }
@Test @Test
public void testValidationWithJSR303() { public void testValidationWithJSR303() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfigurationWithJSR303.class); this.context.register(TestConfigurationWithJSR303.class);
try { assertBindingFailure(2);
this.context.refresh();
fail("Expected exception");
}
catch (BeanCreationException ex) {
BindException bex = (BindException) ex.getRootCause();
assertEquals(2, bex.getErrorCount());
}
} }
@Test @Test
...@@ -159,14 +138,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -159,14 +138,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
public void testValidationWithCustomValidator() { public void testValidationWithCustomValidator() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfigurationWithCustomValidator.class); this.context.register(TestConfigurationWithCustomValidator.class);
try { assertBindingFailure(1);
this.context.refresh();
fail("Expected exception");
}
catch (BeanCreationException ex) {
BindException bex = (BindException) ex.getRootCause();
assertEquals(1, bex.getErrorCount());
}
} }
@Test @Test
...@@ -177,15 +149,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -177,15 +149,7 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
this.context.setEnvironment(env); this.context.setEnvironment(env);
this.context.register(TestConfigurationWithCustomValidator.class, this.context.register(TestConfigurationWithCustomValidator.class,
PropertyWithValidatingSetter.class); PropertyWithValidatingSetter.class);
try { assertBindingFailure(1);
// PropertyWithValidatingSetter should not use validator
this.context.refresh();
fail("Expected exception");
}
catch (BeanCreationException ex) {
BindException bex = (BindException) ex.getRootCause();
assertEquals(1, bex.getErrorCount());
}
} }
@Test @Test
...@@ -294,21 +258,17 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -294,21 +258,17 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
@Test @Test
public void relaxedPropertyNamesSame() throws Exception { public void relaxedPropertyNamesSame() throws Exception {
this.context = new AnnotationConfigApplicationContext(); testRelaxedPropertyNames("test.FOO_BAR:test1", "test.FOO_BAR:test2");
EnvironmentTestUtils.addEnvironment(this.context, "test.FOO_BAR:test1");
EnvironmentTestUtils.addEnvironment(this.context, "test.FOO_BAR:test2");
this.context.register(RelaxedPropertyNames.class);
this.context.refresh();
assertThat(this.context.getBean(RelaxedPropertyNames.class).getFooBar(),
equalTo("test2"));
} }
@Test @Test
public void relaxedPropertyNamesMixed() throws Exception { public void relaxedPropertyNamesMixed() throws Exception {
// gh-3385 testRelaxedPropertyNames("test.foo-bar:test1", "test.FOO_BAR:test2");
}
private void testRelaxedPropertyNames(String... environment) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "test.foo-bar:test1"); EnvironmentTestUtils.addEnvironment(this.context, environment);
EnvironmentTestUtils.addEnvironment(this.context, "test.FOO_BAR:test2");
this.context.register(RelaxedPropertyNames.class); this.context.register(RelaxedPropertyNames.class);
this.context.refresh(); this.context.refresh();
assertThat(this.context.getBean(RelaxedPropertyNames.class).getFooBar(), assertThat(this.context.getBean(RelaxedPropertyNames.class).getFooBar(),
...@@ -326,6 +286,17 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -326,6 +286,17 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
.getValue(), equalTo("test1")); .getValue(), equalTo("test1"));
} }
private void assertBindingFailure(int errorCount) {
try {
this.context.refresh();
fail("Expected exception");
}
catch (BeanCreationException ex) {
BindException bex = (BindException) ex.getRootCause();
assertEquals(errorCount, bex.getErrorCount());
}
}
@Configuration @Configuration
@EnableConfigurationProperties @EnableConfigurationProperties
public static class TestConfigurationWithValidatingSetter { public static class TestConfigurationWithValidatingSetter {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment