|
16 | 16 |
|
17 | 17 | package org.springframework.format.datetime.standard;
|
18 | 18 |
|
| 19 | +import java.time.DateTimeException; |
19 | 20 | import java.time.Duration;
|
20 | 21 | import java.time.Instant;
|
21 | 22 | import java.time.LocalDate;
|
@@ -320,6 +321,35 @@ void testBindISODate() {
|
320 | 321 | assertThat(binder.getBindingResult().getFieldValue("isoLocalDate")).isEqualTo("2009-10-31");
|
321 | 322 | }
|
322 | 323 |
|
| 324 | + @Test |
| 325 | + void isoLocalDateWithInvalidFormat() { |
| 326 | + MutablePropertyValues propertyValues = new MutablePropertyValues(); |
| 327 | + String propertyName = "isoLocalDate"; |
| 328 | + propertyValues.add(propertyName, "2009-31-10"); |
| 329 | + binder.bind(propertyValues); |
| 330 | + BindingResult bindingResult = binder.getBindingResult(); |
| 331 | + assertThat(bindingResult.getErrorCount()).isEqualTo(1); |
| 332 | + FieldError fieldError = bindingResult.getFieldError(propertyName); |
| 333 | + assertThat(fieldError.unwrap(TypeMismatchException.class)) |
| 334 | + .hasMessageContaining("for property 'isoLocalDate'") |
| 335 | + .hasCauseInstanceOf(ConversionFailedException.class).getCause() |
| 336 | + .hasMessageContaining("for value '2009-31-10'") |
| 337 | + .hasCauseInstanceOf(IllegalArgumentException.class).getCause() |
| 338 | + .hasMessageContaining("Parse attempt failed for value [2009-31-10]") |
| 339 | + .hasCauseInstanceOf(DateTimeParseException.class).getCause() |
| 340 | + // Unable to parse date time value "2009-31-10" using configuration from |
| 341 | + // @org.springframework.format.annotation.DateTimeFormat(pattern=, style=SS, iso=DATE, fallbackPatterns=[]) |
| 342 | + .hasMessageContainingAll( |
| 343 | + "Unable to parse date time value \"2009-31-10\" using configuration from", |
| 344 | + "@org.springframework.format.annotation.DateTimeFormat", |
| 345 | + "iso=DATE", "fallbackPatterns=[]") |
| 346 | + .hasCauseInstanceOf(DateTimeParseException.class).getCause() |
| 347 | + .hasMessageStartingWith("Text '2009-31-10'") |
| 348 | + .hasCauseInstanceOf(DateTimeException.class).getCause() |
| 349 | + .hasMessageContaining("Invalid value for MonthOfYear (valid values 1 - 12): 31") |
| 350 | + .hasNoCause(); |
| 351 | + } |
| 352 | + |
323 | 353 | @Test
|
324 | 354 | void testBindISOTime() {
|
325 | 355 | MutablePropertyValues propertyValues = new MutablePropertyValues();
|
@@ -519,9 +549,12 @@ void patternLocalDateWithUnsupportedPattern() {
|
519 | 549 | .hasMessageContainingAll(
|
520 | 550 | "Unable to parse date time value \"210302\" using configuration from",
|
521 | 551 | "@org.springframework.format.annotation.DateTimeFormat",
|
522 |
| - "yyyy-MM-dd", "M/d/yy", "yyyyMMdd", "yyyy.MM.dd"); |
| 552 | + "yyyy-MM-dd", "M/d/yy", "yyyyMMdd", "yyyy.MM.dd") |
| 553 | + .hasCauseInstanceOf(DateTimeParseException.class).getCause() |
| 554 | + .hasMessageStartingWith("Text '210302'") |
| 555 | + .hasNoCause(); |
523 | 556 | }
|
524 |
| -} |
| 557 | + } |
525 | 558 |
|
526 | 559 |
|
527 | 560 | public static class DateTimeBean {
|
|
0 commit comments