You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestExcelStyleDateFormatter.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertTrue;
  19. import java.text.DateFormatSymbols;
  20. import java.text.FieldPosition;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import java.util.Map;
  28. import java.util.stream.Collectors;
  29. import java.util.stream.Stream;
  30. import org.apache.poi.util.LocaleUtil;
  31. import org.junit.Test;
  32. public class TestExcelStyleDateFormatter {
  33. private static final String EXCEL_DATE_FORMAT = "MMMMM";
  34. private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
  35. private final int jreVersion;
  36. public TestExcelStyleDateFormatter() {
  37. jreVersion = Integer.parseInt(System.getProperty("java.version")
  38. .replace("1.8", "8").replaceAll("(\\d+).*", "$1"));
  39. }
  40. /**
  41. * [Bug 60369] Month format 'MMMMM' issue with TEXT-formula and Java 8
  42. */
  43. @Test
  44. public void test60369() {
  45. Map<Locale, String> testMap = initializeLocales();
  46. // We have to set up dates as well.
  47. List<Date> testDates = Stream.of("1980-01-12", "1995-02-11", "2045-03-10", "2016-04-09", "2017-05-08",
  48. "1945-06-07", "1998-07-06", "2099-08-05", "1988-09-04", "2023-10-03", "1978-11-02", "1890-12-01")
  49. .map(this::parseDate).collect(Collectors.toList());
  50. // Let's iterate over the test setup.
  51. final String provider = System.getProperty("java.locale.providers");
  52. final FieldPosition fp = new FieldPosition(java.text.DateFormat.MONTH_FIELD);
  53. final ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT);
  54. final StringBuffer sb = new StringBuffer();
  55. for (Map.Entry<Locale,String> me : testMap.entrySet()) {
  56. final Locale locale = me.getKey();
  57. final String expected = me.getValue();
  58. formatter.setDateFormatSymbols(DateFormatSymbols.getInstance(locale));
  59. int month = 0;
  60. for (Date d : testDates) {
  61. sb.setLength(0);
  62. String result = formatter.format(d, sb, fp).toString();
  63. String msg = "Failed testDates for locale " + locale + ", provider: " + provider +
  64. " and date " + d + ", having: " + result;
  65. int actIdx = (Locale.CHINESE.equals(locale) && jreVersion >= 12) ? 1 : 0;
  66. assertNotNull(msg, result);
  67. assertTrue(msg, result.length() > actIdx);
  68. assertEquals(msg, expected.charAt(month), result.charAt(actIdx));
  69. month++;
  70. }
  71. }
  72. }
  73. private Date parseDate(String dateStr) {
  74. try {
  75. return DATE_FORMAT.parse(dateStr);
  76. } catch (ParseException e) {
  77. return new Date(0);
  78. }
  79. }
  80. /**
  81. * Setting up the locale to be tested together with a list of asserted
  82. * unicode-formatted results and put them in a map.
  83. */
  84. private Map<Locale, String> initializeLocales() {
  85. Map<Locale, String> testMap = new HashMap<>();
  86. testMap.put(Locale.GERMAN, "JFMAMJJASOND");
  87. testMap.put(new Locale("de", "AT"), "JFMAMJJASOND");
  88. testMap.put(Locale.UK, "JFMAMJJASOND");
  89. testMap.put(new Locale("en", "IN"), "JFMAMJJASOND");
  90. testMap.put(new Locale("in", "ID"), "JFMAMJJASOND");
  91. testMap.put(Locale.FRENCH, "jfmamjjasond");
  92. testMap.put(new Locale("ru", "RU"),
  93. "\u044f\u0444\u043c\u0430\u043c\u0438\u0438\u0430\u0441\u043e\u043d\u0434");
  94. testMap.put(Locale.CHINESE, jreVersion < 12
  95. ? "\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341\u5341\u5341"
  96. : "123456789111");
  97. testMap.put(new Locale("tr", "TR"),
  98. "\u004f\u015e\u004d\u004e\u004d\u0048\u0054\u0041\u0045\u0045\u004b\u0041");
  99. testMap.put(new Locale("hu", "HU"),
  100. "\u006a\u0066\u006d\u00e1\u006d\u006a\u006a\u0061\u0073\u006f\u006e\u0064");
  101. return testMap;
  102. }
  103. @Test
  104. public void testConstruct() {
  105. new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT, LocaleUtil.getUserLocale());
  106. new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT);
  107. }
  108. @Test
  109. public void testWithLocale() throws ParseException {
  110. Locale before = LocaleUtil.getUserLocale();
  111. try {
  112. LocaleUtil.setUserLocale(Locale.GERMAN);
  113. String dateStr = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT).format(
  114. DATE_FORMAT.parse("2016-03-26"));
  115. assertEquals("M", dateStr);
  116. } finally {
  117. LocaleUtil.setUserLocale(before);
  118. }
  119. }
  120. @Test
  121. public void testWithPattern() throws ParseException {
  122. String dateStr = new ExcelStyleDateFormatter("yyyy|" + EXCEL_DATE_FORMAT + "|").format(
  123. DATE_FORMAT.parse("2016-03-26"));
  124. assertEquals("2016|M|", dateStr);
  125. }
  126. }