1 /*
   2  * Copyright (c) 2015, 2019, 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 // A test class to be launched in AppCDS mode, has basic+
  26 // coverage of string operations
  27 
  28 import sun.hotspot.WhiteBox;
  29 
  30 public class HelloStringPlus {
  31     public static void main(String args[]) {
  32         // Let's reference the string that is in archive
  33         String testString1 = "shared_test_string_unique_14325";
  34         System.out.println("Hello String: " + testString1);
  35 
  36         WhiteBox wb = WhiteBox.getWhiteBox();
  37         if (!wb.isShared(testString1) && !wb.areSharedStringsIgnored()) {
  38             throw new RuntimeException("testString1 is not shared");
  39         }
  40 
  41         // Check other basic string operations
  42         // Interning and equality
  43         String[] testArray = new String[] {"shared_", "test_", "string_", "intern_", "12345"};
  44         String toBeInterned = "";
  45 
  46         StringBuilder sb = new StringBuilder();
  47         for (String s : testArray) {
  48             sb.append(s);
  49         }
  50         toBeInterned = sb.toString();
  51 
  52         System.out.println("About to intern a string: " + toBeInterned);
  53         toBeInterned.intern();
  54 
  55         // check equality
  56         if (testString1.equals(toBeInterned))
  57             throw new RuntimeException("Equality test 1 failed");
  58 
  59         if (!testString1.equals("shared_test_string" + '_' + "unique_14325"))
  60             throw new RuntimeException("Equality test 2 failed");
  61 
  62         // Chech the hash code functionality; no special assertions, just make sure
  63         // no crashe or exception occurs
  64         System.out.println("testString1.hashCode() = " + testString1.hashCode());
  65 
  66         // Check intern() method for "" string
  67         String empty = "";
  68         String empty_interned = empty.intern();
  69         if (!wb.isShared(empty)) {
  70            throw new RuntimeException("Empty string should be shared");
  71         }
  72         if (empty_interned != empty) {
  73             throw new RuntimeException("Different string is returned from intern() for empty string");
  74         }
  75     }
  76 }