Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build on Java 21 #3214

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Check warning on line 49 in bundle/src/main/java/com/adobe/acs/commons/data/Variant.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/data/Variant.java#L48-L49

Added lines #L48 - L49 were not covered by tests
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),

Check warning on line 60 in bundle/src/main/java/com/adobe/acs/commons/data/Variant.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/data/Variant.java#L57-L60

Added lines #L57 - L60 were not covered by tests
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)

Check warning on line 66 in bundle/src/main/java/com/adobe/acs/commons/data/Variant.java

View check run for this annotation

Codecov / codecov/patch

bundle/src/main/java/com/adobe/acs/commons/data/Variant.java#L62-L66

Added lines #L62 - L66 were not covered by tests
};

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
Loading