Path: blob/master/test/jdk/java/lang/String/CompactString/SerializationTest.java
41152 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import static jdk.test.lib.util.SerializationUtils.*;27import static org.testng.Assert.assertEquals;2829/*30* @test31* @bug 807755932* @library /test/lib33* @build jdk.test.lib.util.SerializationUtils34* @summary Tests Compact String. This one is testing String serialization35* among -XX:+CompactStrings/-XX:-CompactStrings/LegacyString36* @run testng/othervm -XX:+CompactStrings SerializationTest37* @run testng/othervm -XX:-CompactStrings SerializationTest38*/3940public class SerializationTest {41@DataProvider42public Object[][] provider() {43return new Object[][] {44// every byte array is serialized from corresponding String object45// by previous JDK(build 1.8.0_45-b14).46new Object[] { "", new byte[] { -84, -19, 0, 5, 116, 0, 0 } },47new Object[] { "A", new byte[] { -84, -19, 0, 5, 116, 0, 1, 65 } },48new Object[] { "AB", new byte[] { -84, -19, 0, 5, 116, 0, 2, 65, 66 } },49new Object[] { "abcdefghijk",50new byte[] {-84, -19, 0, 5, 116, 0, 11, 97, 98, 99, 100, 101,51102, 103, 104, 105, 106, 107 } },52new Object[] { "\uff21", new byte[] { -84, -19, 0, 5, 116, 0, 3, -17, -68, -95 } },53new Object[] { "\uff21\uff22", new byte[] { -84, -19, 0, 5, 116, 0, 6, -17, -68,54-95, -17, -68, -94 } },55new Object[] { "\uff21A\uff21A\uff21A\uff21A\uff21A",56new byte[] { -84, -19, 0, 5, 116, 0, 20, -17, -68, -95, 65, -17, -68,57-95, 65, -17, -68, -95, 65, -17, -68, -95, 65, -17, -68, -95, 65 } },58new Object[] { "A\uff21B\uff22C\uff23D\uff24E\uff25F\uff26G\uff27H\uff28",59new byte[] { -84, -19, 0, 5, 116, 0, 32, 65, -17, -68, -95, 66, -17, -68,60-94, 67, -17, -68, -93, 68, -17, -68, -92, 69, -17, -68, -91, 70, -17,61-68, -90, 71, -17, -68, -89, 72, -17, -68, -88 } },62new Object[] { "\uff21A\uff22B\uff23C\uff24D\uff25E\uff26F\uff27G\uff28H",63new byte[] { -84, -19, 0, 5, 116, 0, 32, -17, -68, -95, 65, -17, -68,64-94, 66, -17, -68, -93, 67, -17, -68, -92, 68, -17, -68, -91, 69, -17,65-68, -90, 70, -17, -68, -89, 71, -17, -68, -88, 72 } },66new Object[] { "\ud801\udc00\ud801\udc01\uff21A",67new byte[] { -84, -19, 0, 5, 116, 0, 16, -19, -96, -127, -19, -80, -128,68-19, -96, -127, -19, -80, -127, -17, -68, -95, 65 } },69new Object[] { "\uff21\uff22\uff21\uff22\uff21\uff22\uff21\uff22\uff21\uff22",70new byte[] { -84, -19, 0, 5, 116, 0, 30, -17, -68, -95, -17, -68, -94, -17,71-68, -95, -17, -68, -94, -17, -68, -95, -17, -68, -94, -17, -68, -95, -17,72-68, -94, -17, -68, -95, -17, -68, -94 } } };73}7475/*76* Verify serialization works between Compact String/Legacy String77*/78@Test(dataProvider = "provider")79public void test(String strContent, byte[] baInJDK8) throws Exception {80// Serialize a String object into byte array.81byte[] ba = serialize(strContent);82assertEquals(ba, baInJDK8);83// Deserialize a String object from byte array which is generated by previous JDK(build 1.8.0_45-b14).84Object obj = deserialize(ba);85assertEquals(obj.getClass(), String.class);86assertEquals((String)obj, strContent);87}88}899091