TestUTF8BOM.java revision 395
855N/A/*
1472N/A * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
855N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
855N/A *
855N/A * This code is free software; you can redistribute it and/or modify it
855N/A * under the terms of the GNU General Public License version 2 only, as
855N/A * published by the Free Software Foundation.
855N/A *
855N/A * This code is distributed in the hope that it will be useful, but WITHOUT
855N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
855N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
855N/A * version 2 for more details (a copy is included in the LICENSE file that
855N/A * accompanied this code).
855N/A *
855N/A * You should have received a copy of the GNU General Public License version
855N/A * 2 along with this work; if not, write to the Free Software Foundation,
855N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
855N/A *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1472N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1472N/A * have any questions.
855N/A */
855N/A
855N/A/* @test
855N/A @bug 4508058 6378911
855N/A @summary Check if UTF8 decoder handles BOM correctly
855N/A */
855N/A
855N/Aimport java.io.IOException;
855N/Aimport java.nio.BufferOverflowException;
855N/Aimport java.nio.*;
855N/Aimport java.nio.charset.*;
855N/A
855N/A/* The fix for 6378911 is to backout the change we made for 4508058,
855N/A so this regtest is modified accordingly to leave the beginning
855N/A BOM untouched during decoding.
855N/A */
855N/Apublic class TestUTF8BOM {
855N/A private static ByteBuffer bf = ByteBuffer.allocateDirect(1000);
855N/A private static void testDecode(String expected, byte[] input)
855N/A throws Exception
855N/A {
855N/A String out = new String(input, "utf8");
855N/A if (!out.equals(expected)) {
855N/A failureReport (out, expected);
855N/A throw new Exception("UTF_8 Decoding test failed");
855N/A }
855N/A
855N/A //try directBuffer.
855N/A bf.clear();
855N/A bf.put(input).flip();
855N/A out = Charset.forName("UTF-8")
855N/A .decode(bf)
855N/A .toString();
855N/A if (!out.equals(expected)) {
855N/A failureReport (out, expected);
855N/A throw new Exception("UTF_8 Decoding test failed(directbuffer)");
855N/A }
855N/A }
855N/A
855N/A private static void failureReport(String testStr,
855N/A String expected) {
855N/A
855N/A System.err.println ("Expected Characters:");
855N/A for (int i = 0; i < expected.length() ; i++) {
855N/A System.out.println("expected char[" + i + "] : " +
855N/A Integer.toHexString((int)expected.charAt(i)) +
855N/A " obtained char[" + i + "] : " +
Integer.toHexString((int)testStr.charAt(i)));
}
}
public static void main (String[] args) throws Exception {
// Test 1: with BOM at beginning
testDecode("\ufeff\u0092\u0093",
new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf,
(byte) 0xc2, (byte) 0x92,
(byte) 0xc2, (byte) 0x93 });
// Test 2: with BOM at middle
testDecode("\u9200\ufeff\u9300",
new byte[] { (byte) 0xe9, (byte) 0x88, (byte) 0x80,
(byte) 0xef, (byte) 0xbb, (byte) 0xbf,
(byte) 0xe9, (byte) 0x8c, (byte) 0x80 });
// Test 3: with BOM at end
testDecode("\u9200\u9300\ufeff",
new byte[] { (byte) 0xe9, (byte) 0x88, (byte) 0x80,
(byte) 0xe9, (byte) 0x8c, (byte) 0x80,
(byte) 0xef, (byte) 0xbb, (byte) 0xbf });
System.err.println ("\nPASSED UTF-8 decode BOM test");
}
}