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