File: UnicodeTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (283 lines) | stat: -rw-r--r-- 12,623 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 5030265
 * @modules jdk.compiler
 *          jdk.zipfs
 * @compile  -XDignore.symbol.file UnicodeTest.java
 * @run main/othervm UnicodeTest
 * @summary Verify that the J2RE can handle all legal Unicode characters
 *          in class names unless limited by the file system encoding
 *          or the encoding used for command line arguments.
 * @author Norbert Lindenberg, ksrini
 */

/*
 * This class creates Java source files using Unicode characters
 * that test the limits of what's possible
 * - in situations where the platform encoding imposes limits
 *   (command line arguments, non-Unicode file system)
 * - in situations where full Unicode is supported
 *   (file system access in UTF-8 locales and on Windows 2000++,
 *    jar file contents)
 *
 * This test needs to be run in othervm as the locale is reset.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Locale;

public class UnicodeTest extends TestHelper {
    static final File UnicodeTestSrc        = new File("UnicodeTest-src");
    static final File UnicodeTestClasses    = new File("UnicodeTest-classes");
    static final String UnicodeTestJarName  = "UnicodeTest" + JAR_FILE_EXT;
    static final File UnicodeTestJar        = new File(UnicodeTestJarName);
    static final File SolarisUnicodeTestJar = new File(TEST_SOURCES_DIR,
                                                       UnicodeTestJarName);

    /*
     * the main method is a port of the shell based test to a java, this
     * eliminates the need for MKS on windows, thus we can rely on consistent
     * results regardless of the shell being used.
     */
    public static void main(String... args) throws Exception {
        System.out.println("creating test source files");
        UnicodeTestSrc.mkdirs();
        UnicodeTestClasses.mkdirs();
        String classname = generateSources();
        File javaFile = new File(UnicodeTestSrc, classname + JAVA_FILE_EXT);
        System.out.println("building test apps");
        compile("-encoding", "UTF-8",
                "-sourcepath", UnicodeTestSrc.getAbsolutePath(),
                "-d", UnicodeTestClasses.getAbsolutePath(),
                javaFile.getAbsolutePath());

        createJar("-cvfm", UnicodeTestJar.getAbsolutePath(),
                  new File(UnicodeTestSrc, "MANIFEST.MF").getAbsolutePath(),
                  "-C", UnicodeTestClasses.getAbsolutePath(), ".");

        if (!UnicodeTestJar.exists()) {
            throw new Error("failed to create " + UnicodeTestJar.getAbsolutePath());
        }

        System.out.println("running test app using class file");
        TestResult tr = doExec(javaCmd,
                        "-cp", UnicodeTestClasses.getAbsolutePath(), classname);
        if (!tr.isOK()) {
            System.out.println(tr);
            throw new RuntimeException("test fails");
        }

        System.out.println("delete generated files with non-ASCII names");
        recursiveDelete(UnicodeTestSrc);
        recursiveDelete(UnicodeTestClasses);

        /*
         * test in whatever the default locale is
         */
        runJarTests();

        /*
         * if the Japanese locale is available, test in that locale as well
         */
        if (setLocale(Locale.JAPANESE)) {
            runJarTests();
        }

       /*
        * if we can switch to a C locale, then test whether jar files with
        * non-ASCII characters in the manifest still work in this crippled
        * environment
        */
        if (setLocale(Locale.ENGLISH)) {
            runJarTests();
        }
        // thats it we are outta here
    }

    static void runJarTests() {
        System.out.println("running test app using newly built jar file in " +
                Locale.getDefault());
        runTest(UnicodeTestJar);

        System.out.println("running test app using jar file " +
                "(built with Solaris UTF-8 locale) in " + Locale.getDefault());
        runTest(SolarisUnicodeTestJar);
    }

    static void runTest(File testJar) {
        TestResult tr = doExec(javaCmd, "-jar", testJar.getAbsolutePath());
        if (!tr.isOK()) {
            System.out.println(tr);
            throw new RuntimeException("test fails");
        }
    }

    static boolean setLocale(Locale desired) {
        if (Locale.getDefault().equals(desired)) {
            return true;  // already set nothing more
        }
        for (Locale l : Locale.getAvailableLocales()) {
            if (l == desired) {
                Locale.setDefault(l);
                return true;
            }
        }
        return false;
    }

    static String generateSources() throws Exception {
        String commandLineClassNameSuffix = commandLineClassNameSuffix();
        String commandLineClassName = "ClassA" + commandLineClassNameSuffix;
        String manifestClassName = "ClassB" +
                (hasUnicodeFileSystem() ? unicode : commandLineClassNameSuffix);

        generateSource(commandLineClassName, manifestClassName);
        generateSource(manifestClassName, commandLineClassName);
        generateManifest(manifestClassName);
        return commandLineClassName;
    }

    private static final String defaultEncoding = Charset.defaultCharset().name();

    // language names taken from java.util.Locale.getDisplayLanguage for the respective language
    private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
    private static final String s_chinese = "\u4e2d\u6587";
    private static final String t_chinese = "\u4e2d\u6587";
    private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
    private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
    private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
    private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
    private static final String japanese = "\u65e5\u672c\u8a9e";
    private static final String korean = "\ud55c\uad6d\uc5b4";
    private static final String lithuanian = "Lietuvi\u0173";
    private static final String czech = "\u010de\u0161tina";
    private static final String turkish = "T\u00fcrk\u00e7e";
    private static final String spanish = "espa\u00f1ol";
    private static final String thai = "\u0e44\u0e17\u0e22";
    private static final String unicode = arabic + s_chinese + t_chinese
            + russian + hindi + greek + hebrew + japanese + korean
            + lithuanian + czech + turkish + spanish + thai;

    private static String commandLineClassNameSuffix() {

        // Mapping from main platform encodings to language names
        // for Unix and Windows, respectively. Use empty suffix
        // for Windows encodings where OEM encoding differs.
        // Use null if encoding isn't used.
        String[][] names = {
            { "UTF-8",          unicode,        ""              },
            { "windows-1256",   null,           ""              },
            { "iso-8859-6",     arabic,         null            },
            { "GBK",            s_chinese,      s_chinese       },
            { "GB18030",        s_chinese,      s_chinese       },
            { "GB2312",         s_chinese,      null            },
            { "x-windows-950",  null,           t_chinese       },
            { "x-MS950-HKSCS",  null,           t_chinese       },
            { "x-euc-tw",       t_chinese,      null            },
            { "Big5",           t_chinese,      null            },
            { "Big5-HKSCS",     t_chinese,      null            },
            { "windows-1251",   null,           ""              },
            { "iso-8859-5",     russian,        null            },
            { "koi8-r",         russian,        null            },
            { "windows-1253",   null,           ""              },
            { "iso-8859-7",     greek,          null            },
            { "windows-1255",   null,           ""              },
            { "iso8859-8",      hebrew,         null            },
            { "windows-31j",    null,           japanese        },
            { "x-eucJP-Open",   japanese,       null            },
            { "x-EUC-JP-LINUX", japanese,       null            },
            { "x-pck",          japanese,       null            },
            { "x-windows-949",  null,           korean          },
            { "euc-kr",         korean,         null            },
            { "windows-1257",   null,           ""              },
            { "iso-8859-13",    lithuanian,     null            },
            { "windows-1250",   null,           ""              },
            { "iso-8859-2",     czech,          null            },
            { "windows-1254",   null,           ""              },
            { "iso-8859-9",     turkish,        null            },
            { "windows-1252",   null,           ""              },
            { "iso-8859-1",     spanish,        null            },
            { "iso-8859-15",    spanish,        null            },
            { "x-windows-874",  null,           thai            },
            { "tis-620",        thai,           null            },
        };

        int column = isWindows ? 2 : 1;
        for (int i = 0; i < names.length; i++) {
             if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
                 return names[i][column];
             }
         }
         return "";
    }

    private static boolean hasUnicodeFileSystem() {
        return (isWindows) ? true : defaultEncoding.equalsIgnoreCase("UTF-8");
    }

    private static void generateSource(String thisClass, String otherClass) throws Exception {
        File file = new File(UnicodeTestSrc, thisClass + JAVA_FILE_EXT);
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        out.write("public class " + thisClass + " {\n");
        out.write("    public static void main(String[] args) {\n");
        out.write("        if (!" + otherClass + "." + otherClass.toLowerCase() + "().equals(\"" + otherClass + "\")) {\n");
        out.write("            throw new RuntimeException();\n");
        out.write("        }\n");
        out.write("    }\n");
        out.write("    public static String " + thisClass.toLowerCase() + "() {\n");
        out.write("        return \"" + thisClass + "\";\n");
        out.write("    }\n");
        out.write("}\n");
        out.close();
    }

    private static void generateManifest(String mainClass) throws Exception {
        File file = new File(UnicodeTestSrc, "MANIFEST.MF");
        FileOutputStream out = new FileOutputStream(file);
        out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
        // Header lines are limited to 72 bytes.
        // The manifest spec doesn't say we have to break at character boundaries,
        // so we rudely break at byte boundaries.
        byte[] headerBytes = ("Main-Class: " + mainClass + "\n").getBytes("UTF-8");
        if (headerBytes.length <= 72) {
            out.write(headerBytes);
        } else {
            out.write(headerBytes, 0, 72);
            int start = 72;
            while (headerBytes.length > start) {
                out.write((byte) '\n');
                out.write((byte) ' ');
                int count = Math.min(71, headerBytes.length - start);
                out.write(headerBytes, start, count);
                start += count;
            }
        }
        out.close();
    }
}