1 /* 2 * Copyright (c) 2015, 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 java.io.*; 25 import java.lang.reflect.Field; 26 27 import org.testng.annotations.BeforeClass; 28 import org.testng.annotations.DataProvider; 29 import org.testng.annotations.Test; 30 31 import static org.testng.Assert.*; 32 33 /* 34 * @test 35 * @bug 8077559 36 * @summary Tests Compact String. This one is testing 37 * if Compact String enable/disable VM Options is indeed working in String class, 38 * it's verified by testing if the VM option affect coder and 39 * COMPACT_STRINGS field in String class. 40 * @run testng/othervm -XX:+CompactStrings -DCompactStringEnabled=true VMOptionsTest 41 * @run testng/othervm -XX:-CompactStrings -DCompactStringEnabled=false VMOptionsTest 42 * @run testng/othervm -DCompactStringEnabled=true VMOptionsTest 43 */ 44 45 public class VMOptionsTest { 46 boolean compactStringEnabled; 47 // corresponding "COMPACT_STRINGS" field in String class. 48 Field COMPACT_STRINGS; 49 // corresponding "coder" field in String class. 50 Field coder; 51 52 // corresponding coder type in String class. 53 final byte LATIN1 = 0; 54 final byte UTF16 = 1; 55 56 @BeforeClass 57 public void setUp() throws Exception { 58 compactStringEnabled = Boolean.valueOf(System.getProperty("CompactStringEnabled", null)); 59 COMPACT_STRINGS = String.class.getDeclaredField("COMPACT_STRINGS"); 60 COMPACT_STRINGS.setAccessible(true); 61 coder = String.class.getDeclaredField("coder"); 62 coder.setAccessible(true); 63 } 64 65 @DataProvider 66 public Object[][] provider() { 67 return new Object[][] { 68 new Object[] {"", LATIN1}, 69 new Object[] {"abc", LATIN1}, 70 new Object[] {"A\uff21", UTF16}, 71 new Object[] {"\uff21\uff22", UTF16} 72 }; 73 } 74 75 /* 76 * verify the coder field in String objects. 77 */ 78 @Test(dataProvider = "provider") 79 public void testCoder(String str, byte expected) throws Exception { 80 byte c = (byte) coder.get(str); 81 expected = compactStringEnabled ? expected : UTF16; 82 assertEquals(c, expected); 83 } 84 85 /* 86 * verify the COMPACT_STRINGS flag in String objects. 87 */ 88 @Test(dataProvider = "provider") 89 public void testCompactStringFlag(String str, byte ignore) throws Exception { 90 assertTrue(COMPACT_STRINGS.get(str).equals(compactStringEnabled)); 91 } 92 }