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.
|
* 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.
|
* 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.
|
* A pattern String that defines a custom format for the property.
|
||||||
|
|
@ -49,7 +49,7 @@ public @interface DateTimeFormat {
|
||||||
/**
|
/**
|
||||||
* Supported DateTimeFormat styles.
|
* Supported DateTimeFormat styles.
|
||||||
*/
|
*/
|
||||||
public enum FormatStyle {
|
public enum Style {
|
||||||
SHORT {
|
SHORT {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "S";
|
return "S";
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
package org.springframework.ui.format.jodatime;
|
package org.springframework.ui.format.jodatime;
|
||||||
|
|
||||||
import org.joda.time.format.DateTimeFormatter;
|
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.
|
* Formats properties annotated with the {@link DateTimeFormat} annotation.
|
||||||
|
|
@ -32,13 +32,13 @@ public final class DateTimeFormatAnnotationFormatterFactory extends AbstractDate
|
||||||
if (!pattern.isEmpty()) {
|
if (!pattern.isEmpty()) {
|
||||||
return forPattern(pattern);
|
return forPattern(pattern);
|
||||||
} else {
|
} else {
|
||||||
FormatStyle dateStyle = annotation.dateStyle();
|
Style dateStyle = annotation.dateStyle();
|
||||||
FormatStyle timeStyle = annotation.timeStyle();
|
Style timeStyle = annotation.timeStyle();
|
||||||
return forDateTimeStyle(dateStyle, 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());
|
return org.joda.time.format.DateTimeFormat.forStyle(dateStyle.toString() + timeStyle.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,9 @@ import java.lang.annotation.Target;
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface ISODateTimeFormat {
|
public @interface ISODateTimeFormat {
|
||||||
|
|
||||||
FormatStyle value();
|
Style value();
|
||||||
|
|
||||||
public enum FormatStyle {
|
public enum Style {
|
||||||
DATE, TIME, DATE_TIME
|
DATE, TIME, DATE_TIME
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
package org.springframework.ui.format.jodatime;
|
package org.springframework.ui.format.jodatime;
|
||||||
|
|
||||||
import org.joda.time.format.DateTimeFormatter;
|
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.
|
* 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> {
|
public final class ISODateTimeFormatAnnotationFormatterFactory extends AbstractDateTimeAnnotationFormatterFactory<ISODateTimeFormat> {
|
||||||
|
|
||||||
protected DateTimeFormatter configureDateTimeFormatterFrom(ISODateTimeFormat annotation) {
|
protected DateTimeFormatter configureDateTimeFormatterFrom(ISODateTimeFormat annotation) {
|
||||||
FormatStyle style = annotation.value();
|
Style style = annotation.value();
|
||||||
if (style == FormatStyle.DATE) {
|
if (style == Style.DATE) {
|
||||||
return org.joda.time.format.ISODateTimeFormat.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();
|
return org.joda.time.format.ISODateTimeFormat.time();
|
||||||
} else {
|
} else {
|
||||||
return org.joda.time.format.ISODateTimeFormat.dateTime();
|
return org.joda.time.format.ISODateTimeFormat.dateTime();
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,11 @@ import org.joda.time.LocalDateTime;
|
||||||
import org.joda.time.LocalTime;
|
import org.joda.time.LocalTime;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.MutablePropertyValues;
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
import org.springframework.ui.format.jodatime.DateTimeFormat.Style;
|
||||||
import org.springframework.ui.format.support.FormattingConversionService;
|
import org.springframework.ui.format.support.FormattingConversionService;
|
||||||
import org.springframework.validation.DataBinder;
|
import org.springframework.validation.DataBinder;
|
||||||
|
|
||||||
|
|
@ -46,6 +48,7 @@ public class JodaTimeFormattingTests {
|
||||||
propertyValues.addPropertyValue("localDate", "10/31/09");
|
propertyValues.addPropertyValue("localDate", "10/31/09");
|
||||||
binder.bind(propertyValues);
|
binder.bind(propertyValues);
|
||||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||||
|
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -56,22 +59,53 @@ public class JodaTimeFormattingTests {
|
||||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
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;
|
private LocalDate localDate;
|
||||||
|
|
||||||
|
@DateTimeFormat(dateStyle = Style.MEDIUM)
|
||||||
|
private LocalDate localDateAnnotated;
|
||||||
|
|
||||||
private LocalTime localTime;
|
private LocalTime localTime;
|
||||||
|
|
||||||
|
@DateTimeFormat(timeStyle = Style.LONG)
|
||||||
|
private LocalDate localTimeAnnotated;
|
||||||
|
|
||||||
private LocalDateTime localDateTime;
|
private LocalDateTime localDateTime;
|
||||||
|
|
||||||
|
@DateTimeFormat(dateStyle = Style.FULL, timeStyle = Style.FULL)
|
||||||
|
private LocalDateTime localDateTimeAnnotated;
|
||||||
|
|
||||||
private DateTime dateTime;
|
private DateTime dateTime;
|
||||||
|
|
||||||
|
@DateTimeFormat(dateStyle = Style.MEDIUM, timeStyle = Style.SHORT)
|
||||||
|
private LocalDateTime dateTimeAnnotated;
|
||||||
|
|
||||||
private Date date;
|
private Date date;
|
||||||
|
|
||||||
|
@DateTimeFormat(dateStyle = Style.SHORT)
|
||||||
|
private Date dateAnnotated;
|
||||||
|
|
||||||
private Calendar calendar;
|
private Calendar calendar;
|
||||||
|
|
||||||
|
@DateTimeFormat(dateStyle = Style.SHORT)
|
||||||
|
private Calendar calendarAnnotated;
|
||||||
|
|
||||||
private Long millis;
|
private Long millis;
|
||||||
|
|
||||||
|
@DateTimeFormat(dateStyle = Style.SHORT)
|
||||||
|
private Long millisAnnotated;
|
||||||
|
|
||||||
public LocalDate getLocalDate() {
|
public LocalDate getLocalDate() {
|
||||||
return localDate;
|
return localDate;
|
||||||
}
|
}
|
||||||
|
|
@ -80,6 +114,14 @@ public class JodaTimeFormattingTests {
|
||||||
this.localDate = localDate;
|
this.localDate = localDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDate getLocalDateAnnotated() {
|
||||||
|
return localDateAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocalDateAnnotated(LocalDate localDateAnnotated) {
|
||||||
|
this.localDateAnnotated = localDateAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
public LocalTime getLocalTime() {
|
public LocalTime getLocalTime() {
|
||||||
return localTime;
|
return localTime;
|
||||||
}
|
}
|
||||||
|
|
@ -88,6 +130,14 @@ public class JodaTimeFormattingTests {
|
||||||
this.localTime = localTime;
|
this.localTime = localTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDate getLocalTimeAnnotated() {
|
||||||
|
return localTimeAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocalTimeAnnotated(LocalDate localTimeAnnotated) {
|
||||||
|
this.localTimeAnnotated = localTimeAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
public LocalDateTime getLocalDateTime() {
|
public LocalDateTime getLocalDateTime() {
|
||||||
return localDateTime;
|
return localDateTime;
|
||||||
}
|
}
|
||||||
|
|
@ -96,6 +146,14 @@ public class JodaTimeFormattingTests {
|
||||||
this.localDateTime = localDateTime;
|
this.localDateTime = localDateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getLocalDateTimeAnnotated() {
|
||||||
|
return localDateTimeAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
|
||||||
|
this.localDateTimeAnnotated = localDateTimeAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
public DateTime getDateTime() {
|
public DateTime getDateTime() {
|
||||||
return dateTime;
|
return dateTime;
|
||||||
}
|
}
|
||||||
|
|
@ -104,6 +162,14 @@ public class JodaTimeFormattingTests {
|
||||||
this.dateTime = dateTime;
|
this.dateTime = dateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getDateTimeAnnotated() {
|
||||||
|
return dateTimeAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateTimeAnnotated(LocalDateTime dateTimeAnnotated) {
|
||||||
|
this.dateTimeAnnotated = dateTimeAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
public Date getDate() {
|
public Date getDate() {
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
@ -112,6 +178,14 @@ public class JodaTimeFormattingTests {
|
||||||
this.date = date;
|
this.date = date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date getDateAnnotated() {
|
||||||
|
return dateAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateAnnotated(Date dateAnnotated) {
|
||||||
|
this.dateAnnotated = dateAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
public Calendar getCalendar() {
|
public Calendar getCalendar() {
|
||||||
return calendar;
|
return calendar;
|
||||||
}
|
}
|
||||||
|
|
@ -120,6 +194,14 @@ public class JodaTimeFormattingTests {
|
||||||
this.calendar = calendar;
|
this.calendar = calendar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Calendar getCalendarAnnotated() {
|
||||||
|
return calendarAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCalendarAnnotated(Calendar calendarAnnotated) {
|
||||||
|
this.calendarAnnotated = calendarAnnotated;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getMillis() {
|
public Long getMillis() {
|
||||||
return millis;
|
return millis;
|
||||||
}
|
}
|
||||||
|
|
@ -128,5 +210,13 @@ public class JodaTimeFormattingTests {
|
||||||
this.millis = millis;
|
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.DateTimeFormatAnnotationFormatterFactory;
|
||||||
import org.springframework.ui.format.jodatime.DateTimeParser;
|
import org.springframework.ui.format.jodatime.DateTimeParser;
|
||||||
import org.springframework.ui.format.jodatime.ReadablePartialPrinter;
|
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;
|
import org.springframework.ui.format.number.IntegerFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -105,7 +105,7 @@ public class FormattingConversionServiceTests {
|
||||||
private static class Model {
|
private static class Model {
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@org.springframework.ui.format.jodatime.DateTimeFormat(dateStyle = FormatStyle.SHORT)
|
@org.springframework.ui.format.jodatime.DateTimeFormat(dateStyle = Style.SHORT)
|
||||||
public Date date;
|
public Date date;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue