Skip to content

Commit

Permalink
Fix build on Java 21 (#3214)
Browse files Browse the repository at this point in the history
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

Co-authored-by: david g <[email protected]>
  • Loading branch information
kwin and davidjgonzalez authored Jan 17, 2024
1 parent 7b542e0 commit 84e4a5f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
21 changes: 11 additions & 10 deletions bundle/src/main/java/com/adobe/acs/commons/data/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,25 @@
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<Long> longVal = Optional.empty();
private Optional<Double> doubleVal = Optional.empty();
private Optional<String> stringVal = Optional.empty();
private Optional<Boolean> booleanVal = Optional.empty();
private Optional<Date> 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() {
Expand Down
23 changes: 16 additions & 7 deletions bundle/src/test/java/com/adobe/acs/commons/data/VariantTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 84e4a5f

Please sign in to comment.