1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.java.time.chrono; 27 28 import java.time.*; 29 import java.time.chrono.*; 30 import java.time.format.*; 31 import java.util.Arrays; 32 import java.util.Locale; 33 import java.util.stream.Stream; 34 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 import static org.testng.Assert.assertEquals; 38 39 /** 40 * Tests Era.getDisplayName() correctly returns the name based on each 41 * chrono implementation. 42 * Note: The exact result may depend on locale data provider's implementation. 43 * 44 * @bug 8171049 8224105 45 */ 46 @Test 47 public class TestEraDisplayName { 48 private static final Locale THAI = Locale.forLanguageTag("th-TH"); 49 private static final Locale EGYPT = Locale.forLanguageTag("ar-EG"); 50 51 private static final LocalDate REIWA_1ST = LocalDate.of(2019, 5, 1); 52 private static final DateTimeFormatter JAPANESE_FORMATTER = 53 DateTimeFormatter.ofPattern("yyyy MM dd GGGG G GGGGG") 54 .withChronology(JapaneseChronology.INSTANCE); 55 56 @DataProvider(name="eraDisplayName") 57 Object[][] eraDisplayName() { 58 return new Object[][] { 59 // Era, text style, displyay locale, expected name 60 // IsoEra 61 { IsoEra.BCE, TextStyle.FULL, Locale.US, "Before Christ" }, 62 { IsoEra.CE, TextStyle.FULL, Locale.US, "Anno Domini" }, 63 { IsoEra.BCE, TextStyle.FULL, Locale.JAPAN, "\u7d00\u5143\u524d" }, 64 { IsoEra.CE, TextStyle.FULL, Locale.JAPAN, "\u897f\u66a6" }, 65 { IsoEra.BCE, TextStyle.SHORT, Locale.US, "BC" }, 66 { IsoEra.CE, TextStyle.SHORT, Locale.US, "AD" }, 67 { IsoEra.BCE, TextStyle.SHORT, Locale.JAPAN, "\u7d00\u5143\u524d" }, 68 { IsoEra.CE, TextStyle.SHORT, Locale.JAPAN, "\u897f\u66a6" }, 69 { IsoEra.BCE, TextStyle.NARROW, Locale.US, "B" }, 70 { IsoEra.CE, TextStyle.NARROW, Locale.US, "A" }, 71 { IsoEra.BCE, TextStyle.NARROW, Locale.JAPAN, "BC" }, 72 { IsoEra.CE, TextStyle.NARROW, Locale.JAPAN, "AD" }, 73 74 // JapaneseEra 75 { JapaneseEra.MEIJI, TextStyle.FULL, Locale.US, "Meiji" }, 76 { JapaneseEra.TAISHO, TextStyle.FULL, Locale.US, "Taish\u014d" }, 77 { JapaneseEra.SHOWA, TextStyle.FULL, Locale.US, "Sh\u014dwa" }, 78 { JapaneseEra.HEISEI, TextStyle.FULL, Locale.US, "Heisei" }, 79 { JapaneseEra.REIWA, TextStyle.FULL, Locale.US, "Reiwa" }, 80 { JapaneseEra.MEIJI, TextStyle.FULL, Locale.JAPAN, "\u660e\u6cbb" }, 81 { JapaneseEra.TAISHO, TextStyle.FULL, Locale.JAPAN, "\u5927\u6b63" }, 82 { JapaneseEra.SHOWA, TextStyle.FULL, Locale.JAPAN, "\u662d\u548c" }, 83 { JapaneseEra.HEISEI, TextStyle.FULL, Locale.JAPAN, "\u5e73\u6210" }, 84 { JapaneseEra.REIWA, TextStyle.FULL, Locale.JAPAN, "\u4ee4\u548c" }, 85 { JapaneseEra.MEIJI, TextStyle.SHORT, Locale.US, "Meiji" }, 86 { JapaneseEra.TAISHO, TextStyle.SHORT, Locale.US, "Taish\u014d" }, 87 { JapaneseEra.SHOWA, TextStyle.SHORT, Locale.US, "Sh\u014dwa" }, 88 { JapaneseEra.HEISEI, TextStyle.SHORT, Locale.US, "Heisei" }, 89 { JapaneseEra.REIWA, TextStyle.SHORT, Locale.US, "Reiwa" }, 90 { JapaneseEra.MEIJI, TextStyle.SHORT, Locale.JAPAN, "\u660e\u6cbb" }, 91 { JapaneseEra.TAISHO, TextStyle.SHORT, Locale.JAPAN, "\u5927\u6b63" }, 92 { JapaneseEra.SHOWA, TextStyle.SHORT, Locale.JAPAN, "\u662d\u548c" }, 93 { JapaneseEra.HEISEI, TextStyle.SHORT, Locale.JAPAN, "\u5e73\u6210" }, 94 { JapaneseEra.REIWA, TextStyle.SHORT, Locale.JAPAN, "\u4ee4\u548c" }, 95 { JapaneseEra.MEIJI, TextStyle.NARROW, Locale.US, "M" }, 96 { JapaneseEra.TAISHO, TextStyle.NARROW, Locale.US, "T" }, 97 { JapaneseEra.SHOWA, TextStyle.NARROW, Locale.US, "S" }, 98 { JapaneseEra.HEISEI, TextStyle.NARROW, Locale.US, "H" }, 99 { JapaneseEra.REIWA, TextStyle.NARROW, Locale.US, "R" }, 100 { JapaneseEra.MEIJI, TextStyle.NARROW, Locale.JAPAN, "M" }, 101 { JapaneseEra.TAISHO, TextStyle.NARROW, Locale.JAPAN, "T" }, 102 { JapaneseEra.SHOWA, TextStyle.NARROW, Locale.JAPAN, "S" }, 103 { JapaneseEra.HEISEI, TextStyle.NARROW, Locale.JAPAN, "H" }, 104 { JapaneseEra.REIWA, TextStyle.NARROW, Locale.JAPAN, "R" }, 105 106 // ThaiBuddhistEra 107 { ThaiBuddhistEra.BEFORE_BE, TextStyle.FULL, Locale.US, "BC" }, 108 { ThaiBuddhistEra.BE, TextStyle.FULL, Locale.US, "BE" }, 109 { ThaiBuddhistEra.BEFORE_BE, TextStyle.FULL, THAI, "BC" }, 110 { ThaiBuddhistEra.BE, TextStyle.FULL, THAI, 111 "\u0e1e\u0e38\u0e17\u0e18\u0e28\u0e31\u0e01\u0e23\u0e32\u0e0a" }, 112 { ThaiBuddhistEra.BEFORE_BE, TextStyle.SHORT, Locale.US, "BC" }, 113 { ThaiBuddhistEra.BE, TextStyle.SHORT, Locale.US, "BE" }, 114 { ThaiBuddhistEra.BEFORE_BE, TextStyle.SHORT, THAI, "BC" }, 115 { ThaiBuddhistEra.BE, TextStyle.SHORT, THAI, "\u0e1e.\u0e28." }, 116 { ThaiBuddhistEra.BEFORE_BE, TextStyle.NARROW, Locale.US, "BC" }, 117 { ThaiBuddhistEra.BE, TextStyle.NARROW, Locale.US, "BE" }, 118 { ThaiBuddhistEra.BEFORE_BE, TextStyle.NARROW, THAI, "BC" }, 119 { ThaiBuddhistEra.BE, TextStyle.NARROW, THAI, "\u0e1e.\u0e28." }, 120 121 // MinguoEra 122 { MinguoEra.BEFORE_ROC, TextStyle.FULL, Locale.US, "Before R.O.C." }, 123 { MinguoEra.ROC, TextStyle.FULL, Locale.US, "Minguo" }, 124 { MinguoEra.BEFORE_ROC, TextStyle.FULL, Locale.TAIWAN, "\u6c11\u570b\u524d" }, 125 { MinguoEra.ROC, TextStyle.FULL, Locale.TAIWAN, "\u6c11\u570b" }, 126 { MinguoEra.BEFORE_ROC, TextStyle.SHORT, Locale.US, "Before R.O.C." }, 127 { MinguoEra.ROC, TextStyle.SHORT, Locale.US, "Minguo" }, 128 { MinguoEra.BEFORE_ROC, TextStyle.SHORT, Locale.TAIWAN, "\u6c11\u570b\u524d" }, 129 { MinguoEra.ROC, TextStyle.SHORT, Locale.TAIWAN, "\u6c11\u570b" }, 130 { MinguoEra.BEFORE_ROC, TextStyle.NARROW, Locale.US, "Before R.O.C." }, 131 { MinguoEra.ROC, TextStyle.NARROW, Locale.US, "Minguo" }, 132 { MinguoEra.BEFORE_ROC, TextStyle.NARROW, Locale.TAIWAN, "\u6c11\u570b\u524d" }, 133 { MinguoEra.ROC, TextStyle.NARROW, Locale.TAIWAN, "\u6c11\u570b" }, 134 135 // HijrahEra 136 { HijrahEra.AH, TextStyle.FULL, Locale.US, "AH" }, 137 { HijrahEra.AH, TextStyle.FULL, EGYPT, "\u0647\u0640" }, 138 { HijrahEra.AH, TextStyle.SHORT, Locale.US, "AH" }, 139 { HijrahEra.AH, TextStyle.SHORT, EGYPT, "\u0647\u0640" }, 140 { HijrahEra.AH, TextStyle.NARROW, Locale.US, "AH" }, 141 { HijrahEra.AH, TextStyle.NARROW, EGYPT, "\u0647\u0640" }, 142 }; 143 } 144 145 @DataProvider 146 Object[][] allLocales() { 147 return Arrays.stream(Locale.getAvailableLocales()) 148 .map(Stream::of) 149 .map(Stream::toArray) 150 .toArray(Object[][]::new); 151 } 152 153 @Test(dataProvider="eraDisplayName") 154 public void test_eraDisplayName(Era era, TextStyle style, Locale locale, String expected) { 155 assertEquals(era.getDisplayName(style, locale), expected); 156 } 157 158 @Test(dataProvider="allLocales") 159 public void test_reiwaNames(Locale locale) throws DateTimeParseException { 160 DateTimeFormatter f = JAPANESE_FORMATTER.withLocale(locale); 161 assertEquals(LocalDate.parse(REIWA_1ST.format(f), f), REIWA_1ST); 162 } 163 }