test/java/time/tck/java/time/TestChronology.java

Print this page




  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 }


  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 }