updated favoring style pattern string instead of Style enum for DateTimeFormat

This commit is contained in:
Keith Donald
2009-11-09 21:07:41 +00:00
parent 7ed6c164b6
commit 6f4112af80
7 changed files with 80 additions and 116 deletions

View File

@@ -18,7 +18,7 @@ import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.ISODateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.Style;
import org.springframework.format.annotation.ISODateTimeFormat.ISO;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.validation.DataBinder;
@@ -137,6 +137,15 @@ public class JodaTimeFormattingTests {
assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated"));
}
@Test
public void testBindDateTimeAnnotatedDefault() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("dateTimeAnnotatedDefault", "10/31/09 12:00 PM");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault"));
}
@Test
public void testBindDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
@@ -218,52 +227,67 @@ public class JodaTimeFormattingTests {
assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
@Test
public void testBindISODateTimeDefault() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("isoDateTimeDefault", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTimeDefault"));
}
public static class JodaTimeBean {
private LocalDate localDate;
@DateTimeFormat(dateStyle = Style.MEDIUM)
@DateTimeFormat(style="M-")
private LocalDate localDateAnnotated;
private LocalTime localTime;
@DateTimeFormat(timeStyle = Style.MEDIUM)
@DateTimeFormat(style="-M")
private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@DateTimeFormat(dateStyle = Style.FULL, timeStyle = Style.FULL)
@DateTimeFormat(style="FF")
private LocalDateTime localDateTimeAnnotated;
private DateTime dateTime;
@DateTimeFormat(dateStyle = Style.MEDIUM, timeStyle = Style.SHORT)
@DateTimeFormat(style="MS")
private DateTime dateTimeAnnotated;
@DateTimeFormat
private DateTime dateTimeAnnotatedDefault;
private Date date;
@DateTimeFormat(dateStyle = Style.SHORT)
@DateTimeFormat(style="S-")
private Date dateAnnotated;
private Calendar calendar;
@DateTimeFormat(dateStyle = Style.SHORT)
@DateTimeFormat(style="S-")
private Calendar calendarAnnotated;
private Long millis;
@DateTimeFormat(dateStyle = Style.SHORT)
@DateTimeFormat(style="S-")
private Long millisAnnotated;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE)
@ISODateTimeFormat(ISO.DATE)
private LocalDate isoDate;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.TIME)
@ISODateTimeFormat(ISO.TIME)
private LocalTime isoTime;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE_TIME)
@ISODateTimeFormat(ISO.DATE_TIME)
private DateTime isoDateTime;
@ISODateTimeFormat
private DateTime isoDateTimeDefault;
public LocalDate getLocalDate() {
return localDate;
}
@@ -328,6 +352,14 @@ public class JodaTimeFormattingTests {
this.dateTimeAnnotated = dateTimeAnnotated;
}
public DateTime getDateTimeAnnotatedDefault() {
return dateTimeAnnotatedDefault;
}
public void setDateTimeAnnotatedDefault(DateTime dateTimeAnnotatedDefault) {
this.dateTimeAnnotatedDefault = dateTimeAnnotatedDefault;
}
public Date getDate() {
return date;
}
@@ -400,5 +432,13 @@ public class JodaTimeFormattingTests {
this.isoDateTime = isoDateTime;
}
public DateTime getIsoDateTimeDefault() {
return isoDateTimeDefault;
}
public void setIsoDateTimeDefault(DateTime isoDateTimeDefault) {
this.isoDateTimeDefault = isoDateTimeDefault;
}
}
}

View File

@@ -31,12 +31,10 @@ import org.junit.Test;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.annotation.DateTimeFormat.Style;
import org.springframework.format.datetime.joda.DateTimeFormatAnnotationFormatterFactory;
import org.springframework.format.datetime.joda.DateTimeParser;
import org.springframework.format.datetime.joda.ReadablePartialPrinter;
import org.springframework.format.number.IntegerFormatter;
import org.springframework.format.support.FormattingConversionService;
/**
* @author Keith Donald
@@ -106,7 +104,7 @@ public class FormattingConversionServiceTests {
private static class Model {
@SuppressWarnings("unused")
@org.springframework.format.annotation.DateTimeFormat(dateStyle = Style.SHORT)
@org.springframework.format.annotation.DateTimeFormat(style="S-")
public Date date;
}