@Test public void testBindDateTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("dateTime", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC())); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("dateTime").toString(); assertTrue(value.startsWith("10/31/09")); }
@Test public void testBindDateTimeAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("dateTimeAnnotated", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC())); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("dateTimeAnnotated").toString(); assertTrue(value.startsWith("Oct 31, 2009")); }
@Test public void testBindLocalDateTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); assertTrue(value.startsWith("10/31/09")); assertTrue(value.endsWith("12:00 PM")); }
@Test public void testBindLocalDateTimeAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTimeAnnotated", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString(); assertTrue(value.startsWith("Oct 31, 2009")); assertTrue(value.endsWith("12:00 PM")); }
@Test public void testBindDateTimeOverflow() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("dateAnnotatedPattern", "02/29/09 12:00 PM"); binder.bind(propertyValues); assertEquals(1, binder.getBindingResult().getErrorCount()); }
@Test public void testBindDateTimeOverflow() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("dateTimeAnnotatedPattern", "02/29/09 12:00 PM"); binder.bind(propertyValues); assertEquals(1, binder.getBindingResult().getErrorCount()); }
@Test public void testBindLocalDateWithSpecificFormatter() { JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar(); registrar.setDateFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd")); setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", "20091031"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("20091031", binder.getBindingResult().getFieldValue("localDate")); }
@Test public void testBindLocalTimeWithSpecificFormatter() { JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar(); registrar.setTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("HHmmss")); setup(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "130000"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("130000", binder.getBindingResult().getFieldValue("localTime")); }
@Test public void testBindCalendarAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("calendarAnnotated", "10/31/09"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated")); }
@Test public void testBindISODateTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000Z"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime")); }
@Test public void testPercentFormatting() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("percent", "53%"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("53%", binder.getBindingResult().getFieldValue("percent")); }
@Test public void testPatternFormatting() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("pattern", "1,25.00"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("1,25.00", binder.getBindingResult().getFieldValue("pattern")); }
@Test public void testBindDateAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("dateAnnotated", "10/31/09"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated")); }
@Test public void testBindLocalTimeAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTimeAnnotated", "12:00:00 PM"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTimeAnnotated")); }
@Test public void testBindInstantAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("instantAnnotated", "2009-10-31T12:00:00.000Z"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("instantAnnotated")); }
@Test public void testBindLongAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("millisAnnotated", "10/31/09"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated")); }
@Test public void testBindDateAnnotatedPattern() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("dateAnnotatedPattern", "10/31/09 1:05"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10/31/09 1:05", binder.getBindingResult().getFieldValue("dateAnnotatedPattern")); }
@Test public void testBindISOTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("isoTime", "12:00:00.000-05:00"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("17:00:00.000Z", binder.getBindingResult().getFieldValue("isoTime")); }
@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 testPatternList2FormattingListElement() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("patternList2[0]", "1,25.00"); propertyValues.add("patternList2[1]", "2,35.00"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("1,25.00", binder.getBindingResult().getFieldValue("patternList2[0]")); assertEquals("2,35.00", binder.getBindingResult().getFieldValue("patternList2[1]")); }