1 /* 2 * Copyright (c) 2013, 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 package test.java.time.format; 24 25 import static org.testng.Assert.assertEquals; 26 27 import java.time.LocalDate; 28 import java.time.chrono.ChronoLocalDate; 29 import java.time.chrono.Chronology; 30 import java.time.chrono.HijrahChronology; 31 import java.time.chrono.IsoChronology; 32 import java.time.chrono.JapaneseChronology; 33 import java.time.chrono.MinguoChronology; 34 import java.time.chrono.ThaiBuddhistChronology; 35 import java.time.format.DateTimeFormatSymbols; 36 import java.time.format.DateTimeFormatter; 37 import java.time.format.DateTimeFormatterBuilder; 38 import java.time.format.DateTimeParseException; 39 import java.time.format.FormatStyle; 40 import java.time.format.TextStyle; 41 import java.time.temporal.TemporalAccessor; 42 import java.time.temporal.TemporalQuery; 43 import java.util.Locale; 44 45 import org.testng.annotations.BeforeMethod; 46 import org.testng.annotations.DataProvider; 47 import org.testng.annotations.Test; 48 49 /** 50 * Test DateTimeFormatter with non-ISO chronology. 51 * 52 * Strings in test data are all dependent on CLDR data which may change 53 * in future CLDR releases. 54 */ 55 @Test 56 public class TestNonIsoFormatter { 57 private static final Chronology ISO8601 = IsoChronology.INSTANCE; 58 private static final Chronology JAPANESE = JapaneseChronology.INSTANCE; 59 private static final Chronology HIJRAH = HijrahChronology.INSTANCE; 60 private static final Chronology MINGUO = MinguoChronology.INSTANCE; 61 private static final Chronology BUDDHIST = ThaiBuddhistChronology.INSTANCE; 62 63 private static final LocalDate IsoDate = LocalDate.of(2013, 2, 11); 64 65 private static final Locale ARABIC = new Locale("ar"); 66 private static final Locale thTH = new Locale("th", "TH"); 67 private static final Locale thTHTH = Locale.forLanguageTag("th-TH-u-nu-thai"); 68 69 @BeforeMethod 70 public void setUp() { 71 } 72 73 @DataProvider(name="format_data") 74 Object[][] formatData() { 75 return new Object[][] { 76 // Chronology, Format Locale, Numbering Locale, ChronoLocalDate, expected string 77 { JAPANESE, Locale.JAPANESE, Locale.JAPANESE, JAPANESE.date(IsoDate), 78 "\u5e73\u621025\u5e742\u670811\u65e5\u6708\u66dc\u65e5" }, // Japanese Heisei 25-02-11 (Mon) 79 { HIJRAH, ARABIC, ARABIC, HIJRAH.date(IsoDate), 80 "\u0627\u0644\u0627\u062b\u0646\u064a\u0646\u060c 1 \u0631\u0628\u064a\u0639 " 81 + "\u0627\u0644\u0622\u062e\u0631 1434" }, // Hijrah AH 1434-04-01 (Mon) 82 { MINGUO, Locale.TAIWAN, Locale.TAIWAN, MINGUO.date(IsoDate), 83 "\u6c11\u570b102\u5e742\u670811\u65e5\u661f\u671f\u4e00" }, // Minguo ROC 102-02-11 (Mon) 84 { BUDDHIST, thTH, thTH, BUDDHIST.date(IsoDate), 85 "\u0e27\u0e31\u0e19\u0e08\u0e31\u0e19\u0e17\u0e23\u0e4c\u0e17\u0e35\u0e48" 86 + " 11 \u0e01\u0e38\u0e21\u0e20\u0e32\u0e1e\u0e31\u0e19\u0e18\u0e4c" 87 + " \u0e1e.\u0e28. 2556" }, // ThaiBuddhist BE 2556-02-11 88 { BUDDHIST, thTH, thTHTH, BUDDHIST.date(IsoDate), 89 "\u0e27\u0e31\u0e19\u0e08\u0e31\u0e19\u0e17\u0e23\u0e4c\u0e17\u0e35\u0e48 \u0e51\u0e51 " 90 + "\u0e01\u0e38\u0e21\u0e20\u0e32\u0e1e\u0e31\u0e19\u0e18\u0e4c \u0e1e.\u0e28. " 91 + "\u0e52\u0e55\u0e55\u0e56" }, // ThaiBuddhist BE 2556-02-11 (with Thai digits) 92 }; 93 } 94 95 @DataProvider(name="invalid_text") 96 Object[][] invalidText() { 97 return new Object[][] { 98 // TODO: currently fixed Chronology and Locale. 99 // line commented out, as S64.01.09 seems like a reasonable thing to parse 100 // (era "S" ended on S64.01.07, but a little leniency is a good thing 101 // { "\u662d\u548c64\u5e741\u67089\u65e5\u6708\u66dc\u65e5" }, // S64.01.09 (Mon) 102 { "\u662d\u548c65\u5e741\u67081\u65e5\u6708\u66dc\u65e5" }, // S65.01.01 (Mon) 103 }; 104 } 105 106 @DataProvider(name="chrono_names") 107 Object[][] chronoNamesData() { 108 return new Object[][] { 109 // Chronology, Locale, Chronology Name 110 { ISO8601, Locale.ENGLISH, "ISO" }, // No data in CLDR; Use Id. 111 { BUDDHIST, Locale.ENGLISH, "Buddhist Calendar" }, 112 { HIJRAH, Locale.ENGLISH, "Hijrah-umalqura" }, // No data in CLDR; Use Id. 113 { JAPANESE, Locale.ENGLISH, "Japanese Calendar" }, 114 { MINGUO, Locale.ENGLISH, "Minguo Calendar" }, 115 116 { ISO8601, Locale.JAPANESE, "ISO" }, // No data in CLDR; Use Id. 117 { JAPANESE, Locale.JAPANESE, "\u548c\u66a6" }, 118 { BUDDHIST, Locale.JAPANESE, "\u30bf\u30a4\u4ecf\u6559\u66a6" }, 119 120 { ISO8601, thTH, "ISO" }, // No data in CLDR; Use Id. 121 { JAPANESE, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e0d\u0e35\u0e48\u0e1b\u0e38\u0e48\u0e19" }, 122 { BUDDHIST, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e1e\u0e38\u0e17\u0e18" }, 123 }; 124 } 125 126 @Test(dataProvider="format_data") 127 public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale, 128 ChronoLocalDate<?> date, String expected) { 129 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL) 130 .withChronology(chrono).withLocale(formatLocale) 131 .withSymbols(DateTimeFormatSymbols.of(numberingLocale)); 132 String text = dtf.format(date); 133 assertEquals(text, expected); 134 } 135 136 @Test(dataProvider="format_data") 137 public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale, 138 ChronoLocalDate<?> expected, String text) { 139 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL) 140 .withChronology(chrono).withLocale(formatLocale) 141 .withSymbols(DateTimeFormatSymbols.of(numberingLocale)); 142 TemporalAccessor temporal = dtf.parse(text); 143 ChronoLocalDate<?> date = chrono.date(temporal); 144 assertEquals(date, expected); 145 } 146 147 @Test(dataProvider="invalid_text", expectedExceptions=DateTimeParseException.class) 148 public void test_parseInvalidText(String text) { 149 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL) 150 .withChronology(JAPANESE).withLocale(Locale.JAPANESE); 151 dtf.parse(text); 152 } 153 154 @Test(dataProvider="chrono_names") 155 public void test_chronoNames(Chronology chrono, Locale locale, String expected) { 156 DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendChronologyText(TextStyle.SHORT) 157 .toFormatter(locale); 158 String text = dtf.format(chrono.dateNow()); 159 assertEquals(text, expected); 160 TemporalAccessor ta = dtf.parse(text); 161 Chronology cal = ta.query(TemporalQuery.chronology()); 162 assertEquals(cal, chrono); 163 } 164 }