Skip to content

Commit 1c05980

Browse files
committed
Make test Java15BugFastDateParserTest more lenient on Java 26-ea
1 parent fd154de commit 1c05980

File tree

4 files changed

+98
-13
lines changed

4 files changed

+98
-13
lines changed

src/main/java/org/apache/commons/lang3/SystemUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public class SystemUtils {
383383
/**
384384
* A constant for the Java version, may be null.
385385
*/
386-
private static final JavaVersion JAVA_SPECIFICATION_VERSION_ENUM = JavaVersion.get(JAVA_SPECIFICATION_VERSION);
386+
static final JavaVersion JAVA_SPECIFICATION_VERSION_ENUM = JavaVersion.get(JAVA_SPECIFICATION_VERSION);
387387

388388
/**
389389
* A constant for the System Property {@code java.util.prefs.PreferencesFactory}. A class name.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3;
19+
20+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
21+
22+
import java.text.ParseException;
23+
import java.util.Arrays;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Locale;
27+
import java.util.Map;
28+
29+
/**
30+
* Offers tests a way to skip problematic Locales with a JUnit assumption.
31+
*/
32+
public class LocaleProblems {
33+
34+
// needs a better name.
35+
private static final Map<JavaVersion, List<String>> UNSUPPORTED_CAT_A;
36+
static {
37+
UNSUPPORTED_CAT_A = new HashMap<>();
38+
// "cv_RU" is a problem on Java version: 26-beta, vendor: Eclipse Adoptium, runtime: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/26.0.0-ea.27.0.ea/x64
39+
// Same for "cv"
40+
UNSUPPORTED_CAT_A.put(JavaVersion.JAVA_26, Arrays.asList("cv", "cv_RU"));
41+
}
42+
43+
// needs a better name.
44+
private static final Map<JavaVersion, List<String>> UNSUPPORTED_CAT_B;
45+
static {
46+
UNSUPPORTED_CAT_B = new HashMap<>();
47+
// "cv_RU_#Cyrl" is a problem on Java version: 26-beta, vendor: Eclipse Adoptium, runtime: /opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/26.0.0-ea.27.0.ea/x64
48+
UNSUPPORTED_CAT_B.put(JavaVersion.JAVA_26, Arrays.asList("cv", "cv_RU", "cv_RU_#Cyrl"));
49+
}
50+
51+
private static void assumeLocaleSupported(final Locale locale, final Map<JavaVersion, List<String>> unsupportedCatA, final ParseException e) {
52+
final boolean supported = isSupported(locale, unsupportedCatA);
53+
if (!supported) {
54+
System.out.printf("Failing test assumption for locale '%s' on Java version %s.%n", locale, SystemUtils.JAVA_SPECIFICATION_VERSION_ENUM);
55+
if (e != null) {
56+
// Not the whole stack trace, just the exception message.
57+
System.out.printf("\t%s%n", e);
58+
}
59+
}
60+
assumeTrue(supported);
61+
}
62+
63+
public static void assumeLocaleSupportedA(final Locale locale) {
64+
assumeLocaleSupported(locale, UNSUPPORTED_CAT_A, null);
65+
}
66+
67+
public static void assumeLocaleSupportedB(final Locale locale, final ParseException e) {
68+
assumeLocaleSupported(locale, UNSUPPORTED_CAT_B, e);
69+
}
70+
71+
private static boolean isSupported(final Locale locale, final Map<JavaVersion, List<String>> unsupported) {
72+
final List<String> list = unsupported.get(SystemUtils.JAVA_SPECIFICATION_VERSION_ENUM);
73+
return list == null || !list.contains(locale.toString());
74+
}
75+
}

src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.stream.Stream;
4040

4141
import org.apache.commons.lang3.AbstractLangTest;
42+
import org.apache.commons.lang3.LocaleProblems;
4243
import org.apache.commons.lang3.LocaleUtils;
4344
import org.apache.commons.lang3.SerializationUtils;
4445
import org.apache.commons.lang3.SystemUtils;
@@ -55,6 +56,7 @@
5556
import org.junitpioneer.jupiter.ReadsDefaultTimeZone;
5657
import org.junitpioneer.jupiter.cartesian.ArgumentSets;
5758
import org.junitpioneer.jupiter.cartesian.CartesianTest;
59+
import org.opentest4j.AssertionFailedError;
5860

5961
/**
6062
* Tests {@link org.apache.commons.lang3.time.FastDateParser}.
@@ -724,7 +726,7 @@ void testTzParses(final Locale locale) throws Exception {
724726
}
725727

726728
private void validateSdfFormatFdpParseEquality(final String formatStr, final Locale locale, final TimeZone timeZone,
727-
final FastDateParser dateParser, final Date inDate, final int year, final Date csDate) throws ParseException {
729+
final FastDateParser fastDateParser, final Date inDate, final int year, final Date csDate) throws ParseException {
728730
final SimpleDateFormat sdf = new SimpleDateFormat(formatStr, locale);
729731
sdf.setTimeZone(timeZone);
730732
if (formatStr.equals(SHORT_FORMAT)) {
@@ -734,13 +736,14 @@ private void validateSdfFormatFdpParseEquality(final String formatStr, final Loc
734736
// System.out.printf("[Java %s] Date: '%s' formatted with '%s' -> '%s'%n", SystemUtils.JAVA_RUNTIME_VERSION, inDate,
735737
// formatStr, fmt);
736738
try {
737-
final Date out = dateParser.parse(fmt);
739+
final Date out = fastDateParser.parse(fmt);
738740
assertEquals(inDate, out, "format: '" + formatStr + "', locale: '" + locale + "', time zone: '"
739741
+ timeZone.getID() + "', year: " + year + ", parse: '" + fmt);
740-
} catch (final ParseException pe) {
742+
} catch (final ParseException e) {
743+
LocaleProblems.assumeLocaleSupportedB(locale, e);
741744
if (year >= 1868 || !locale.getCountry().equals("JP")) {
742745
// LANG-978
743-
throw pe;
746+
throw new AssertionFailedError("locale " + locale, e);
744747
}
745748
}
746749
}

src/test/java/org/apache/commons/lang3/time/Java15BugFastDateParserTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.TimeZone;
2626

2727
import org.apache.commons.lang3.AbstractLangTest;
28+
import org.apache.commons.lang3.LocaleProblems;
2829
import org.apache.commons.lang3.LocaleUtils;
2930
import org.apache.commons.lang3.function.TriFunction;
3031
import org.junit.jupiter.api.Test;
@@ -65,6 +66,19 @@ void testJava15BuggyLocaleTestAll(final Locale locale) throws ParseException {
6566
testSingleLocale(locale);
6667
}
6768

69+
private void testLocale(final TriFunction<String, TimeZone, Locale, DateParser> dbProvider, final String format, final boolean eraBC, final Calendar cal,
70+
final Locale locale) {
71+
LocaleProblems.assumeLocaleSupportedA(locale);
72+
// ja_JP_JP cannot handle dates before 1868 properly
73+
if (eraBC && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
74+
return;
75+
}
76+
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
77+
final DateParser fdf = dbProvider.apply(format, TimeZone.getDefault(), locale);
78+
// If parsing fails, a ParseException will be thrown and the test will fail
79+
FastDateParserTest.checkParse(locale, cal, sdf, fdf);
80+
}
81+
6882
private void testLocales(final TriFunction<String, TimeZone, Locale, DateParser> dbProvider, final String format,
6983
final boolean eraBC) throws Exception {
7084
final Calendar cal = Calendar.getInstance(TimeZones.GMT);
@@ -74,14 +88,7 @@ private void testLocales(final TriFunction<String, TimeZone, Locale, DateParser>
7488
cal.set(Calendar.ERA, GregorianCalendar.BC);
7589
}
7690
for (final Locale locale : LocaleUtils.availableLocaleList()) {
77-
// ja_JP_JP cannot handle dates before 1868 properly
78-
if (eraBC && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
79-
continue;
80-
}
81-
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
82-
final DateParser fdf = dbProvider.apply(format, TimeZone.getDefault(), locale);
83-
// If parsing fails, a ParseException will be thrown and the test will fail
84-
FastDateParserTest.checkParse(locale, cal, sdf, fdf);
91+
testLocale(dbProvider, format, eraBC, cal, locale);
8592
}
8693
}
8794

0 commit comments

Comments
 (0)