1 /*
   2  * Copyright (c) 2012, 2013, 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.util.Objects;
  25 import java.util.Comparator;
  26 import jdk.internal.misc.JavaLangAccess;
  27 import jdk.internal.misc.SharedSecrets;
  28 
  29 /*
  30  * @test
  31  * @bug 8013528
  32  * @summary Test JavaLangAccess.newUnsafeString
  33  * @modules java.base/jdk.internal.misc
  34  * @compile -XDignore.symbol.file NewUnsafeString.java
  35  */
  36 public class NewUnsafeString {
  37 
  38     static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
  39 
  40     public static void testNewUnsafeString() {
  41         String benchmark = "exemplar";
  42         String constructorCopy = new String(benchmark);
  43         char[] jlaChars = benchmark.toCharArray();
  44         String jlaCopy = jla.newStringUnsafe(jlaChars);
  45 
  46         if (benchmark == constructorCopy) {
  47             throw new Error("should be different instances");
  48         }
  49         if (!benchmark.equals(constructorCopy)) {
  50             throw new Error("Copy not equal");
  51         }
  52         if (0 != Objects.compare(benchmark, constructorCopy, Comparator.naturalOrder())) {
  53             throw new Error("Copy not equal");
  54         }
  55 
  56         if (benchmark == jlaCopy) {
  57             throw new Error("should be different instances");
  58         }
  59         if (!benchmark.equals(jlaCopy)) {
  60             throw new Error("Copy not equal");
  61         }
  62         if (0 != Objects.compare(benchmark, jlaCopy, Comparator.naturalOrder())) {
  63             throw new Error("Copy not equal");
  64         }
  65 
  66         if (constructorCopy == jlaCopy) {
  67             throw new Error("should be different instances");
  68         }
  69         if (!constructorCopy.equals(jlaCopy)) {
  70             throw new Error("Copy not equal");
  71         }
  72         if (0 != Objects.compare(constructorCopy, jlaCopy, Comparator.naturalOrder())) {
  73             throw new Error("Copy not equal");
  74         }
  75 
  76         // The following is extremely "evil". Never ever do this in non-test code.
  77         jlaChars[0] = 'X';
  78         if (!"Xxemplar".equals(jlaCopy)) {
  79             throw new Error("jla.newStringUnsafe did not use provided string");
  80         }
  81 
  82     }
  83 
  84     public static void main(String[] args) {
  85         testNewUnsafeString();
  86     }
  87 }