@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); }
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); }
private void setup(DateTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); DateTimeBean bean = new DateTimeBean(); bean.getChildren().add(new DateTimeBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); DateTimeContext context = new DateTimeContext(); context.setTimeZone(ZoneId.of("-05:00")); DateTimeContextHolder.setDateTimeContext(context); }
@Test public void testBindingWithFormatterAgainstList() { BeanWithIntegerList tb = new BeanWithIntegerList(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("integerList[0]", "1"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Integer(1), tb.getIntegerList().get(0)); assertEquals("1", binder.getBindingResult().getFieldValue("integerList[0]")); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test public void testBindingWithFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1,2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(1.2), tb.getMyFloat()); assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat")); PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class); assertNotNull(editor); editor.setValue(new Float(1.4)); assertEquals("1,4", editor.getAsText()); editor = binder.getBindingResult().findEditor("myFloat", null); assertNotNull(editor); editor.setAsText("1,6"); assertEquals(new Float(1.6), editor.getValue()); } finally { LocaleContextHolder.resetLocaleContext(); } }
@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 public void testOptionalProperty() { OptionalHolder bean = new OptionalHolder(); DataBinder binder = new DataBinder(bean); binder.setConversionService(new DefaultConversionService()); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("id", "1"); pvs.add("name", null); binder.bind(pvs); assertEquals("1", bean.getId()); assertFalse(bean.getName().isPresent()); pvs = new MutablePropertyValues(); pvs.add("id", "2"); pvs.add("name", "myName"); binder.bind(pvs); assertEquals("2", bean.getId()); assertEquals("myName", bean.getName().get()); }
@Before public void setUp() { DefaultConversionService.addDefaultConverters(conversionService); conversionService.setEmbeddedValueResolver(new StringValueResolver() { @Override public String resolveStringValue(String strVal) { if ("${pattern}".equals(strVal)) { return "#,##.00"; } else { return strVal; } } }); conversionService.addFormatterForFieldType(Number.class, new NumberStyleFormatter()); conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); LocaleContextHolder.setLocale(Locale.US); binder = new DataBinder(new TestBean()); binder.setConversionService(conversionService); }
@Test public void testBindingErrorWithFormatterAgainstList() { BeanWithIntegerList tb = new BeanWithIntegerList(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("integerList[0]", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertTrue(tb.getIntegerList().isEmpty()); assertEquals("1x2", binder.getBindingResult().getFieldValue("integerList[0]")); assertTrue(binder.getBindingResult().hasFieldErrors("integerList[0]")); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test public void testBindingErrorWithFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(0.0), tb.getMyFloat()); assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat")); assertTrue(binder.getBindingResult().hasFieldErrors("myFloat")); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test public void testBindingErrorWithFormatterAgainstFields() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); binder.initDirectFieldAccess(); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1x2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(0.0), tb.getMyFloat()); assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat")); assertTrue(binder.getBindingResult().hasFieldErrors("myFloat")); } finally { LocaleContextHolder.resetLocaleContext(); } }
@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()); }
@Test public void testAmountWithNumberFormat3() { FormattedMoneyHolder3 bean = new FormattedMoneyHolder3(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "10%"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10%", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountWithNumberFormat4() { FormattedMoneyHolder4 bean = new FormattedMoneyHolder4(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "010.500"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testBindingErrorWithParseExceptionFromFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new ParseException(text, 0); } @Override public String print(String object, Locale locale) { return object; } }); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); assertEquals("test", binder.getBindingResult().getFieldValue("name")); }
@Test public void testBindingErrorWithRuntimeExceptionFromFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatter(new Formatter<String>() { @Override public String parse(String text, Locale locale) throws ParseException { throw new RuntimeException(text); } @Override public String print(String object, Locale locale) { return object; } }); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "test"); binder.bind(pvs); assertTrue(binder.getBindingResult().hasFieldErrors("name")); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("name").getCode()); assertEquals("test", binder.getBindingResult().getFieldValue("name")); }
@Test public void testAmountWithNumberFormat5() { FormattedMoneyHolder5 bean = new FormattedMoneyHolder5(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "USD 10.50"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); LocaleContextHolder.setLocale(Locale.CANADA); binder.bind(propertyValues); LocaleContextHolder.setLocale(Locale.US); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountWithNumberFormat1() { FormattedMoneyHolder1 bean = new FormattedMoneyHolder1(); 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.50", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); LocaleContextHolder.setLocale(Locale.CANADA); binder.bind(propertyValues); LocaleContextHolder.setLocale(Locale.US); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("$10.50", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("CAD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testAmountAndUnit() { MoneyHolder bean = new MoneyHolder(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "USD 10.50"); propertyValues.add("unit", "USD"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount")); assertEquals("USD", binder.getBindingResult().getFieldValue("unit")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); LocaleContextHolder.setLocale(Locale.CANADA); binder.bind(propertyValues); LocaleContextHolder.setLocale(Locale.US); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount")); assertEquals("USD", binder.getBindingResult().getFieldValue("unit")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }