TestUTF8BOM.java revision 395:bc9a0bba6e72
1/* 2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24/* @test 25 @bug 4508058 6378911 26 @summary Check if UTF8 decoder handles BOM correctly 27 */ 28 29import java.io.IOException; 30import java.nio.BufferOverflowException; 31import java.nio.*; 32import java.nio.charset.*; 33 34/* The fix for 6378911 is to backout the change we made for 4508058, 35 so this regtest is modified accordingly to leave the beginning 36 BOM untouched during decoding. 37 */ 38public class TestUTF8BOM { 39 private static ByteBuffer bf = ByteBuffer.allocateDirect(1000); 40 private static void testDecode(String expected, byte[] input) 41 throws Exception 42 { 43 String out = new String(input, "utf8"); 44 if (!out.equals(expected)) { 45 failureReport (out, expected); 46 throw new Exception("UTF_8 Decoding test failed"); 47 } 48 49 //try directBuffer. 50 bf.clear(); 51 bf.put(input).flip(); 52 out = Charset.forName("UTF-8") 53 .decode(bf) 54 .toString(); 55 if (!out.equals(expected)) { 56 failureReport (out, expected); 57 throw new Exception("UTF_8 Decoding test failed(directbuffer)"); 58 } 59 } 60 61 private static void failureReport(String testStr, 62 String expected) { 63 64 System.err.println ("Expected Characters:"); 65 for (int i = 0; i < expected.length() ; i++) { 66 System.out.println("expected char[" + i + "] : " + 67 Integer.toHexString((int)expected.charAt(i)) + 68 " obtained char[" + i + "] : " + 69 Integer.toHexString((int)testStr.charAt(i))); 70 } 71 } 72 73 public static void main (String[] args) throws Exception { 74 // Test 1: with BOM at beginning 75 testDecode("\ufeff\u0092\u0093", 76 new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf, 77 (byte) 0xc2, (byte) 0x92, 78 (byte) 0xc2, (byte) 0x93 }); 79 // Test 2: with BOM at middle 80 testDecode("\u9200\ufeff\u9300", 81 new byte[] { (byte) 0xe9, (byte) 0x88, (byte) 0x80, 82 (byte) 0xef, (byte) 0xbb, (byte) 0xbf, 83 (byte) 0xe9, (byte) 0x8c, (byte) 0x80 }); 84 85 // Test 3: with BOM at end 86 testDecode("\u9200\u9300\ufeff", 87 new byte[] { (byte) 0xe9, (byte) 0x88, (byte) 0x80, 88 (byte) 0xe9, (byte) 0x8c, (byte) 0x80, 89 (byte) 0xef, (byte) 0xbb, (byte) 0xbf }); 90 System.err.println ("\nPASSED UTF-8 decode BOM test"); 91 } 92} 93