395N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
395N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
395N/A *
395N/A * This code is free software; you can redistribute it and/or modify it
395N/A * under the terms of the GNU General Public License version 2 only, as
395N/A * published by the Free Software Foundation.
395N/A *
395N/A * This code is distributed in the hope that it will be useful, but WITHOUT
395N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
395N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
395N/A * version 2 for more details (a copy is included in the LICENSE file that
395N/A * accompanied this code).
395N/A *
395N/A * You should have received a copy of the GNU General Public License version
395N/A * 2 along with this work; if not, write to the Free Software Foundation,
395N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
395N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
395N/A */
395N/A
395N/A/* @test
395N/A @bug 4403848 6348426 6407730
395N/A @summary Check correctness of the UTF-16 converter in all its flavors
395N/A */
395N/A
395N/Aimport java.io.IOException;
395N/Aimport java.nio.BufferOverflowException;
395N/Aimport java.nio.*;
395N/Aimport java.nio.charset.*;
395N/A
395N/Apublic class TestUTF_16 {
395N/A
395N/A private static void testDecode(String charset,
395N/A String expected,
395N/A byte[] input)
395N/A throws Exception
395N/A {
395N/A String out = new String(input, charset);
395N/A if (!out.equals(expected)) {
395N/A failureReport (out, expected);
395N/A throw new Exception("UTF_16 Decoding test failed " + charset);
395N/A }
395N/A }
395N/A
395N/A private static void testEncode(String charset,
395N/A String input,
395N/A byte[] expected)
395N/A throws Exception
395N/A {
395N/A byte[] testBytes = input.getBytes(charset);
395N/A for (int i = 0; i< expected.length; i++)
395N/A if (testBytes[i] != expected[i])
395N/A throw new Exception("UTF_16 Encoding test failed " + charset);
395N/A
395N/A }
395N/A
395N/A private static void warn(String s) {
395N/A System.err.println("FAILED Test 4403848 UTF-16" +
395N/A s) ;
395N/A }
395N/A
395N/A private static void failureReport(String testStr,
395N/A String expected) {
395N/A
395N/A System.err.println ("Expected Characters:");
395N/A for (int i = 0; i < expected.length() ; i++) {
395N/A warn("expected char[" + i + "] : " +
395N/A Integer.toHexString((int)expected.charAt(i)) +
395N/A "obtained char[" + i + "] : " +
395N/A Integer.toHexString((int)testStr.charAt(i)));
395N/A }
395N/A }
395N/A
395N/A /*
395N/A private static void checkResult(char[] expected,
395N/A String testStr,
395N/A String testName)
395N/A throws Exception
395N/A {
395N/A if (testStr.length() != expected.length)
395N/A failureReport(testStr, expected);
395N/A
395N/A for (int i = 0; i < testStr.length(); i++) {
395N/A if (testStr.charAt(i) != expected[i]) {
395N/A failureReport(testStr, expected);
395N/A throw new Exception ("REGTEST TestUTF16 failed: "
395N/A + testName);
395N/A }
395N/A }
395N/A System.err.println ("Test " + testName + " PASSED");
395N/A return;
395N/A }
395N/A */
395N/A
395N/A private static void test() throws Exception {
395N/A
395N/A // Tests: Check decoding of UTF-16.
395N/A // Ensures correct endian polarity
395N/A // of the decoders and appropriate
395N/A // interpretation of BOM bytes where
395N/A // they are required.
395N/A
395N/A // Test 1: BigEndian UTF-16 Decoding
395N/A
395N/A testDecode("UTF_16BE", "\u0092\u0093",
395N/A new byte[] { (byte) 0x00, (byte) 0x92,
395N/A (byte) 0x00, (byte) 0x93 });
395N/A
395N/A // Test 1a: BigEndian UTF-16 Decoding. BOM bytes provided.
395N/A testDecode("UTF_16BE", "\ufeff\u0092\u0093",
395N/A new byte[] { (byte) 0xfe, (byte) 0xff,
395N/A (byte) 0x00, (byte) 0x92,
395N/A (byte) 0x00, (byte) 0x93 });
395N/A
395N/A testDecode("UTF_16LE", "\u9200\u9300",
395N/A new byte[] { (byte) 0x00, (byte) 0x92,
395N/A (byte) 0x00, (byte) 0x93 });
395N/A
395N/A // Test 2a: LittleEndian UTF-16 Decoding, BOM bytes provided.
395N/A testDecode("UTF_16LE", "\ufeff\u9200\u9300",
395N/A new byte[] { (byte) 0xff, (byte) 0xfe,
395N/A (byte) 0x00, (byte) 0x92,
395N/A (byte) 0x00, (byte) 0x93 });
395N/A
395N/A // Test 3: UTF-16 (with mandatory byte order mark) Decoding
395N/A
395N/A testDecode("UTF-16", "\u9200\u9300",
395N/A new byte[] { (byte) 0xfe, (byte) 0xff,
395N/A (byte) 0x92, (byte) 0x00,
395N/A (byte) 0x93, (byte) 0x00 });
395N/A
395N/A
395N/A // Test 3a: UTF-16 BOM omitted. This should decode OK.
395N/A testDecode("UTF-16", "\u9200\u9300",
395N/A new byte[] { (byte) 0x92, (byte) 0x00,
395N/A (byte) 0x93, (byte) 0x00 });
395N/A
395N/A
395N/A // Test 4: encoding using UTF-16
395N/A // BOM must be emitted when encoding and must be BigEndian.
395N/A
395N/A testEncode("UTF-16", "\u0123",
395N/A new byte[] { (byte) 0xfe, (byte) 0xff,
395N/A (byte) 0x01, (byte) 0x23 });
395N/A
395N/A // Test 5:
395N/A if (CoderResult.OVERFLOW !=
395N/A Charset.forName("UTF_16")
395N/A .newDecoder()
395N/A .decode((ByteBuffer)(ByteBuffer.allocate(4)
395N/A .put(new byte[]
395N/A {(byte)0xd8,(byte)0x00,
395N/A (byte)0xdc,(byte)0x01})
395N/A .flip()),
395N/A CharBuffer.allocate(1),
395N/A true)) {
395N/A throw new Exception ("REGTEST TestUTF16 Overflow test failed");
395N/A }
395N/A
395N/A // Test 6: decoding using UTF_16LE_BOM/UnicodeLittle
395N/A // UnicodeLittle should accept non-BOM byte sequence
395N/A
395N/A testDecode("UnicodeLittle", "Arial",
395N/A new byte[] { 'A', 0, 'r', 0, 'i', 0, 'a', 0, 'l', 0});
395N/A
395N/A System.err.println ("\nPASSED UTF-16 encoder test");
395N/A
395N/A // Reversed BOM in middle of stream Negative test.
395N/A
395N/A /*
395N/A boolean caughtException = false;
395N/A try {
395N/A String out = new String(new byte[] {(byte)0x00,
395N/A (byte)0x92,
395N/A (byte)0xff,
395N/A (byte)0xfe},
395N/A "UTF-16");
395N/A } catch (IOException e) { caughtException = true; }
395N/A
395N/A if (caughtException == false)
395N/A throw new Exception ("Incorrectly parsed BOM in middle of input");
395N/A */
395N/A
395N/A // Fixed included with bug 4403848 fixes buffer sizing
395N/A // issue due to non provision of additional 2 bytes
395N/A // headroom for initial BOM bytes for UTF-16 encoding.
395N/A System.err.println ("OVERALL PASS OF UTF-16 Test");
395N/A }
395N/A
395N/A public static void main (String[] args) throws Exception {
395N/A test();
395N/A }
395N/A}