From db052b3b7b82c5024e094e197039a0547495ce4a Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Thu, 16 Nov 2023 17:18:37 +0100 Subject: [PATCH] Fix build on Java 21 Ignore test parsing "12:00 am" on JDK21 due to https://bugs.openjdk.org/browse/JDK-8304925 Use always the fixed locale (instead of the platform default) for Variant date formats This fixes #3213 --- .../com/adobe/acs/commons/data/Variant.java | 21 +++++++++-------- .../adobe/acs/commons/data/VariantTest.java | 23 +++++++++++++------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/bundle/src/main/java/com/adobe/acs/commons/data/Variant.java b/bundle/src/main/java/com/adobe/acs/commons/data/Variant.java index 5cd582ab8a..4247c31cc8 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/data/Variant.java +++ b/bundle/src/main/java/com/adobe/acs/commons/data/Variant.java @@ -45,7 +45,8 @@ public final class Variant { private Class baseType = null; - private static final FastDateFormat STANDARD_DATE_FORMAT = FastDateFormat.getDateTimeInstance(FastDateFormat.SHORT, FastDateFormat.SHORT); + private static final Locale STANDARD_LOCALE = Locale.ROOT; + private static final FastDateFormat STANDARD_DATE_FORMAT = FastDateFormat.getDateTimeInstance(FastDateFormat.SHORT, FastDateFormat.SHORT, STANDARD_LOCALE); private Optional longVal = Optional.empty(); private Optional doubleVal = Optional.empty(); private Optional stringVal = Optional.empty(); @@ -53,16 +54,16 @@ public final class Variant { private Optional dateVal = Optional.empty(); private static final FastDateFormat[] DATE_FORMATS = { - FastDateFormat.getDateInstance(FastDateFormat.SHORT), - FastDateFormat.getDateInstance(FastDateFormat.LONG), - FastDateFormat.getTimeInstance(FastDateFormat.SHORT), - FastDateFormat.getTimeInstance(FastDateFormat.LONG), + FastDateFormat.getDateInstance(FastDateFormat.SHORT, STANDARD_LOCALE), + FastDateFormat.getDateInstance(FastDateFormat.LONG, STANDARD_LOCALE), + FastDateFormat.getTimeInstance(FastDateFormat.SHORT, STANDARD_LOCALE), + FastDateFormat.getTimeInstance(FastDateFormat.LONG, STANDARD_LOCALE), STANDARD_DATE_FORMAT, - FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.SHORT), - FastDateFormat.getDateTimeInstance(FastDateFormat.SHORT, FastDateFormat.LONG), - FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.LONG), - FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL), - FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSXXX") + FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.SHORT, STANDARD_LOCALE), + FastDateFormat.getDateTimeInstance(FastDateFormat.SHORT, FastDateFormat.LONG, STANDARD_LOCALE), + FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.LONG, STANDARD_LOCALE), + FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, STANDARD_LOCALE), + FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", STANDARD_LOCALE) }; public Variant() { diff --git a/bundle/src/test/java/com/adobe/acs/commons/data/VariantTest.java b/bundle/src/test/java/com/adobe/acs/commons/data/VariantTest.java index 45436a8421..6357ed9336 100644 --- a/bundle/src/test/java/com/adobe/acs/commons/data/VariantTest.java +++ b/bundle/src/test/java/com/adobe/acs/commons/data/VariantTest.java @@ -17,14 +17,23 @@ */ package com.adobe.acs.commons.data; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + import java.text.SimpleDateFormat; import java.time.Instant; import java.util.Calendar; import java.util.Date; import java.util.Locale; + +import org.apache.commons.lang3.JavaVersion; +import org.apache.commons.lang3.SystemUtils; import org.junit.Test; -import static org.junit.Assert.*; public class VariantTest { @@ -68,14 +77,14 @@ public void calendarConversion() { assertEquals(nowInstant, Variant.convert(now, Instant.class)); assertEquals(nowInstant, Variant.convert(nowDate, Instant.class)); - // Locale.getDefault() and Locale.getDefault(Locale.Category.FORMAT) may return different values in certain OS settings. - // Variant uses the former, SimpleDateFormat uses the latter by default. - // To make things consistent, pass Locale.getDefault() to SimpleDateFormat explicitly. - String nowStringShort = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG, Locale.getDefault()).format(nowDate); - String nowStringLong = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG, Locale.getDefault()).format(nowDate); + // Variant uses Locale.ROOT + String nowStringShort = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG, Locale.ROOT).format(nowDate); + String nowStringLong = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.LONG, SimpleDateFormat.LONG, Locale.ROOT).format(nowDate); assertNotNull(Variant.convert(nowStringLong, Date.class).getTime()); assertNotNull(Variant.convert(nowStringShort, Date.class).getTime()); - assertNotNull(Variant.convert("12:00 AM", Date.class).getTime()); + // Due to https://bugs.openjdk.org/browse/JDK-8304925 the following assertion does not work on JDK20 or newer + assumeTrue(SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_17)); + assertNotNull(Variant.convert("11:00 AM", Date.class).getTime()); } @Test