• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.chrono;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertNotNull;
36 import static org.testng.Assert.assertSame;
37 import static org.testng.Assert.assertTrue;
38 
39 import java.io.ByteArrayInputStream;
40 import java.io.ByteArrayOutputStream;
41 import java.io.ObjectInputStream;
42 import java.io.ObjectOutputStream;
43 import java.util.Locale;
44 import java.util.Set;
45 
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeMethod;
48 import org.testng.annotations.DataProvider;
49 import org.testng.annotations.Test;
50 import org.threeten.bp.chrono.Chronology;
51 import org.threeten.bp.chrono.ChronoLocalDate;
52 import org.threeten.bp.chrono.HijrahChronology;
53 import org.threeten.bp.chrono.IsoChronology;
54 import org.threeten.bp.chrono.JapaneseChronology;
55 import org.threeten.bp.chrono.MinguoChronology;
56 import org.threeten.bp.chrono.ThaiBuddhistChronology;
57 import org.threeten.bp.temporal.ChronoField;
58 
59 /**
60  * Test Chrono class.
61  */
62 @Test
63 public class TestChronology {
64 
65     @BeforeMethod
setUp()66     public void setUp() {
67         // Ensure each of the classes are initialized (until initialization is fixed)
68         Chronology c;
69         c = HijrahChronology.INSTANCE;
70         c = IsoChronology.INSTANCE;
71         c = JapaneseChronology.INSTANCE;
72         c = MinguoChronology.INSTANCE;
73         c = ThaiBuddhistChronology.INSTANCE;
74         c.toString();  // avoids variable being marked as unused
75     }
76 
77     //-----------------------------------------------------------------------
78     // regular data factory for names and descriptions of available calendars
79     //-----------------------------------------------------------------------
80     @DataProvider(name = "calendars")
data_of_calendars()81     Object[][] data_of_calendars() {
82         return new Object[][] {
83                     {"Hijrah-umalqura", "islamic-umalqura", "Hijrah calendar"},
84                     {"ISO", "iso8601", "ISO calendar"},
85                     {"Japanese", "japanese", "Japanese calendar"},
86                     {"Minguo", "roc", "Minguo Calendar"},
87                     {"ThaiBuddhist", "buddhist", "ThaiBuddhist calendar"},
88                 };
89     }
90 
91     @Test(dataProvider = "calendars")
test_getters(String chronoId, String calendarSystemType, String description)92     public void test_getters(String chronoId, String calendarSystemType, String description) {
93         Chronology chrono = Chronology.of(chronoId);
94         assertNotNull(chrono, "Required calendar not found by ID: " + chronoId);
95         assertEquals(chrono.getId(), chronoId);
96         assertEquals(chrono.getCalendarType(), calendarSystemType);
97     }
98 
99     @Test(dataProvider = "calendars")
test_required_calendars(String chronoId, String calendarSystemType, String description)100     public void test_required_calendars(String chronoId, String calendarSystemType, String description) {
101         Chronology chrono = Chronology.of(chronoId);
102         assertNotNull(chrono, "Required calendar not found by ID: " + chronoId);
103         chrono = Chronology.of(calendarSystemType);
104         assertNotNull(chrono, "Required calendar not found by type: " + chronoId);
105         Set<Chronology> cals = Chronology.getAvailableChronologies();
106         assertTrue(cals.contains(chrono), "Required calendar not found in set of available calendars");
107     }
108 
109     @Test
test_calendar_list()110     public void test_calendar_list() {
111         Set<Chronology> chronos = Chronology.getAvailableChronologies();
112         assertNotNull(chronos, "Required list of calendars must be non-null");
113         for (Chronology chrono : chronos) {
114             Chronology lookup = Chronology.of(chrono.getId());
115             assertNotNull(lookup, "Required calendar not found: " + chrono);
116         }
117         assertEquals(chronos.size() >= data_of_calendars().length, true, "Required list of calendars too short");
118     }
119 
120     /**
121      * Compute the number of days from the Epoch and compute the date from the number of days.
122      */
123     @Test(dataProvider = "calendars")
test_epoch(String name, String alias, String description)124     public void test_epoch(String name, String alias, String description) {
125         Chronology chrono = Chronology.of(name); // a chronology. In practice this is rarely hardcoded
126         ChronoLocalDate date1 = chrono.dateNow();
127         long epoch1 = date1.getLong(ChronoField.EPOCH_DAY);
128         ChronoLocalDate date2 = date1.with(ChronoField.EPOCH_DAY, epoch1);
129         assertEquals(date1, date2, "Date from epoch day is not same date: " + date1 + " != " + date2);
130         long epoch2 = date1.getLong(ChronoField.EPOCH_DAY);
131         assertEquals(epoch1, epoch2, "Epoch day not the same: " + epoch1 + " != " + epoch2);
132     }
133 
134     //-----------------------------------------------------------------------
135     // locale based lookup
136     //-----------------------------------------------------------------------
137     @DataProvider(name = "calendarsystemtype")
data_CalendarType()138     Object[][] data_CalendarType() {
139         return new Object[][] {
140             {HijrahChronology.INSTANCE, "islamic-umalqura"},
141             {IsoChronology.INSTANCE, "iso8601"},
142             {JapaneseChronology.INSTANCE, "japanese"},
143             {MinguoChronology.INSTANCE, "roc"},
144             {ThaiBuddhistChronology.INSTANCE, "buddhist"},
145         };
146     }
147 
148     @Test(dataProvider = "calendarsystemtype")
test_getCalendarType(Chronology chrono, String calendarType)149     public void test_getCalendarType(Chronology chrono, String calendarType) {
150         assertEquals(chrono.getCalendarType(), calendarType);
151     }
152 
153 //    @Test(dataProvider = "calendarsystemtype")
154 //    public void test_lookupLocale(Chronology chrono, String calendarType) {
155 //        Locale locale = new Locale.Builder().setLanguage("en").setRegion("CA").setUnicodeLocaleKeyword("ca", calendarType).build();
156 //        assertEquals(Chronology.ofLocale(locale), chrono);
157 //    }
158 
159     @Test
test_lookupLocale_jp_JP()160     public void test_lookupLocale_jp_JP() {
161         Chronology test = Chronology.ofLocale(new Locale("ja", "JP"));
162         Assert.assertEquals(test.getId(), "ISO");
163         Assert.assertEquals(test, IsoChronology.INSTANCE);
164     }
165 
166     @Test
test_lookupLocale_jp_JP_JP()167     public void test_lookupLocale_jp_JP_JP() {
168         Chronology test = Chronology.ofLocale(new Locale("ja", "JP", "JP"));
169         Assert.assertEquals(test.getId(), "Japanese");
170         Assert.assertEquals(test, JapaneseChronology.INSTANCE);
171     }
172 
173     //-----------------------------------------------------------------------
174     // serialization; serialize and check each calendar system
175     //-----------------------------------------------------------------------
176     @Test(dataProvider = "calendarsystemtype")
test_chronoSerializationSingleton(Chronology chrono, String calendarType)177     public void test_chronoSerializationSingleton(Chronology chrono, String calendarType) throws Exception {
178         Chronology orginal = chrono;
179         ByteArrayOutputStream baos = new ByteArrayOutputStream();
180         ObjectOutputStream out = new ObjectOutputStream(baos);
181         out.writeObject(orginal);
182         out.close();
183         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
184         ObjectInputStream in = new ObjectInputStream(bais);
185         Chronology ser = (Chronology) in.readObject();
186         assertSame(ser, chrono, "Deserialized Chrono is not the singleton serialized");
187     }
188 
189 }
190