Skip to content

Commit

Permalink
Only register Date converters with global format
Browse files Browse the repository at this point in the history
Change JodaTimeFormatterRegistrar and DateFormatterRegistrar to only
register converters for the Date and Calendar types when a global format
has been defined. This means that the ObjectToObject converter will
handle String->Date conversion using the deprecated Date(String)
constructor (as was the case with Spring 3.1).

Issue: SPR-10105
  • Loading branch information
philwebb committed Feb 12, 2013
1 parent dbe3c23 commit 66ae626
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,24 @@
public class DateFormatterRegistrar implements FormatterRegistrar {


private DateFormatter dateFormatter = new DateFormatter();
private DateFormatter dateFormatter;


public void registerFormatters(FormatterRegistry registry) {
addDateConverters(registry);
registry.addFormatter(this.dateFormatter);
registry.addFormatterForFieldType(Calendar.class, this.dateFormatter);
registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());

// In order to retain back compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if(this.dateFormatter != null) {
registry.addFormatter(this.dateFormatter);
registry.addFormatterForFieldType(Calendar.class, this.dateFormatter);
}
}

/**
* Set the date formatter to register. If not specified the default {@link DateFormatter}
* will be used. This method can be used if additional formatter configuration is
* required.
* Set the date formatter to register. If not specified no formatter is registered.
* This method can be used if global formatter configuration is required.
* @param dateFormatter the date formatter
*/
public void setFormatter(DateFormatter dateFormatter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,16 @@ public void registerFormatters(FormatterRegistry registry) {
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
ReadableInstant.class, Date.class, Calendar.class);
ReadableInstant.class);

// In order to retain back compatibility we only register Date/Calendar
// types when a user defined formatter is specified (see SPR-10105)
if(this.formatters.containsKey(Type.DATE_TIME)) {
addFormatterForFields(registry,
new ReadableInstantPrinter(dateTimeFormatter),
new DateTimeParser(dateTimeFormatter),
Date.class, Calendar.class);
}

registry.addFormatterForFieldAnnotation(
new JodaDateTimeFormatAnnotationFormatterFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package org.springframework.format.datetime;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -50,9 +53,12 @@ public class DateFormattingTests {

@Before
public void setUp() {
DefaultConversionService.addDefaultConverters(conversionService);

DateFormatterRegistrar registrar = new DateFormatterRegistrar();
setUp(registrar);
}

private void setUp(DateFormatterRegistrar registrar) {
DefaultConversionService.addDefaultConverters(conversionService);
registrar.registerFormatters(conversionService);

SimpleDateBean bean = new SimpleDateBean();
Expand Down Expand Up @@ -187,13 +193,48 @@ public void testBindNestedDateAnnotated() {
}

@Test
public void dateToString() throws Exception {
public void dateToStringWithoutGlobalFormat() throws Exception {
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.class), TypeDescriptor.valueOf(String.class));
String expected = date.toString();
assertEquals(expected, actual);
}

@Test
public void dateToStringWithGlobalFormat() throws Exception {
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
registrar.setFormatter(new DateFormatter());
setUp(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.class), TypeDescriptor.valueOf(String.class));
String expected = new DateFormatter().print(date, Locale.US);
assertEquals(expected, actual);
}

@Test
@SuppressWarnings("deprecation")
public void stringToDateWithoutGlobalFormat() throws Exception {
// SPR-10105
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.class);
assertThat(date, equalTo(new Date(string)));
}

@Test
public void stringToDateWithGlobalFormat() throws Exception {
// SPR-10105
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
DateFormatter dateFormatter = new DateFormatter();
dateFormatter.setIso(ISO.DATE_TIME);
registrar.setFormatter(dateFormatter);
setUp(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-06-01T14:23:05.003+0000";
Date date = this.conversionService.convert(string, Date.class);
assertNotNull(date);
}


@SuppressWarnings("unused")
private static class SimpleDateBean {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package org.springframework.format.datetime.joda;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -32,7 +37,6 @@
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.core.convert.TypeDescriptor;
Expand All @@ -42,8 +46,6 @@
import org.springframework.format.support.FormattingConversionService;
import org.springframework.validation.DataBinder;

import static org.junit.Assert.*;

/**
* @author Keith Donald
* @author Juergen Hoeller
Expand Down Expand Up @@ -459,13 +461,40 @@ public void testBindMutableDateTimeAnnotated() {
}

@Test
public void dateToString() throws Exception {
public void dateToStringWithFormat() throws Exception {
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.shortDateTime());
setUp(registrar);
Date date = new Date();
Object actual = this.conversionService.convert(date, TypeDescriptor.valueOf(Date.class), TypeDescriptor.valueOf(String.class));
String expected = JodaTimeContextHolder.getFormatter(org.joda.time.format.DateTimeFormat.shortDateTime(), Locale.US).print(new DateTime(date));
assertEquals(expected, actual);
}

@Test
@SuppressWarnings("deprecation")
public void stringToDateWithoutGlobalFormat() throws Exception {
// SPR-10105
String string = "Sat, 12 Aug 1995 13:30:00 GM";
Date date = this.conversionService.convert(string, Date.class);
assertThat(date, equalTo(new Date(string)));
}

@Test
public void stringToDateWithGlobalFormat() throws Exception {
// SPR-10105
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
DateTimeFormatterFactory factory = new DateTimeFormatterFactory();
factory.setIso(ISO.DATE_TIME);
registrar.setDateTimeFormatter(factory.createDateTimeFormatter());
setUp(registrar);
// This is a format that cannot be parsed by new Date(String)
String string = "2009-10-31T07:00:00.000-05:00";
Date date = this.conversionService.convert(string, Date.class);
assertNotNull(date);
}


@SuppressWarnings("unused")
private static class JodaTimeBean {

Expand Down

0 comments on commit 66ae626

Please sign in to comment.