diff --git a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeFormattingConfigurer.java b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeFormattingConfigurer.java index 31415f8d52..3a8aef8de6 100644 --- a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeFormattingConfigurer.java +++ b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeFormattingConfigurer.java @@ -36,8 +36,6 @@ import org.springframework.ui.format.Printer; */ public class JodaTimeFormattingConfigurer { - private FormatterRegistry formatterRegistry; - private String dateStyle; private String timeStyle; @@ -46,15 +44,6 @@ public class JodaTimeFormattingConfigurer { private boolean useISOFormat; - /** - * Creates a new JodaTimeFormattingConfigurer that installs into the provided FormatterRegistry. - * Call {@link #registerJodaTimeFormatting()} to install. - * @param formatterRegistry the registry to register Joda Time formatters with - */ - public JodaTimeFormattingConfigurer(FormatterRegistry formatterRegistry) { - this.formatterRegistry = formatterRegistry; - } - /** * Set the default format style of Joda {@link LocalDate} objects. * Default is {@link DateTimeFormat#shortDate()}. @@ -95,25 +84,25 @@ public class JodaTimeFormattingConfigurer { /** * Install Joda Time formatters given the current configuration of this {@link JodaTimeFormattingConfigurer}. */ - public void registerJodaTimeFormatting() { - JodaTimeConverters.registerConverters(this.formatterRegistry.getConverterRegistry()); + public void installJodaTimeFormatting(FormatterRegistry formatterRegistry) { + JodaTimeConverters.registerConverters(formatterRegistry.getConverterRegistry()); DateTimeFormatter jodaDateFormatter = getJodaDateFormatter(); - this.formatterRegistry.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(jodaDateFormatter), new DateTimeParser(jodaDateFormatter)); + formatterRegistry.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(jodaDateFormatter), new DateTimeParser(jodaDateFormatter)); DateTimeFormatter jodaTimeFormatter = getJodaTimeFormatter(); - this.formatterRegistry.addFormatterForFieldType(LocalTime.class, new ReadablePartialPrinter(jodaTimeFormatter), new DateTimeParser(jodaTimeFormatter)); + formatterRegistry.addFormatterForFieldType(LocalTime.class, new ReadablePartialPrinter(jodaTimeFormatter), new DateTimeParser(jodaTimeFormatter)); DateTimeFormatter jodaDateTimeFormatter = getJodaDateTimeFormatter(); Parser dateTimeParser = new DateTimeParser(jodaDateTimeFormatter); - this.formatterRegistry.addFormatterForFieldType(LocalDateTime.class, new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser); + formatterRegistry.addFormatterForFieldType(LocalDateTime.class, new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser); Printer readableInstantPrinter = new ReadableInstantPrinter(jodaDateTimeFormatter); - this.formatterRegistry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser); - this.formatterRegistry.addFormatterForFieldType(Calendar.class, readableInstantPrinter, dateTimeParser); - this.formatterRegistry.addFormatterForFieldType(Date.class, new MillisecondInstantPrinter(jodaDateTimeFormatter), dateTimeParser); + formatterRegistry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser); + formatterRegistry.addFormatterForFieldType(Calendar.class, readableInstantPrinter, dateTimeParser); + formatterRegistry.addFormatterForFieldType(Date.class, new MillisecondInstantPrinter(jodaDateTimeFormatter), dateTimeParser); - this.formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); + formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); } // internal helpers 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 new file mode 100644 index 0000000000..768fcd74af --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java @@ -0,0 +1,132 @@ +package org.springframework.ui.format.jodatime; + +import static org.junit.Assert.assertEquals; + +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import org.joda.time.DateTime; +import org.joda.time.LocalDate; +import org.joda.time.LocalDateTime; +import org.joda.time.LocalTime; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.MutablePropertyValues; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.ui.format.support.FormattingConversionService; +import org.springframework.validation.DataBinder; + +public class JodaTimeFormattingTests { + + private FormattingConversionService conversionService = new FormattingConversionService(); + + private DataBinder binder; + + @Before + public void setUp() { + JodaTimeFormattingConfigurer configurer = new JodaTimeFormattingConfigurer(); + configurer.installJodaTimeFormatting(conversionService); + + binder = new DataBinder(new JodaTimeBean()); + binder.setConversionService(conversionService); + + LocaleContextHolder.setLocale(Locale.US); + } + + @After + public void tearDown() { + LocaleContextHolder.setLocale(null); + } + + @Test + public void testBindLocalDate() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.addPropertyValue("localDate", "10/31/09"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + } + + @Test + public void testBindLocalDateArray() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.addPropertyValue("localDate", new String[] { "10/31/09" }); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + } + + private static class JodaTimeBean { + + private LocalDate localDate; + + private LocalTime localTime; + + private LocalDateTime localDateTime; + + private DateTime dateTime; + + private Date date; + + private Calendar calendar; + + private Long millis; + + public LocalDate getLocalDate() { + return localDate; + } + + public void setLocalDate(LocalDate localDate) { + this.localDate = localDate; + } + + public LocalTime getLocalTime() { + return localTime; + } + + public void setLocalTime(LocalTime localTime) { + this.localTime = localTime; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public void setLocalDateTime(LocalDateTime localDateTime) { + this.localDateTime = localDateTime; + } + + public DateTime getDateTime() { + return dateTime; + } + + public void setDateTime(DateTime dateTime) { + this.dateTime = dateTime; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public Calendar getCalendar() { + return calendar; + } + + public void setCalendar(Calendar calendar) { + this.calendar = calendar; + } + + public Long getMillis() { + return millis; + } + + public void setMillis(Long millis) { + this.millis = millis; + } + + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java index 5c0f8fe330..dba0a1300a 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java @@ -16,9 +16,7 @@ package org.springframework.core.convert.support; -import java.util.Collection; import java.util.Locale; -import java.util.Map; /** * Default implementation of a conversion service. Will automatically register from string @@ -34,21 +32,6 @@ public class DefaultConversionService extends GenericConversionService { * Create a new default conversion service, installing the default converters. */ public DefaultConversionService() { - addGenericConverter(Object[].class, Object[].class, new ArrayToArrayConverter(this)); - addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(this)); - addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(this)); - addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(this)); - addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(this)); - addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter(this)); - addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter(this)); - addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(this)); - addGenericConverter(Map.class, Map.class, new MapToMapConverter(this)); - addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(this)); - addGenericConverter(Map.class, Collection.class, new MapToCollectionConverter(this)); - addGenericConverter(Map.class, Object.class, new MapToObjectConverter(this)); - addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(this)); - addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(this)); - addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(this)); addConverter(String.class, Boolean.class, new StringToBooleanConverter()); addConverter(String.class, Character.class, new StringToCharacterConverter()); addConverter(String.class, Locale.class, new StringToLocaleConverter()); diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 6f92b9a362..4d06012976 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -19,6 +19,7 @@ package org.springframework.core.convert.support; import static org.springframework.core.convert.support.ConversionUtils.invokeConverter; import java.lang.reflect.Array; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; @@ -61,6 +62,24 @@ public class GenericConversionService implements ConversionService, ConverterReg } }; + public GenericConversionService() { + addGenericConverter(Object[].class, Object[].class, new ArrayToArrayConverter(this)); + addGenericConverter(Object[].class, Collection.class, new ArrayToCollectionConverter(this)); + addGenericConverter(Object[].class, Map.class, new ArrayToMapConverter(this)); + addGenericConverter(Object[].class, Object.class, new ArrayToObjectConverter(this)); + addGenericConverter(Collection.class, Collection.class, new CollectionToCollectionConverter(this)); + addGenericConverter(Collection.class, Object[].class, new CollectionToArrayConverter(this)); + addGenericConverter(Collection.class, Map.class, new CollectionToMapConverter(this)); + addGenericConverter(Collection.class, Object.class, new CollectionToObjectConverter(this)); + addGenericConverter(Map.class, Map.class, new MapToMapConverter(this)); + addGenericConverter(Map.class, Object[].class, new MapToArrayConverter(this)); + addGenericConverter(Map.class, Collection.class, new MapToCollectionConverter(this)); + addGenericConverter(Map.class, Object.class, new MapToObjectConverter(this)); + addGenericConverter(Object.class, Object[].class, new ObjectToArrayConverter(this)); + addGenericConverter(Object.class, Collection.class, new ObjectToCollectionConverter(this)); + addGenericConverter(Object.class, Map.class, new ObjectToMapConverter(this)); + } + /** * Registers the converters in the set provided. * JavaBean-friendly alternative to calling {@link #addConverter(Converter)}.