Home | History | Annotate | Download | only in serial
      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.serial;
     58 
     59 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
     60 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
     61 import static java.time.temporal.ChronoField.YEAR;
     62 
     63 import java.io.ByteArrayOutputStream;
     64 import java.io.DataOutputStream;
     65 import java.io.ObjectStreamConstants;
     66 import java.time.chrono.ChronoLocalDate;
     67 import java.time.chrono.HijrahChronology;
     68 import java.time.chrono.HijrahDate;
     69 import java.time.chrono.JapaneseDate;
     70 import java.time.chrono.JapaneseEra;
     71 import java.time.chrono.MinguoDate;
     72 import java.time.chrono.ThaiBuddhistDate;
     73 
     74 import org.testng.annotations.DataProvider;
     75 import org.testng.annotations.Test;
     76 
     77 
     78 
     79 import tck.java.time.AbstractTCKTest;
     80 
     81 /**
     82  * Test serialization of built-in chronologies.
     83  */
     84 @Test
     85 public class TCKChronoLocalDateSerialization extends AbstractTCKTest {
     86 
     87     static final int CHRONO_TYPE = 1;            // java.time.chrono.Ser.CHRONO_TYPE
     88     static final int JAPANESE_DATE_TYPE = 4;     // java.time.chrono.Ser.JAPANESE_DATE_TYPE
     89     static final int HIJRAH_DATE_TYPE = 6;       // java.time.chrono.Ser.HIJRAH_DATE_TYPE
     90     static final int MINGUO_DATE_TYPE = 7;       // java.time.chrono.Ser.MINGUO_DATE_TYPE
     91     static final int THAIBUDDHIST_DATE_TYPE = 8; // java.time.chrono.Ser.THAIBUDDHIST_DATE_TYPE
     92 
     93     //-----------------------------------------------------------------------
     94     // Regular data factory for names and descriptions of available calendars
     95     //-----------------------------------------------------------------------
     96     @DataProvider(name = "calendars")
     97     Object[][] data_of_calendars() {
     98         return new Object[][]{
     99             {JapaneseDate.of(JapaneseEra.HEISEI, 25, 01, 05), JAPANESE_DATE_TYPE},
    100             {MinguoDate.of(102, 01, 05),                      MINGUO_DATE_TYPE},
    101             {ThaiBuddhistDate.of(2556, 01, 05),               THAIBUDDHIST_DATE_TYPE},
    102         };
    103     }
    104 
    105 
    106     //-----------------------------------------------------------------------
    107     // Test Serialization of Calendars
    108     //-----------------------------------------------------------------------
    109     @Test( dataProvider="calendars")
    110     public void test_ChronoSerialization(ChronoLocalDate date, int dateType) throws Exception {
    111         assertSerializable(date);
    112     }
    113 
    114     //-----------------------------------------------------------------------
    115     // Test that serialization produces exact sequence of bytes
    116     //-----------------------------------------------------------------------
    117     @Test(dataProvider="calendars")
    118     private void test_serialization_format(ChronoLocalDate date, int dateType) throws Exception {
    119         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    120         try (DataOutputStream dos = new DataOutputStream(baos) ) {
    121             dos.writeByte(dateType);
    122             dos.writeInt(date.get(YEAR));
    123             dos.writeByte(date.get(MONTH_OF_YEAR));
    124             dos.writeByte(date.get(DAY_OF_MONTH));
    125         }
    126         byte[] bytes = baos.toByteArray();
    127         assertSerializedBySer(date, bytes);
    128     }
    129 
    130     //-----------------------------------------------------------------------
    131     // Test HijrajDate serialization is a type, Chronology, year, month, day
    132     //-----------------------------------------------------------------------
    133     @Test()
    134     public void test_hijrahSerialization_format() throws Exception {
    135         HijrahChronology chrono = HijrahChronology.INSTANCE;
    136         HijrahDate date = HijrahDate.of(1433, 10, 29);
    137         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    138 
    139         // Expect the type of the HijrahDate in the stream
    140         byte[] hijrahDateBytes = new byte[] {HIJRAH_DATE_TYPE};
    141 
    142         // Literal reference to Hijrah-Umalqura Chronology
    143         byte[] hijrahChronoBytes = new byte[] {
    144             115, 113, 0, 126, 0, 0,                        /* p w \u0001 \u0006 s q \u0000 ~ \u0000 \u0000 */
    145             119, 18, 1, 0, 15, 72, 105, 106, 114, 97,      /* w \u0012 \u0001 \u0000 \u000f H i j r a */
    146             104, 45, 117, 109, 97, 108, 113, 117, 114, 97, /* h - u m a l q u r a */
    147             120,                                           /*  \u001d x */
    148         };
    149 
    150         // Build the sequence that represents the data in the stream
    151         baos = new ByteArrayOutputStream();
    152         try (DataOutputStream dos = new DataOutputStream(baos) ) {
    153             dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
    154             dos.writeByte(6);   // 6 bytes follow
    155             dos.writeInt(date.get(YEAR));
    156             dos.writeByte(date.get(MONTH_OF_YEAR));
    157             dos.writeByte(date.get(DAY_OF_MONTH));
    158             dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
    159         }
    160         byte[] dateBytes = baos.toByteArray();
    161 
    162         assertSerializedBySer(date, hijrahDateBytes, hijrahChronoBytes, dateBytes);
    163     }
    164 
    165 
    166     //-----------------------------------------------------------------------
    167     // Regular data factory for names and descriptions of available calendars
    168     //-----------------------------------------------------------------------
    169     @DataProvider(name = "invalidSerialformClasses")
    170     Object[][] invalid_serial_classes() {
    171         return new Object[][]{
    172             {JapaneseEra.class},
    173             {JapaneseDate.class},
    174             {MinguoDate.class},
    175             {ThaiBuddhistDate.class},
    176             {HijrahDate.class},
    177         };
    178     }
    179 
    180     @Test(dataProvider="invalidSerialformClasses")
    181     public void test_invalid_serialform(Class<?> clazz) throws Exception {
    182         assertNotSerializable(clazz);
    183     }
    184 
    185 }
    186