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  */
  43 
  44 public class VMOptionsTest {
  45     boolean compactStringEnabled;
  46     // corresponding "COMPACT_STRINGS" field in String class.
  47     Field COMPACT_STRINGS;
  48     // corresponding "coder" field in String class.
  49     Field coder;
  50 
  51     // corresponding coder type in String class.
  52     final byte LATIN1 = 0;
  53     final byte UTF16  = 1;
  54 
  55     @BeforeClass
  56     public void setUp() throws Exception {
  57         compactStringEnabled = Boolean.valueOf(System.getProperty("CompactStringEnabled", null));
  58         COMPACT_STRINGS = String.class.getDeclaredField("COMPACT_STRINGS");
  59         COMPACT_STRINGS.setAccessible(true);
  60         coder = String.class.getDeclaredField("coder");
  61         coder.setAccessible(true);
  62     }
  63 
  64     @DataProvider
  65     public Object[][] provider() {
  66         return new Object[][] {
  67                 new Object[] {"", LATIN1},
  68                 new Object[] {"abc", LATIN1},
  69                 new Object[] {"A\uff21", UTF16},
  70                 new Object[] {"\uff21\uff22", UTF16}
  71         };
  72     }
  73 
  74     /*
  75      * verify the coder field in String objects.
  76      */
  77     @Test(dataProvider = "provider")
  78     public void testCoder(String str, byte expected) throws Exception {
  79         byte c = (byte) coder.get(str);
  80         expected = compactStringEnabled ? expected : UTF16;
  81         assertEquals(c, expected);
  82     }
  83 
  84     /*
  85      * verify the COMPACT_STRINGS flag in String objects.
  86      */
  87     @Test(dataProvider = "provider")
  88     public void testCompactStringFlag(String str, byte ignore) throws Exception {
  89         assertTrue(COMPACT_STRINGS.get(str).equals(compactStringEnabled));
  90     }
  91 }