Path: blob/master/test/jdk/java/lang/String/nativeEncoding/StringPlatformChars.java
41152 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @run main/othervm/native -Xcheck:jni StringPlatformChars26*/27import java.util.Arrays;2829public class StringPlatformChars {3031private static final String JNU_ENCODING = System.getProperty("sun.jnu.encoding");3233public static void main(String... args) throws Exception {34System.out.println("sun.jnu.encoding: " + JNU_ENCODING);35System.loadLibrary("stringPlatformChars");3637// Test varying lengths, provoking different allocation paths38StringBuilder unicodeSb = new StringBuilder();39StringBuilder asciiSb = new StringBuilder();40StringBuilder latinSb = new StringBuilder();4142for (int i = 0; i < 2000; i++) {43unicodeSb.append('\uFEFE');44testString(unicodeSb.toString());4546asciiSb.append('x');47testString(asciiSb.toString());4849latinSb.append('\u00FE');50testString(latinSb.toString());5152testString(latinSb.toString() + asciiSb.toString() + unicodeSb.toString());53}5455// Exhaustively test simple Strings made up of all possible chars:56for (char c = '\u0001'; c < Character.MAX_VALUE; c++) {57testString(String.valueOf(c));58}59// Special case: \u0000 is treated as end-of-string in the native code,60// so strings with it should be truncated:61if (getBytes("\u0000abcdef").length != 0 ||62getBytes("a\u0000bcdef").length != 1) {63System.out.println("Mismatching values for strings including \\u0000");64throw new AssertionError();65}66}6768private static void testString(String s) throws Exception {69byte[] nativeBytes = getBytes(s);70byte[] stringBytes = s.getBytes(JNU_ENCODING);7172if (!Arrays.equals(nativeBytes, stringBytes)) {73System.out.println("Mismatching values for: '" + s + "' " + Arrays.toString(s.chars().toArray()));74System.out.println("Native: " + Arrays.toString(nativeBytes));75System.out.println("String: " + Arrays.toString(stringBytes));76throw new AssertionError(s);77}7879String javaNewS = new String(nativeBytes, JNU_ENCODING);80String nativeNewS = newString(nativeBytes);81if (!javaNewS.equals(nativeNewS)) {82System.out.println("New string via native doesn't match via java: '" + javaNewS + "' and '" + nativeNewS + "'");83throw new AssertionError(s);84}85}8687static native byte[] getBytes(String string);8889static native String newString(byte[] bytes);90}919293