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