@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { DataBinder dataBinder = new DataBinder(dubboConfig); // Set ignored* dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind dataBinder.bind(propertyValues); }
@Before public void setup() { this.conversionService = new DefaultFormattingConversionService(); DataBinder dataBinder = new DataBinder(null); dataBinder.setConversionService(conversionService); this.redirectAttributes = new RedirectAttributesModelMap(dataBinder); }
private void setup(DateFormatterRegistrar registrar) { DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); SimpleDateBean bean = new SimpleDateBean(); bean.getChildren().add(new SimpleDateBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); }
@Test public void testAutoGrowWithinDefaultLimit() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); MutablePropertyValues mpvs = new MutablePropertyValues(); mpvs.add("friends[4]", ""); binder.bind(mpvs); assertEquals(5, testBean.getFriends().size()); }
@Test // SPR-14888 public void testSetAutoGrowCollectionLimit() { BeanWithIntegerList tb = new BeanWithIntegerList(); DataBinder binder = new DataBinder(tb); binder.setAutoGrowCollectionLimit(257); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("integerList[256]", "1"); binder.bind(pvs); assertEquals(257, tb.getIntegerList().size()); assertEquals(Integer.valueOf(1), tb.getIntegerList().get(256)); assertEquals(Integer.valueOf(1), binder.getBindingResult().getFieldValue("integerList[256]")); }
@Test public void testAutoGrowWithinCustomLimit() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); binder.setAutoGrowCollectionLimit(10); MutablePropertyValues mpvs = new MutablePropertyValues(); mpvs.add("friends[4]", ""); binder.bind(mpvs); assertEquals(5, testBean.getFriends().size()); }
@Test public void nestedBindingWithDisabledAutoGrow() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.setAutoGrowNestedPaths(false); binder.initDirectFieldAccess(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("spouse.name", "Kerry")); thrown.expect(NullValueInNestedPathException.class); binder.bind(pvs); }
@Test public void testBindingWithCustomEditorOnObjectField() { BeanWithObjectProperty tb = new BeanWithObjectProperty(); DataBinder binder = new DataBinder(tb); binder.registerCustomEditor(Integer.class, "object", new CustomNumberEditor(Integer.class, true)); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("object", "1"); binder.bind(pvs); assertEquals(new Integer(1), tb.getObject()); }
private void setup(JodaTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); JodaTimeBean bean = new JodaTimeBean(); bean.getChildren().add(new JodaTimeBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); JodaTimeContext context = new JodaTimeContext(); context.setTimeZone(DateTimeZone.forID("-05:00")); JodaTimeContextHolder.setJodaTimeContext(context); }
@Test public void testBindingNoErrorsWithIgnoreInvalid() { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); binder.setIgnoreInvalidFields(true); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("spouse.age", 32); binder.bind(pvs); }
@Test public void testBindingNullToEmptyCollection() { IndexedTestBean tb = new IndexedTestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class, true)); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("set", null); binder.bind(pvs); assertTrue(tb.getSet() instanceof TreeSet); assertTrue(tb.getSet().isEmpty()); }
@Test // SPR-14888 public void testSetAutoGrowCollectionLimitAfterInitialization() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods"); DataBinder binder = new DataBinder(new BeanWithIntegerList()); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); binder.setAutoGrowCollectionLimit(257); }
@Test // SPR-15009 public void testCallSetMessageCodesResolverTwice() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("DataBinder is already initialized with MessageCodesResolver"); TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); binder.setMessageCodesResolver(new DefaultMessageCodesResolver()); binder.setMessageCodesResolver(new DefaultMessageCodesResolver()); }
@Test public void testBindingWithAllowedFields() throws BindException { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod); binder.setAllowedFields("name", "myparam"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("age", "32x"); binder.bind(pvs); binder.close(); assertTrue("changed name correctly", rod.getName().equals("Rod")); assertTrue("did not change age", rod.getAge() == 0); }
@Test public void testBindDateWithoutErrorFallingBackToDateConstructor() { DataBinder binder = new DataBinder(new JodaTimeBean()); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); }
@Test // SPR-15009 public void testSetMessageCodesResolverIsNullAfterInitializeBindingResult() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); binder.initBeanPropertyAccess(); binder.setMessageCodesResolver(null); MutablePropertyValues mpv = new MutablePropertyValues(); mpv.add("age", "invalid"); binder.bind(mpv); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("age").getCode()); // Keep a default MessageCodesResolver }
@Test public void testConversionWithInappropriateStringEditor() { DataBinder dataBinder = new DataBinder(null); DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); dataBinder.setConversionService(conversionService); dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); NameBean bean = new NameBean("Fred"); assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class)); conversionService.addConverter(new NameBeanConverter()); assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class)); }
@Test // SPR-15009 public void testSetCustomMessageCodesResolverAfterInitializeBindingResult() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); binder.initBeanPropertyAccess(); DefaultMessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver(); messageCodesResolver.setPrefix("errors."); binder.setMessageCodesResolver(messageCodesResolver); MutablePropertyValues mpv = new MutablePropertyValues(); mpv.add("age", "invalid"); binder.bind(mpv); assertEquals("errors.typeMismatch", binder.getBindingResult().getFieldError("age").getCode()); }
@Test public void testBindingWithDefaultConversionNoErrors() throws BindException { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); assertTrue(binder.isIgnoreUnknownFields()); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("jedi", "on"); binder.bind(pvs); binder.close(); assertEquals("Rod", rod.getName()); assertTrue(rod.isJedi()); }
@Test public void testAmountWithNumberFormat2() { FormattedMoneyHolder2 bean = new FormattedMoneyHolder2(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "10.50"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10.5", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }