1 /*
2  * Copyright (c) 2015, 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 import org.testng.annotations.DataProvider;
25 import org.testng.annotations.Test;
26 
27 import static jdk.test.lib.util.SerializationUtils.*;
28 import static org.testng.Assert.assertEquals;
29 
30 /*
31  * @test
32  * @bug 8077559
33  * @library /test/lib
34  * @build jdk.test.lib.util.SerializationUtils
35  * @summary Tests Compact String. This one is testing String serialization
36  *          among -XX:+CompactStrings/-XX:-CompactStrings/LegacyString
37  * @run testng/othervm -XX:+CompactStrings SerializationTest
38  * @run testng/othervm -XX:-CompactStrings SerializationTest
39  */
40 
41 public class SerializationTest {
42     @DataProvider
provider()43     public Object[][] provider() {
44         return new Object[][] {
45                 // every byte array is serialized from corresponding String object
46                 // by previous JDK(build 1.8.0_45-b14).
47                 new Object[] { "", new byte[] { -84, -19, 0, 5, 116, 0, 0 } },
48                 new Object[] { "A", new byte[] { -84, -19, 0, 5, 116, 0, 1, 65 } },
49                 new Object[] { "AB", new byte[] { -84, -19, 0, 5, 116, 0, 2, 65, 66 } },
50                 new Object[] { "abcdefghijk",
51                         new byte[] {-84, -19, 0, 5, 116, 0, 11, 97, 98, 99, 100, 101,
52                         102, 103, 104, 105, 106, 107 } },
53                 new Object[] { "\uff21", new byte[] { -84, -19, 0, 5, 116, 0, 3, -17, -68, -95 } },
54                 new Object[] { "\uff21\uff22", new byte[] { -84, -19, 0, 5, 116, 0, 6, -17, -68,
55                         -95, -17, -68, -94 } },
56                 new Object[] { "\uff21A\uff21A\uff21A\uff21A\uff21A",
57                         new byte[] { -84, -19, 0, 5, 116, 0, 20, -17, -68, -95, 65, -17, -68,
58                         -95, 65, -17, -68, -95, 65, -17, -68, -95, 65, -17, -68, -95, 65 } },
59                 new Object[] { "A\uff21B\uff22C\uff23D\uff24E\uff25F\uff26G\uff27H\uff28",
60                         new byte[] { -84, -19, 0, 5, 116, 0, 32, 65, -17, -68, -95, 66, -17, -68,
61                         -94, 67, -17, -68, -93, 68, -17, -68, -92, 69, -17, -68, -91, 70, -17,
62                         -68, -90, 71, -17, -68, -89, 72, -17, -68, -88 } },
63                 new Object[] { "\uff21A\uff22B\uff23C\uff24D\uff25E\uff26F\uff27G\uff28H",
64                         new byte[] { -84, -19, 0, 5, 116, 0, 32, -17, -68, -95, 65, -17, -68,
65                         -94, 66, -17, -68, -93, 67, -17, -68, -92, 68, -17, -68, -91, 69, -17,
66                         -68, -90, 70, -17, -68, -89, 71, -17, -68, -88, 72 } },
67                 new Object[] { "\ud801\udc00\ud801\udc01\uff21A",
68                         new byte[] { -84, -19, 0, 5, 116, 0, 16, -19, -96, -127, -19, -80, -128,
69                         -19, -96, -127, -19, -80, -127, -17, -68, -95, 65 } },
70                 new Object[] { "\uff21\uff22\uff21\uff22\uff21\uff22\uff21\uff22\uff21\uff22",
71                         new byte[] { -84, -19, 0, 5, 116, 0, 30, -17, -68, -95, -17, -68, -94, -17,
72                         -68, -95, -17, -68, -94, -17, -68, -95, -17, -68, -94, -17, -68, -95, -17,
73                         -68, -94, -17, -68, -95, -17, -68, -94 } } };
74     }
75 
76     /*
77      * Verify serialization works between Compact String/Legacy String
78      */
79     @Test(dataProvider = "provider")
test(String strContent, byte[] baInJDK8)80     public void test(String strContent, byte[] baInJDK8) throws Exception {
81         // Serialize a String object into byte array.
82         byte[] ba = serialize(strContent);
83         assertEquals(ba, baInJDK8);
84         // Deserialize a String object from byte array which is generated by previous JDK(build 1.8.0_45-b14).
85         Object obj = deserialize(ba);
86         assertEquals(obj.getClass(), String.class);
87         assertEquals((String)obj, strContent);
88     }
89 }
90