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.

TestLocaleUtil.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.util;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  18. import static org.junit.jupiter.api.Assumptions.assumeFalse;
  19. import java.util.Calendar;
  20. import java.util.Locale;
  21. import java.util.TimeZone;
  22. import org.junit.jupiter.api.AfterEach;
  23. import org.junit.jupiter.api.BeforeEach;
  24. import org.junit.jupiter.api.Test;
  25. class TestLocaleUtil {
  26. // This unit test assumes that the user's locale isn't ja-JP and timezone isn't Asia/Tokyo
  27. // If this is the case, change the values to something else
  28. private static final Locale ja_JP = Locale.JAPAN;
  29. private static final TimeZone TOKYO = TimeZone.getTimeZone("Asia/Tokyo");
  30. private static final Calendar JAPAN_CALENDAR = Calendar.getInstance(TOKYO, ja_JP);
  31. /**
  32. * Reset the Locale to the user default before the test so that it isn't influenced
  33. * by the LocaleUtil's state being changed by previous tests.
  34. */
  35. @BeforeEach
  36. @SuppressForbidden("implementation around default locales in POI")
  37. void setUp() {
  38. // reset the user locale and time zone so that tests do not interfere with each other
  39. // the other and better way would be to run each test in its own thread since
  40. // LocaleUtil uses per-thread settings.
  41. // Helpful, but not ASL 2.0 licensed:
  42. // http://www.codeaffine.com/2014/07/21/a-junit-rule-to-run-a-test-in-its-own-thread/
  43. LocaleUtil.setUserLocale(Locale.getDefault());
  44. LocaleUtil.setUserTimeZone(TimeZone.getDefault());
  45. assumeFalse(ja_JP.equals(LocaleUtil.getUserLocale()));
  46. assumeFalse(TOKYO.equals(LocaleUtil.getUserTimeZone()));
  47. }
  48. /**
  49. * Reset the Locale to the user default after the test so that it doesn't influence
  50. * other tests.
  51. */
  52. @AfterEach
  53. void tearDown() {
  54. LocaleUtil.resetUserLocale();
  55. LocaleUtil.resetUserTimeZone();
  56. }
  57. @Test
  58. @SuppressForbidden("implementation around default locales in POI")
  59. void userLocale() {
  60. Locale DEFAULT_LOCALE = LocaleUtil.getUserLocale();
  61. assertEquals(DEFAULT_LOCALE, LocaleUtil.getUserLocale());
  62. assertNotEquals(ja_JP, LocaleUtil.getUserLocale());
  63. LocaleUtil.setUserLocale(ja_JP);
  64. assertEquals(ja_JP, LocaleUtil.getUserLocale());
  65. LocaleUtil.resetUserLocale();
  66. assertEquals(DEFAULT_LOCALE, LocaleUtil.getUserLocale());
  67. }
  68. @Test
  69. @SuppressForbidden("implementation around default locales in POI")
  70. void userTimeZone() {
  71. TimeZone DEFAULT_TIME_ZONE = LocaleUtil.getUserTimeZone();
  72. assertEquals(DEFAULT_TIME_ZONE, LocaleUtil.getUserTimeZone());
  73. assertNotEquals(TOKYO, LocaleUtil.getUserTimeZone());
  74. LocaleUtil.setUserTimeZone(TOKYO);
  75. assertEquals(TOKYO, LocaleUtil.getUserTimeZone());
  76. }
  77. @Test
  78. @SuppressForbidden("implementation around default locales in POI")
  79. void localeCalendar() {
  80. Locale DEFAULT_LOCALE = LocaleUtil.getUserLocale();
  81. TimeZone DEFAULT_TIME_ZONE = LocaleUtil.getUserTimeZone();
  82. Calendar DEFAULT_CALENDAR = LocaleUtil.getLocaleCalendar();
  83. assertEquals(DEFAULT_LOCALE, LocaleUtil.getUserLocale());
  84. assertEquals(DEFAULT_TIME_ZONE, LocaleUtil.getUserTimeZone());
  85. assertCalendarEquals(DEFAULT_CALENDAR, LocaleUtil.getLocaleCalendar());
  86. assertNotEquals(ja_JP, LocaleUtil.getUserLocale());
  87. assertNotEquals(TOKYO, LocaleUtil.getUserTimeZone());
  88. assertCalendarNotEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar());
  89. LocaleUtil.setUserLocale(ja_JP);
  90. LocaleUtil.setUserTimeZone(TOKYO);
  91. assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar());
  92. assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(TOKYO));
  93. // FIXME: These might affect the time zone due to daylight savings:
  94. //assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(2016, 00, 01));
  95. //assertCalendarEquals(JAPAN_CALENDAR, LocaleUtil.getLocaleCalendar(2016, 00, 01, 00, 00, 00));
  96. }
  97. private static void assertCalendarNotEquals(Calendar expected, Calendar actual) {
  98. // FIXME: add more tests to compare calendars, ignoring whether the dates are equal
  99. assertNotEquals(expected.getTimeZone(), actual.getTimeZone(), "time zone");
  100. }
  101. private static void assertCalendarEquals(Calendar expected, Calendar actual) {
  102. // FIXME: add more tests to compare calendars, ignoring whether the set dates are equal
  103. assertEquals(expected.getTimeZone(), actual.getTimeZone(), "time zone");
  104. }
  105. }