alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (I18NArgTest.java)

This example Java source code file (I18NArgTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

error, i18nargtest, ioexception, katakana, ms932, runtimeexception, string, testhelper, testresult

The I18NArgTest.java Java example source code

/*
 * Copyright (c) 2013, 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 8016110
 * @summary verify Japanese character in an argument are treated correctly
 * @compile -XDignore.symbol.file I18NArgTest.java
 * @run main I18NArgTest
 */
import java.io.IOException;

public class I18NArgTest extends TestHelper {
    public static void main(String... args) throws IOException {
        if (!isWindows) {
            return;
        }
        if (!"MS932".equals(System.getProperty("sun.jnu.encoding"))) {
            System.err.println("MS932 encoding not set, test skipped");
            return;
        }
        if (args.length == 0) {
            execTest(0x30bd); // MS932 Katakana SO, 0x835C
        } else {
            testCharacters(args);
        }
    }
    static void execTest(int unicodeValue) {
        String hexValue = Integer.toHexString(unicodeValue);
        String unicodeStr = Character.toString((char)unicodeValue);
        execTest("\"" + unicodeStr + "\"", hexValue);
        execTest("\\" + unicodeStr + "\\", hexValue);
        execTest(" " + unicodeStr + " ", hexValue);
        execTest("'" + unicodeStr + "'", hexValue);
        execTest("\t" + unicodeStr + "\t", hexValue);
        execTest("*" + unicodeStr + "*", hexValue);
        execTest("?" + unicodeStr + "?", hexValue);

        execTest("\"" + unicodeStr + unicodeStr + "\"", hexValue + hexValue);
        execTest("\\" + unicodeStr + unicodeStr + "\\", hexValue + hexValue);
        execTest(" " + unicodeStr + unicodeStr + " ", hexValue + hexValue);
        execTest("'" + unicodeStr + unicodeStr + "'", hexValue + hexValue);
        execTest("\t" + unicodeStr + unicodeStr + "\t", hexValue + hexValue);
        execTest("*" + unicodeStr + unicodeStr + "*", hexValue + hexValue);
        execTest("?" + unicodeStr + unicodeStr + "?", hexValue + hexValue);

        execTest("\"" + unicodeStr + "a" + unicodeStr + "\"", hexValue + "61" + hexValue);
        execTest("\\" + unicodeStr + "a" + unicodeStr + "\\", hexValue + "61" + hexValue);
        execTest(" " + unicodeStr + "a" + unicodeStr + " ", hexValue + "61"+ hexValue);
        execTest("'" + unicodeStr + "a" + unicodeStr + "'", hexValue + "61"+ hexValue);
        execTest("\t" + unicodeStr + "a" + unicodeStr + "\t", hexValue + "61"+ hexValue);
        execTest("*" + unicodeStr + "a" + unicodeStr + "*", hexValue + "61"+ hexValue);
        execTest("?" + unicodeStr + "a" + unicodeStr + "?", hexValue + "61"+ hexValue);

        execTest("\"" + unicodeStr + "\u00b1" + unicodeStr + "\"", hexValue + "b1" + hexValue);
        execTest("\\" + unicodeStr + "\u00b1" + unicodeStr + "\\", hexValue + "b1" + hexValue);
        execTest(" " + unicodeStr + "\u00b1" + unicodeStr + " ", hexValue + "b1"+ hexValue);
        execTest("'" + unicodeStr + "\u00b1" + unicodeStr + "'", hexValue + "b1"+ hexValue);
        execTest("\t" + unicodeStr + "\u00b1" + unicodeStr + "\t", hexValue + "b1"+ hexValue);
        execTest("*" + unicodeStr + "\u00b1" + unicodeStr + "*", hexValue + "b1"+ hexValue);
        execTest("?" + unicodeStr + "\u00b1" + unicodeStr + "?", hexValue + "b1"+ hexValue);
    }
    static void execTest(String unicodeStr, String hexValue) {
        TestResult tr = doExec(javaCmd,
                "-Dtest.src=" + TEST_SOURCES_DIR.getAbsolutePath(),
                "-Dtest.classes=" + TEST_CLASSES_DIR.getAbsolutePath(),
                "-cp", TEST_CLASSES_DIR.getAbsolutePath(),
                "I18NArgTest", unicodeStr, hexValue);
        System.out.println(tr.testOutput);
        if (!tr.isOK()) {
            System.err.println(tr);
            throw new RuntimeException("test fails");
        }
    }
    static void testCharacters(String... args) {
        String input = args[0];
        String expected = args[1];
        String hexValue = "";
        for (int i = 0; i < input.length(); i++) {
            hexValue = hexValue.concat(Integer.toHexString((int)input.charAt(i)));
        }
        System.out.println("input:" + input);
        System.out.println("expected:" + expected);
        System.out.println("obtained:" + hexValue);
        if (!hexValue.contains(expected)) {
            String message = "Error: output does not contain expected value" +
                "expected:" + expected + " obtained:" + hexValue;
            throw new RuntimeException(message);
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java I18NArgTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.