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.DecimalStyle;
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.TemporalQueries;
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;
111 { ISO8601, Locale.ENGLISH, "ISO" }, // No data in CLDR; Use Id.
112 { BUDDHIST, Locale.ENGLISH, "Buddhist Calendar" },
113 { HIJRAH, Locale.ENGLISH, "Islamic Umm al-Qura Calendar" }, // JDK-8015986
114 { JAPANESE, Locale.ENGLISH, "Japanese Calendar" },
115 { MINGUO, Locale.ENGLISH, "Minguo Calendar" },
116
117 { ISO8601, Locale.JAPANESE, "ISO" }, // No data in CLDR; Use Id.
118 { JAPANESE, Locale.JAPANESE, "\u548c\u66a6" },
119 { BUDDHIST, Locale.JAPANESE, "\u30bf\u30a4\u4ecf\u6559\u66a6" },
120
121 { ISO8601, thTH, "ISO" }, // No data in CLDR; Use Id.
122 { JAPANESE, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e0d\u0e35\u0e48\u0e1b\u0e38\u0e48\u0e19" },
123 { BUDDHIST, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e1e\u0e38\u0e17\u0e18" },
124
125 { HIJRAH, ARABIC, "\u0644\u062a\u0642\u0648\u064a\u0645 "
126 + "\u0627\u0644\u0647\u062c\u0631\u064a\u060c "
127 + "\u0623\u0645 \u0627\u0644\u0642\u0631\u0649" }, // JDK-8015986
128 };
129 }
130
131 @Test(dataProvider="format_data")
132 public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale,
133 ChronoLocalDate date, String expected) {
134 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
135 .withChronology(chrono).withLocale(formatLocale)
136 .withDecimalStyle(DecimalStyle.of(numberingLocale));
137 String text = dtf.format(date);
138 assertEquals(text, expected);
139 }
140
141 @Test(dataProvider="format_data")
142 public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale,
143 ChronoLocalDate expected, String text) {
144 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
145 .withChronology(chrono).withLocale(formatLocale)
146 .withDecimalStyle(DecimalStyle.of(numberingLocale));
147 TemporalAccessor temporal = dtf.parse(text);
148 ChronoLocalDate date = chrono.date(temporal);
149 assertEquals(date, expected);
150 }
151
152 @Test(dataProvider="invalid_text", expectedExceptions=DateTimeParseException.class)
153 public void test_parseInvalidText(String text) {
154 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
155 .withChronology(JAPANESE).withLocale(Locale.JAPANESE);
156 dtf.parse(text);
157 }
158
159 @Test(dataProvider="chrono_names")
160 public void test_chronoNames(Chronology chrono, Locale locale, String expected) {
161 DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendChronologyText(TextStyle.SHORT)
162 .toFormatter(locale);
163 String text = dtf.format(chrono.dateNow());
164 assertEquals(text, expected);
165 TemporalAccessor ta = dtf.parse(text);
166 Chronology cal = ta.query(TemporalQueries.chronology());
167 assertEquals(cal, chrono);
168 }
169 }
|
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 */
29
30 package test.java.time.format;
31
32 import static org.testng.Assert.assertEquals;
33
34 import java.time.LocalDate;
35 import java.time.chrono.ChronoLocalDate;
36 import java.time.chrono.Chronology;
37 import java.time.chrono.HijrahChronology;
38 import java.time.chrono.IsoChronology;
39 import java.time.chrono.JapaneseChronology;
40 import java.time.chrono.MinguoChronology;
41 import java.time.chrono.ThaiBuddhistChronology;
42 import java.time.format.DecimalStyle;
43 import java.time.format.DateTimeFormatter;
44 import java.time.format.DateTimeFormatterBuilder;
45 import java.time.format.DateTimeParseException;
46 import java.time.format.FormatStyle;
47 import java.time.format.ResolverStyle;
48 import java.time.format.TextStyle;
49 import java.time.temporal.TemporalAccessor;
50 import java.time.temporal.TemporalQueries;
51 import java.util.Locale;
52
53 import org.testng.annotations.BeforeMethod;
54 import org.testng.annotations.DataProvider;
55 import org.testng.annotations.Test;
56
57 /**
58 * Test DateTimeFormatter with non-ISO chronology.
59 *
60 * Strings in test data are all dependent on CLDR data which may change
61 * in future CLDR releases.
62 */
63 @Test
64 public class TestNonIsoFormatter {
65 private static final Chronology ISO8601 = IsoChronology.INSTANCE;
66 private static final Chronology JAPANESE = JapaneseChronology.INSTANCE;
67 private static final Chronology HIJRAH = HijrahChronology.INSTANCE;
119 { ISO8601, Locale.ENGLISH, "ISO" }, // No data in CLDR; Use Id.
120 { BUDDHIST, Locale.ENGLISH, "Buddhist Calendar" },
121 { HIJRAH, Locale.ENGLISH, "Islamic Umm al-Qura Calendar" }, // JDK-8015986
122 { JAPANESE, Locale.ENGLISH, "Japanese Calendar" },
123 { MINGUO, Locale.ENGLISH, "Minguo Calendar" },
124
125 { ISO8601, Locale.JAPANESE, "ISO" }, // No data in CLDR; Use Id.
126 { JAPANESE, Locale.JAPANESE, "\u548c\u66a6" },
127 { BUDDHIST, Locale.JAPANESE, "\u30bf\u30a4\u4ecf\u6559\u66a6" },
128
129 { ISO8601, thTH, "ISO" }, // No data in CLDR; Use Id.
130 { JAPANESE, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e0d\u0e35\u0e48\u0e1b\u0e38\u0e48\u0e19" },
131 { BUDDHIST, thTH, "\u0e1b\u0e0f\u0e34\u0e17\u0e34\u0e19\u0e1e\u0e38\u0e17\u0e18" },
132
133 { HIJRAH, ARABIC, "\u0644\u062a\u0642\u0648\u064a\u0645 "
134 + "\u0627\u0644\u0647\u062c\u0631\u064a\u060c "
135 + "\u0623\u0645 \u0627\u0644\u0642\u0631\u0649" }, // JDK-8015986
136 };
137 }
138
139 @DataProvider(name="lenient_eraYear")
140 Object[][] lenientEraYear() {
141 return new Object[][] {
142 // Chronology, lenient era/year, strict era/year
143 { JAPANESE, "Meiji 123", "Heisei 2" },
144 { JAPANESE, "Showa 65", "Heisei 2" },
145 { JAPANESE, "Heisei 32", "NewEra 2" }, // NewEra
146 };
147 }
148
149 @Test(dataProvider="format_data")
150 public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale,
151 ChronoLocalDate date, String expected) {
152 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
153 .withChronology(chrono).withLocale(formatLocale)
154 .withDecimalStyle(DecimalStyle.of(numberingLocale));
155 String text = dtf.format(date);
156 assertEquals(text, expected);
157 }
158
159 @Test(dataProvider="format_data")
160 public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale,
161 ChronoLocalDate expected, String text) {
162 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
163 .withChronology(chrono).withLocale(formatLocale)
164 .withDecimalStyle(DecimalStyle.of(numberingLocale));
165 TemporalAccessor temporal = dtf.parse(text);
166 ChronoLocalDate date = chrono.date(temporal);
167 assertEquals(date, expected);
168 }
169
170 @Test(dataProvider="invalid_text", expectedExceptions=DateTimeParseException.class)
171 public void test_parseInvalidText(String text) {
172 DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
173 .withChronology(JAPANESE).withLocale(Locale.JAPANESE);
174 dtf.parse(text);
175 }
176
177 @Test(dataProvider="chrono_names")
178 public void test_chronoNames(Chronology chrono, Locale locale, String expected) {
179 DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendChronologyText(TextStyle.SHORT)
180 .toFormatter(locale);
181 String text = dtf.format(chrono.dateNow());
182 assertEquals(text, expected);
183 TemporalAccessor ta = dtf.parse(text);
184 Chronology cal = ta.query(TemporalQueries.chronology());
185 assertEquals(cal, chrono);
186 }
187
188 @Test(dataProvider="lenient_eraYear")
189 public void test_lenientEraYear(Chronology chrono, String lenient, String strict) {
190 String mdStr = "-01-01";
191 DateTimeFormatter dtf = new DateTimeFormatterBuilder()
192 .appendPattern("GGGG y-M-d")
193 .toFormatter()
194 .withChronology(chrono);
195 DateTimeFormatter dtfLenient = dtf.withResolverStyle(ResolverStyle.LENIENT);
196 assertEquals(LocalDate.parse(lenient+mdStr, dtfLenient), LocalDate.parse(strict+mdStr, dtf));
197 }
198 }
|