• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *      http://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.time;
19 
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.Calendar;
23 import java.util.GregorianCalendar;
24 import java.util.Locale;
25 import java.util.TimeZone;
26 
27 import org.apache.commons.lang3.AbstractLangTest;
28 import org.apache.commons.lang3.function.TriFunction;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.params.ParameterizedTest;
31 import org.junit.jupiter.params.provider.MethodSource;
32 
33 /**
34  * These tests fail on Java 15 due to a bug which was only fixed for Java 16.
35  * <ul>
36  * <li>https://bugs.openjdk.java.net/browse/JDK-8248434</li>
37  * <li>https://bugs.openjdk.java.net/browse/JDK-8248655</li>
38  * </ul>
39  */
40 public class Java15BugFastDateParserTest extends AbstractLangTest {
41 
42     /** @see org.apache.commons.lang3.time.FastDateParserTest#dateParserParameters() */
43     private static final String DATE_PARSER_PARAMETERS = "org.apache.commons.lang3.time.FastDateParserTest#dateParserParameters()";
44 
45     @Test
java15BuggyLocaleTest()46     public void java15BuggyLocaleTest() throws ParseException {
47         final String buggyLocaleName = "ff_LR_#Adlm";
48         Locale buggyLocale = null;
49         for (final Locale locale : Locale.getAvailableLocales()) {
50             if (buggyLocaleName.equals(locale.toString())) {
51                 buggyLocale = locale;
52                 break;
53             }
54         }
55         if (buggyLocale == null) {
56             return;
57         }
58         testSingleLocale(buggyLocale);
59     }
60 
61     @Test
java15BuggyLocaleTestAll()62     public void java15BuggyLocaleTestAll() throws ParseException {
63         for (final Locale locale : Locale.getAvailableLocales()) {
64             testSingleLocale(locale);
65         }
66     }
67 
testLocales(final TriFunction<String, TimeZone, Locale, DateParser> dbProvider, final String format, final boolean eraBC)68     private void testLocales(final TriFunction<String, TimeZone, Locale, DateParser> dbProvider, final String format,
69         final boolean eraBC) throws Exception {
70 
71         final Calendar cal = Calendar.getInstance(TimeZones.GMT);
72         cal.clear();
73         cal.set(2003, Calendar.FEBRUARY, 10);
74         if (eraBC) {
75             cal.set(Calendar.ERA, GregorianCalendar.BC);
76         }
77 
78         for (final Locale locale : Locale.getAvailableLocales()) {
79             // ja_JP_JP cannot handle dates before 1868 properly
80             if (eraBC && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
81                 continue;
82             }
83             final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
84             final DateParser fdf = dbProvider.apply(format, TimeZone.getDefault(), locale);
85 
86             // If parsing fails, a ParseException will be thrown and the test will fail
87             FastDateParserTest.checkParse(locale, cal, sdf, fdf);
88         }
89     }
90 
91     @ParameterizedTest
92     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_Long_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)93     public void testLocales_Long_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
94         throws Exception {
95         testLocales(dpProvider, FastDateParserTest.LONG_FORMAT, false);
96     }
97 
98     @ParameterizedTest
99     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_Long_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)100     public void testLocales_Long_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
101         throws Exception {
102         testLocales(dpProvider, FastDateParserTest.LONG_FORMAT, true);
103     }
104 
105     @ParameterizedTest
106     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_LongNoEra_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)107     public void testLocales_LongNoEra_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
108         throws Exception {
109         testLocales(dpProvider, FastDateParserTest.LONG_FORMAT_NOERA, false);
110     }
111 
112     @ParameterizedTest
113     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_LongNoEra_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)114     public void testLocales_LongNoEra_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
115         throws Exception {
116         testLocales(dpProvider, FastDateParserTest.LONG_FORMAT_NOERA, true);
117     }
118 
119     @ParameterizedTest
120     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_Short_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)121     public void testLocales_Short_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
122         throws Exception {
123         testLocales(dpProvider, FastDateParserTest.SHORT_FORMAT, false);
124     }
125 
126     @ParameterizedTest
127     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_Short_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)128     public void testLocales_Short_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
129         throws Exception {
130         testLocales(dpProvider, FastDateParserTest.SHORT_FORMAT, true);
131     }
132 
133     @ParameterizedTest
134     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_ShortNoEra_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)135     public void testLocales_ShortNoEra_AD(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
136         throws Exception {
137         testLocales(dpProvider, FastDateParserTest.SHORT_FORMAT_NOERA, false);
138     }
139 
140     @ParameterizedTest
141     @MethodSource(DATE_PARSER_PARAMETERS)
testLocales_ShortNoEra_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)142     public void testLocales_ShortNoEra_BC(final TriFunction<String, TimeZone, Locale, DateParser> dpProvider)
143         throws Exception {
144         testLocales(dpProvider, FastDateParserTest.SHORT_FORMAT_NOERA, true);
145     }
146 
testSingleLocale(final Locale locale)147     private void testSingleLocale(final Locale locale) throws ParseException {
148         final Calendar cal = Calendar.getInstance(TimeZones.GMT);
149         cal.clear();
150         cal.set(2003, Calendar.FEBRUARY, 10);
151         final SimpleDateFormat sdf = new SimpleDateFormat(FastDateParserTest.LONG_FORMAT, locale);
152         final String formattedDate = sdf.format(cal.getTime());
153         sdf.parse(formattedDate);
154         sdf.parse(formattedDate.toUpperCase(locale));
155         sdf.parse(formattedDate.toLowerCase(locale));
156     }
157 
158 }
159