From f1fed9c174705aa4f2817c94fc519f17b670976b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 21 May 2024 17:39:11 +0200 Subject: [PATCH] Polishing (cherry picked from commit 20dea0dae2725fbf371ff720fceb82e99d5fdd41) --- .../datetime/standard/InstantFormatter.java | 6 +- .../format/datetime/DateFormattingTests.java | 4 +- .../standard/DateTimeFormattingTests.java | 116 +++++++++--------- .../standard/InstantFormatterTests.java | 19 ++- 4 files changed, 71 insertions(+), 74 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java index 456c0ad0909..06ff8213de5 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,12 +41,12 @@ public class InstantFormatter implements Formatter { @Override public Instant parse(String text, Locale locale) throws ParseException { - if (text.length() > 0 && Character.isAlphabetic(text.charAt(0))) { + if (!text.isEmpty() && Character.isAlphabetic(text.charAt(0))) { // assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT" return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text)); } else { - // assuming UTC instant a la "2007-12-03T10:15:30.00Z" + // assuming UTC instant a la "2007-12-03T10:15:30.000Z" return Instant.parse(text); } } 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 2ea58f5e06a..6f7751545e3 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,7 +52,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Juergen Hoeller * @author Sam Brannen */ -public class DateFormattingTests { +class DateFormattingTests { private final FormattingConversionService conversionService = new FormattingConversionService(); 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 f9d64235a0e..0d5c243091d 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -105,7 +105,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", "10/31/09"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09"); } @@ -117,7 +117,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", "October 31, 2009"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("October 31, 2009"); } @@ -129,7 +129,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", "20091031"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("20091031"); } @@ -138,7 +138,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", new String[] {"10/31/09"}); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); } @Test @@ -146,7 +146,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("styleLocalDate", "Oct 31, 2009"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("styleLocalDate")).isEqualTo("Oct 31, 2009"); } @@ -164,7 +164,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("children[0].styleLocalDate", "Oct 31, 2009"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("children[0].styleLocalDate")).isEqualTo("Oct 31, 2009"); } @@ -174,7 +174,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("styleLocalDate", "Oct 31, 2009"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("styleLocalDate")).isEqualTo("Oct 31, 2009"); } @@ -193,7 +193,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", new GregorianCalendar(2009, 9, 31, 0, 0)); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09"); } @@ -202,7 +202,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "12:00 PM"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM"); } @@ -214,7 +214,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "12:00:00 PM"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00:00 PM"); } @@ -226,7 +226,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "130000"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("130000"); } @@ -235,7 +235,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("styleLocalTime", "12:00:00 PM"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("styleLocalTime")).isEqualTo("12:00:00 PM"); } @@ -244,7 +244,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0)); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM"); } @@ -253,10 +253,9 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0)); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); - assertThat(value.startsWith("10/31/09")).isTrue(); - assertThat(value.endsWith("12:00 PM")).isTrue(); + assertThat(value).startsWith("10/31/09").endsWith("12:00 PM"); } @Test @@ -264,10 +263,9 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("styleLocalDateTime", LocalDateTime.of(2009, 10, 31, 12, 0)); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); String value = binder.getBindingResult().getFieldValue("styleLocalDateTime").toString(); - assertThat(value.startsWith("Oct 31, 2009")).isTrue(); - assertThat(value.endsWith("12:00:00 PM")).isTrue(); + assertThat(value).startsWith("Oct 31, 2009").endsWith("12:00:00 PM"); } @Test @@ -275,10 +273,9 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0)); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); - assertThat(value.startsWith("10/31/09")).isTrue(); - assertThat(value.endsWith("12:00 PM")).isTrue(); + assertThat(value).startsWith("10/31/09").endsWith("12:00 PM"); } @Test @@ -289,10 +286,9 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0)); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); - assertThat(value.startsWith("Oct 31, 2009")).isTrue(); - assertThat(value.endsWith("12:00:00 PM")).isTrue(); + assertThat(value).startsWith("Oct 31, 2009").endsWith("12:00:00 PM"); } @Test @@ -300,7 +296,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("patternLocalDateTime", "10/31/09 12:00 PM"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("patternLocalDateTime")).isEqualTo("10/31/09 12:00 PM"); } @@ -317,7 +313,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("isoLocalDate", "2009-10-31"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("isoLocalDate")).isEqualTo("2009-10-31"); } @@ -356,7 +352,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("isoLocalTime", "12:00:00"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("isoLocalTime")).isEqualTo("12:00:00"); } @@ -365,7 +361,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("isoLocalTime", "12:00:00.000-05:00"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("isoLocalTime")).isEqualTo("12:00:00"); } @@ -374,7 +370,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("isoLocalDateTime", "2009-10-31T12:00:00"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("isoLocalDateTime")).isEqualTo("2009-10-31T12:00:00"); } @@ -383,7 +379,7 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("isoLocalDateTime", "2009-10-31T12:00:00.000Z"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); assertThat(binder.getBindingResult().getFieldValue("isoLocalDateTime")).isEqualTo("2009-10-31T12:00:00"); } @@ -392,8 +388,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("instant", "2009-10-31T12:00:00.000Z"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31T12:00")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("instant").toString()).startsWith("2009-10-31T12:00"); } @Test @@ -405,8 +401,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("instant", new Date(109, 9, 31, 12, 0)); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("instant").toString()).startsWith("2009-10-31"); } finally { TimeZone.setDefault(defaultZone); @@ -418,8 +414,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("period", "P6Y3M1D"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("period").toString().equals("P6Y3M1D")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("period").toString()).isEqualTo("P6Y3M1D"); } @Test @@ -427,8 +423,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("duration", "PT8H6M12.345S"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("duration").toString().equals("PT8H6M12.345S")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("duration").toString()).isEqualTo("PT8H6M12.345S"); } @Test @@ -436,8 +432,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("year", "2007"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("year").toString().equals("2007")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("year").toString()).isEqualTo("2007"); } @Test @@ -445,8 +441,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("month", "JULY"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("month").toString().equals("JULY")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("month").toString()).isEqualTo("JULY"); } @Test @@ -454,8 +450,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("month", "July"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("month").toString().equals("JULY")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("month").toString()).isEqualTo("JULY"); } @Test @@ -463,8 +459,8 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("yearMonth", "2007-12"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("yearMonth").toString()).isEqualTo("2007-12"); } @Test @@ -472,10 +468,11 @@ class DateTimeFormattingTests { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("monthDay", "--12-03"); binder.bind(propertyValues); - assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0); - assertThat(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03")).isTrue(); + assertThat(binder.getBindingResult().getErrorCount()).isZero(); + assertThat(binder.getBindingResult().getFieldValue("monthDay").toString()).isEqualTo("--12-03"); } + @Nested class FallbackPatternTests { @@ -487,7 +484,7 @@ class DateTimeFormattingTests { propertyValues.add(propertyName, propertyValue); binder.bind(propertyValues); BindingResult bindingResult = binder.getBindingResult(); - assertThat(bindingResult.getErrorCount()).isEqualTo(0); + assertThat(bindingResult.getErrorCount()).isZero(); assertThat(bindingResult.getFieldValue(propertyName)).isEqualTo("3/2/21"); } @@ -499,11 +496,12 @@ class DateTimeFormattingTests { propertyValues.add(propertyName, propertyValue); binder.bind(propertyValues); BindingResult bindingResult = binder.getBindingResult(); - assertThat(bindingResult.getErrorCount()).isEqualTo(0); + assertThat(bindingResult.getErrorCount()).isZero(); assertThat(bindingResult.getFieldValue(propertyName)).isEqualTo("2021-03-02"); } @ParameterizedTest(name = "input date: {0}") + // @ValueSource(strings = {"12:00:00\u202FPM", "12:00:00", "12:00"}) @ValueSource(strings = {"12:00:00 PM", "12:00:00", "12:00"}) void styleLocalTime(String propertyValue) { String propertyName = "styleLocalTimeWithFallbackPatterns"; @@ -511,7 +509,8 @@ class DateTimeFormattingTests { propertyValues.add(propertyName, propertyValue); binder.bind(propertyValues); BindingResult bindingResult = binder.getBindingResult(); - assertThat(bindingResult.getErrorCount()).isEqualTo(0); + assertThat(bindingResult.getErrorCount()).isZero(); + // assertThat(bindingResult.getFieldValue(propertyName)).asString().matches("12:00:00\\SPM"); assertThat(bindingResult.getFieldValue(propertyName)).isEqualTo("12:00:00 PM"); } @@ -523,7 +522,7 @@ class DateTimeFormattingTests { propertyValues.add(propertyName, propertyValue); binder.bind(propertyValues); BindingResult bindingResult = binder.getBindingResult(); - assertThat(bindingResult.getErrorCount()).isEqualTo(0); + assertThat(bindingResult.getErrorCount()).isZero(); assertThat(bindingResult.getFieldValue(propertyName)).isEqualTo("2021-03-02T12:00:00"); } @@ -565,10 +564,10 @@ class DateTimeFormattingTests { @DateTimeFormat(style = "M-") private LocalDate styleLocalDate; - @DateTimeFormat(style = "S-", fallbackPatterns = { "yyyy-MM-dd", "yyyyMMdd", "yyyy.MM.dd" }) + @DateTimeFormat(style = "S-", fallbackPatterns = {"yyyy-MM-dd", "yyyyMMdd", "yyyy.MM.dd"}) private LocalDate styleLocalDateWithFallbackPatterns; - @DateTimeFormat(pattern = "yyyy-MM-dd", fallbackPatterns = { "M/d/yy", "yyyyMMdd", "yyyy.MM.dd" }) + @DateTimeFormat(pattern = "yyyy-MM-dd", fallbackPatterns = {"M/d/yy", "yyyyMMdd", "yyyy.MM.dd"}) private LocalDate patternLocalDateWithFallbackPatterns; private LocalTime localTime; @@ -576,7 +575,7 @@ class DateTimeFormattingTests { @DateTimeFormat(style = "-M") private LocalTime styleLocalTime; - @DateTimeFormat(style = "-M", fallbackPatterns = { "HH:mm:ss", "HH:mm"}) + @DateTimeFormat(style = "-M", fallbackPatterns = {"HH:mm:ss", "HH:mm"}) private LocalTime styleLocalTimeWithFallbackPatterns; private LocalDateTime localDateTime; @@ -596,7 +595,7 @@ class DateTimeFormattingTests { @DateTimeFormat(iso = ISO.DATE_TIME) private LocalDateTime isoLocalDateTime; - @DateTimeFormat(iso = ISO.DATE_TIME, fallbackPatterns = { "yyyy-MM-dd HH:mm:ss", "M/d/yy HH:mm"}) + @DateTimeFormat(iso = ISO.DATE_TIME, fallbackPatterns = {"yyyy-MM-dd HH:mm:ss", "M/d/yy HH:mm"}) private LocalDateTime isoLocalDateTimeWithFallbackPatterns; private Instant instant; @@ -615,7 +614,6 @@ class DateTimeFormattingTests { private final List children = new ArrayList<>(); - public LocalDate getLocalDate() { return this.localDate; } diff --git a/spring-context/src/test/java/org/springframework/format/datetime/standard/InstantFormatterTests.java b/spring-context/src/test/java/org/springframework/format/datetime/standard/InstantFormatterTests.java index 16ba2cbd5e5..bd4058fdcae 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/standard/InstantFormatterTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/standard/InstantFormatterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.format.datetime.standard; import java.text.ParseException; import java.time.Instant; import java.time.format.DateTimeFormatter; +import java.util.Locale; import java.util.Random; import java.util.stream.Stream; @@ -49,13 +50,12 @@ class InstantFormatterTests { private final InstantFormatter instantFormatter = new InstantFormatter(); + @ParameterizedTest @ArgumentsSource(ISOSerializedInstantProvider.class) void should_parse_an_ISO_formatted_string_representation_of_an_Instant(String input) throws ParseException { Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from); - - Instant actual = instantFormatter.parse(input, null); - + Instant actual = instantFormatter.parse(input, Locale.US); assertThat(actual).isEqualTo(expected); } @@ -63,9 +63,7 @@ class InstantFormatterTests { @ArgumentsSource(RFC1123SerializedInstantProvider.class) void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant(String input) throws ParseException { Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from); - - Instant actual = instantFormatter.parse(input, null); - + Instant actual = instantFormatter.parse(input, Locale.US); assertThat(actual).isEqualTo(expected); } @@ -73,12 +71,11 @@ class InstantFormatterTests { @ArgumentsSource(RandomInstantProvider.class) void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale(Instant input) { String expected = DateTimeFormatter.ISO_INSTANT.format(input); - - String actual = instantFormatter.print(input, null); - + String actual = instantFormatter.print(input, Locale.US); assertThat(actual).isEqualTo(expected); } + private static class RandomInstantProvider implements ArgumentsProvider { private static final long DATA_SET_SIZE = 10; @@ -100,6 +97,7 @@ class InstantFormatterTests { } } + private static class ISOSerializedInstantProvider extends RandomInstantProvider { @Override @@ -108,6 +106,7 @@ class InstantFormatterTests { } } + private static class RFC1123SerializedInstantProvider extends RandomInstantProvider { // RFC-1123 supports only 4-digit years