Reduce code duplication

This commit is contained in:
Stephane Nicoll 2015-10-28 11:57:59 +01:00
parent 1173f771e3
commit 954d137210
1 changed files with 22 additions and 51 deletions

View File

@ -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 {
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 {
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 {
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 {
@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 {
.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 {