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 25 import sun.hotspot.WhiteBox; 26 27 public class InternStringTest { 28 public static String passed_output1 = "Found shared string."; 29 public static String passed_output2 = "Shared strings are equal."; 30 public static String passed_output3 = "Found shared string containing latin1 supplement chars."; 31 public static String passed_output4 = "Found shared string containing non-western chars."; 32 public static final String latin1Sup = "XXXX \u00a3 YYYY"; // \u00a3 = The pound sign 33 public static final String nonWestern = "XXXX \u5678 YYYY"; // \u5678 = Unicode Han Character 'ton (metric or English)' 34 35 public static void main(String[] args) throws Exception { 36 WhiteBox wb = WhiteBox.getWhiteBox(); 37 38 // All string literals are shared. 39 String shared1 = "LiveOak"; 40 String interned1 = shared1.intern(); 41 if (wb.areSharedStringsIgnored() || wb.isShared(interned1)) { 42 System.out.println(passed_output1); 43 } else { 44 throw new RuntimeException("Failed: String is not shared."); 45 } 46 47 // Test 2: shared_string1.intern() == shared_string2.intern() 48 String shared2 = "LiveOak"; 49 String interned2 = shared2.intern(); 50 if (interned1 == interned2) { 51 System.out.println(passed_output2); 52 } else { 53 throw new RuntimeException("Not equal!"); 54 } 55 56 // Test 3: interned strings with a char in latin1 supplement block [\u0080-\u00ff] 57 { 58 String a = "X" + latin1Sup.substring(1); 59 String b = a.intern(); 60 61 if (wb.areSharedStringsIgnored() || wb.isShared(b)) { 62 System.out.println(passed_output3); 63 } else { 64 throw new RuntimeException("Failed: expected shared string with latin1-supplement chars."); 65 } 66 } 67 68 // Test 5: interned strings with non-western characters 69 { 70 String a = "X" + nonWestern.substring(1); 71 String b = a.intern(); 72 if (wb.areSharedStringsIgnored() || wb.isShared(b)) { 73 System.out.println(passed_output4); 74 } else { 75 throw new RuntimeException("Failed: expected shared string with non-western chars."); 76 } 77 } 78 } 79 }