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; 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.util.Locale; 72 import java.util.Set; 73 74 import java.time.chrono.Chronology; 75 import java.time.temporal.ChronoField; 76 import java.time.chrono.HijrahChronology; 77 import java.time.chrono.JapaneseChronology; 78 import java.time.chrono.MinguoChronology; 79 import java.time.chrono.ThaiBuddhistChronology; 80 import java.time.chrono.ChronoLocalDate; 81 import java.time.chrono.IsoChronology; 82 83 import org.testng.annotations.DataProvider; 84 import org.testng.annotations.Test; 85 86 /** 87 * Test Chronology class. 88 */ 89 @Test 90 public class TestChronology { 91 92 //----------------------------------------------------------------------- 93 // regular data factory for names and descriptions of available calendars 94 //----------------------------------------------------------------------- 95 @DataProvider(name = "calendars") 96 Object[][] data_of_calendars() { 97 return new Object[][] { 98 {"Hijrah", "islamicc", "Hijrah calendar"}, 99 {"ISO", "iso8601", "ISO calendar"}, 100 {"Japanese", "japanese", "Japanese calendar"}, 101 {"Minguo", "roc", "Minguo Calendar"}, 102 {"ThaiBuddhist", "buddhist", "ThaiBuddhist calendar"}, 103 }; 104 } 105 106 @Test(dataProvider = "calendars") 107 public void test_getters(String chronoId, String calendarSystemType, String description) { 108 Chronology chrono = Chronology.of(chronoId); 109 assertNotNull(chrono, "Required calendar not found by ID: " + chronoId); 110 assertEquals(chrono.getId(), chronoId); 111 assertEquals(chrono.getCalendarType(), calendarSystemType); 112 } 113 114 @Test(dataProvider = "calendars") 115 public void test_required_calendars(String chronoId, String calendarSystemType, String description) { 116 Chronology chrono = Chronology.of(chronoId); 117 assertNotNull(chrono, "Required calendar not found by ID: " + chronoId); 118 chrono = Chronology.of(calendarSystemType); 119 assertNotNull(chrono, "Required calendar not found by type: " + chronoId); 120 Set<Chronology> cals = Chronology.getAvailableChronologies(); 121 assertTrue(cals.contains(chrono), "Required calendar not found in set of available calendars"); 122 } 123 124 @Test(groups="tck") 125 public void test_calendar_list() { 126 Set<Chronology> chronos = Chronology.getAvailableChronologies(); 127 assertNotNull(chronos, "Required list of calendars must be non-null"); 128 for (Chronology chrono : chronos) { 129 Chronology lookup = Chronology.of(chrono.getId()); 130 assertNotNull(lookup, "Required calendar not found: " + chrono); 131 } 132 assertEquals(chronos.size() >= data_of_calendars().length, true, "Chronology.getAvailableChronologies().size = " + chronos.size() 133 + ", expected >= " + data_of_calendars().length); 134 } 135 136 /** 137 * Compute the number of days from the Epoch and compute the date from the number of days. 138 */ 139 @Test(dataProvider = "calendars", groups="tck") 140 public void test_epoch(String name, String alias, String description) { 141 Chronology chrono = Chronology.of(name); // a chronology. In practice this is rarely hardcoded 142 ChronoLocalDate<?> date1 = chrono.dateNow(); 143 long epoch1 = date1.getLong(ChronoField.EPOCH_DAY); 144 ChronoLocalDate<?> date2 = date1.with(ChronoField.EPOCH_DAY, epoch1); 145 assertEquals(date1, date2, "Date from epoch day is not same date: " + date1 + " != " + date2); 146 long epoch2 = date1.getLong(ChronoField.EPOCH_DAY); 147 assertEquals(epoch1, epoch2, "Epoch day not the same: " + epoch1 + " != " + epoch2); 148 } 149 150 //----------------------------------------------------------------------- 151 // locale based lookup 152 //----------------------------------------------------------------------- 153 @DataProvider(name = "calendarsystemtype") 154 Object[][] data_CalendarType() { 155 return new Object[][] { 156 {HijrahChronology.INSTANCE, "islamicc"}, 157 {IsoChronology.INSTANCE, "iso8601"}, 158 {JapaneseChronology.INSTANCE, "japanese"}, 159 {MinguoChronology.INSTANCE, "roc"}, 160 {ThaiBuddhistChronology.INSTANCE, "buddhist"}, 161 }; 162 } 163 164 @Test(dataProvider = "calendarsystemtype", groups="tck") 165 public void test_getCalendarType(Chronology chrono, String calendarType) { 166 assertEquals(chrono.getCalendarType(), calendarType); 167 } 168 169 @Test(dataProvider = "calendarsystemtype", groups="tck") 170 public void test_lookupLocale(Chronology chrono, String calendarType) { 171 Locale locale = new Locale.Builder().setLanguage("en").setRegion("CA").setUnicodeLocaleKeyword("ca", calendarType).build(); 172 assertEquals(Chronology.ofLocale(locale), chrono); 173 } 174 175 176 //----------------------------------------------------------------------- 177 // serialization; serialize and check each calendar system 178 //----------------------------------------------------------------------- 179 @Test(groups={"tck","implementation"}, dataProvider = "calendarsystemtype") 180 public void test_chronoSerializationSingleton(Chronology chrono, String calendarType) throws Exception { 181 Chronology orginal = chrono; 182 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 183 ObjectOutputStream out = new ObjectOutputStream(baos); 184 out.writeObject(orginal); 185 out.close(); 186 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 187 ObjectInputStream in = new ObjectInputStream(bais); 188 Chronology ser = (Chronology) in.readObject(); 189 assertSame(ser, chrono, "Deserialized Chronology is not the singleton serialized"); 190 } 191 192 }