From e489706f130c712f4a7f92ac6c893a6f0f7fa26c Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 18 Apr 2021 15:48:00 +0200 Subject: [PATCH] Ensure @DateTimeFormat tests pass on Java 9+ Prior to this commit, two tests for exception handling regarding @DateTimeFormat processing only passed on Java 8. This is due to the fact that the toString() implementation for annotations changed in Java 9. Specifically, the representation for arrays changed from [] to {}, and strings are enclosed in double quotes beginning with Java 9. This commit ensures that the affected @DateTimeFormat tests pass on Java 9+, by making the assertions more lenient regarding toString() output for annotations. See gh-26777 See gh-26804 --- .../format/datetime/DateFormattingTests.java | 6 +++++- .../format/datetime/standard/DateTimeFormattingTests.java | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java b/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java index 15342c69b6..df7729181f 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java @@ -140,10 +140,14 @@ public class DateFormattingTests { .hasCauseInstanceOf(ParseException.class).getCause() // Unable to parse date time value "99/01/01" using configuration from // @org.springframework.format.annotation.DateTimeFormat(pattern=, style=S-, iso=NONE, fallbackPatterns=[]) + // We do not check "fallbackPatterns=[]", since the array representation in the toString() + // implementation for annotations changed from [] to {} in Java 9. In addition, strings + // are enclosed in double quotes beginning with Java 9. Thus, we cannot check directly + // for the presence of "style=S-". .hasMessageContainingAll( "Unable to parse date time value \"99/01/01\" using configuration from", "@org.springframework.format.annotation.DateTimeFormat", - "style=S-", "iso=NONE", "fallbackPatterns=[]") + "style=", "S-", "iso=NONE") .hasCauseInstanceOf(ParseException.class).getCause() .hasMessageStartingWith("Unparseable date: \"99/01/01\"") .hasNoCause(); diff --git a/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java b/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java index 6aa28756f6..23a62770fd 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java @@ -339,10 +339,11 @@ class DateTimeFormattingTests { .hasCauseInstanceOf(DateTimeParseException.class).getCause() // Unable to parse date time value "2009-31-10" using configuration from // @org.springframework.format.annotation.DateTimeFormat(pattern=, style=SS, iso=DATE, fallbackPatterns=[]) + // We do not check "fallbackPatterns=[]", since the array representation in the toString() + // implementation for annotations changed from [] to {} in Java 9. .hasMessageContainingAll( "Unable to parse date time value \"2009-31-10\" using configuration from", - "@org.springframework.format.annotation.DateTimeFormat", - "iso=DATE", "fallbackPatterns=[]") + "@org.springframework.format.annotation.DateTimeFormat", "iso=DATE") .hasCauseInstanceOf(DateTimeParseException.class).getCause() .hasMessageStartingWith("Text '2009-31-10'") .hasCauseInstanceOf(DateTimeException.class).getCause()