diff options
Diffstat (limited to 'src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java')
-rw-r--r-- | src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java | 174 |
1 files changed, 86 insertions, 88 deletions
diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java index f4e61801a5..b7fd43e367 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestExcelStyleDateFormatter.java @@ -18,124 +18,122 @@ package org.apache.poi.ss.usermodel; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.text.DateFormatSymbols; import java.text.FieldPosition; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.poi.util.LocaleUtil; import org.junit.Test; public class TestExcelStyleDateFormatter { private static final String EXCEL_DATE_FORMAT = "MMMMM"; + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT); + private final int jreVersion; + + public TestExcelStyleDateFormatter() { + jreVersion = Integer.parseInt(System.getProperty("java.version") + .replace("1.8", "8").replaceAll("(\\d+).*", "$1")); + } /** * [Bug 60369] Month format 'MMMMM' issue with TEXT-formula and Java 8 */ @Test - public void test60369() throws ParseException { - Map<Locale, List<String>> testMap = initializeLocales(); + public void test60369() { + Map<Locale, String> testMap = initializeLocales(); // We have to set up dates as well. - SimpleDateFormat testDateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT); - List<Date> testDates = Arrays.asList( - testDateFormat.parse("12.01.1980"), - testDateFormat.parse("11.02.1995"), - testDateFormat.parse("10.03.2045"), - testDateFormat.parse("09.04.2016"), - testDateFormat.parse("08.05.2017"), - testDateFormat.parse("07.06.1945"), - testDateFormat.parse("06.07.1998"), - testDateFormat.parse("05.08.2099"), - testDateFormat.parse("04.09.1988"), - testDateFormat.parse("03.10.2023"), - testDateFormat.parse("02.11.1978"), - testDateFormat.parse("01.12.1890")); + List<Date> testDates = Stream.of("1980-01-12", "1995-02-11", "2045-03-10", "2016-04-09", "2017-05-08", + "1945-06-07", "1998-07-06", "2099-08-05", "1988-09-04", "2023-10-03", "1978-11-02", "1890-12-01") + .map(this::parseDate).collect(Collectors.toList()); // Let's iterate over the test setup. - for (Locale locale : testMap.keySet()) { - ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT, new DateFormatSymbols(locale)); - for (int i = 0; i < testDates.size(); i++) { - // Call the method to be tested! - String result = - formatter.format(testDates.get(i), - new StringBuffer(), - new FieldPosition(java.text.DateFormat.MONTH_FIELD)).toString(); - //System.err.println(result + " - " + getUnicode(result.charAt(0))); - assertEquals("Failed for locale " + locale + ", provider: " + System.getProperty("java.locale.providers") + - " and date " + testDates.get(i) + ", having: " + result, - getUnicode(testMap.get(locale).get(i).charAt(0)), getUnicode(result.charAt(0))); + final String provider = System.getProperty("java.locale.providers"); + final FieldPosition fp = new FieldPosition(java.text.DateFormat.MONTH_FIELD); + final ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT); + final StringBuffer sb = new StringBuffer(); + + for (Map.Entry<Locale,String> me : testMap.entrySet()) { + final Locale locale = me.getKey(); + final String expected = me.getValue(); + formatter.setDateFormatSymbols(DateFormatSymbols.getInstance(locale)); + int month = 0; + for (Date d : testDates) { + sb.setLength(0); + String result = formatter.format(d, sb, fp).toString(); + String msg = "Failed testDates for locale " + locale + ", provider: " + provider + + " and date " + d + ", having: " + result; + + int actIdx = localeIndex(locale); + + assertNotNull(msg, result); + assertTrue(msg, result.length() > actIdx); + assertEquals(msg, expected.charAt(month), result.charAt(actIdx)); + month++; } } } - private Map<Locale, List<String>> initializeLocales() { - // Setting up the locale to be tested together with a list of asserted unicode-formatted results and put them in a map. - Locale germanLocale = Locale.GERMAN; - List<String> germanResultList = Arrays.asList("\u004a", "\u0046", "\u004d", "\u0041", "\u004d", - "\u004a", "\u004a", "\u0041", "\u0053", "\u004f", "\u004e", "\u0044"); - - Locale russianLocale = new Locale("ru", "RU"); - List<String> russianResultList = Arrays.asList("\u044f", "\u0444", "\u043c", "\u0430", "\u043c", - "\u0438", "\u0438", "\u0430", "\u0441", "\u043e", "\u043d", "\u0434"); - - Locale austrianLocale = new Locale("de", "AT"); - List<String> austrianResultList = Arrays.asList("\u004a", "\u0046", "\u004d", "\u0041", "\u004d", - "\u004a", "\u004a", "\u0041", "\u0053", "\u004f", "\u004e", "\u0044"); - - Locale englishLocale = Locale.UK; - List<String> englishResultList = Arrays.asList("\u004a", "\u0046", "\u004d", "\u0041", "\u004d", - "\u004a", "\u004a", "\u0041", "\u0053", "\u004f", "\u004e", "\u0044"); - - Locale frenchLocale = Locale.FRENCH; - List<String> frenchResultList = Arrays.asList("\u006a", "\u0066", "\u006d", "\u0061", "\u006d", - "\u006a", "\u006a", "\u0061", "\u0073", "\u006f", "\u006e", "\u0064"); - - Locale chineseLocale = Locale.CHINESE; - List<String> chineseResultList = Arrays.asList("\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", - "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341", "\u5341", "\u5341"); - - Locale turkishLocale = new Locale("tr", "TR"); - List<String> turkishResultList = Arrays.asList("\u004f", "\u015e", "\u004d", "\u004e", "\u004d", - "\u0048", "\u0054", "\u0041", "\u0045", "\u0045", "\u004b", "\u0041"); - - Locale hungarianLocale = new Locale("hu", "HU"); - List<String> hungarianResultList = Arrays.asList("\u006a", "\u0066", "\u006d", "\u00e1", "\u006d", - "\u006a", "\u006a", "\u0061", "\u0073", "\u006f", "\u006e", "\u0064"); - - Locale indianLocale = new Locale("en", "IN"); - List<String> indianResultList = Arrays.asList("\u004a", "\u0046", "\u004d", "\u0041", "\u004d", - "\u004a", "\u004a", "\u0041", "\u0053", "\u004f", "\u004e", "\u0044"); - - Locale indonesianLocale = new Locale("in", "ID"); - List<String> indonesianResultList = Arrays.asList("\u004a", "\u0046", "\u004d", "\u0041", "\u004d", - "\u004a", "\u004a", "\u0041", "\u0053", "\u004f", "\u004e", "\u0044"); - - - Map<Locale, List<String>> testMap = new HashMap<>(); - testMap.put(germanLocale, germanResultList); - testMap.put(russianLocale, russianResultList); - testMap.put(austrianLocale, austrianResultList); - testMap.put(englishLocale, englishResultList); - testMap.put(frenchLocale, frenchResultList); - testMap.put(chineseLocale, chineseResultList); - testMap.put(turkishLocale, turkishResultList); - testMap.put(hungarianLocale, hungarianResultList); - testMap.put(indianLocale, indianResultList); - testMap.put(indonesianLocale, indonesianResultList); + /** + * Depending on the JRE version, the provider setting and the locale, a different result + * is expected and selected via an index + */ + private int localeIndex(Locale locale) { + final String provider = System.getProperty("java.locale.providers"); + return jreVersion < 12 || + !locale.equals (Locale.CHINESE) || + (provider != null && provider.startsWith("JRE")) + ? 0 : 1; + } - return testMap; + private Date parseDate(String dateStr) { + try { + return DATE_FORMAT.parse(dateStr); + } catch (ParseException e) { + return new Date(0); + } } - private String getUnicode(char c) { - return "\\u" + Integer.toHexString(c | 0x10000).substring(1); + /** + * Setting up the locale to be tested together with a list of asserted + * unicode-formatted results and put them in a map. + */ + private Map<Locale, String> initializeLocales() { + Map<Locale, String> testMap = new HashMap<>(); + + testMap.put(Locale.GERMAN, "JFMAMJJASOND"); + testMap.put(new Locale("de", "AT"), "JFMAMJJASOND"); + testMap.put(Locale.UK, "JFMAMJJASOND"); + testMap.put(new Locale("en", "IN"), "JFMAMJJASOND"); + testMap.put(new Locale("in", "ID"), "JFMAMJJASOND"); + testMap.put(Locale.FRENCH, "jfmamjjasond"); + + testMap.put(new Locale("ru", "RU"), + "\u044f\u0444\u043c\u0430\u043c\u0438\u0438\u0430\u0441\u043e\u043d\u0434"); + + testMap.put(Locale.CHINESE, new String[]{ + "\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341\u5341\u5341", + "123456789111" + }[localeIndex(Locale.CHINESE)]); + + testMap.put(new Locale("tr", "TR"), + "\u004f\u015e\u004d\u004e\u004d\u0048\u0054\u0041\u0045\u0045\u004b\u0041"); + + testMap.put(new Locale("hu", "HU"), + "\u006a\u0066\u006d\u00e1\u006d\u006a\u006a\u0061\u0073\u006f\u006e\u0064"); + + return testMap; } @Test @@ -150,7 +148,7 @@ public class TestExcelStyleDateFormatter { try { LocaleUtil.setUserLocale(Locale.GERMAN); String dateStr = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT).format( - new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT).parse("2016-03-26")); + DATE_FORMAT.parse("2016-03-26")); assertEquals("M", dateStr); } finally { LocaleUtil.setUserLocale(before); @@ -160,7 +158,7 @@ public class TestExcelStyleDateFormatter { @Test public void testWithPattern() throws ParseException { String dateStr = new ExcelStyleDateFormatter("yyyy|" + EXCEL_DATE_FORMAT + "|").format( - new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT).parse("2016-03-26")); + DATE_FORMAT.parse("2016-03-26")); assertEquals("2016|M|", dateStr); } } |