Path: blob/master/test/jdk/java/lang/String/CompactString/VMOptionsTest.java
41152 views
/*1* Copyright (c) 2015, 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 java.io.*;24import java.lang.reflect.Field;2526import org.testng.annotations.BeforeClass;27import org.testng.annotations.DataProvider;28import org.testng.annotations.Test;2930import static org.testng.Assert.*;3132/*33* @test34* @bug 807755935* @summary Tests Compact String. This one is testing36* if Compact String enable/disable VM Options is indeed working in String class,37* it's verified by testing if the VM option affect coder and38* COMPACT_STRINGS field in String class.39* @modules java.base/java.lang:open40* @run testng/othervm -XX:+CompactStrings -DCompactStringEnabled=true VMOptionsTest41* @run testng/othervm -XX:-CompactStrings -DCompactStringEnabled=false VMOptionsTest42*/4344public class VMOptionsTest {45boolean compactStringEnabled;46// corresponding "COMPACT_STRINGS" field in String class.47Field COMPACT_STRINGS;48// corresponding "coder" field in String class.49Field coder;5051// corresponding coder type in String class.52final byte LATIN1 = 0;53final byte UTF16 = 1;5455@BeforeClass56public void setUp() throws Exception {57compactStringEnabled = Boolean.valueOf(System.getProperty("CompactStringEnabled", null));58COMPACT_STRINGS = String.class.getDeclaredField("COMPACT_STRINGS");59COMPACT_STRINGS.setAccessible(true);60coder = String.class.getDeclaredField("coder");61coder.setAccessible(true);62}6364@DataProvider65public Object[][] provider() {66return new Object[][] {67new Object[] {"", LATIN1},68new Object[] {"abc", LATIN1},69new Object[] {"A\uff21", UTF16},70new Object[] {"\uff21\uff22", UTF16}71};72}7374/*75* verify the coder field in String objects.76*/77@Test(dataProvider = "provider")78public void testCoder(String str, byte expected) throws Exception {79byte c = (byte) coder.get(str);80expected = compactStringEnabled ? expected : UTF16;81assertEquals(c, expected);82}8384/*85* verify the COMPACT_STRINGS flag in String objects.86*/87@Test(dataProvider = "provider")88public void testCompactStringFlag(String str, byte ignore) throws Exception {89assertTrue(COMPACT_STRINGS.get(str).equals(compactStringEnabled));90}91}929394