From 40b787c71a5b1cf17f9237a2e1ff3d58cebc929a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 20 Nov 2013 15:55:13 +0000 Subject: [PATCH] Add test for allowed/disallowed fields --- .../boot/bind/RelaxedDataBinderTests.java | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java index 14d415af21..8094ffc479 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java @@ -148,6 +148,33 @@ public class RelaxedDataBinderTests { } } + @Test + public void testAllowedFields() throws Exception { + VanillaTarget target = new VanillaTarget(); + RelaxedDataBinder binder = getBinder(target, null); + binder.setAllowedFields("foo"); + binder.setIgnoreUnknownFields(false); + BindingResult result = bind(binder, target, "foo: bar\n" + "value: 123\n" + + "bar: spam"); + assertEquals(0, target.getValue()); + assertEquals("bar", target.getFoo()); + assertEquals(0, result.getErrorCount()); + } + + @Test + public void testDisallowedFields() throws Exception { + VanillaTarget target = new VanillaTarget(); + RelaxedDataBinder binder = getBinder(target, null); + // Disallowed fields are not unknown... + binder.setDisallowedFields("foo", "bar"); + binder.setIgnoreUnknownFields(false); + BindingResult result = bind(binder, target, "foo: bar\n" + "value: 123\n" + + "bar: spam"); + assertEquals(123, target.getValue()); + assertEquals(null, target.getFoo()); + assertEquals(0, result.getErrorCount()); + } + @Test public void testBindNested() throws Exception { TargetWithNestedObject target = new TargetWithNestedObject(); @@ -309,20 +336,29 @@ public class RelaxedDataBinderTests { return bind(target, values, null); } - private BindingResult bind(Object target, String values, String namePrefix) + private BindingResult bind(DataBinder binder, Object target, String values) throws Exception { Properties properties = PropertiesLoaderUtils .loadProperties(new ByteArrayResource(values.getBytes())); - DataBinder binder = new RelaxedDataBinder(target, namePrefix); + binder.bind(new MutablePropertyValues(properties)); + binder.validate(); + + return binder.getBindingResult(); + } + + private BindingResult bind(Object target, String values, String namePrefix) + throws Exception { + return bind(getBinder(target, namePrefix), target, values); + } + + private RelaxedDataBinder getBinder(Object target, String namePrefix) { + RelaxedDataBinder binder = new RelaxedDataBinder(target, namePrefix); binder.setIgnoreUnknownFields(false); LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.afterPropertiesSet(); binder.setValidator(validatorFactoryBean); binder.setConversionService(this.conversionService); - binder.bind(new MutablePropertyValues(properties)); - binder.validate(); - - return binder.getBindingResult(); + return binder; } @Documented