formatting tests and polishing
This commit is contained in:
parent
9ec337b736
commit
ff39bc376f
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<ISODateTimeFormat> {
|
||||
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue