• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.  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.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")
transitionData()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             // Android-changed: Integrate OpenJDK support for Japanese Era Reiwa.
64             // { JapaneseEra.of(3),      1,  5,  1, 2019 },
65             { JapaneseEra.REIWA,      1,  5,  1, 2019 },
66         };
67     }
68 
69     @DataProvider(name="day_year_data")
dayYearData()70     Object[][] dayYearData() {
71         return new Object[][] {
72             // Japanese era, yearOfEra, dayOfYear, month, dayOfMonth
73             { JapaneseEra.MEIJI,  45,  211,  7, 29 },
74             { JapaneseEra.TAISHO,  1,    1,  7, 30 },
75             { JapaneseEra.TAISHO,  2,   60,  3,  1 },
76             { JapaneseEra.TAISHO, 15,  358, 12, 24 },
77             { JapaneseEra.SHOWA,   1,    1, 12, 25 },
78             { JapaneseEra.SHOWA,   2,    8,  1,  8 },
79             { JapaneseEra.SHOWA,  64,    7,  1,  7 },
80             { JapaneseEra.HEISEI,  1,    1,  1,  8 },
81             { JapaneseEra.HEISEI,  2,    8,  1,  8 },
82             { JapaneseEra.HEISEI, 31,  120,  4, 30 },
83             // Android-changed: Integrate OpenJDK support for Japanese Era Reiwa.
84             // { JapaneseEra.of(3),   1,    1,  5,  1 },
85             { JapaneseEra.REIWA,   1,    1,  5,  1 },
86         };
87     }
88 
89     @DataProvider(name="range_data")
rangeData()90     Object[][] rangeData() {
91         return new Object[][] {
92             // field, minSmallest, minLargest, maxSmallest, maxLargest
93             { ChronoField.ERA,         -1, -1, 3, 3},
94             { ChronoField.YEAR_OF_ERA, 1, 1, 15, 999999999-2019}, // depends on the current era
95             { ChronoField.DAY_OF_YEAR, 1, 1, 7, 366},
96             { ChronoField.YEAR, 1873, 1873, 999999999, 999999999},
97         };
98     }
99 
100     @DataProvider(name="invalid_dates")
invalidDatesData()101     Object[][] invalidDatesData() {
102         return new Object[][] {
103             // Japanese era, yearOfEra, month, dayOfMonth
104             { JapaneseEra.MEIJI,      6,  2, 29 },
105             { JapaneseEra.MEIJI,     45,  7, 30 },
106             { JapaneseEra.MEIJI,     46,  1,  1 },
107             { JapaneseEra.TAISHO,     1,  7, 29 },
108             { JapaneseEra.TAISHO,     2,  2, 29 },
109             { JapaneseEra.TAISHO,    15, 12, 25 },
110             { JapaneseEra.TAISHO,    16,  1,  1 },
111             { JapaneseEra.SHOWA,      1, 12, 24 },
112             { JapaneseEra.SHOWA,      2,  2, 29 },
113             { JapaneseEra.SHOWA,     64,  1,  8 },
114             { JapaneseEra.SHOWA,     65,  1,  1 },
115             { JapaneseEra.HEISEI,     1,  1,  7 },
116             { JapaneseEra.HEISEI,     1,  2, 29 },
117             { JapaneseEra.HEISEI,    31,  5,  1 },
118             // Android-changed: Integrate OpenJDK support for Japanese Era Reiwa.
119             // { JapaneseEra.of(3),      1,  4, 30 },
120             // { JapaneseEra.of(3), Year.MAX_VALUE,  12, 31 },
121             { JapaneseEra.REIWA,      1,  4, 30 },
122             { JapaneseEra.REIWA, Year.MAX_VALUE,  12, 31 },
123         };
124     }
125 
126     @DataProvider(name="invalid_eraYear")
invalidEraYearData()127     Object[][] invalidEraYearData() {
128         return new Object[][] {
129             // Japanese era, yearOfEra
130             { JapaneseEra.MEIJI,     -1 },
131             { JapaneseEra.MEIJI,      0 },
132             { JapaneseEra.MEIJI,     46 },
133             { JapaneseEra.TAISHO,    -1 },
134             { JapaneseEra.TAISHO,     0 },
135             { JapaneseEra.TAISHO,    16 },
136             { JapaneseEra.SHOWA,     -1 },
137             { JapaneseEra.SHOWA,      0 },
138             { JapaneseEra.SHOWA,     65 },
139             { JapaneseEra.HEISEI,    -1 },
140             { JapaneseEra.HEISEI,     0 },
141             { JapaneseEra.HEISEI,    32 },
142             // Android-changed: Integrate OpenJDK support for Japanese Era Reiwa.
143             // { JapaneseEra.of(3),     -1 },
144             // { JapaneseEra.of(3),      0 },
145             // { JapaneseEra.of(3), Year.MAX_VALUE },
146             { JapaneseEra.REIWA,     -1 },
147             { JapaneseEra.REIWA,      0 },
148             { JapaneseEra.REIWA, Year.MAX_VALUE },
149         };
150     }
151 
152     @DataProvider(name="invalid_day_year_data")
invalidDayYearData()153     Object[][] invalidDayYearData() {
154         return new Object[][] {
155             // Japanese era, yearOfEra, dayOfYear
156             { JapaneseEra.MEIJI,  45, 240 },
157             { JapaneseEra.TAISHO,  1, 365 },
158             { JapaneseEra.TAISHO,  2, 366 },
159             { JapaneseEra.TAISHO, 15, 359 },
160             { JapaneseEra.SHOWA,   1,   8 },
161             { JapaneseEra.SHOWA,   2, 366 },
162             { JapaneseEra.SHOWA,  64,   8 },
163             { JapaneseEra.HEISEI,  1, 360 },
164             { JapaneseEra.HEISEI,  2, 366 },
165             { JapaneseEra.HEISEI, 31, 121 },
166             // Android-changed: Integrate OpenJDK support for Japanese Era Reiwa.
167             // { JapaneseEra.of(3),   1, 246 },
168             // { JapaneseEra.of(3),   2, 367 },
169             { JapaneseEra.REIWA,   1, 246 },
170             { JapaneseEra.REIWA,   2, 367 },
171         };
172     }
173 
174     @DataProvider
eraNameData()175     Object[][] eraNameData() {
176         return new Object[][] {
177             // Japanese era, name, exception
178             { "Meiji",  JapaneseEra.MEIJI,      null },
179             { "Taisho", JapaneseEra.TAISHO,     null },
180             { "Showa",  JapaneseEra.SHOWA,      null },
181             { "Heisei", JapaneseEra.HEISEI,     null },
182             // Android-changed: Integrate OpenJDK support for Japanese Era Reiwa.
183             // { "Reiwa", JapaneseEra.of(3),       null },
184             { "Reiwa", JapaneseEra.REIWA,       null },
185             { "NewEra", null,                   IllegalArgumentException.class},
186         };
187     }
188 
189     @Test
test_ofLocale()190     public void test_ofLocale() {
191         // must be a singleton
192         assertEquals(Chronology.ofLocale(jaJPJP) == JAPANESE, true);
193     }
194 
195     @Test(dataProvider="transitions")
test_transitions(JapaneseEra era, int yearOfEra, int month, int dayOfMonth, int gregorianYear)196     public void test_transitions(JapaneseEra era, int yearOfEra, int month, int dayOfMonth, int gregorianYear) {
197         assertEquals(JAPANESE.prolepticYear(era, yearOfEra), gregorianYear);
198 
199         JapaneseDate jdate1 = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
200         JapaneseDate jdate2 = JapaneseDate.of(gregorianYear, month, dayOfMonth);
201         assertEquals(jdate1, jdate2);
202     }
203 
204     @Test(dataProvider="range_data")
test_range(ChronoField field, int minSmallest, int minLargest, int maxSmallest, int maxLargest)205     public void test_range(ChronoField field, int minSmallest, int minLargest, int maxSmallest, int maxLargest) {
206         ValueRange range = JAPANESE.range(field);
207         assertEquals(range.getMinimum(), minSmallest);
208         assertEquals(range.getLargestMinimum(), minLargest);
209         assertEquals(range.getSmallestMaximum(), maxSmallest);
210         assertEquals(range.getMaximum(), maxLargest);
211     }
212 
213     @Test(dataProvider="day_year_data")
test_firstDayOfEra(JapaneseEra era, int yearOfEra, int dayOfYear, int month, int dayOfMonth)214     public void test_firstDayOfEra(JapaneseEra era, int yearOfEra, int dayOfYear, int month, int dayOfMonth) {
215         JapaneseDate date1 = JAPANESE.dateYearDay(era, yearOfEra, dayOfYear);
216         JapaneseDate date2 = JAPANESE.date(era, yearOfEra, month, dayOfMonth);
217         assertEquals(date1, date2);
218     }
219 
220     @Test(dataProvider="invalid_dates", expectedExceptions=DateTimeException.class)
test_invalidDate(JapaneseEra era, int yearOfEra, int month, int dayOfMonth)221     public void test_invalidDate(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {
222         JapaneseDate jdate = JapaneseDate.of(era, yearOfEra, month, dayOfMonth);
223         System.out.printf("No DateTimeException with %s %d.%02d.%02d%n", era, yearOfEra, month, dayOfMonth);
224     }
225 
226     @Test(dataProvider="invalid_eraYear", expectedExceptions=DateTimeException.class)
test_invalidEraYear(JapaneseEra era, int yearOfEra)227     public void test_invalidEraYear(JapaneseEra era, int yearOfEra) {
228         int year = JAPANESE.prolepticYear(era, yearOfEra);
229         System.out.printf("No DateTimeException with era=%s, year=%d%n", era, yearOfEra);
230     }
231 
232     @Test(dataProvider="invalid_day_year_data", expectedExceptions=DateTimeException.class)
test_invalidDayYear(JapaneseEra era, int yearOfEra, int dayOfYear)233     public void test_invalidDayYear(JapaneseEra era, int yearOfEra, int dayOfYear) {
234         JapaneseDate date = JAPANESE.dateYearDay(era, yearOfEra, dayOfYear);
235         System.out.printf("No DateTimeException with era=%s, year=%d, dayOfYear=%d%n", era, yearOfEra, dayOfYear);
236     }
237 
238     @Test(dataProvider="eraNameData")
test_eraName(String eraName, JapaneseEra era, Class expectedEx)239     public void test_eraName(String eraName, JapaneseEra era, Class expectedEx) {
240         try {
241             assertEquals(JapaneseEra.valueOf(eraName), era);
242         } catch (Exception ex) {
243             assertTrue(expectedEx.isInstance(ex));
244         }
245     }
246 }
247