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.temporal.*;
31 import java.util.List;
32 import java.util.Locale;
33
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36 import static org.testng.Assert.assertEquals;
37
38 /**
39 * Tests for the Japanese chronology
40 */
41 @Test
42 public class TestJapaneseChronology {
43 private static final JapaneseChronology JAPANESE = JapaneseChronology.INSTANCE;
44 private static final Locale jaJPJP = Locale.forLanguageTag("ja-JP-u-ca-japanese");
45
46 @DataProvider(name="transitions")
47 Object[][] transitionData() {
48 return new Object[][] {
49 // Japanese era, yearOfEra, month, dayOfMonth, gregorianYear
50 { JapaneseEra.MEIJI, 6, 1, 1, 1873 },
51 // Meiji-Taisho transition isn't accurate. 1912-07-30 is the last day of Meiji
52 // and the first day of Taisho.
53 { JapaneseEra.MEIJI, 45, 7, 29, 1912 },
54 { JapaneseEra.TAISHO, 1, 7, 30, 1912 },
55 // Same for Taisho-Showa transition. 1926-12-25 is the last day of Taisho
56 // and the first day of Showa.
57 { JapaneseEra.TAISHO, 15, 12, 24, 1926 },
58 { JapaneseEra.SHOWA, 1, 12, 25, 1926 },
59 { JapaneseEra.SHOWA, 64, 1, 7, 1989 },
60 { JapaneseEra.HEISEI, 1, 1, 8, 1989 },
61 { JapaneseEra.HEISEI, 31, 4, 30, 2019 },
62 { JapaneseEra.of(3), 1, 5, 1, 2019 }, // NEWERA
63 };
64 }
65
66 @DataProvider(name="day_year_data")
67 Object[][] dayYearData() {
68 return new Object[][] {
69 // Japanese era, yearOfEra, dayOfYear, month, dayOfMonth
70 { JapaneseEra.MEIJI, 45, 211, 7, 29 },
71 { JapaneseEra.TAISHO, 1, 1, 7, 30 },
72 { JapaneseEra.TAISHO, 2, 60, 3, 1 },
73 { JapaneseEra.TAISHO, 15, 358, 12, 24 },
74 { JapaneseEra.SHOWA, 1, 1, 12, 25 },
75 { JapaneseEra.SHOWA, 2, 8, 1, 8 },
76 { JapaneseEra.SHOWA, 64, 7, 1, 7 },
77 { JapaneseEra.HEISEI, 1, 1, 1, 8 },
78 { JapaneseEra.HEISEI, 2, 8, 1, 8 },
79 { JapaneseEra.HEISEI, 31, 120, 4, 30 },
80 { JapaneseEra.of(3), 1, 1, 5, 1 }, // NEWERA
81 };
82 }
83
84 @DataProvider(name="range_data")
85 Object[][] rangeData() {
86 return new Object[][] {
87 // field, minSmallest, minLargest, maxSmallest, maxLargest
88 { ChronoField.ERA, -1, -1, 3, 3},
89 { ChronoField.YEAR_OF_ERA, 1, 1, 15, 999999999-2019}, // depends on the current era
90 { ChronoField.DAY_OF_YEAR, 1, 1, 7, 366},
91 { ChronoField.YEAR, 1873, 1873, 999999999, 999999999},
92 };
93 }
94
95 @DataProvider(name="invalid_dates")
96 Object[][] invalidDatesData() {
97 return new Object[][] {
98 // Japanese era, yearOfEra, month, dayOfMonth
99 { JapaneseEra.MEIJI, 6, 2, 29 },
100 { JapaneseEra.MEIJI, 45, 7, 30 },
101 { JapaneseEra.MEIJI, 46, 1, 1 },
102 { JapaneseEra.TAISHO, 1, 7, 29 },
103 { JapaneseEra.TAISHO, 2, 2, 29 },
104 { JapaneseEra.TAISHO, 15, 12, 25 },
105 { JapaneseEra.TAISHO, 16, 1, 1 },
106 { JapaneseEra.SHOWA, 1, 12, 24 },
107 { JapaneseEra.SHOWA, 2, 2, 29 },
108 { JapaneseEra.SHOWA, 64, 1, 8 },
109 { JapaneseEra.SHOWA, 65, 1, 1 },
110 { JapaneseEra.HEISEI, 1, 1, 7 },
111 { JapaneseEra.HEISEI, 1, 2, 29 },
112 { JapaneseEra.HEISEI, 31, 5, 1 },
113 { JapaneseEra.of(3), 1, 4, 30 }, // NEWERA
114 { JapaneseEra.of(3), Year.MAX_VALUE, 12, 31 }, // NEWERA
115 };
116 }
117
118 @DataProvider(name="invalid_eraYear")
119 Object[][] invalidEraYearData() {
120 return new Object[][] {
121 // Japanese era, yearOfEra
122 { JapaneseEra.MEIJI, -1 },
123 { JapaneseEra.MEIJI, 0 },
124 { JapaneseEra.MEIJI, 46 },
125 { JapaneseEra.TAISHO, -1 },
126 { JapaneseEra.TAISHO, 0 },
127 { JapaneseEra.TAISHO, 16 },
128 { JapaneseEra.SHOWA, -1 },
129 { JapaneseEra.SHOWA, 0 },
130 { JapaneseEra.SHOWA, 65 },
131 { JapaneseEra.HEISEI, -1 },
132 { JapaneseEra.HEISEI, 0 },
133 { JapaneseEra.HEISEI, 32 },
134 { JapaneseEra.of(3), -1 }, // NEWERA
135 { JapaneseEra.of(3), 0 }, // NEWERA
136 { JapaneseEra.of(3), Year.MAX_VALUE }, // NEWERA
137 };
138 }
139
140 @DataProvider(name="invalid_day_year_data")
141 Object[][] invalidDayYearData() {
142 return new Object[][] {
143 // Japanese era, yearOfEra, dayOfYear
144 { JapaneseEra.MEIJI, 45, 240 },
145 { JapaneseEra.TAISHO, 1, 365 },
146 { JapaneseEra.TAISHO, 2, 366 },
147 { JapaneseEra.TAISHO, 15, 359 },
148 { JapaneseEra.SHOWA, 1, 8 },
149 { JapaneseEra.SHOWA, 2, 366 },
150 { JapaneseEra.SHOWA, 64, 8 },
151 { JapaneseEra.HEISEI, 1, 360 },
152 { JapaneseEra.HEISEI, 2, 366 },
153 { JapaneseEra.HEISEI, 31, 121 },
154 { JapaneseEra.of(3), 1, 246 }, // NEWERA
155 { JapaneseEra.of(3), 2, 367 }, // NEWERA
156 };
157 }
158
159 @Test
160 public void test_ofLocale() {
161 // must be a singleton
162 assertEquals(Chronology.ofLocale(jaJPJP) == JAPANESE, true);
163 }
164
165 @Test(dataProvider="transitions")
166 public void test_transitions(JapaneseEra era, int yearOfEra, int month, int dayOfMonth, int gregorianYear) {
167 assertEquals(JAPANESE.prolepticYear(era, yearOfEra), gregorianYear);
168
169 JapaneseDate jdate1 = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
170 JapaneseDate jdate2 = JapaneseDate.of(gregorianYear, month, dayOfMonth);
171 assertEquals(jdate1, jdate2);
172 }
173
174 @Test(dataProvider="range_data")
175 public void test_range(ChronoField field, int minSmallest, int minLargest, int maxSmallest, int maxLargest) {
187 assertEquals(date1, date2);
188 }
189
190 @Test(dataProvider="invalid_dates", expectedExceptions=DateTimeException.class)
191 public void test_invalidDate(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
192 JapaneseDate jdate = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
193 System.out.printf("No DateTimeException with %s %d.%02d.%02d%n", era, yearOfEra, month, dayOfMonth);
194 }
195
196 @Test(dataProvider="invalid_eraYear", expectedExceptions=DateTimeException.class)
197 public void test_invalidEraYear(JapaneseEra era, int yearOfEra) {
198 int year = JAPANESE.prolepticYear(era, yearOfEra);
199 System.out.printf("No DateTimeException with era=%s, year=%d%n", era, yearOfEra);
200 }
201
202 @Test(dataProvider="invalid_day_year_data", expectedExceptions=DateTimeException.class)
203 public void test_invalidDayYear(JapaneseEra era, int yearOfEra, int dayOfYear) {
204 JapaneseDate date = JAPANESE.dateYearDay(era, yearOfEra, dayOfYear);
205 System.out.printf("No DateTimeException with era=%s, year=%d, dayOfYear=%d%n", era, yearOfEra, dayOfYear);
206 }
207 }
|
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.temporal.*;
31 import java.util.List;
32 import java.util.Locale;
33
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36 import static org.testng.Assert.assertEquals;
37 import static org.testng.Assert.assertTrue;
38
39 /**
40 * Tests for the Japanese chronology
41 */
42 @Test
43 public class TestJapaneseChronology {
44 private static final JapaneseChronology JAPANESE = JapaneseChronology.INSTANCE;
45 private static final Locale jaJPJP = Locale.forLanguageTag("ja-JP-u-ca-japanese");
46
47 @DataProvider(name="transitions")
48 Object[][] transitionData() {
49 return new Object[][] {
50 // Japanese era, yearOfEra, month, dayOfMonth, gregorianYear
51 { JapaneseEra.MEIJI, 6, 1, 1, 1873 },
52 // Meiji-Taisho transition isn't accurate. 1912-07-30 is the last day of Meiji
53 // and the first day of Taisho.
54 { JapaneseEra.MEIJI, 45, 7, 29, 1912 },
55 { JapaneseEra.TAISHO, 1, 7, 30, 1912 },
56 // Same for Taisho-Showa transition. 1926-12-25 is the last day of Taisho
57 // and the first day of Showa.
58 { JapaneseEra.TAISHO, 15, 12, 24, 1926 },
59 { JapaneseEra.SHOWA, 1, 12, 25, 1926 },
60 { JapaneseEra.SHOWA, 64, 1, 7, 1989 },
61 { JapaneseEra.HEISEI, 1, 1, 8, 1989 },
62 { JapaneseEra.HEISEI, 31, 4, 30, 2019 },
63 { JapaneseEra.of(3), 1, 5, 1, 2019 },
64 };
65 }
66
67 @DataProvider(name="day_year_data")
68 Object[][] dayYearData() {
69 return new Object[][] {
70 // Japanese era, yearOfEra, dayOfYear, month, dayOfMonth
71 { JapaneseEra.MEIJI, 45, 211, 7, 29 },
72 { JapaneseEra.TAISHO, 1, 1, 7, 30 },
73 { JapaneseEra.TAISHO, 2, 60, 3, 1 },
74 { JapaneseEra.TAISHO, 15, 358, 12, 24 },
75 { JapaneseEra.SHOWA, 1, 1, 12, 25 },
76 { JapaneseEra.SHOWA, 2, 8, 1, 8 },
77 { JapaneseEra.SHOWA, 64, 7, 1, 7 },
78 { JapaneseEra.HEISEI, 1, 1, 1, 8 },
79 { JapaneseEra.HEISEI, 2, 8, 1, 8 },
80 { JapaneseEra.HEISEI, 31, 120, 4, 30 },
81 { JapaneseEra.of(3), 1, 1, 5, 1 },
82 };
83 }
84
85 @DataProvider(name="range_data")
86 Object[][] rangeData() {
87 return new Object[][] {
88 // field, minSmallest, minLargest, maxSmallest, maxLargest
89 { ChronoField.ERA, -1, -1, 3, 3},
90 { ChronoField.YEAR_OF_ERA, 1, 1, 15, 999999999-2019}, // depends on the current era
91 { ChronoField.DAY_OF_YEAR, 1, 1, 7, 366},
92 { ChronoField.YEAR, 1873, 1873, 999999999, 999999999},
93 };
94 }
95
96 @DataProvider(name="invalid_dates")
97 Object[][] invalidDatesData() {
98 return new Object[][] {
99 // Japanese era, yearOfEra, month, dayOfMonth
100 { JapaneseEra.MEIJI, 6, 2, 29 },
101 { JapaneseEra.MEIJI, 45, 7, 30 },
102 { JapaneseEra.MEIJI, 46, 1, 1 },
103 { JapaneseEra.TAISHO, 1, 7, 29 },
104 { JapaneseEra.TAISHO, 2, 2, 29 },
105 { JapaneseEra.TAISHO, 15, 12, 25 },
106 { JapaneseEra.TAISHO, 16, 1, 1 },
107 { JapaneseEra.SHOWA, 1, 12, 24 },
108 { JapaneseEra.SHOWA, 2, 2, 29 },
109 { JapaneseEra.SHOWA, 64, 1, 8 },
110 { JapaneseEra.SHOWA, 65, 1, 1 },
111 { JapaneseEra.HEISEI, 1, 1, 7 },
112 { JapaneseEra.HEISEI, 1, 2, 29 },
113 { JapaneseEra.HEISEI, 31, 5, 1 },
114 { JapaneseEra.of(3), 1, 4, 30 },
115 { JapaneseEra.of(3), Year.MAX_VALUE, 12, 31 },
116 };
117 }
118
119 @DataProvider(name="invalid_eraYear")
120 Object[][] invalidEraYearData() {
121 return new Object[][] {
122 // Japanese era, yearOfEra
123 { JapaneseEra.MEIJI, -1 },
124 { JapaneseEra.MEIJI, 0 },
125 { JapaneseEra.MEIJI, 46 },
126 { JapaneseEra.TAISHO, -1 },
127 { JapaneseEra.TAISHO, 0 },
128 { JapaneseEra.TAISHO, 16 },
129 { JapaneseEra.SHOWA, -1 },
130 { JapaneseEra.SHOWA, 0 },
131 { JapaneseEra.SHOWA, 65 },
132 { JapaneseEra.HEISEI, -1 },
133 { JapaneseEra.HEISEI, 0 },
134 { JapaneseEra.HEISEI, 32 },
135 { JapaneseEra.of(3), -1 },
136 { JapaneseEra.of(3), 0 },
137 { JapaneseEra.of(3), Year.MAX_VALUE },
138 };
139 }
140
141 @DataProvider(name="invalid_day_year_data")
142 Object[][] invalidDayYearData() {
143 return new Object[][] {
144 // Japanese era, yearOfEra, dayOfYear
145 { JapaneseEra.MEIJI, 45, 240 },
146 { JapaneseEra.TAISHO, 1, 365 },
147 { JapaneseEra.TAISHO, 2, 366 },
148 { JapaneseEra.TAISHO, 15, 359 },
149 { JapaneseEra.SHOWA, 1, 8 },
150 { JapaneseEra.SHOWA, 2, 366 },
151 { JapaneseEra.SHOWA, 64, 8 },
152 { JapaneseEra.HEISEI, 1, 360 },
153 { JapaneseEra.HEISEI, 2, 366 },
154 { JapaneseEra.HEISEI, 31, 121 },
155 { JapaneseEra.of(3), 1, 246 },
156 { JapaneseEra.of(3), 2, 367 },
157 };
158 }
159
160 @DataProvider
161 Object[][] eraNameData() {
162 return new Object[][] {
163 // Japanese era, name, exception
164 { "Meiji", JapaneseEra.MEIJI, null },
165 { "Taisho", JapaneseEra.TAISHO, null },
166 { "Showa", JapaneseEra.SHOWA, null },
167 { "Heisei", JapaneseEra.HEISEI, null },
168 { "Reiwa", JapaneseEra.of(3), null },
169 { "NewEra", null, IllegalArgumentException.class},
170 };
171 }
172
173 @Test
174 public void test_ofLocale() {
175 // must be a singleton
176 assertEquals(Chronology.ofLocale(jaJPJP) == JAPANESE, true);
177 }
178
179 @Test(dataProvider="transitions")
180 public void test_transitions(JapaneseEra era, int yearOfEra, int month, int dayOfMonth, int gregorianYear) {
181 assertEquals(JAPANESE.prolepticYear(era, yearOfEra), gregorianYear);
182
183 JapaneseDate jdate1 = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
184 JapaneseDate jdate2 = JapaneseDate.of(gregorianYear, month, dayOfMonth);
185 assertEquals(jdate1, jdate2);
186 }
187
188 @Test(dataProvider="range_data")
189 public void test_range(ChronoField field, int minSmallest, int minLargest, int maxSmallest, int maxLargest) {
201 assertEquals(date1, date2);
202 }
203
204 @Test(dataProvider="invalid_dates", expectedExceptions=DateTimeException.class)
205 public void test_invalidDate(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
206 JapaneseDate jdate = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
207 System.out.printf("No DateTimeException with %s %d.%02d.%02d%n", era, yearOfEra, month, dayOfMonth);
208 }
209
210 @Test(dataProvider="invalid_eraYear", expectedExceptions=DateTimeException.class)
211 public void test_invalidEraYear(JapaneseEra era, int yearOfEra) {
212 int year = JAPANESE.prolepticYear(era, yearOfEra);
213 System.out.printf("No DateTimeException with era=%s, year=%d%n", era, yearOfEra);
214 }
215
216 @Test(dataProvider="invalid_day_year_data", expectedExceptions=DateTimeException.class)
217 public void test_invalidDayYear(JapaneseEra era, int yearOfEra, int dayOfYear) {
218 JapaneseDate date = JAPANESE.dateYearDay(era, yearOfEra, dayOfYear);
219 System.out.printf("No DateTimeException with era=%s, year=%d, dayOfYear=%d%n", era, yearOfEra, dayOfYear);
220 }
221
222 @Test(dataProvider="eraNameData")
223 public void test_eraName(String eraName, JapaneseEra era, Class expectedEx) {
224 try {
225 assertEquals(JapaneseEra.valueOf(eraName), era);
226 } catch (Exception ex) {
227 assertTrue(expectedEx.isInstance(ex));
228 }
229 }
230 }
|