Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/UnicodeTest.java
38833 views
/*1* Copyright (c) 2007, 2012, 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* @bug 503026526* @compile -XDignore.symbol.file UnicodeTest.java27* @run main/othervm UnicodeTest28* @summary Verify that the J2RE can handle all legal Unicode characters29* in class names unless limited by the file system encoding30* or the encoding used for command line arguments.31* @author Norbert Lindenberg, ksrini32*/3334/*35* This class creates Java source files using Unicode characters36* that test the limits of what's possible37* - in situations where the platform encoding imposes limits38* (command line arguments, non-Unicode file system)39* - in situations where full Unicode is supported40* (file system access in UTF-8 locales and on Windows 2000++,41* jar file contents)42*43* This test needs to be run in othervm as the locale is reset.44*/4546import java.io.File;47import java.io.FileOutputStream;48import java.io.OutputStreamWriter;49import java.nio.charset.Charset;50import java.util.Locale;5152public class UnicodeTest extends TestHelper {53static final File UnicodeTestSrc = new File("UnicodeTest-src");54static final File UnicodeTestClasses = new File("UnicodeTest-classes");55static final String UnicodeTestJarName = "UnicodeTest" + JAR_FILE_EXT;56static final File UnicodeTestJar = new File(UnicodeTestJarName);57static final File SolarisUnicodeTestJar = new File(TEST_SOURCES_DIR,58UnicodeTestJarName);5960/*61* the main method is a port of the shell based test to a java, this62* eliminates the need for MKS on windows, thus we can rely on consistent63* results regardless of the shell being used.64*/65public static void main(String... args) throws Exception {66System.out.println("creating test source files");67UnicodeTestSrc.mkdirs();68UnicodeTestClasses.mkdirs();69String classname = generateSources();70File javaFile = new File(UnicodeTestSrc, classname + JAVA_FILE_EXT);71System.out.println("building test apps");72compile("-encoding", "UTF-8",73"-sourcepath", UnicodeTestSrc.getAbsolutePath(),74"-d", UnicodeTestClasses.getAbsolutePath(),75javaFile.getAbsolutePath());7677createJar("-cvfm", UnicodeTestJar.getAbsolutePath(),78new File(UnicodeTestSrc, "MANIFEST.MF").getAbsolutePath(),79"-C", UnicodeTestClasses.getAbsolutePath(), ".");8081if (!UnicodeTestJar.exists()) {82throw new Error("failed to create " + UnicodeTestJar.getAbsolutePath());83}8485System.out.println("running test app using class file");86TestResult tr = doExec(javaCmd,87"-cp", UnicodeTestClasses.getAbsolutePath(), classname);88if (!tr.isOK()) {89System.out.println(tr);90throw new RuntimeException("test fails");91}9293System.out.println("delete generated files with non-ASCII names");94recursiveDelete(UnicodeTestSrc);95recursiveDelete(UnicodeTestClasses);9697/*98* test in whatever the default locale is99*/100runJarTests();101102/*103* if the Japanese locale is available, test in that locale as well104*/105if (setLocale(Locale.JAPANESE)) {106runJarTests();107}108109/*110* if we can switch to a C locale, then test whether jar files with111* non-ASCII characters in the manifest still work in this crippled112* environment113*/114if (setLocale(Locale.ENGLISH)) {115runJarTests();116}117// thats it we are outta here118}119120static void runJarTests() {121System.out.println("running test app using newly built jar file in " +122Locale.getDefault());123runTest(UnicodeTestJar);124125System.out.println("running test app using jar file " +126"(built with Solaris UTF-8 locale) in " + Locale.getDefault());127runTest(SolarisUnicodeTestJar);128}129130static void runTest(File testJar) {131TestResult tr = doExec(javaCmd, "-jar", testJar.getAbsolutePath());132if (!tr.isOK()) {133System.out.println(tr);134throw new RuntimeException("test fails");135}136}137138static boolean setLocale(Locale desired) {139if (Locale.getDefault().equals(desired)) {140return true; // already set nothing more141}142for (Locale l : Locale.getAvailableLocales()) {143if (l == desired) {144Locale.setDefault(l);145return true;146}147}148return false;149}150151static String generateSources() throws Exception {152String commandLineClassNameSuffix = commandLineClassNameSuffix();153String commandLineClassName = "ClassA" + commandLineClassNameSuffix;154String manifestClassName = "ClassB" +155(hasUnicodeFileSystem() ? unicode : commandLineClassNameSuffix);156157generateSource(commandLineClassName, manifestClassName);158generateSource(manifestClassName, commandLineClassName);159generateManifest(manifestClassName);160return commandLineClassName;161}162163private static final String defaultEncoding = Charset.defaultCharset().name();164165// language names taken from java.util.Locale.getDisplayLanguage for the respective language166private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";167private static final String s_chinese = "\u4e2d\u6587";168private static final String t_chinese = "\u4e2d\u6587";169private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";170private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";171private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";172private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";173private static final String japanese = "\u65e5\u672c\u8a9e";174private static final String korean = "\ud55c\uad6d\uc5b4";175private static final String lithuanian = "Lietuvi\u0173";176private static final String czech = "\u010de\u0161tina";177private static final String turkish = "T\u00fcrk\u00e7e";178private static final String spanish = "espa\u00f1ol";179private static final String thai = "\u0e44\u0e17\u0e22";180private static final String unicode = arabic + s_chinese + t_chinese181+ russian + hindi + greek + hebrew + japanese + korean182+ lithuanian + czech + turkish + spanish + thai;183184private static String commandLineClassNameSuffix() {185186// Mapping from main platform encodings to language names187// for Unix and Windows, respectively. Use empty suffix188// for Windows encodings where OEM encoding differs.189// Use null if encoding isn't used.190String[][] names = {191{ "UTF-8", unicode, "" },192{ "windows-1256", null, "" },193{ "iso-8859-6", arabic, null },194{ "GBK", s_chinese, s_chinese },195{ "GB18030", s_chinese, s_chinese },196{ "GB2312", s_chinese, null },197{ "x-windows-950", null, t_chinese },198{ "x-MS950-HKSCS", null, t_chinese },199{ "x-euc-tw", t_chinese, null },200{ "Big5", t_chinese, null },201{ "Big5-HKSCS", t_chinese, null },202{ "windows-1251", null, "" },203{ "iso-8859-5", russian, null },204{ "koi8-r", russian, null },205{ "windows-1253", null, "" },206{ "iso-8859-7", greek, null },207{ "windows-1255", null, "" },208{ "iso8859-8", hebrew, null },209{ "windows-31j", null, japanese },210{ "x-eucJP-Open", japanese, null },211{ "x-EUC-JP-LINUX", japanese, null },212{ "x-pck", japanese, null },213{ "x-windows-949", null, korean },214{ "euc-kr", korean, null },215{ "windows-1257", null, "" },216{ "iso-8859-13", lithuanian, null },217{ "windows-1250", null, "" },218{ "iso-8859-2", czech, null },219{ "windows-1254", null, "" },220{ "iso-8859-9", turkish, null },221{ "windows-1252", null, "" },222{ "iso-8859-1", spanish, null },223{ "iso-8859-15", spanish, null },224{ "x-windows-874", null, thai },225{ "tis-620", thai, null },226};227228int column = isWindows ? 2 : 1;229for (int i = 0; i < names.length; i++) {230if (names[i][0].equalsIgnoreCase(defaultEncoding)) {231return names[i][column];232}233}234return "";235}236237private static boolean hasUnicodeFileSystem() {238return (isWindows) ? true : defaultEncoding.equalsIgnoreCase("UTF-8");239}240241private static void generateSource(String thisClass, String otherClass) throws Exception {242File file = new File(UnicodeTestSrc, thisClass + JAVA_FILE_EXT);243OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");244out.write("public class " + thisClass + " {\n");245out.write(" public static void main(String[] args) {\n");246out.write(" if (!" + otherClass + "." + otherClass.toLowerCase() + "().equals(\"" + otherClass + "\")) {\n");247out.write(" throw new RuntimeException();\n");248out.write(" }\n");249out.write(" }\n");250out.write(" public static String " + thisClass.toLowerCase() + "() {\n");251out.write(" return \"" + thisClass + "\";\n");252out.write(" }\n");253out.write("}\n");254out.close();255}256257private static void generateManifest(String mainClass) throws Exception {258File file = new File(UnicodeTestSrc, "MANIFEST.MF");259FileOutputStream out = new FileOutputStream(file);260out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));261// Header lines are limited to 72 bytes.262// The manifest spec doesn't say we have to break at character boundaries,263// so we rudely break at byte boundaries.264byte[] headerBytes = ("Main-Class: " + mainClass + "\n").getBytes("UTF-8");265if (headerBytes.length <= 72) {266out.write(headerBytes);267} else {268out.write(headerBytes, 0, 72);269int start = 72;270while (headerBytes.length > start) {271out.write((byte) '\n');272out.write((byte) ' ');273int count = Math.min(71, headerBytes.length - start);274out.write(headerBytes, start, count);275start += count;276}277}278out.close();279}280}281282283