more joda time formatting tests

This commit is contained in:
Keith Donald 2009-11-05 05:19:27 +00:00
parent 8a15486de3
commit ba0f1da96f
3 changed files with 147 additions and 24 deletions

View File

@ -41,23 +41,23 @@ import org.springframework.ui.format.Printer;
*/ */
abstract class AbstractDateTimeAnnotationFormatterFactory<A extends Annotation> implements AnnotationFormatterFactory<A> { abstract class AbstractDateTimeAnnotationFormatterFactory<A extends Annotation> implements AnnotationFormatterFactory<A> {
private final Set<Class<?>> propertyTypes; private final Set<Class<?>> fieldTypes;
public AbstractDateTimeAnnotationFormatterFactory() { public AbstractDateTimeAnnotationFormatterFactory() {
this.propertyTypes = Collections.unmodifiableSet(createFieldTypes()); this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
} }
public Set<Class<?>> getFieldTypes() { public Set<Class<?>> getFieldTypes() {
return propertyTypes; return this.fieldTypes;
} }
public Printer<?> getPrinter(A annotation, Class<?> propertyType) { public Printer<?> getPrinter(A annotation, Class<?> fieldType) {
DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation); DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation);
if (ReadableInstant.class.isAssignableFrom(propertyType)) { if (ReadableInstant.class.isAssignableFrom(fieldType)) {
return new ReadableInstantPrinter(formatter); return new ReadableInstantPrinter(formatter);
} else if (ReadablePartial.class.isAssignableFrom(propertyType)) { } else if (ReadablePartial.class.isAssignableFrom(fieldType)) {
return new ReadablePartialPrinter(formatter); return new ReadablePartialPrinter(formatter);
} else if (Calendar.class.isAssignableFrom(propertyType)) { } else if (Calendar.class.isAssignableFrom(fieldType)) {
// assumes Calendar->ReadableInstant converter is registered // assumes Calendar->ReadableInstant converter is registered
return new ReadableInstantPrinter(formatter); return new ReadableInstantPrinter(formatter);
} else { } else {
@ -80,15 +80,15 @@ abstract class AbstractDateTimeAnnotationFormatterFactory<A extends Annotation>
// internal helpers // internal helpers
private Set<Class<?>> createFieldTypes() { private Set<Class<?>> createFieldTypes() {
Set<Class<?>> propertyTypes = new HashSet<Class<?>>(5); Set<Class<?>> fieldTypes = new HashSet<Class<?>>(7);
propertyTypes.add(LocalDate.class); fieldTypes.add(LocalDate.class);
propertyTypes.add(LocalTime.class); fieldTypes.add(LocalTime.class);
propertyTypes.add(LocalDateTime.class); fieldTypes.add(LocalDateTime.class);
propertyTypes.add(DateTime.class); fieldTypes.add(DateTime.class);
propertyTypes.add(Date.class); fieldTypes.add(Date.class);
propertyTypes.add(Calendar.class); fieldTypes.add(Calendar.class);
propertyTypes.add(Long.class); fieldTypes.add(Long.class);
return propertyTypes; return fieldTypes;
} }
} }

View File

@ -48,6 +48,7 @@ final class JodaTimeConverters {
registry.addConverter(new DateTimeToDateMidnightConverter()); registry.addConverter(new DateTimeToDateMidnightConverter());
registry.addConverter(new DateTimeToDateConverter()); registry.addConverter(new DateTimeToDateConverter());
registry.addConverter(new DateTimeToCalendarConverter()); registry.addConverter(new DateTimeToCalendarConverter());
registry.addConverter(new DateTimeToLongConverter());
registry.addConverter(new DateToLongConverter()); registry.addConverter(new DateToLongConverter());
registry.addConverter(new CalendarToReadableInstantConverter()); registry.addConverter(new CalendarToReadableInstantConverter());
} }
@ -114,6 +115,16 @@ final class JodaTimeConverters {
} }
} }
/**
* Used when binding a parsed DateTime to a java.lang.Long field.
* @see DateTimeParser
*/
private static class DateTimeToLongConverter implements Converter<DateTime, Long> {
public Long convert(DateTime source) {
return source.getMillis();
}
}
/** /**
* Used when printing a java.util.Date field with a MillisecondInstantPrinter. * Used when printing a java.util.Date field with a MillisecondInstantPrinter.
* @see MillisecondInstantPrinter * @see MillisecondInstantPrinter

View File

@ -12,6 +12,7 @@ import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime; import org.joda.time.LocalTime;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
@ -29,7 +30,7 @@ public class JodaTimeFormattingTests {
public void setUp() { public void setUp() {
JodaTimeFormattingConfigurer configurer = new JodaTimeFormattingConfigurer(); JodaTimeFormattingConfigurer configurer = new JodaTimeFormattingConfigurer();
configurer.installJodaTimeFormatting(conversionService); configurer.installJodaTimeFormatting(conversionService);
binder = new DataBinder(new JodaTimeBean()); binder = new DataBinder(new JodaTimeBean());
binder.setConversionService(conversionService); binder.setConversionService(conversionService);
@ -66,6 +67,117 @@ public class JodaTimeFormattingTests {
assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated")); assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
} }
@Test
public void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("localTime", "12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
}
@Test
public void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("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 testBindLocalDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("localDateTime", "10/31/09 12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
}
@Test
public void testBindLocalDateTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("localDateTimeAnnotated", "Saturday, October 31, 2009 12:00:00 PM ");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Saturday, October 31, 2009 12:00:00 PM ", binder.getBindingResult().getFieldValue("localDateTimeAnnotated"));
}
@Test
@Ignore
public void testBindDateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("dateTime", "10/31/09 12:00 PM");
binder.bind(propertyValues);
System.out.println(binder.getBindingResult());
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTime"));
}
@Test
public void testBindDateTimeAnnotated() {
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"));
}
@Test
public void testBindDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("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"));
}
@Test
public void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("dateAnnotated", "10/31/09");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
}
@Test
public void testBindCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("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.addPropertyValue("calendarAnnotated", "10/31/09");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
}
@Test
public void testBindLong() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("millis", "1256961600");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
}
@Test
public void testBindLongAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("millisAnnotated", "10/31/09");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
}
public static class JodaTimeBean { public static class JodaTimeBean {
@ -76,8 +188,8 @@ public class JodaTimeFormattingTests {
private LocalTime localTime; private LocalTime localTime;
@DateTimeFormat(timeStyle = Style.LONG) @DateTimeFormat(timeStyle = Style.MEDIUM)
private LocalDate localTimeAnnotated; private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime; private LocalDateTime localDateTime;
@ -87,7 +199,7 @@ public class JodaTimeFormattingTests {
private DateTime dateTime; private DateTime dateTime;
@DateTimeFormat(dateStyle = Style.MEDIUM, timeStyle = Style.SHORT) @DateTimeFormat(dateStyle = Style.MEDIUM, timeStyle = Style.SHORT)
private LocalDateTime dateTimeAnnotated; private DateTime dateTimeAnnotated;
private Date date; private Date date;
@ -128,11 +240,11 @@ public class JodaTimeFormattingTests {
this.localTime = localTime; this.localTime = localTime;
} }
public LocalDate getLocalTimeAnnotated() { public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated; return localTimeAnnotated;
} }
public void setLocalTimeAnnotated(LocalDate localTimeAnnotated) { public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated; this.localTimeAnnotated = localTimeAnnotated;
} }
@ -160,11 +272,11 @@ public class JodaTimeFormattingTests {
this.dateTime = dateTime; this.dateTime = dateTime;
} }
public LocalDateTime getDateTimeAnnotated() { public DateTime getDateTimeAnnotated() {
return dateTimeAnnotated; return dateTimeAnnotated;
} }
public void setDateTimeAnnotated(LocalDateTime dateTimeAnnotated) { public void setDateTimeAnnotated(DateTime dateTimeAnnotated) {
this.dateTimeAnnotated = dateTimeAnnotated; this.dateTimeAnnotated = dateTimeAnnotated;
} }