jdk/test/tools/launcher/UnicodeTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Used by UnicodeTest.sh.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This class creates Java source files using Unicode characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * that test the limits of what's possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * - in situations where the platform encoding imposes limits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *   (command line arguments, non-Unicode file system)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * - in situations where full Unicode is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   (file system access in UTF-8 locales and on Windows 2000++,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *    jar file contents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Norbert Lindenberg
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class UnicodeTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String commandLineClassNameSuffix = commandLineClassNameSuffix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String commandLineClassName = "ClassA" + commandLineClassNameSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        String manifestClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (hasUnicodeFileSystem()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            manifestClassName = "ClassB" + unicode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            manifestClassName = "ClassB" + commandLineClassNameSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        generateSource(commandLineClassName, manifestClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        generateSource(manifestClassName, commandLineClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        generateManifest(manifestClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println(commandLineClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final String fileSeparator = System.getProperty("file.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final String defaultEncoding = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // language names taken from java.util.Locale.getDisplayLanguage for the respective language
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String s_chinese = "\u4e2d\u6587";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final String t_chinese = "\u4e2d\u6587";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final String japanese = "\u65e5\u672c\u8a9e";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final String korean = "\ud55c\uad6d\uc5b4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final String lithuanian = "Lietuvi\u0173";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final String czech = "\u010de\u0161tina";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final String turkish = "T\u00fcrk\u00e7e";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final String spanish = "espa\u00f1ol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final String thai = "\u0e44\u0e17\u0e22";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final String unicode = arabic + s_chinese + t_chinese
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            + russian + hindi + greek + hebrew + japanese + korean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            + lithuanian + czech + turkish + spanish + thai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static String commandLineClassNameSuffix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Mapping from main platform encodings to language names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // for Unix and Windows, respectively. Use empty suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // for Windows encodings where OEM encoding differs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Use null if encoding isn't used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        String[][] names = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            { "UTF-8",          unicode,        ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            { "windows-1256",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            { "iso-8859-6",     arabic,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            { "GBK",            s_chinese,      s_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            { "GB18030",        s_chinese,      s_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            { "GB2312",         s_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            { "x-windows-950",  null,           t_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            { "x-MS950-HKSCS",  null,           t_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            { "x-euc-tw",       t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            { "Big5",           t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            { "Big5-HKSCS",     t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            { "windows-1251",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            { "iso-8859-5",     russian,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            { "koi8-r",         russian,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            { "windows-1253",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            { "iso-8859-7",     greek,          null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            { "windows-1255",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            { "iso8859-8",      hebrew,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            { "windows-31j",    null,           japanese        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            { "x-eucJP-Open",   japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            { "x-EUC-JP-LINUX", japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            { "x-pck",          japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            { "x-windows-949",  null,           korean          },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            { "euc-kr",         korean,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            { "windows-1257",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            { "iso-8859-13",    lithuanian,     null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            { "windows-1250",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            { "iso-8859-2",     czech,          null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            { "windows-1254",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            { "iso-8859-9",     turkish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            { "windows-1252",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            { "iso-8859-1",     spanish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            { "iso-8859-15",    spanish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            { "x-windows-874",  null,           thai            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            { "tis-620",        thai,           null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (osName.startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            column = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            column = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                 return names[i][column];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static boolean hasUnicodeFileSystem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (osName.startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return ! osName.startsWith("Windows 9") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                   ! osName.equals("Windows Me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return defaultEncoding.equalsIgnoreCase("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static void generateSource(String thisClass, String otherClass) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        String fileName = "UnicodeTest-src" + fileSeparator + thisClass + ".java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        out.write("public class " + thisClass + " {\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        out.write("    public static void main(String[] args) {\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        out.write("        if (!" + otherClass + "." + otherClass.toLowerCase() + "().equals(\"" + otherClass + "\")) {\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        out.write("            throw new RuntimeException();\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        out.write("        }\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        out.write("    }\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        out.write("    public static String " + thisClass.toLowerCase() + "() {\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        out.write("        return \"" + thisClass + "\";\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        out.write("    }\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        out.write("}\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private static void generateManifest(String mainClass) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        String fileName = "UnicodeTest-src" + fileSeparator + "MANIFEST.MF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        FileOutputStream out = new FileOutputStream(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // Header lines are limited to 72 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // The manifest spec doesn't say we have to break at character boundaries,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // so we rudely break at byte boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        byte[] headerBytes = ("Main-Class: " + mainClass + "\n").getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (headerBytes.length <= 72) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            out.write(headerBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            out.write(headerBytes, 0, 72);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            int start = 72;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            while (headerBytes.length > start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                out.write((byte) '\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                out.write((byte) ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                int count = Math.min(71, headerBytes.length - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                out.write(headerBytes, start, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                start += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}