diff options
author | Emmanuel Bourg <ebourg@apache.org> | 2016-08-02 11:13:32 +0200 |
---|---|---|
committer | Emmanuel Bourg <ebourg@apache.org> | 2016-08-02 11:13:32 +0200 |
commit | f69f2a4b8ea697b3a631c0dc7a470e3c9793fee3 (patch) | |
tree | db2f25b29aa3e59c463ab41d3f2856f6265bb1a5 /spring-context/src/test/java/org/springframework/format/datetime/joda | |
parent | 5575b60c30c5a0c308c4ba3a2db93956d8c1746c (diff) |
Imported Upstream version 4.2.6
Diffstat (limited to 'spring-context/src/test/java/org/springframework/format/datetime/joda')
2 files changed, 109 insertions, 84 deletions
diff --git a/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java index 944421af..122d3218 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -77,7 +77,9 @@ public class DateTimeFormatterFactoryTests { @Test public void createDateTimeFormatterInOrderOfPropertyPriority() throws Exception { factory.setStyle("SS"); - assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("10/21/09 12:10 PM")); + String value = applyLocale(factory.createDateTimeFormatter()).print(dateTime); + assertTrue(value.startsWith("10/21/09")); + assertTrue(value.endsWith("12:10 PM")); factory.setIso(ISO.DATE); assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("2009-10-21")); diff --git a/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java b/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java index 6cfbd538..1e96b13c 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,11 +24,15 @@ import java.util.Locale; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; +import org.joda.time.Duration; import org.joda.time.Instant; import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; import org.joda.time.LocalTime; -import org.joda.time.MutableDateTime; +import org.joda.time.MonthDay; +import org.joda.time.Period; +import org.joda.time.YearMonth; +import org.joda.time.chrono.ISOChronology; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -56,6 +60,7 @@ public class JodaTimeFormattingTests { private DataBinder binder; + @Before public void setUp() { JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar(); @@ -65,7 +70,6 @@ public class JodaTimeFormattingTests { private void setUp(JodaTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); - registrar.registerFormatters(conversionService); JodaTimeBean bean = new JodaTimeBean(); @@ -85,6 +89,7 @@ public class JodaTimeFormattingTests { JodaTimeContextHolder.setJodaTimeContext(null); } + @Test public void testJodaTimePatternsForStyle() { System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("SS", LocaleContextHolder.getLocale())); @@ -226,28 +231,33 @@ public class JodaTimeFormattingTests { @Test public void testBindLocalDateTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("localDateTime", "10/31/09 12:00 PM"); + propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("localDateTime")); + 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", "Oct 31, 2009 12:00 PM"); + propertyValues.add("localDateTimeAnnotated", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("localDateTimeAnnotated")); + String value = binder.getBindingResult().getFieldValue("localDateTimeAnnotated").toString(); + assertTrue(value.startsWith("Oct 31, 2009")); + assertTrue(value.endsWith("12:00 PM")); } @Test public void testBindDateTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("dateTime", "10/31/09 12:00 PM"); + propertyValues.add("dateTime", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC())); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTime")); + String value = binder.getBindingResult().getFieldValue("dateTime").toString(); + assertTrue(value.startsWith("10/31/09")); } @Test @@ -256,10 +266,12 @@ public class JodaTimeFormattingTests { registrar.setDateTimeStyle("MM"); setUp(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("localDateTime", "Oct 31, 2009 12:00:00 PM"); + propertyValues.add("localDateTime", new LocalDateTime(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("Oct 31, 2009 12:00:00 PM", binder.getBindingResult().getFieldValue("localDateTime")); + String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); + assertTrue(value.startsWith("Oct 31, 2009")); + assertTrue(value.endsWith("12:00:00 PM")); } @Test @@ -289,10 +301,11 @@ public class JodaTimeFormattingTests { @Test public void testBindDateTimeAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("dateTimeAnnotated", "Oct 31, 2009 12:00 PM"); + propertyValues.add("dateTimeAnnotated", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC())); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated")); + String value = binder.getBindingResult().getFieldValue("dateTimeAnnotated").toString(); + assertTrue(value.startsWith("Oct 31, 2009")); } @Test @@ -307,19 +320,11 @@ public class JodaTimeFormattingTests { @Test public void testBindDateTimeAnnotatedDefault() { MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("dateTimeAnnotatedDefault", "10/31/09 12:00 PM"); + propertyValues.add("dateTimeAnnotatedDefault", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC())); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault")); - } - - @Test - public void testBindDate() { - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("date", "10/31/09 12:00 PM"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date")); + String value = binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault").toString(); + assertTrue(value.startsWith("10/31/09")); } @Test @@ -350,15 +355,6 @@ public class JodaTimeFormattingTests { } @Test - public void testBindCalendar() { - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("calendar", "10/31/09 12:00 PM"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("calendar")); - } - - @Test public void testBindCalendarAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("calendarAnnotated", "10/31/09"); @@ -413,15 +409,6 @@ public class JodaTimeFormattingTests { } @Test - public void testBindInstant() { - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("instant", "10/31/09 12:00 PM"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("instant")); - } - - @Test public void testBindInstantAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("instantAnnotated", "2009-10-31T12:00:00.000Z"); @@ -431,15 +418,6 @@ public class JodaTimeFormattingTests { } @Test - public void testBindMutableDateTime() { - MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("mutableDateTime", "10/31/09 12:00 PM"); - binder.bind(propertyValues); - assertEquals(0, binder.getBindingResult().getErrorCount()); - assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("mutableDateTime")); - } - - @Test public void testBindMutableDateTimeAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("mutableDateTimeAnnotated", "2009-10-31T12:00:00.000Z"); @@ -482,6 +460,42 @@ public class JodaTimeFormattingTests { assertNotNull(date); } + @Test + public void testBindPeriod() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("period", "P6Y3M1D"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertTrue(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D")); + } + + @Test + public void testBindDuration() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("duration", "PT72.345S"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertTrue(binder.getBindingResult().getFieldValue("duration").toString().equals("PT72.345S")); + } + + @Test + public void testBindYearMonth() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("yearMonth", "2007-12"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertTrue(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12")); + } + + @Test + public void testBindMonthDay() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("monthDay", "--12-03"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertTrue(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03")); + } + @SuppressWarnings("unused") private static class JodaTimeBean { @@ -512,9 +526,6 @@ public class JodaTimeFormattingTests { @DateTimeFormat(style="S-") private Date dateAnnotated; - @DateTimeFormat - private Calendar calendar; - @DateTimeFormat(style="S-") private Calendar calendarAnnotated; @@ -537,16 +548,20 @@ public class JodaTimeFormattingTests { @DateTimeFormat(iso=ISO.DATE_TIME) private DateTime isoDateTime; - private Instant instant; - @DateTimeFormat(iso=ISO.DATE_TIME) private Instant instantAnnotated; - private MutableDateTime mutableDateTime; - @DateTimeFormat(iso=ISO.DATE_TIME) private Instant mutableDateTimeAnnotated; + private Period period; + + private Duration duration; + + private YearMonth yearMonth; + + private MonthDay monthDay; + private final List<JodaTimeBean> children = new ArrayList<JodaTimeBean>(); public LocalDate getLocalDate() { @@ -645,14 +660,6 @@ public class JodaTimeFormattingTests { this.dateAnnotated = dateAnnotated; } - public Calendar getCalendar() { - return calendar; - } - - public void setCalendar(Calendar calendar) { - this.calendar = calendar; - } - public Calendar getCalendarAnnotated() { return calendarAnnotated; } @@ -702,14 +709,6 @@ public class JodaTimeFormattingTests { this.isoDateTime = isoDateTime; } - public Instant getInstant() { - return instant; - } - - public void setInstant(Instant instant) { - this.instant = instant; - } - public Instant getInstantAnnotated() { return instantAnnotated; } @@ -718,14 +717,6 @@ public class JodaTimeFormattingTests { this.instantAnnotated = instantAnnotated; } - public MutableDateTime getMutableDateTime() { - return mutableDateTime; - } - - public void setMutableDateTime(MutableDateTime mutableDateTime) { - this.mutableDateTime = mutableDateTime; - } - public Instant getMutableDateTimeAnnotated() { return mutableDateTimeAnnotated; } @@ -734,6 +725,38 @@ public class JodaTimeFormattingTests { this.mutableDateTimeAnnotated = mutableDateTimeAnnotated; } + public Period getPeriod() { + return period; + } + + public void setPeriod(Period period) { + this.period = period; + } + + public Duration getDuration() { + return duration; + } + + public void setDuration(Duration duration) { + this.duration = duration; + } + + public YearMonth getYearMonth() { + return yearMonth; + } + + public void setYearMonth(YearMonth yearMonth) { + this.yearMonth = yearMonth; + } + + public MonthDay getMonthDay() { + return monthDay; + } + + public void setMonthDay(MonthDay monthDay) { + this.monthDay = monthDay; + } + public List<JodaTimeBean> getChildren() { return children; } |