1 /*
2  * Copyright (c) 2012, 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 
24 /*
25  * This file is available under and governed by the GNU General Public
26  * License version 2 only, as published by the Free Software Foundation.
27  * However, the following notice accompanied the original version of this
28  * file:
29  *
30  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
31  *
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions are met:
36  *
37  *  * Redistributions of source code must retain the above copyright notice,
38  *    this list of conditions and the following disclaimer.
39  *
40  *  * Redistributions in binary form must reproduce the above copyright notice,
41  *    this list of conditions and the following disclaimer in the documentation
42  *    and/or other materials provided with the distribution.
43  *
44  *  * Neither the name of JSR-310 nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 package tck.java.time.chrono;
61 
62 import static org.testng.Assert.assertEquals;
63 import static org.testng.Assert.assertNotNull;
64 import static org.testng.Assert.assertSame;
65 import static org.testng.Assert.assertTrue;
66 
67 import java.io.ByteArrayInputStream;
68 import java.io.ByteArrayOutputStream;
69 import java.io.ObjectInputStream;
70 import java.io.ObjectOutputStream;
71 import java.time.ZoneId;
72 import java.time.Clock;
73 import java.time.DateTimeException;
74 import java.time.chrono.ChronoLocalDate;
75 import java.time.chrono.Chronology;
76 import java.time.chrono.HijrahChronology;
77 import java.time.chrono.HijrahEra;
78 import java.time.chrono.IsoChronology;
79 import java.time.chrono.IsoEra;
80 import java.time.chrono.JapaneseChronology;
81 import java.time.chrono.JapaneseEra;
82 import java.time.chrono.MinguoChronology;
83 import java.time.chrono.MinguoEra;
84 import java.time.chrono.ThaiBuddhistChronology;
85 import java.time.chrono.ThaiBuddhistEra;
86 import java.time.format.TextStyle;
87 import java.time.temporal.ChronoField;
88 import java.util.Locale;
89 import java.util.Set;
90 
91 import org.testng.annotations.DataProvider;
92 import org.testng.annotations.Test;
93 
94 /**
95  * Test Chronology class.
96  */
97 @Test
98 public class TCKChronology {
99 
100     //-----------------------------------------------------------------------
101     // regular data factory for ID and calendarType of available calendars
102     //-----------------------------------------------------------------------
103     @DataProvider(name = "calendarNameAndType")
data_of_calendars()104     Object[][] data_of_calendars() {
105         return new Object[][] {
106                     {"Hijrah-umalqura", "islamic-umalqura"},
107                     {"ISO", "iso8601"},
108                     {"Japanese", "japanese"},
109                     {"Minguo", "roc"},
110                     {"ThaiBuddhist", "buddhist"},
111                 };
112     }
113 
114     @Test(dataProvider = "calendarNameAndType")
test_getters(String chronoId, String calendarSystemType)115     public void test_getters(String chronoId, String calendarSystemType) {
116         Chronology chrono = Chronology.of(chronoId);
117         assertNotNull(chrono, "Required calendar not found by ID: " + chronoId);
118         assertEquals(chrono.getId(), chronoId);
119         assertEquals(chrono.getCalendarType(), calendarSystemType);
120     }
121 
122     @Test(dataProvider = "calendarNameAndType")
test_required_calendars(String chronoId, String calendarSystemType)123     public void test_required_calendars(String chronoId, String calendarSystemType) {
124         Chronology chrono = Chronology.of(chronoId);
125         assertNotNull(chrono, "Required calendar not found by ID: " + chronoId);
126         chrono = Chronology.of(calendarSystemType);
127         assertNotNull(chrono, "Required calendar not found by type: " + chronoId);
128         Set<Chronology> cals = Chronology.getAvailableChronologies();
129         assertTrue(cals.contains(chrono), "Required calendar not found in set of available calendars");
130     }
131 
132     @Test
test_calendar_list()133     public void test_calendar_list() {
134         Set<Chronology> chronos = Chronology.getAvailableChronologies();
135         assertNotNull(chronos, "Required list of calendars must be non-null");
136         for (Chronology chrono : chronos) {
137             Chronology lookup = Chronology.of(chrono.getId());
138             assertNotNull(lookup, "Required calendar not found: " + chrono);
139         }
140         assertEquals(chronos.size() >= data_of_calendars().length, true, "Chronology.getAvailableChronologies().size = " + chronos.size()
141                 + ", expected >= " + data_of_calendars().length);
142     }
143 
144     //-----------------------------------------------------------------------
145     // getDisplayName()
146     //-----------------------------------------------------------------------
147     @DataProvider(name = "calendarDisplayName")
data_of_calendarDisplayNames()148     Object[][] data_of_calendarDisplayNames() {
149         return new Object[][] {
150                     {"Hijrah", "Islamic Umm al-Qura Calendar"},
151                     {"ISO", "ISO"},
152                     {"Japanese", "Japanese Calendar"},
153                     {"Minguo", "Minguo Calendar"},
154                     {"ThaiBuddhist", "Buddhist Calendar"},
155                 };
156     }
157 
158     @Test(dataProvider = "calendarDisplayName")
test_getDisplayName(String chronoId, String calendarDisplayName)159     public void test_getDisplayName(String chronoId, String calendarDisplayName) {
160         Chronology chrono = Chronology.of(chronoId);
161         assertEquals(chrono.getDisplayName(TextStyle.FULL, Locale.ENGLISH), calendarDisplayName);
162     }
163 
164     /**
165      * Compute the number of days from the Epoch and compute the date from the number of days.
166      */
167     @Test(dataProvider = "calendarNameAndType")
test_epoch(String name, String alias)168     public void test_epoch(String name, String alias) {
169         Chronology chrono = Chronology.of(name); // a chronology. In practice this is rarely hardcoded
170         ChronoLocalDate date1 = chrono.dateNow();
171         long epoch1 = date1.getLong(ChronoField.EPOCH_DAY);
172         ChronoLocalDate date2 = date1.with(ChronoField.EPOCH_DAY, epoch1);
173         assertEquals(date1, date2, "Date from epoch day is not same date: " + date1 + " != " + date2);
174         long epoch2 = date1.getLong(ChronoField.EPOCH_DAY);
175         assertEquals(epoch1, epoch2, "Epoch day not the same: " + epoch1 + " != " + epoch2);
176     }
177 
178     @Test(dataProvider = "calendarNameAndType")
test_dateEpochDay(String name, String alias)179     public void test_dateEpochDay(String name, String alias) {
180         Chronology chrono = Chronology.of(name);
181         ChronoLocalDate date = chrono.dateNow();
182         long epochDay = date.getLong(ChronoField.EPOCH_DAY);
183         ChronoLocalDate test = chrono.dateEpochDay(epochDay);
184         assertEquals(test, date);
185     }
186 
187     //-----------------------------------------------------------------------
188     // locale based lookup
189     //-----------------------------------------------------------------------
190     @DataProvider(name = "calendarsystemtype")
data_CalendarType()191     Object[][] data_CalendarType() {
192         return new Object[][] {
193             {HijrahChronology.INSTANCE, "islamic-umalqura"},
194             {IsoChronology.INSTANCE, "iso8601"},
195             {JapaneseChronology.INSTANCE, "japanese"},
196             {MinguoChronology.INSTANCE, "roc"},
197             {ThaiBuddhistChronology.INSTANCE, "buddhist"},
198         };
199     }
200 
201     @Test(dataProvider = "calendarsystemtype")
test_getCalendarType(Chronology chrono, String calendarType)202     public void test_getCalendarType(Chronology chrono, String calendarType) {
203         String type = calendarType;
204         assertEquals(chrono.getCalendarType(), type);
205     }
206 
207     @Test(dataProvider = "calendarsystemtype")
test_lookupLocale(Chronology chrono, String calendarType)208     public void test_lookupLocale(Chronology chrono, String calendarType) {
209         Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
210         builder.setUnicodeLocaleKeyword("ca", calendarType);
211         Locale locale = builder.build();
212         assertEquals(Chronology.ofLocale(locale), chrono);
213     }
214 
215     //-----------------------------------------------------------------------
216     // dateNow()
217     //-----------------------------------------------------------------------
218     @Test
test_MinguoChronology_dateNow()219     public void test_MinguoChronology_dateNow() {
220         ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
221         Clock clock = Clock.system(zoneId_paris);
222 
223         Chronology chrono = Chronology.of("Minguo");
224         assertEquals(chrono.dateNow(), MinguoChronology.INSTANCE.dateNow());
225         assertEquals(chrono.dateNow(zoneId_paris), MinguoChronology.INSTANCE.dateNow(zoneId_paris));
226         assertEquals(chrono.dateNow(clock), MinguoChronology.INSTANCE.dateNow(clock));
227     }
228 
229     @Test
test_IsoChronology_dateNow()230     public void test_IsoChronology_dateNow() {
231         ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
232         Clock clock = Clock.system(zoneId_paris);
233 
234         Chronology chrono = Chronology.of("ISO");
235         assertEquals(chrono.dateNow(), IsoChronology.INSTANCE.dateNow());
236         assertEquals(chrono.dateNow(zoneId_paris), IsoChronology.INSTANCE.dateNow(zoneId_paris));
237         assertEquals(chrono.dateNow(clock), IsoChronology.INSTANCE.dateNow(clock));
238     }
239 
240     @Test
test_JapaneseChronology_dateNow()241     public void test_JapaneseChronology_dateNow() {
242         ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
243         Clock clock = Clock.system(zoneId_paris);
244 
245         Chronology chrono = Chronology.of("Japanese");
246         assertEquals(chrono.dateNow(), JapaneseChronology.INSTANCE.dateNow());
247         assertEquals(chrono.dateNow(zoneId_paris), JapaneseChronology.INSTANCE.dateNow(zoneId_paris));
248         assertEquals(chrono.dateNow(clock), JapaneseChronology.INSTANCE.dateNow(clock));
249     }
250 
251     @Test
test_ThaiBuddhistChronology_dateNow()252     public void test_ThaiBuddhistChronology_dateNow() {
253         ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
254         Clock clock = Clock.system(zoneId_paris);
255 
256         Chronology chrono = Chronology.of("ThaiBuddhist");
257         assertEquals(chrono.dateNow(), ThaiBuddhistChronology.INSTANCE.dateNow());
258         assertEquals(chrono.dateNow(zoneId_paris), ThaiBuddhistChronology.INSTANCE.dateNow(zoneId_paris));
259         assertEquals(chrono.dateNow(clock), ThaiBuddhistChronology.INSTANCE.dateNow(clock));
260     }
261 
262     //-----------------------------------------------------------------------
263     // dateYearDay() and date()
264     //-----------------------------------------------------------------------
265     @Test
test_HijrahChronology_dateYearDay()266     public void test_HijrahChronology_dateYearDay() {
267         Chronology chrono = Chronology.of("Hijrah");
268         ChronoLocalDate date1 = chrono.dateYearDay(HijrahEra.AH, 1434, 178);
269         ChronoLocalDate date2 = chrono.date(HijrahEra.AH, 1434, 7, 1);
270         assertEquals(date1, HijrahChronology.INSTANCE.dateYearDay(HijrahEra.AH, 1434, 178));
271         assertEquals(date2, HijrahChronology.INSTANCE.dateYearDay(HijrahEra.AH, 1434, 178));
272     }
273 
274     @Test
test_MinguoChronology_dateYearDay()275     public void test_MinguoChronology_dateYearDay() {
276         Chronology chrono = Chronology.of("Minguo");
277         ChronoLocalDate date1 = chrono.dateYearDay(MinguoEra.ROC, 5, 60);
278         ChronoLocalDate date2 = chrono.date(MinguoEra.ROC, 5, 2, 29);
279         assertEquals(date1, MinguoChronology.INSTANCE.dateYearDay(MinguoEra.ROC, 5, 60));
280         assertEquals(date2, MinguoChronology.INSTANCE.dateYearDay(MinguoEra.ROC, 5, 60));
281     }
282 
283     @Test
test_IsoChronology_dateYearDay()284     public void test_IsoChronology_dateYearDay() {
285         Chronology chrono = Chronology.of("ISO");
286         ChronoLocalDate date1 = chrono.dateYearDay(IsoEra.CE, 5, 60);
287         ChronoLocalDate date2 = chrono.date(IsoEra.CE, 5, 3, 1);
288         assertEquals(date1, IsoChronology.INSTANCE.dateYearDay(IsoEra.CE, 5, 60));
289         assertEquals(date2, IsoChronology.INSTANCE.dateYearDay(IsoEra.CE, 5, 60));
290     }
291 
292     @Test
test_JapaneseChronology_dateYearDay()293     public void test_JapaneseChronology_dateYearDay() {
294         Chronology chrono = Chronology.of("Japanese");
295         ChronoLocalDate date1 = chrono.dateYearDay(JapaneseEra.HEISEI, 8, 60);
296         ChronoLocalDate date2 = chrono.date(JapaneseEra.HEISEI, 8, 2, 29);
297         assertEquals(date1, JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.HEISEI, 8, 60));
298         assertEquals(date2, JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.HEISEI, 8, 60));
299     }
300 
301     @Test
test_ThaiBuddhistChronology_dateYearDay()302     public void test_ThaiBuddhistChronology_dateYearDay() {
303         Chronology chrono = Chronology.of("ThaiBuddhist");
304         ChronoLocalDate date1 = chrono.dateYearDay(ThaiBuddhistEra.BE, 2459, 60);
305         ChronoLocalDate date2 = chrono.date(ThaiBuddhistEra.BE, 2459, 2, 29);
306         assertEquals(date1, ThaiBuddhistChronology.INSTANCE.dateYearDay(ThaiBuddhistEra.BE, 2459, 60));
307         assertEquals(date2, ThaiBuddhistChronology.INSTANCE.dateYearDay(ThaiBuddhistEra.BE, 2459, 60));
308     }
309 
310     /**
311      * Test lookup by calendarType of each chronology.
312      * Verify that the calendar can be found by {@link java.time.chrono.Chronology#ofLocale}.
313      */
314     @Test
test_ofLocaleByType()315     public void test_ofLocaleByType() {
316         // Test that all available chronologies can be successfully found using ofLocale
317         Set<Chronology> chronos = Chronology.getAvailableChronologies();
318         for (Chronology chrono : chronos) {
319             Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
320             builder.setUnicodeLocaleKeyword("ca", chrono.getCalendarType());
321             Locale locale = builder.build();
322             assertEquals(Chronology.ofLocale(locale), chrono, "Lookup by type");
323         }
324     }
325 
326     @Test(expectedExceptions=DateTimeException.class)
test_lookupLocale()327     public void test_lookupLocale() {
328         Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
329         builder.setUnicodeLocaleKeyword("ca", "xxx");
330 
331         Locale locale = builder.build();
332         Chronology.ofLocale(locale);
333     }
334 
335     @Test(expectedExceptions = DateTimeException.class)
test_noChrono()336     public void test_noChrono() {
337         Chronology chrono = Chronology.of("FooFoo");
338     }
339 
340 }
341