iso date time format tests

This commit is contained in:
Keith Donald 2009-11-05 17:18:54 +00:00
parent 8de34c6fa7
commit d65454175d
4 changed files with 70 additions and 4 deletions

View File

@ -42,7 +42,7 @@ public final class JodaTimeContextHolder {
* @param context the current JodaTimeContext, or <code>null</code> to clear
* the thread-bound context
*/
public static void setLocaleContext(JodaTimeContext context) {
public static void setJodaTimeContext(JodaTimeContext context) {
jodaTimeContextHolder.set(context);
}

View File

@ -103,6 +103,7 @@ public class JodaTimeFormattingConfigurer {
formatterRegistry.addFormatterForFieldType(Date.class, new MillisecondInstantPrinter(jodaDateTimeFormatter), dateTimeParser);
formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
formatterRegistry.addFormatterForFieldAnnotation(new ISODateTimeFormatAnnotationFormatterFactory());
}
// internal helpers

View File

@ -7,6 +7,7 @@ import java.util.Date;
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
@ -16,6 +17,7 @@ import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.ISODateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.Style;
import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer;
import org.springframework.format.support.FormattingConversionService;
@ -36,11 +38,15 @@ public class JodaTimeFormattingTests {
binder.setConversionService(conversionService);
LocaleContextHolder.setLocale(Locale.US);
JodaTimeContext context = new JodaTimeContext();
context.setTimeZone(DateTimeZone.forID("-05:00"));
JodaTimeContextHolder.setJodaTimeContext(context);
}
@After
public void tearDown() {
LocaleContextHolder.setLocale(null);
JodaTimeContextHolder.setJodaTimeContext(null);
}
@Test
@ -120,7 +126,6 @@ public class JodaTimeFormattingTests {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("dateTimeAnnotated", "Oct 31, 2009 12:00 PM");
binder.bind(propertyValues);
System.out.println(binder.getBindingResult());
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated"));
}
@ -179,6 +184,33 @@ public class JodaTimeFormattingTests {
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
}
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("isoDate", "2009-10-31");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("12:00:00.000", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("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"));
}
public static class JodaTimeBean {
private LocalDate localDate;
@ -216,6 +248,15 @@ public class JodaTimeFormattingTests {
@DateTimeFormat(dateStyle = Style.SHORT)
private Long millisAnnotated;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE)
private LocalDate isoDate;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.TIME)
private LocalTime isoTime;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE_TIME)
private DateTime isoDateTime;
public LocalDate getLocalDate() {
return localDate;
}
@ -328,5 +369,29 @@ public class JodaTimeFormattingTests {
this.millisAnnotated = millisAnnotated;
}
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public DateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(DateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
}
}

View File

@ -326,7 +326,7 @@ public class MappingTests {
// field to multiple fields
.addAssemblerMapping("activationDateTime", new Converter<Map<String, String>, DateTime>() {
public DateTime convert(Map<String, String> source) {
MutableDateTime dateTime = new MutableDateTime(DateTimeZone.forID("-04:00"));
MutableDateTime dateTime = new MutableDateTime(DateTimeZone.forID("-05:00"));
dateTime.setYear(Integer.parseInt(source.get("year")));
dateTime.setMonthOfYear(Integer.parseInt(source.get("month")));
dateTime.setDayOfMonth(Integer.parseInt(source.get("day")));
@ -344,7 +344,7 @@ public class MappingTests {
source.put("activationDateTime.hour", "12");
source.put("activationDateTime.minute", "0");
Account account = mapper.map(source, new Account());
assertEquals(ISODateTimeFormat.dateTime().withOffsetParsed().parseDateTime("2009-10-12T12:00:00.000-04:00"), account
assertEquals(ISODateTimeFormat.dateTime().withOffsetParsed().parseDateTime("2009-10-12T12:00:00.000-05:00"), account
.getActivationDateTime());
}