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