Browse Source

joda time formatting tests

pull/23217/head
Keith Donald 15 years ago
parent
commit
9ec337b736
  1. 29
      org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeFormattingConfigurer.java
  2. 132
      org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java
  3. 17
      org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java
  4. 19
      org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

29
org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeFormattingConfigurer.java

@ -36,8 +36,6 @@ import org.springframework.ui.format.Printer; @@ -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 { @@ -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 { @@ -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<DateTime> dateTimeParser = new DateTimeParser(jodaDateTimeFormatter);
this.formatterRegistry.addFormatterForFieldType(LocalDateTime.class, new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser);
formatterRegistry.addFormatterForFieldType(LocalDateTime.class, new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser);
Printer<ReadableInstant> 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

132
org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java

@ -0,0 +1,132 @@ @@ -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;
}
}
}

17
org.springframework.core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java

@ -16,9 +16,7 @@ @@ -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 <i>from string</i>
@ -34,21 +32,6 @@ public class DefaultConversionService extends GenericConversionService { @@ -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());

19
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

@ -19,6 +19,7 @@ package org.springframework.core.convert.support; @@ -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 @@ -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)}.

Loading…
Cancel
Save