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