Browse Source

Polish "Add support for YearMonth and MonthDay in @DateTimeFormat"

See gh-1215
pull/27762/head
Stephane Nicoll 4 years ago
parent
commit
a57ea39707
  1. 3
      spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java
  2. 30
      spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java

3
spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java

@ -19,9 +19,9 @@ package org.springframework.format.datetime.standard; @@ -19,9 +19,9 @@ package org.springframework.format.datetime.standard;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.MonthDay;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@ -68,7 +68,6 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu @@ -68,7 +68,6 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
}
@Override
public final Set<Class<?>> getFieldTypes() {
return FIELD_TYPES;

30
spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java

@ -473,9 +473,9 @@ class DateTimeFormattingTests { @@ -473,9 +473,9 @@ class DateTimeFormattingTests {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonthAnnotatedPattern", "12/2007");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("yearMonthAnnotatedPattern").toString().equals("12/2007"));
assertEquals(YearMonth.parse("2007-12"), binder.getBindingResult().getRawFieldValue("yearMonthAnnotatedPattern"));
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
assertThat(binder.getBindingResult().getFieldValue("yearMonthAnnotatedPattern")).isEqualTo("12/2007");
assertThat(binder.getBindingResult().getRawFieldValue("yearMonthAnnotatedPattern")).isEqualTo(YearMonth.parse("2007-12"));
}
@Test
@ -487,6 +487,16 @@ class DateTimeFormattingTests { @@ -487,6 +487,16 @@ class DateTimeFormattingTests {
assertThat(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03")).isTrue();
}
@Test
public void testBindMonthDayAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDayAnnotatedPattern", "1/3");
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
assertThat(binder.getBindingResult().getFieldValue("monthDayAnnotatedPattern")).isEqualTo("1/3");
assertThat(binder.getBindingResult().getRawFieldValue("monthDayAnnotatedPattern")).isEqualTo(MonthDay.parse("--01-03"));
}
@Nested
class FallbackPatternTests {
@ -568,16 +578,6 @@ class DateTimeFormattingTests { @@ -568,16 +578,6 @@ class DateTimeFormattingTests {
}
}
@Test
public void testBindMonthDayAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDayAnnotatedPattern", "1/3");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("monthDayAnnotatedPattern").toString().equals("1/3"));
assertEquals(MonthDay.parse("--01-03"), binder.getBindingResult().getRawFieldValue("monthDayAnnotatedPattern"));
}
public static class DateTimeBean {
@ -635,11 +635,11 @@ class DateTimeFormattingTests { @@ -635,11 +635,11 @@ class DateTimeFormattingTests {
@DateTimeFormat(pattern="MM/uuuu")
private YearMonth yearMonthAnnotatedPattern;
private MonthDay monthDay;
@DateTimeFormat(pattern="M/d")
private MonthDay monthDayAnnotatedPattern;
private MonthDay monthDay;
private final List<DateTimeBean> children = new ArrayList<>();

Loading…
Cancel
Save