1 /* 2 * Copyright (c) 2007, 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 /* 25 * @test 26 * @bug 5030265 27 * @modules jdk.compiler 28 * jdk.zipfs 29 * @compile -XDignore.symbol.file UnicodeTest.java 30 * @run main/othervm UnicodeTest 31 * @summary Verify that the J2RE can handle all legal Unicode characters 32 * in class names unless limited by the file system encoding 33 * or the encoding used for command line arguments. 34 * @author Norbert Lindenberg, ksrini 35 */ 36 37 /* 38 * This class creates Java source files using Unicode characters 39 * that test the limits of what's possible 40 * - in situations where the platform encoding imposes limits 41 * (command line arguments, non-Unicode file system) 42 * - in situations where full Unicode is supported 43 * (file system access in UTF-8 locales and on Windows 2000++, 44 * jar file contents) 45 * 46 * This test needs to be run in othervm as the locale is reset. 47 */ 48 49 import java.io.File; 50 import java.io.FileOutputStream; 51 import java.io.OutputStreamWriter; 52 import java.nio.charset.Charset; 53 import java.util.Locale; 54 55 public class UnicodeTest extends TestHelper { 56 static final File UnicodeTestSrc = new File("UnicodeTest-src"); 57 static final File UnicodeTestClasses = new File("UnicodeTest-classes"); 58 static final String UnicodeTestJarName = "UnicodeTest" + JAR_FILE_EXT; 59 static final File UnicodeTestJar = new File(UnicodeTestJarName); 60 static final File SolarisUnicodeTestJar = new File(TEST_SOURCES_DIR, 61 UnicodeTestJarName); 62 63 /* 64 * the main method is a port of the shell based test to a java, this 65 * eliminates the need for MKS on windows, thus we can rely on consistent 66 * results regardless of the shell being used. 67 */ main(String... args)68 public static void main(String... args) throws Exception { 69 System.out.println("creating test source files"); 70 UnicodeTestSrc.mkdirs(); 71 UnicodeTestClasses.mkdirs(); 72 String classname = generateSources(); 73 File javaFile = new File(UnicodeTestSrc, classname + JAVA_FILE_EXT); 74 System.out.println("building test apps"); 75 compile("-encoding", "UTF-8", 76 "-sourcepath", UnicodeTestSrc.getAbsolutePath(), 77 "-d", UnicodeTestClasses.getAbsolutePath(), 78 javaFile.getAbsolutePath()); 79 80 createJar("-cvfm", UnicodeTestJar.getAbsolutePath(), 81 new File(UnicodeTestSrc, "MANIFEST.MF").getAbsolutePath(), 82 "-C", UnicodeTestClasses.getAbsolutePath(), "."); 83 84 if (!UnicodeTestJar.exists()) { 85 throw new Error("failed to create " + UnicodeTestJar.getAbsolutePath()); 86 } 87 88 System.out.println("running test app using class file"); 89 TestResult tr = doExec(javaCmd, 90 "-cp", UnicodeTestClasses.getAbsolutePath(), classname); 91 if (!tr.isOK()) { 92 System.out.println(tr); 93 throw new RuntimeException("test fails"); 94 } 95 96 System.out.println("delete generated files with non-ASCII names"); 97 recursiveDelete(UnicodeTestSrc); 98 recursiveDelete(UnicodeTestClasses); 99 100 /* 101 * test in whatever the default locale is 102 */ 103 runJarTests(); 104 105 /* 106 * if the Japanese locale is available, test in that locale as well 107 */ 108 if (setLocale(Locale.JAPANESE)) { 109 runJarTests(); 110 } 111 112 /* 113 * if we can switch to a C locale, then test whether jar files with 114 * non-ASCII characters in the manifest still work in this crippled 115 * environment 116 */ 117 if (setLocale(Locale.ENGLISH)) { 118 runJarTests(); 119 } 120 // thats it we are outta here 121 } 122 runJarTests()123 static void runJarTests() { 124 System.out.println("running test app using newly built jar file in " + 125 Locale.getDefault()); 126 runTest(UnicodeTestJar); 127 128 System.out.println("running test app using jar file " + 129 "(built with Solaris UTF-8 locale) in " + Locale.getDefault()); 130 runTest(SolarisUnicodeTestJar); 131 } 132 runTest(File testJar)133 static void runTest(File testJar) { 134 TestResult tr = doExec(javaCmd, "-jar", testJar.getAbsolutePath()); 135 if (!tr.isOK()) { 136 System.out.println(tr); 137 throw new RuntimeException("test fails"); 138 } 139 } 140 setLocale(Locale desired)141 static boolean setLocale(Locale desired) { 142 if (Locale.getDefault().equals(desired)) { 143 return true; // already set nothing more 144 } 145 for (Locale l : Locale.getAvailableLocales()) { 146 if (l == desired) { 147 Locale.setDefault(l); 148 return true; 149 } 150 } 151 return false; 152 } 153 generateSources()154 static String generateSources() throws Exception { 155 String commandLineClassNameSuffix = commandLineClassNameSuffix(); 156 String commandLineClassName = "ClassA" + commandLineClassNameSuffix; 157 String manifestClassName = "ClassB" + 158 (hasUnicodeFileSystem() ? unicode : commandLineClassNameSuffix); 159 160 generateSource(commandLineClassName, manifestClassName); 161 generateSource(manifestClassName, commandLineClassName); 162 generateManifest(manifestClassName); 163 return commandLineClassName; 164 } 165 166 private static final String defaultEncoding = Charset.defaultCharset().name(); 167 168 // language names taken from java.util.Locale.getDisplayLanguage for the respective language 169 private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629"; 170 private static final String s_chinese = "\u4e2d\u6587"; 171 private static final String t_chinese = "\u4e2d\u6587"; 172 private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439"; 173 private static final String hindi = "\u0939\u093f\u0902\u0926\u0940"; 174 private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac"; 175 private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea"; 176 private static final String japanese = "\u65e5\u672c\u8a9e"; 177 private static final String korean = "\ud55c\uad6d\uc5b4"; 178 private static final String lithuanian = "Lietuvi\u0173"; 179 private static final String czech = "\u010de\u0161tina"; 180 private static final String turkish = "T\u00fcrk\u00e7e"; 181 private static final String spanish = "espa\u00f1ol"; 182 private static final String thai = "\u0e44\u0e17\u0e22"; 183 private static final String unicode = arabic + s_chinese + t_chinese 184 + russian + hindi + greek + hebrew + japanese + korean 185 + lithuanian + czech + turkish + spanish + thai; 186 commandLineClassNameSuffix()187 private static String commandLineClassNameSuffix() { 188 189 // Mapping from main platform encodings to language names 190 // for Unix and Windows, respectively. Use empty suffix 191 // for Windows encodings where OEM encoding differs. 192 // Use null if encoding isn't used. 193 String[][] names = { 194 { "UTF-8", unicode, "" }, 195 { "windows-1256", null, "" }, 196 { "iso-8859-6", arabic, null }, 197 { "GBK", s_chinese, s_chinese }, 198 { "GB18030", s_chinese, s_chinese }, 199 { "GB2312", s_chinese, null }, 200 { "x-windows-950", null, t_chinese }, 201 { "x-MS950-HKSCS", null, t_chinese }, 202 { "x-euc-tw", t_chinese, null }, 203 { "Big5", t_chinese, null }, 204 { "Big5-HKSCS", t_chinese, null }, 205 { "windows-1251", null, "" }, 206 { "iso-8859-5", russian, null }, 207 { "koi8-r", russian, null }, 208 { "windows-1253", null, "" }, 209 { "iso-8859-7", greek, null }, 210 { "windows-1255", null, "" }, 211 { "iso8859-8", hebrew, null }, 212 { "windows-31j", null, japanese }, 213 { "x-eucJP-Open", japanese, null }, 214 { "x-EUC-JP-LINUX", japanese, null }, 215 { "x-pck", japanese, null }, 216 { "x-windows-949", null, korean }, 217 { "euc-kr", korean, null }, 218 { "windows-1257", null, "" }, 219 { "iso-8859-13", lithuanian, null }, 220 { "windows-1250", null, "" }, 221 { "iso-8859-2", czech, null }, 222 { "windows-1254", null, "" }, 223 { "iso-8859-9", turkish, null }, 224 { "windows-1252", null, "" }, 225 { "iso-8859-1", spanish, null }, 226 { "iso-8859-15", spanish, null }, 227 { "x-windows-874", null, thai }, 228 { "tis-620", thai, null }, 229 }; 230 231 int column = isWindows ? 2 : 1; 232 for (int i = 0; i < names.length; i++) { 233 if (names[i][0].equalsIgnoreCase(defaultEncoding)) { 234 return names[i][column]; 235 } 236 } 237 return ""; 238 } 239 hasUnicodeFileSystem()240 private static boolean hasUnicodeFileSystem() { 241 return (isWindows) ? true : defaultEncoding.equalsIgnoreCase("UTF-8"); 242 } 243 generateSource(String thisClass, String otherClass)244 private static void generateSource(String thisClass, String otherClass) throws Exception { 245 File file = new File(UnicodeTestSrc, thisClass + JAVA_FILE_EXT); 246 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); 247 out.write("public class " + thisClass + " {\n"); 248 out.write(" public static void main(String[] args) {\n"); 249 out.write(" if (!" + otherClass + "." + otherClass.toLowerCase() + "().equals(\"" + otherClass + "\")) {\n"); 250 out.write(" throw new RuntimeException();\n"); 251 out.write(" }\n"); 252 out.write(" }\n"); 253 out.write(" public static String " + thisClass.toLowerCase() + "() {\n"); 254 out.write(" return \"" + thisClass + "\";\n"); 255 out.write(" }\n"); 256 out.write("}\n"); 257 out.close(); 258 } 259 generateManifest(String mainClass)260 private static void generateManifest(String mainClass) throws Exception { 261 File file = new File(UnicodeTestSrc, "MANIFEST.MF"); 262 FileOutputStream out = new FileOutputStream(file); 263 out.write("Manifest-Version: 1.0\n".getBytes("UTF-8")); 264 // Header lines are limited to 72 bytes. 265 // The manifest spec doesn't say we have to break at character boundaries, 266 // so we rudely break at byte boundaries. 267 byte[] headerBytes = ("Main-Class: " + mainClass + "\n").getBytes("UTF-8"); 268 if (headerBytes.length <= 72) { 269 out.write(headerBytes); 270 } else { 271 out.write(headerBytes, 0, 72); 272 int start = 72; 273 while (headerBytes.length > start) { 274 out.write((byte) '\n'); 275 out.write((byte) ' '); 276 int count = Math.min(71, headerBytes.length - start); 277 out.write(headerBytes, start, count); 278 start += count; 279 } 280 } 281 out.close(); 282 } 283 } 284