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. 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 /* 27 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 28 * 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions are met: 33 * 34 * * Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 37 * * Redistributions in binary form must reproduce the above copyright notice, 38 * this list of conditions and the following disclaimer in the documentation 39 * and/or other materials provided with the distribution. 40 * 41 * * Neither the name of JSR-310 nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 package tck.java.time.chrono; 58 59 import static org.testng.Assert.assertEquals; 60 61 import java.time.LocalDate; 62 import java.time.LocalDateTime; 63 import java.time.LocalTime; 64 import java.time.ZoneId; 65 import java.time.ZonedDateTime; 66 import java.time.chrono.ChronoLocalDate; 67 import java.time.chrono.Chronology; 68 import java.time.chrono.Era; 69 import java.time.chrono.IsoChronology; 70 import java.time.temporal.ChronoField; 71 import java.time.temporal.Queries; 72 import java.time.temporal.TemporalAccessor; 73 import java.time.temporal.TemporalField; 74 import java.time.temporal.TemporalQuery; 75 import java.time.temporal.ValueRange; 76 import java.util.List; 77 78 import org.testng.annotations.Test; 79 80 /** 81 * Test. 82 */ 83 @Test 84 public class TCKChronology { 85 // Can only work with IsoChronology here 86 // others may be in separate module 87 88 @Test 89 public void factory_from_TemporalAccessor_dateWithChronlogy() { 90 assertEquals(Chronology.from(LocalDate.of(2012, 6, 30)), IsoChronology.INSTANCE); 91 } 92 93 @Test 94 public void factory_from_TemporalAccessor_chronology() { 95 assertEquals(Chronology.from(new TemporalAccessor() { 96 @Override 97 public boolean isSupported(TemporalField field) { 98 throw new UnsupportedOperationException(); 99 } 100 @Override 101 public long getLong(TemporalField field) { 102 throw new UnsupportedOperationException(); 103 } 104 @SuppressWarnings("unchecked") 105 @Override 106 public <R> R query(TemporalQuery<R> query) { 107 if (query == Queries.chronology()) { 108 return (R) IsoChronology.INSTANCE; 109 } 110 throw new UnsupportedOperationException(); 111 } 112 }), IsoChronology.INSTANCE); 113 } 114 115 @Test 116 public void factory_from_TemporalAccessor_noChronology() { 117 assertEquals(Chronology.from(new TemporalAccessor() { 118 @Override 119 public boolean isSupported(TemporalField field) { 120 throw new UnsupportedOperationException(); 121 } 122 123 @Override 124 public long getLong(TemporalField field) { 125 throw new UnsupportedOperationException(); 126 } 127 128 @Override 129 public <R> R query(TemporalQuery<R> query) { 130 if (query == Queries.chronology()) { 131 return null; 132 } 133 throw new UnsupportedOperationException(); 134 } 135 }), IsoChronology.INSTANCE); 136 } 137 138 @Test(expectedExceptions=NullPointerException.class) 139 public void factory_from_TemporalAccessor_null() { 140 Chronology.from(null); 141 } 142 143 //----------------------------------------------------------------------- 144 @Test 145 public void test_date_TemporalAccessor() { 146 assertEquals(IsoChronology.INSTANCE.date(new TemporalAccessor() { 147 @Override 148 public boolean isSupported(TemporalField field) { 149 if (field == ChronoField.EPOCH_DAY) { 150 return true; 151 } 152 throw new UnsupportedOperationException(); 153 } 154 155 @Override 156 public long getLong(TemporalField field) { 157 if (field == ChronoField.EPOCH_DAY) { 158 return LocalDate.of(2012, 6, 30).toEpochDay(); 159 } 160 throw new UnsupportedOperationException(); 161 } 162 163 @SuppressWarnings("unchecked") 164 @Override 165 public <R> R query(TemporalQuery<R> query) { 166 if (query == Queries.localDate()) { 167 return (R) LocalDate.of(2012, 6, 30); 168 } 169 throw new UnsupportedOperationException(); 170 } 171 }), LocalDate.of(2012, 6, 30)); 172 } 173 174 @Test(expectedExceptions=NullPointerException.class) 175 public void test_date_TemporalAccessor_null() { 176 IsoChronology.INSTANCE.date(null); 177 } 178 179 //----------------------------------------------------------------------- 180 @Test 181 public void test_localDateTime_TemporalAccessor() { 182 assertEquals(IsoChronology.INSTANCE.localDateTime(new TemporalAccessor() { 183 @Override 184 public boolean isSupported(TemporalField field) { 185 if (field == ChronoField.EPOCH_DAY || field == ChronoField.NANO_OF_DAY) { 186 return true; 187 } 188 throw new UnsupportedOperationException(); 189 } 190 191 @Override 192 public long getLong(TemporalField field) { 193 if (field == ChronoField.EPOCH_DAY) { 194 return LocalDate.of(2012, 6, 30).toEpochDay(); 195 } 196 if (field == ChronoField.NANO_OF_DAY) { 197 return LocalTime.of(12, 30, 40).toNanoOfDay(); 198 } 199 throw new UnsupportedOperationException(); 200 } 201 202 @SuppressWarnings("unchecked") 203 @Override 204 public <R> R query(TemporalQuery<R> query) { 205 if (query == Queries.localDate()) { 206 return (R) LocalDate.of(2012, 6, 30); 207 } 208 if (query == Queries.localTime()) { 209 return (R) LocalTime.of(12, 30, 40); 210 } 211 throw new UnsupportedOperationException(); 212 } 213 }), LocalDateTime.of(2012, 6, 30, 12, 30, 40)); 214 } 215 216 @Test(expectedExceptions=NullPointerException.class) 217 public void test_localDateTime_TemporalAccessor_null() { 218 IsoChronology.INSTANCE.localDateTime(null); 219 } 220 221 //----------------------------------------------------------------------- 222 @Test 223 public void test_zonedDateTime_TemporalAccessor() { 224 assertEquals(IsoChronology.INSTANCE.zonedDateTime(new TemporalAccessor() { 225 @Override 226 public boolean isSupported(TemporalField field) { 227 if (field == ChronoField.EPOCH_DAY || field == ChronoField.NANO_OF_DAY || 228 field == ChronoField.INSTANT_SECONDS || field == ChronoField.NANO_OF_SECOND) { 229 return true; 230 } 231 throw new UnsupportedOperationException(); 232 } 233 234 @Override 235 public long getLong(TemporalField field) { 236 if (field == ChronoField.INSTANT_SECONDS) { 237 return ZonedDateTime.of(2012, 6, 30, 12, 30, 40, 0, ZoneId.of("Europe/London")).toEpochSecond(); 238 } 239 if (field == ChronoField.NANO_OF_SECOND) { 240 return 0; 241 } 242 if (field == ChronoField.EPOCH_DAY) { 243 return LocalDate.of(2012, 6, 30).toEpochDay(); 244 } 245 if (field == ChronoField.NANO_OF_DAY) { 246 return LocalTime.of(12, 30, 40).toNanoOfDay(); 247 } 248 throw new UnsupportedOperationException(); 249 } 250 251 @SuppressWarnings("unchecked") 252 @Override 253 public <R> R query(TemporalQuery<R> query) { 254 if (query == Queries.localDate()) { 255 return (R) LocalDate.of(2012, 6, 30); 256 } 257 if (query == Queries.localTime()) { 258 return (R) LocalTime.of(12, 30, 40); 259 } 260 if (query == Queries.zoneId() || query == Queries.zone()) { 261 return (R) ZoneId.of("Europe/London"); 262 } 263 throw new UnsupportedOperationException(); 264 } 265 }), ZonedDateTime.of(2012, 6, 30, 12, 30, 40, 0, ZoneId.of("Europe/London"))); 266 } 267 268 @Test(expectedExceptions=NullPointerException.class) 269 public void test_zonedDateTime_TemporalAccessor_null() { 270 IsoChronology.INSTANCE.zonedDateTime(null); 271 } 272 273 } | 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.DateTimeException; 72 import java.time.chrono.ChronoLocalDate; 73 import java.time.chrono.Chronology; 74 import java.time.chrono.HijrahChronology; 75 import java.time.chrono.IsoChronology; 76 import java.time.chrono.JapaneseChronology; 77 import java.time.chrono.MinguoChronology; 78 import java.time.chrono.ThaiBuddhistChronology; 79 import java.time.temporal.ChronoField; 80 import java.util.Locale; 81 import java.util.Set; 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 TCKChronology { 91 92 //----------------------------------------------------------------------- 93 // regular data factory for ID and calendarType of available calendars 94 //----------------------------------------------------------------------- 95 @DataProvider(name = "calendarNameAndType") 96 Object[][] data_of_calendars() { 97 return new Object[][] { 98 {"Hijrah-umalqura", "islamic-umalqura"}, 99 {"ISO", "iso8601"}, 100 {"Japanese", "japanese"}, 101 {"Minguo", "roc"}, 102 {"ThaiBuddhist", "buddhist"}, 103 }; 104 } 105 106 @Test(dataProvider = "calendarNameAndType") 107 public void test_getters(String chronoId, String calendarSystemType) { 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 = "calendarNameAndType") 115 public void test_required_calendars(String chronoId, String calendarSystemType) { 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 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 = "calendarNameAndType") 140 public void test_epoch(String name, String alias) { 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 @Test(dataProvider = "calendarNameAndType") 151 public void test_dateEpochDay(String name, String alias) { 152 Chronology chrono = Chronology.of(name); 153 ChronoLocalDate<?> date = chrono.dateNow(); 154 long epochDay = date.getLong(ChronoField.EPOCH_DAY); 155 ChronoLocalDate<?> test = chrono.dateEpochDay(epochDay); 156 assertEquals(test, date); 157 } 158 159 //----------------------------------------------------------------------- 160 // locale based lookup 161 //----------------------------------------------------------------------- 162 @DataProvider(name = "calendarsystemtype") 163 Object[][] data_CalendarType() { 164 return new Object[][] { 165 {HijrahChronology.INSTANCE, "islamic", "umalqura"}, 166 {IsoChronology.INSTANCE, "iso8601", null}, 167 {JapaneseChronology.INSTANCE, "japanese", null}, 168 {MinguoChronology.INSTANCE, "roc", null}, 169 {ThaiBuddhistChronology.INSTANCE, "buddhist", null}, 170 }; 171 } 172 173 @Test(dataProvider = "calendarsystemtype") 174 public void test_getCalendarType(Chronology chrono, String calendarType, String variant) { 175 String type = calendarType; 176 if (variant != null) { 177 type += '-'; 178 type += variant; 179 } 180 assertEquals(chrono.getCalendarType(), type); 181 } 182 183 @Test(dataProvider = "calendarsystemtype") 184 public void test_lookupLocale(Chronology chrono, String calendarType, String variant) { 185 Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA"); 186 builder.setUnicodeLocaleKeyword("ca", calendarType); 187 if (variant != null) { 188 builder.setUnicodeLocaleKeyword("cv", variant); 189 } 190 Locale locale = builder.build(); 191 assertEquals(Chronology.ofLocale(locale), chrono); 192 } 193 194 195 /** 196 * Test lookup by calendarType of each chronology. 197 * The calendarType is split on "-" to separate the calendar and variant. 198 * Verify that the calendar can be found by {@link java.time.chrono.Chronology#ofLocale}. 199 */ 200 @Test 201 public void test_ofLocaleByType() { 202 // Test that all available chronologies can be successfully found using ofLocale 203 Set<Chronology> chronos = Chronology.getAvailableChronologies(); 204 for (Chronology chrono : chronos) { 205 String[] split = chrono.getCalendarType().split("-"); 206 207 Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA"); 208 builder.setUnicodeLocaleKeyword("ca", split[0]); 209 if (split.length > 1) { 210 builder.setUnicodeLocaleKeyword("cv", split[1]); 211 } 212 Locale locale = builder.build(); 213 assertEquals(Chronology.ofLocale(locale), chrono, "Lookup by type and variant"); 214 } 215 } 216 217 @Test(expectedExceptions=DateTimeException.class) 218 public void test_lookupLocale() { 219 Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA"); 220 builder.setUnicodeLocaleKeyword("ca", "xxx"); 221 builder.setUnicodeLocaleKeyword("cv", "yyy"); 222 223 Locale locale = builder.build(); 224 Chronology.ofLocale(locale); 225 } 226 227 @Test(expectedExceptions = DateTimeException.class) 228 public void test_noChrono() { 229 Chronology chrono = Chronology.of("FooFoo"); 230 } 231 232 //----------------------------------------------------------------------- 233 // serialization; serialize and check each calendar system 234 //----------------------------------------------------------------------- 235 @Test(dataProvider = "calendarNameAndType") 236 public void test_chronoSerializationSingleton(String id, String _calendarType) throws Exception { 237 Chronology original = Chronology.of(id); 238 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 239 ObjectOutputStream out = new ObjectOutputStream(baos); 240 out.writeObject(original); 241 out.close(); 242 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 243 ObjectInputStream in = new ObjectInputStream(bais); 244 Chronology ser = (Chronology) in.readObject(); 245 assertEquals(ser, original, "Deserialized Chronology is not correct"); 246 } 247 248 } |