From ff39bc376f2e618aabb28973252e6a42fbe02fb1 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Wed, 4 Nov 2009 23:02:10 +0000 Subject: [PATCH] formatting tests and polishing --- .../ui/format/jodatime/DateTimeFormat.java | 10 +- ...eTimeFormatAnnotationFormatterFactory.java | 8 +- .../ui/format/jodatime/ISODateTimeFormat.java | 4 +- ...eTimeFormatAnnotationFormatterFactory.java | 8 +- .../jodatime/JodaTimeFormattingTests.java | 98 ++++++++++++++++++- .../FormattingConversionServiceTests.java | 4 +- 6 files changed, 111 insertions(+), 21 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormat.java b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormat.java index d6a065c18e9..b2ee20439d9 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormat.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormat.java @@ -30,15 +30,15 @@ public @interface DateTimeFormat { /** * The style to use for formatting the date portion of the property. - * Defaults to {@link FormatStyle#NONE}. + * Defaults to {@link Style#NONE}. */ - FormatStyle dateStyle() default FormatStyle.NONE; + Style dateStyle() default Style.NONE; /** * The style to use for formatting the time portion of the property. - * Defaults to {@link FormatStyle#NONE}. + * Defaults to {@link Style#NONE}. */ - FormatStyle timeStyle() default FormatStyle.NONE; + Style timeStyle() default Style.NONE; /** * A pattern String that defines a custom format for the property. @@ -49,7 +49,7 @@ public @interface DateTimeFormat { /** * Supported DateTimeFormat styles. */ - public enum FormatStyle { + public enum Style { SHORT { public String toString() { return "S"; diff --git a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormatAnnotationFormatterFactory.java b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormatAnnotationFormatterFactory.java index ed2c515bbcf..75ba4be0e46 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormatAnnotationFormatterFactory.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/DateTimeFormatAnnotationFormatterFactory.java @@ -16,7 +16,7 @@ package org.springframework.ui.format.jodatime; import org.joda.time.format.DateTimeFormatter; -import org.springframework.ui.format.jodatime.DateTimeFormat.FormatStyle; +import org.springframework.ui.format.jodatime.DateTimeFormat.Style; /** * Formats properties annotated with the {@link DateTimeFormat} annotation. @@ -32,13 +32,13 @@ public final class DateTimeFormatAnnotationFormatterFactory extends AbstractDate if (!pattern.isEmpty()) { return forPattern(pattern); } else { - FormatStyle dateStyle = annotation.dateStyle(); - FormatStyle timeStyle = annotation.timeStyle(); + Style dateStyle = annotation.dateStyle(); + Style timeStyle = annotation.timeStyle(); return forDateTimeStyle(dateStyle, timeStyle); } } - private DateTimeFormatter forDateTimeStyle(FormatStyle dateStyle, FormatStyle timeStyle) { + private DateTimeFormatter forDateTimeStyle(Style dateStyle, Style timeStyle) { return org.joda.time.format.DateTimeFormat.forStyle(dateStyle.toString() + timeStyle.toString()); } diff --git a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormat.java b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormat.java index 46e34c3d7b1..dfce7e170b4 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormat.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormat.java @@ -28,9 +28,9 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) public @interface ISODateTimeFormat { - FormatStyle value(); + Style value(); - public enum FormatStyle { + public enum Style { DATE, TIME, DATE_TIME } diff --git a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormatAnnotationFormatterFactory.java b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormatAnnotationFormatterFactory.java index ca5efb019e0..0e4be37a06e 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormatAnnotationFormatterFactory.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/ISODateTimeFormatAnnotationFormatterFactory.java @@ -16,7 +16,7 @@ package org.springframework.ui.format.jodatime; import org.joda.time.format.DateTimeFormatter; -import org.springframework.ui.format.jodatime.ISODateTimeFormat.FormatStyle; +import org.springframework.ui.format.jodatime.ISODateTimeFormat.Style; /** * Formats properties annotated with the {@link ISODateTimeFormat} annotation. @@ -28,10 +28,10 @@ import org.springframework.ui.format.jodatime.ISODateTimeFormat.FormatStyle; public final class ISODateTimeFormatAnnotationFormatterFactory extends AbstractDateTimeAnnotationFormatterFactory { protected DateTimeFormatter configureDateTimeFormatterFrom(ISODateTimeFormat annotation) { - FormatStyle style = annotation.value(); - if (style == FormatStyle.DATE) { + Style style = annotation.value(); + if (style == Style.DATE) { return org.joda.time.format.ISODateTimeFormat.date(); - } else if (style == FormatStyle.TIME) { + } else if (style == Style.TIME) { return org.joda.time.format.ISODateTimeFormat.time(); } else { return org.joda.time.format.ISODateTimeFormat.dateTime(); diff --git a/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java b/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java index 768fcd74afa..170ae4a3aba 100644 --- a/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java +++ b/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java @@ -12,9 +12,11 @@ import org.joda.time.LocalDateTime; import org.joda.time.LocalTime; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.springframework.beans.MutablePropertyValues; import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.ui.format.jodatime.DateTimeFormat.Style; import org.springframework.ui.format.support.FormattingConversionService; import org.springframework.validation.DataBinder; @@ -28,10 +30,10 @@ public class JodaTimeFormattingTests { public void setUp() { JodaTimeFormattingConfigurer configurer = new JodaTimeFormattingConfigurer(); configurer.installJodaTimeFormatting(conversionService); - + binder = new DataBinder(new JodaTimeBean()); binder.setConversionService(conversionService); - + LocaleContextHolder.setLocale(Locale.US); } @@ -46,6 +48,7 @@ public class JodaTimeFormattingTests { propertyValues.addPropertyValue("localDate", "10/31/09"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); + assertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate")); } @Test @@ -56,22 +59,53 @@ public class JodaTimeFormattingTests { assertEquals(0, binder.getBindingResult().getErrorCount()); } - private static class JodaTimeBean { - + @Test + @Ignore + public void testBindLocalDateAnnotated() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.addPropertyValue("localDateAnnotated", "Oct 31, 2009"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated")); + } + + public static class JodaTimeBean { + private LocalDate localDate; + @DateTimeFormat(dateStyle = Style.MEDIUM) + private LocalDate localDateAnnotated; + private LocalTime localTime; + @DateTimeFormat(timeStyle = Style.LONG) + private LocalDate localTimeAnnotated; + private LocalDateTime localDateTime; + @DateTimeFormat(dateStyle = Style.FULL, timeStyle = Style.FULL) + private LocalDateTime localDateTimeAnnotated; + private DateTime dateTime; + @DateTimeFormat(dateStyle = Style.MEDIUM, timeStyle = Style.SHORT) + private LocalDateTime dateTimeAnnotated; + private Date date; + @DateTimeFormat(dateStyle = Style.SHORT) + private Date dateAnnotated; + private Calendar calendar; + @DateTimeFormat(dateStyle = Style.SHORT) + private Calendar calendarAnnotated; + private Long millis; + @DateTimeFormat(dateStyle = Style.SHORT) + private Long millisAnnotated; + public LocalDate getLocalDate() { return localDate; } @@ -80,6 +114,14 @@ public class JodaTimeFormattingTests { this.localDate = localDate; } + public LocalDate getLocalDateAnnotated() { + return localDateAnnotated; + } + + public void setLocalDateAnnotated(LocalDate localDateAnnotated) { + this.localDateAnnotated = localDateAnnotated; + } + public LocalTime getLocalTime() { return localTime; } @@ -88,6 +130,14 @@ public class JodaTimeFormattingTests { this.localTime = localTime; } + public LocalDate getLocalTimeAnnotated() { + return localTimeAnnotated; + } + + public void setLocalTimeAnnotated(LocalDate localTimeAnnotated) { + this.localTimeAnnotated = localTimeAnnotated; + } + public LocalDateTime getLocalDateTime() { return localDateTime; } @@ -96,6 +146,14 @@ public class JodaTimeFormattingTests { this.localDateTime = localDateTime; } + public LocalDateTime getLocalDateTimeAnnotated() { + return localDateTimeAnnotated; + } + + public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) { + this.localDateTimeAnnotated = localDateTimeAnnotated; + } + public DateTime getDateTime() { return dateTime; } @@ -104,6 +162,14 @@ public class JodaTimeFormattingTests { this.dateTime = dateTime; } + public LocalDateTime getDateTimeAnnotated() { + return dateTimeAnnotated; + } + + public void setDateTimeAnnotated(LocalDateTime dateTimeAnnotated) { + this.dateTimeAnnotated = dateTimeAnnotated; + } + public Date getDate() { return date; } @@ -112,6 +178,14 @@ public class JodaTimeFormattingTests { this.date = date; } + public Date getDateAnnotated() { + return dateAnnotated; + } + + public void setDateAnnotated(Date dateAnnotated) { + this.dateAnnotated = dateAnnotated; + } + public Calendar getCalendar() { return calendar; } @@ -120,6 +194,14 @@ public class JodaTimeFormattingTests { this.calendar = calendar; } + public Calendar getCalendarAnnotated() { + return calendarAnnotated; + } + + public void setCalendarAnnotated(Calendar calendarAnnotated) { + this.calendarAnnotated = calendarAnnotated; + } + public Long getMillis() { return millis; } @@ -128,5 +210,13 @@ public class JodaTimeFormattingTests { this.millis = millis; } + public Long getMillisAnnotated() { + return millisAnnotated; + } + + public void setMillisAnnotated(Long millisAnnotated) { + this.millisAnnotated = millisAnnotated; + } + } } diff --git a/org.springframework.context/src/test/java/org/springframework/ui/format/support/FormattingConversionServiceTests.java b/org.springframework.context/src/test/java/org/springframework/ui/format/support/FormattingConversionServiceTests.java index dec00f0a46c..0643a5d2565 100644 --- a/org.springframework.context/src/test/java/org/springframework/ui/format/support/FormattingConversionServiceTests.java +++ b/org.springframework.context/src/test/java/org/springframework/ui/format/support/FormattingConversionServiceTests.java @@ -34,7 +34,7 @@ import org.springframework.core.convert.converter.Converter; import org.springframework.ui.format.jodatime.DateTimeFormatAnnotationFormatterFactory; import org.springframework.ui.format.jodatime.DateTimeParser; import org.springframework.ui.format.jodatime.ReadablePartialPrinter; -import org.springframework.ui.format.jodatime.DateTimeFormat.FormatStyle; +import org.springframework.ui.format.jodatime.DateTimeFormat.Style; import org.springframework.ui.format.number.IntegerFormatter; /** @@ -105,7 +105,7 @@ public class FormattingConversionServiceTests { private static class Model { @SuppressWarnings("unused") - @org.springframework.ui.format.jodatime.DateTimeFormat(dateStyle = FormatStyle.SHORT) + @org.springframework.ui.format.jodatime.DateTimeFormat(dateStyle = Style.SHORT) public Date date; }