1 /* 2 * Copyright (c) 2013, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * 26 * @test 27 * @bug 8206120 28 * @modules jdk.localedata 29 */ 30 31 package test.java.time.format; 32 33 import static org.testng.Assert.assertEquals; 34 35 import dalvik.annotation.compat.VersionCodes; 36 37 import libcore.test.annotation.NonMts; 38 import libcore.test.reasons.NonMtsReasons; 39 40 import java.time.LocalDate; 41 import java.time.chrono.ChronoLocalDate; 42 import java.time.chrono.Chronology; 43 import java.time.chrono.HijrahChronology; 44 import java.time.chrono.IsoChronology; 45 import java.time.chrono.JapaneseChronology; 46 import java.time.chrono.MinguoChronology; 47 import java.time.chrono.ThaiBuddhistChronology; 48 import java.time.format.DecimalStyle; 49 import java.time.format.DateTimeFormatter; 50 import java.time.format.DateTimeFormatterBuilder; 51 import java.time.format.DateTimeParseException; 52 import java.time.format.FormatStyle; 53 import java.time.format.ResolverStyle; 54 import java.time.format.TextStyle; 55 import java.time.temporal.TemporalAccessor; 56 import java.time.temporal.TemporalQueries; 57 import java.util.Locale; 58 59 import org.testng.annotations.BeforeMethod; 60 import org.testng.annotations.DataProvider; 61 import org.testng.annotations.Test; 62 63 /** 64 * Test DateTimeFormatter with non-ISO chronology. 65 * 66 * Strings in test data are all dependent on CLDR data which may change 67 * in future CLDR releases. 68 */ 69 @Test 70 public class TestNonIsoFormatter { 71 private static final Chronology ISO8601 = IsoChronology.INSTANCE; 72 private static final Chronology JAPANESE = JapaneseChronology.INSTANCE; 73 private static final Chronology HIJRAH = HijrahChronology.INSTANCE; 74 private static final Chronology MINGUO = MinguoChronology.INSTANCE; 75 private static final Chronology BUDDHIST = ThaiBuddhistChronology.INSTANCE; 76 77 private static final LocalDate IsoDate = LocalDate.of(2013, 2, 11); 78 79 // Android-changed: Android uses CLDR data. 80 // private static final Locale ARABIC = new Locale("ar"); 81 private static final Locale ARABIC = new Locale("ar", "EG"); 82 private static final Locale thTH = new Locale("th", "TH"); 83 private static final Locale thTHTH = Locale.forLanguageTag("th-TH-u-nu-thai"); 84 private static final Locale jaJPJP = Locale.forLanguageTag("ja-JP-u-ca-japanese"); 85 86 @BeforeMethod setUp()87 public void setUp() { 88 } 89 90 @DataProvider(name="format_data") formatData()91 Object[][] formatData() { 92 return new Object[][] { 93 // Chronology, Format Locale, Numbering Locale, ChronoLocalDate, expected string 94 { JAPANESE, Locale.JAPANESE, Locale.JAPANESE, JAPANESE.date(IsoDate), 95 "\u5e73\u621025\u5e742\u670811\u65e5\u6708\u66dc\u65e5" }, // Japanese Heisei 25-02-11 96 { HIJRAH, ARABIC, ARABIC, HIJRAH.date(IsoDate), 97 "\u0627\u0644\u0627\u062b\u0646\u064a\u0646\u060c \u0661 \u0631\u0628\u064a\u0639 " 98 + "\u0627\u0644\u0622\u062e\u0631 \u0661\u0664\u0663\u0664 \u0647\u0640" }, // Hijrah AH 1434-04-01 (Mon) 99 { MINGUO, Locale.TAIWAN, Locale.TAIWAN, MINGUO.date(IsoDate), 100 "\u6c11\u570b102\u5e742\u670811\u65e5 \u661f\u671f\u4e00" }, // Minguo ROC 102-02-11 (Mon) 101 { BUDDHIST, thTH, thTH, BUDDHIST.date(IsoDate), 102 "\u0e27\u0e31\u0e19\u0e08\u0e31\u0e19\u0e17\u0e23\u0e4c\u0e17\u0e35\u0e48 " 103 + "11 \u0e01\u0e38\u0e21\u0e20\u0e32\u0e1e\u0e31\u0e19\u0e18\u0e4c " 104 + "\u0e1e.\u0e28. 2556" }, // ThaiBuddhist BE 2556-02-11 105 { BUDDHIST, thTH, thTHTH, BUDDHIST.date(IsoDate), 106 "\u0e27\u0e31\u0e19\u0e08\u0e31\u0e19\u0e17\u0e23\u0e4c\u0e17\u0e35\u0e48 \u0e51\u0e51 " 107 + "\u0e01\u0e38\u0e21\u0e20\u0e32\u0e1e\u0e31\u0e19\u0e18\u0e4c " 108 + "\u0e1e.\u0e28. \u0e52\u0e55\u0e55\u0e56" }, // ThaiBuddhist BE 2556-02-11 (with Thai digits) 109 }; 110 } 111 112 @DataProvider(name="invalid_text") invalidText()113 Object[][] invalidText() { 114 return new Object[][] { 115 // TODO: currently fixed Chronology and Locale. 116 // line commented out, as S64.01.09 seems like a reasonable thing to parse 117 // (era "S" ended on S64.01.07, but a little leniency is a good thing 118 // { "\u662d\u548c64\u5e741\u67089\u65e5\u6708\u66dc\u65e5" }, // S64.01.09 (Mon) 119 { "\u662d\u548c65\u5e741\u67081\u65e5\u6708\u66dc\u65e5" }, // S65.01.01 (Mon) 120 }; 121 } 122 123 @DataProvider(name="chrono_names") chronoNamesData()124 Object[][] chronoNamesData() { 125 return new Object[][] { 126 // Chronology, Locale, Chronology Name 127 // Android-changed: CLDR data has changed. 128 { ISO8601, Locale.ENGLISH, "ISO-8601 Calendar" }, 129 { BUDDHIST, Locale.ENGLISH, "Buddhist Calendar" }, 130 { HIJRAH, Locale.ENGLISH, "Hijri Calendar (Umm al-Qura)" }, 131 { JAPANESE, Locale.ENGLISH, "Japanese Calendar" }, 132 { MINGUO, Locale.ENGLISH, "Minguo Calendar" }, 133 134 { ISO8601, Locale.JAPANESE, "ISO-8601" }, 135 { JAPANESE, Locale.JAPANESE, "\u548c\u66a6" }, 136 { BUDDHIST, Locale.JAPANESE, "\u4ecf\u66a6" }, 137 138 { ISO8601, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19 ISO-8601" }, 139 { JAPANESE, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e0d\u0e35\u0e48\u0e1b\u0e38\u0e48\u0e19" }, 140 { BUDDHIST, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e1e\u0e38\u0e17\u0e18" }, 141 142 { HIJRAH, ARABIC, "\u0627\u0644\u062a\u0642\u0648\u064a\u0645 " 143 + "\u0627\u0644\u0647\u062c\u0631\u064a " 144 + "(\u0623\u0645 \u0627\u0644\u0642\u0631\u0649)" }, 145 }; 146 } 147 148 @DataProvider(name="lenient_eraYear") lenientEraYear()149 Object[][] lenientEraYear() { 150 return new Object[][] { 151 // Chronology, lenient era/year, strict era/year 152 { JAPANESE, "Meiji 123", "Heisei 2" }, 153 { JAPANESE, "Sh\u014dwa 65", "Heisei 2" }, 154 { JAPANESE, "Heisei 32", "Reiwa 2" }, 155 }; 156 } 157 158 @Test(dataProvider="format_data") test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale, ChronoLocalDate date, String expected)159 public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale, 160 ChronoLocalDate date, String expected) { 161 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL) 162 .withChronology(chrono).withLocale(formatLocale) 163 .withDecimalStyle(DecimalStyle.of(numberingLocale)); 164 String text = dtf.format(date); 165 assertEquals(text, expected); 166 } 167 168 @Test(dataProvider="format_data") test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale, ChronoLocalDate expected, String text)169 public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale, 170 ChronoLocalDate expected, String text) { 171 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL) 172 .withChronology(chrono).withLocale(formatLocale) 173 .withDecimalStyle(DecimalStyle.of(numberingLocale)); 174 TemporalAccessor temporal = dtf.parse(text); 175 ChronoLocalDate date = chrono.date(temporal); 176 assertEquals(date, expected); 177 } 178 179 @Test(dataProvider="invalid_text", expectedExceptions=DateTimeParseException.class) test_parseInvalidText(String text)180 public void test_parseInvalidText(String text) { 181 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL) 182 .withChronology(JAPANESE).withLocale(Locale.JAPANESE); 183 dtf.parse(text); 184 } 185 186 @NonMts(reason = NonMtsReasons.ICU_VERSION_DEPENDENCY, 187 disabledUntilSdk = VersionCodes.VANILLA_ICE_CREAM) 188 @Test(dataProvider="chrono_names") test_chronoNames(Chronology chrono, Locale locale, String expected)189 public void test_chronoNames(Chronology chrono, Locale locale, String expected) { 190 DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendChronologyText(TextStyle.SHORT) 191 .toFormatter(locale); 192 String text = dtf.format(chrono.dateNow()); 193 assertEquals(text, expected); 194 TemporalAccessor ta = dtf.parse(text); 195 Chronology cal = ta.query(TemporalQueries.chronology()); 196 assertEquals(cal, chrono); 197 } 198 199 @Test(dataProvider="lenient_eraYear") test_lenientEraYear(Chronology chrono, String lenient, String strict)200 public void test_lenientEraYear(Chronology chrono, String lenient, String strict) { 201 String mdStr = "-01-01"; 202 DateTimeFormatter dtf = new DateTimeFormatterBuilder() 203 .appendPattern("GGGG y-M-d") 204 .toFormatter(Locale.ROOT) 205 .withChronology(chrono); 206 DateTimeFormatter dtfLenient = dtf.withResolverStyle(ResolverStyle.LENIENT); 207 assertEquals(LocalDate.parse(lenient+mdStr, dtfLenient), LocalDate.parse(strict+mdStr, dtf)); 208 } 209 } 210