Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
# Conflicts:
#	buildSrc/src/main/java/org/springframework/build/TestConventions.java
  • Loading branch information
sbrannen committed Jul 4, 2024
2 parents 6fa3407 + 89338c9 commit 8eb204b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ private void configureTests(Project project, Test test) {
test.jvmArgs(
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.util=ALL-UNNAMED",
"-Djava.locale.providers=COMPAT",
"-Xshare:off"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void createDateTimeFormatterWithFallback() {
void createDateTimeFormatterInOrderOfPropertyPriority() {
factory.setStylePattern("SS");
String value = applyLocale(factory.createDateTimeFormatter()).format(dateTime);
assertThat(value).startsWith("10/21/09");
assertThat(value).endsWith("12:10 PM");
// \p{Zs} matches any Unicode space character
assertThat(value).startsWith("10/21/09").matches(".+?12:10\\p{Zs}PM");

factory.setIso(ISO.DATE);
assertThat(applyLocale(factory.createDateTimeFormatter()).format(dateTime)).isEqualTo("2009-10-21");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand All @@ -58,6 +59,8 @@
import org.springframework.validation.FieldError;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.condition.JRE.JAVA_19;
import static org.junit.jupiter.api.condition.JRE.JAVA_20;

/**
* @author Keith Donald
Expand All @@ -68,6 +71,12 @@
*/
class DateTimeFormattingTests {

// JDK <= 19 requires a standard space before "AM/PM".
// JDK >= 20 requires a NNBSP before "AM/PM".
// \u202F is a narrow non-breaking space (NNBSP).
private static final String TIME_SEPARATOR = (Runtime.version().feature() < 20 ? " " : "\u202F");


private final FormattingConversionService conversionService = new FormattingConversionService();

private DataBinder binder;
Expand Down Expand Up @@ -210,10 +219,11 @@ void testBindLocalDateFromJavaUtilCalendar() {
@Test
void testBindLocalTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00 PM");
propertyValues.add("localTime", "12:00%sPM".formatted(TIME_SEPARATOR));
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(binder.getBindingResult().getFieldValue("localTime")).asString().matches("12:00\\p{Zs}PM");
}

@Test
Expand All @@ -222,7 +232,8 @@ void testBindLocalTimeWithISO() {
propertyValues.add("localTime", "12:00:00");
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(binder.getBindingResult().getFieldValue("localTime")).asString().matches("12:00\\p{Zs}PM");
}

@Test
Expand All @@ -231,10 +242,11 @@ void testBindLocalTimeWithSpecificStyle() {
registrar.setTimeStyle(FormatStyle.MEDIUM);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
propertyValues.add("localTime", "12:00:00%sPM".formatted(TIME_SEPARATOR));
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(binder.getBindingResult().getFieldValue("localTime")).asString().matches("12:00:00\\p{Zs}PM");
}

@Test
Expand All @@ -252,10 +264,11 @@ void testBindLocalTimeWithSpecificFormatter() {
@Test
void testBindLocalTimeAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("styleLocalTime", "12:00:00 PM");
propertyValues.add("styleLocalTime", "12:00:00%sPM".formatted(TIME_SEPARATOR));
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
assertThat(binder.getBindingResult().getFieldValue("styleLocalTime")).isEqualTo("12:00:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(binder.getBindingResult().getFieldValue("styleLocalTime")).asString().matches("12:00:00\\p{Zs}PM");
}

@Test
Expand All @@ -264,7 +277,8 @@ void testBindLocalTimeFromJavaUtilCalendar() {
propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0));
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(binder.getBindingResult().getFieldValue("localTime")).asString().matches("12:00\\p{Zs}PM");
}

@Test
Expand All @@ -274,7 +288,8 @@ void testBindLocalDateTime() {
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertThat(value).startsWith("10/31/09").endsWith("12:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(value).startsWith("10/31/09").matches(".+?12:00\\p{Zs}PM");
}

@Test
Expand All @@ -284,7 +299,8 @@ void testBindLocalDateTimeWithISO() {
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertThat(value).startsWith("10/31/09").endsWith("12:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(value).startsWith("10/31/09").matches(".+?12:00\\p{Zs}PM");
}

@Test
Expand All @@ -294,7 +310,8 @@ void testBindLocalDateTimeAnnotated() {
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
String value = binder.getBindingResult().getFieldValue("styleLocalDateTime").toString();
assertThat(value).startsWith("Oct 31, 2009").endsWith("12:00:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(value).startsWith("Oct 31, 2009").matches(".+?12:00:00\\p{Zs}PM");
}

@Test
Expand All @@ -304,7 +321,8 @@ void testBindLocalDateTimeFromJavaUtilCalendar() {
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertThat(value).startsWith("10/31/09").endsWith("12:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(value).startsWith("10/31/09").matches(".+?12:00\\p{Zs}PM");
}

@Test
Expand All @@ -317,7 +335,8 @@ void testBindDateTimeWithSpecificStyle() {
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isZero();
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
assertThat(value).startsWith("Oct 31, 2009").endsWith("12:00:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(value).startsWith("Oct 31, 2009").matches(".+?12:00:00\\p{Zs}PM");
}

@Test
Expand Down Expand Up @@ -558,18 +577,32 @@ void patternLocalDate(String propertyValue) {
assertThat(bindingResult.getFieldValue(propertyName)).isEqualTo("2021-03-02");
}

@EnabledForJreRange(max = JAVA_19)
@ParameterizedTest(name = "input date: {0}")
// @ValueSource(strings = {"12:00:00\u202FPM", "12:00:00", "12:00"})
// JDK <= 19 requires a standard space before the "PM".
@ValueSource(strings = {"12:00:00 PM", "12:00:00", "12:00"})
void styleLocalTime(String propertyValue) {
void styleLocalTime_PreJDK20(String propertyValue) {
styleLocalTime(propertyValue);
}

@EnabledForJreRange(min = JAVA_20)
@ParameterizedTest(name = "input date: {0}")
// JDK >= 20 requires a NNBSP before the "PM".
// \u202F is a narrow non-breaking space (NNBSP).
@ValueSource(strings = {"12:00:00\u202FPM", "12:00:00", "12:00"})
void styleLocalTime_PostJDK20(String propertyValue) {
styleLocalTime(propertyValue);
}

private void styleLocalTime(String propertyValue) {
String propertyName = "styleLocalTimeWithFallbackPatterns";
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add(propertyName, propertyValue);
binder.bind(propertyValues);
BindingResult bindingResult = binder.getBindingResult();
assertThat(bindingResult.getErrorCount()).isZero();
// assertThat(bindingResult.getFieldValue(propertyName)).asString().matches("12:00:00\\SPM");
assertThat(bindingResult.getFieldValue(propertyName)).isEqualTo("12:00:00 PM");
// \p{Zs} matches any Unicode space character
assertThat(bindingResult.getFieldValue(propertyName)).asString().matches("12:00:00\\p{Zs}PM");
}

@ParameterizedTest(name = "input date: {0}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ void customizeDateFormatNone() {
cal.set(Calendar.DATE, 1);
Date date = cal.getTime();
bean.setDate(date);
// \p{Zs} matches any Unicode space character
assertThat(gson.toJson(bean))
.startsWith("{\"date\":\"Jan 1, 2014")
.endsWith("12:00:00 AM\"}");
.matches(".+?12:00:00\\p{Zs}AM\"}");
}

@Test
Expand Down

0 comments on commit 8eb204b

Please sign in to comment.