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 // Android-changed: Change expected values to CLDR values here. This test seems to be based 150 // on an old CLDR version (21) with some "invented" values (specifically Hijrah and ISO). 151 return new Object[][] { 152 {"Hijrah", "Islamic Calendar (Umm al-Qura)"}, 153 {"ISO", "ISO-8601 Calendar"}, 154 {"Japanese", "Japanese Calendar"}, 155 {"Minguo", "Minguo Calendar"}, 156 {"ThaiBuddhist", "Buddhist Calendar"}, 157 }; 158 } 159 160 @Test(dataProvider = "calendarDisplayName") test_getDisplayName(String chronoId, String calendarDisplayName)161 public void test_getDisplayName(String chronoId, String calendarDisplayName) { 162 Chronology chrono = Chronology.of(chronoId); 163 assertEquals(chrono.getDisplayName(TextStyle.FULL, Locale.ENGLISH), calendarDisplayName); 164 } 165 166 /** 167 * Compute the number of days from the Epoch and compute the date from the number of days. 168 */ 169 @Test(dataProvider = "calendarNameAndType") test_epoch(String name, String alias)170 public void test_epoch(String name, String alias) { 171 Chronology chrono = Chronology.of(name); // a chronology. In practice this is rarely hardcoded 172 ChronoLocalDate date1 = chrono.dateNow(); 173 long epoch1 = date1.getLong(ChronoField.EPOCH_DAY); 174 ChronoLocalDate date2 = date1.with(ChronoField.EPOCH_DAY, epoch1); 175 assertEquals(date1, date2, "Date from epoch day is not same date: " + date1 + " != " + date2); 176 long epoch2 = date1.getLong(ChronoField.EPOCH_DAY); 177 assertEquals(epoch1, epoch2, "Epoch day not the same: " + epoch1 + " != " + epoch2); 178 } 179 180 @Test(dataProvider = "calendarNameAndType") test_dateEpochDay(String name, String alias)181 public void test_dateEpochDay(String name, String alias) { 182 Chronology chrono = Chronology.of(name); 183 ChronoLocalDate date = chrono.dateNow(); 184 long epochDay = date.getLong(ChronoField.EPOCH_DAY); 185 ChronoLocalDate test = chrono.dateEpochDay(epochDay); 186 assertEquals(test, date); 187 } 188 189 //----------------------------------------------------------------------- 190 // locale based lookup 191 //----------------------------------------------------------------------- 192 @DataProvider(name = "calendarsystemtype") data_CalendarType()193 Object[][] data_CalendarType() { 194 return new Object[][] { 195 {HijrahChronology.INSTANCE, "islamic-umalqura"}, 196 {IsoChronology.INSTANCE, "iso8601"}, 197 {JapaneseChronology.INSTANCE, "japanese"}, 198 {MinguoChronology.INSTANCE, "roc"}, 199 {ThaiBuddhistChronology.INSTANCE, "buddhist"}, 200 }; 201 } 202 203 @Test(dataProvider = "calendarsystemtype") test_getCalendarType(Chronology chrono, String calendarType)204 public void test_getCalendarType(Chronology chrono, String calendarType) { 205 String type = calendarType; 206 assertEquals(chrono.getCalendarType(), type); 207 } 208 209 @Test(dataProvider = "calendarsystemtype") test_lookupLocale(Chronology chrono, String calendarType)210 public void test_lookupLocale(Chronology chrono, String calendarType) { 211 Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA"); 212 builder.setUnicodeLocaleKeyword("ca", calendarType); 213 Locale locale = builder.build(); 214 assertEquals(Chronology.ofLocale(locale), chrono); 215 } 216 217 //----------------------------------------------------------------------- 218 // dateNow() 219 //----------------------------------------------------------------------- 220 @Test test_MinguoChronology_dateNow()221 public void test_MinguoChronology_dateNow() { 222 ZoneId zoneId_paris = ZoneId.of("Europe/Paris"); 223 Clock clock = Clock.system(zoneId_paris); 224 225 Chronology chrono = Chronology.of("Minguo"); 226 assertEquals(chrono.dateNow(), MinguoChronology.INSTANCE.dateNow()); 227 assertEquals(chrono.dateNow(zoneId_paris), MinguoChronology.INSTANCE.dateNow(zoneId_paris)); 228 assertEquals(chrono.dateNow(clock), MinguoChronology.INSTANCE.dateNow(clock)); 229 } 230 231 @Test test_IsoChronology_dateNow()232 public void test_IsoChronology_dateNow() { 233 ZoneId zoneId_paris = ZoneId.of("Europe/Paris"); 234 Clock clock = Clock.system(zoneId_paris); 235 236 Chronology chrono = Chronology.of("ISO"); 237 assertEquals(chrono.dateNow(), IsoChronology.INSTANCE.dateNow()); 238 assertEquals(chrono.dateNow(zoneId_paris), IsoChronology.INSTANCE.dateNow(zoneId_paris)); 239 assertEquals(chrono.dateNow(clock), IsoChronology.INSTANCE.dateNow(clock)); 240 } 241 242 @Test test_JapaneseChronology_dateNow()243 public void test_JapaneseChronology_dateNow() { 244 ZoneId zoneId_paris = ZoneId.of("Europe/Paris"); 245 Clock clock = Clock.system(zoneId_paris); 246 247 Chronology chrono = Chronology.of("Japanese"); 248 assertEquals(chrono.dateNow(), JapaneseChronology.INSTANCE.dateNow()); 249 assertEquals(chrono.dateNow(zoneId_paris), JapaneseChronology.INSTANCE.dateNow(zoneId_paris)); 250 assertEquals(chrono.dateNow(clock), JapaneseChronology.INSTANCE.dateNow(clock)); 251 } 252 253 @Test test_ThaiBuddhistChronology_dateNow()254 public void test_ThaiBuddhistChronology_dateNow() { 255 ZoneId zoneId_paris = ZoneId.of("Europe/Paris"); 256 Clock clock = Clock.system(zoneId_paris); 257 258 Chronology chrono = Chronology.of("ThaiBuddhist"); 259 assertEquals(chrono.dateNow(), ThaiBuddhistChronology.INSTANCE.dateNow()); 260 assertEquals(chrono.dateNow(zoneId_paris), ThaiBuddhistChronology.INSTANCE.dateNow(zoneId_paris)); 261 assertEquals(chrono.dateNow(clock), ThaiBuddhistChronology.INSTANCE.dateNow(clock)); 262 } 263 264 //----------------------------------------------------------------------- 265 // dateYearDay() and date() 266 //----------------------------------------------------------------------- 267 @Test test_HijrahChronology_dateYearDay()268 public void test_HijrahChronology_dateYearDay() { 269 Chronology chrono = Chronology.of("Hijrah"); 270 ChronoLocalDate date1 = chrono.dateYearDay(HijrahEra.AH, 1434, 178); 271 ChronoLocalDate date2 = chrono.date(HijrahEra.AH, 1434, 7, 1); 272 assertEquals(date1, HijrahChronology.INSTANCE.dateYearDay(HijrahEra.AH, 1434, 178)); 273 assertEquals(date2, HijrahChronology.INSTANCE.dateYearDay(HijrahEra.AH, 1434, 178)); 274 } 275 276 @Test test_MinguoChronology_dateYearDay()277 public void test_MinguoChronology_dateYearDay() { 278 Chronology chrono = Chronology.of("Minguo"); 279 ChronoLocalDate date1 = chrono.dateYearDay(MinguoEra.ROC, 5, 60); 280 ChronoLocalDate date2 = chrono.date(MinguoEra.ROC, 5, 2, 29); 281 assertEquals(date1, MinguoChronology.INSTANCE.dateYearDay(MinguoEra.ROC, 5, 60)); 282 assertEquals(date2, MinguoChronology.INSTANCE.dateYearDay(MinguoEra.ROC, 5, 60)); 283 } 284 285 @Test test_IsoChronology_dateYearDay()286 public void test_IsoChronology_dateYearDay() { 287 Chronology chrono = Chronology.of("ISO"); 288 ChronoLocalDate date1 = chrono.dateYearDay(IsoEra.CE, 5, 60); 289 ChronoLocalDate date2 = chrono.date(IsoEra.CE, 5, 3, 1); 290 assertEquals(date1, IsoChronology.INSTANCE.dateYearDay(IsoEra.CE, 5, 60)); 291 assertEquals(date2, IsoChronology.INSTANCE.dateYearDay(IsoEra.CE, 5, 60)); 292 } 293 294 @Test test_JapaneseChronology_dateYearDay()295 public void test_JapaneseChronology_dateYearDay() { 296 Chronology chrono = Chronology.of("Japanese"); 297 ChronoLocalDate date1 = chrono.dateYearDay(JapaneseEra.HEISEI, 8, 60); 298 ChronoLocalDate date2 = chrono.date(JapaneseEra.HEISEI, 8, 2, 29); 299 assertEquals(date1, JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.HEISEI, 8, 60)); 300 assertEquals(date2, JapaneseChronology.INSTANCE.dateYearDay(JapaneseEra.HEISEI, 8, 60)); 301 } 302 303 @Test test_ThaiBuddhistChronology_dateYearDay()304 public void test_ThaiBuddhistChronology_dateYearDay() { 305 Chronology chrono = Chronology.of("ThaiBuddhist"); 306 ChronoLocalDate date1 = chrono.dateYearDay(ThaiBuddhistEra.BE, 2459, 60); 307 ChronoLocalDate date2 = chrono.date(ThaiBuddhistEra.BE, 2459, 2, 29); 308 assertEquals(date1, ThaiBuddhistChronology.INSTANCE.dateYearDay(ThaiBuddhistEra.BE, 2459, 60)); 309 assertEquals(date2, ThaiBuddhistChronology.INSTANCE.dateYearDay(ThaiBuddhistEra.BE, 2459, 60)); 310 } 311 312 /** 313 * Test lookup by calendarType of each chronology. 314 * Verify that the calendar can be found by {@link java.time.chrono.Chronology#ofLocale}. 315 */ 316 @Test test_ofLocaleByType()317 public void test_ofLocaleByType() { 318 // Test that all available chronologies can be successfully found using ofLocale 319 Set<Chronology> chronos = Chronology.getAvailableChronologies(); 320 for (Chronology chrono : chronos) { 321 Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA"); 322 builder.setUnicodeLocaleKeyword("ca", chrono.getCalendarType()); 323 Locale locale = builder.build(); 324 assertEquals(Chronology.ofLocale(locale), chrono, "Lookup by type"); 325 } 326 } 327 328 @Test(expectedExceptions=DateTimeException.class) test_lookupLocale()329 public void test_lookupLocale() { 330 Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA"); 331 builder.setUnicodeLocaleKeyword("ca", "xxx"); 332 333 Locale locale = builder.build(); 334 Chronology.ofLocale(locale); 335 } 336 337 @Test(expectedExceptions = DateTimeException.class) test_noChrono()338 public void test_noChrono() { 339 Chronology chrono = Chronology.of("FooFoo"); 340 } 341 342 } 343