1 /*
2  * Copyright (c) 2012, 2017, 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) 2008-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.serial;
61 
62 import org.testng.annotations.DataProvider;
63 import org.testng.annotations.Test;
64 import tck.java.time.AbstractTCKTest;
65 
66 import java.io.ByteArrayInputStream;
67 import java.io.ByteArrayOutputStream;
68 import java.io.DataOutputStream;
69 import java.io.ObjectInputStream;
70 import java.io.ObjectStreamConstants;
71 import java.time.DateTimeException;
72 import java.time.ZoneId;
73 import java.time.zone.ZoneRulesException;
74 
75 import static org.testng.Assert.assertEquals;
76 import static org.testng.Assert.fail;
77 
78 /**
79  * Test serialization of ZoneId.
80  */
81 @Test
82 public class TCKZoneIdSerialization extends AbstractTCKTest {
83 
84     //-----------------------------------------------------------------------
85     @Test
test_serialization()86     public void test_serialization() throws Exception {
87         assertSerializable(ZoneId.of("Europe/London"));
88         assertSerializable(ZoneId.of("America/Chicago"));
89     }
90 
91     @Test
test_serialization_format()92     public void test_serialization_format() throws Exception {
93         ByteArrayOutputStream baos = new ByteArrayOutputStream();
94         try (DataOutputStream dos = new DataOutputStream(baos) ) {
95             dos.writeByte(7);
96             dos.writeUTF("Europe/London");
97         }
98         byte[] bytes = baos.toByteArray();
99         assertSerializedBySer(ZoneId.of("Europe/London"), bytes);
100     }
101 
102     @Test
test_deserialization_lenient_characters()103     public void test_deserialization_lenient_characters() throws Exception {
104         // an ID can be loaded without validation during deserialization
105         String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
106         ZoneId deser = deserialize(id);
107         // getId, equals, hashCode, toString and normalized are OK
108         assertEquals(deser.getId(), id);
109         assertEquals(deser.toString(), id);
110         assertEquals(deser, deser);
111         assertEquals(deser.hashCode(), deser.hashCode());
112         assertEquals(deser.normalized(), deser);
113         // getting the rules is not
114         try {
115             deser.getRules();
116             fail();
117         } catch (ZoneRulesException ex) {
118             // expected
119         }
120     }
121 
122     @Test(expectedExceptions=DateTimeException.class)
test_deserialization_lenient_badCharacters()123     public void test_deserialization_lenient_badCharacters() throws Exception {
124         // an ID can be loaded without validation during deserialization
125         // but there is a check to ensure the ID format is valid
126         deserialize("|!?");
127     }
128 
129     @Test(dataProvider="offsetBasedValid")
test_deserialization_lenient_offsetNotAllowed_noPrefix(String input, String resolvedId)130     public void test_deserialization_lenient_offsetNotAllowed_noPrefix(String input, String resolvedId) throws Exception {
131         ZoneId deserialized = deserialize(input);
132         assertEquals(deserialized, ZoneId.of(input));
133         assertEquals(deserialized, ZoneId.of(resolvedId));
134     }
135 
136     @Test(dataProvider="offsetBasedValidPrefix")
test_deserialization_lenient_offsetNotAllowed_prefixUTC(String input, String resolvedId, String offsetId)137     public void test_deserialization_lenient_offsetNotAllowed_prefixUTC(String input, String resolvedId, String offsetId) throws Exception {
138         ZoneId deserialized = deserialize("UTC" + input);
139         assertEquals(deserialized, ZoneId.of("UTC" + input));
140         assertEquals(deserialized, ZoneId.of("UTC" + resolvedId));
141     }
142 
143     @Test(dataProvider="offsetBasedValidPrefix")
test_deserialization_lenient_offsetNotAllowed_prefixGMT(String input, String resolvedId, String offsetId)144     public void test_deserialization_lenient_offsetNotAllowed_prefixGMT(String input, String resolvedId, String offsetId) throws Exception {
145         ZoneId deserialized = deserialize("GMT" + input);
146         assertEquals(deserialized, ZoneId.of("GMT" + input));
147         assertEquals(deserialized, ZoneId.of("GMT" + resolvedId));
148     }
149 
150     @Test(dataProvider="offsetBasedValidPrefix")
test_deserialization_lenient_offsetNotAllowed_prefixUT(String input, String resolvedId, String offsetId)151     public void test_deserialization_lenient_offsetNotAllowed_prefixUT(String input, String resolvedId, String offsetId) throws Exception {
152         ZoneId deserialized = deserialize("UT" + input);
153         assertEquals(deserialized, ZoneId.of("UT" + input));
154         assertEquals(deserialized, ZoneId.of("UT" + resolvedId));
155     }
156 
deserialize(String id)157     private ZoneId deserialize(String id) throws Exception {
158         String serClass = ZoneId.class.getPackage().getName() + ".Ser";
159         long serVer = getSUID(ZoneId.class);
160 
161         ByteArrayOutputStream baos = new ByteArrayOutputStream();
162         try (DataOutputStream dos = new DataOutputStream(baos)) {
163             dos.writeShort(ObjectStreamConstants.STREAM_MAGIC);
164             dos.writeShort(ObjectStreamConstants.STREAM_VERSION);
165             dos.writeByte(ObjectStreamConstants.TC_OBJECT);
166             dos.writeByte(ObjectStreamConstants.TC_CLASSDESC);
167             dos.writeUTF(serClass);
168             dos.writeLong(serVer);
169             dos.writeByte(ObjectStreamConstants.SC_EXTERNALIZABLE | ObjectStreamConstants.SC_BLOCK_DATA);
170             dos.writeShort(0);  // number of fields
171             dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);  // end of classdesc
172             dos.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
173             dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
174             dos.writeByte(1 + 2 + id.length());  // length of data (1 byte + 2 bytes UTF length + 32 bytes UTF)
175             dos.writeByte(7);  // ZoneId
176             dos.writeUTF(id);
177             dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);  // end of blockdata
178         }
179         ZoneId deser = null;
180         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
181             deser = (ZoneId) ois.readObject();
182         }
183         return deser;
184     }
185 
186     //-----------------------------------------------------------------------
187     // regular factory and .normalized()
188     //-----------------------------------------------------------------------
189     @DataProvider(name="offsetBasedValid")
data_offsetBasedValid()190     Object[][] data_offsetBasedValid() {
191         return new Object[][] {
192                 {"Z", "Z"},
193                 {"+0", "Z"},
194                 {"-0", "Z"},
195                 {"+00", "Z"},
196                 {"+0000", "Z"},
197                 {"+00:00", "Z"},
198                 {"+000000", "Z"},
199                 {"+00:00:00", "Z"},
200                 {"-00", "Z"},
201                 {"-0000", "Z"},
202                 {"-00:00", "Z"},
203                 {"-000000", "Z"},
204                 {"-00:00:00", "Z"},
205                 {"+5", "+05:00"},
206                 {"+01", "+01:00"},
207                 {"+0100", "+01:00"},
208                 {"+01:00", "+01:00"},
209                 {"+010000", "+01:00"},
210                 {"+01:00:00", "+01:00"},
211                 {"+12", "+12:00"},
212                 {"+1234", "+12:34"},
213                 {"+12:34", "+12:34"},
214                 {"+123456", "+12:34:56"},
215                 {"+12:34:56", "+12:34:56"},
216                 {"-02", "-02:00"},
217                 {"-5", "-05:00"},
218                 {"-0200", "-02:00"},
219                 {"-02:00", "-02:00"},
220                 {"-020000", "-02:00"},
221                 {"-02:00:00", "-02:00"},
222         };
223     }
224 
225     //-----------------------------------------------------------------------
226     @DataProvider(name="offsetBasedValidPrefix")
data_offsetBasedValidPrefix()227     Object[][] data_offsetBasedValidPrefix() {
228         return new Object[][] {
229                 {"", "", "Z"},
230                 {"+0", "", "Z"},
231                 {"-0", "", "Z"},
232                 {"+00", "", "Z"},
233                 {"+0000", "", "Z"},
234                 {"+00:00", "", "Z"},
235                 {"+000000", "", "Z"},
236                 {"+00:00:00", "", "Z"},
237                 {"-00", "", "Z"},
238                 {"-0000", "", "Z"},
239                 {"-00:00", "", "Z"},
240                 {"-000000", "", "Z"},
241                 {"-00:00:00", "", "Z"},
242                 {"+5", "+05:00", "+05:00"},
243                 {"+01", "+01:00", "+01:00"},
244                 {"+0100", "+01:00", "+01:00"},
245                 {"+01:00", "+01:00", "+01:00"},
246                 {"+010000", "+01:00", "+01:00"},
247                 {"+01:00:00", "+01:00", "+01:00"},
248                 {"+12", "+12:00", "+12:00"},
249                 {"+1234", "+12:34", "+12:34"},
250                 {"+12:34", "+12:34", "+12:34"},
251                 {"+123456", "+12:34:56", "+12:34:56"},
252                 {"+12:34:56", "+12:34:56", "+12:34:56"},
253                 {"-02", "-02:00", "-02:00"},
254                 {"-5", "-05:00", "-05:00"},
255                 {"-0200", "-02:00", "-02:00"},
256                 {"-02:00", "-02:00", "-02:00"},
257                 {"-020000", "-02:00", "-02:00"},
258                 {"-02:00:00", "-02:00", "-02:00"},
259         };
260     }
261 
262 
263 
264 }
265