SurrogateGB18030Test.java revision 2362
0N/A/*
1988N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/* @test
1879N/A @bug 4896454
1879N/A @summary Check GB18030 surrogate encoding/decoding handling
1879N/A */
1879N/A
1879N/Aimport java.nio.*;
1879N/Aimport java.nio.charset.*;
1879N/A
1879N/Apublic class SurrogateGB18030Test {
1879N/A public static void main(String[] args) throws Exception {
1879N/A SurrogateGB18030Test test = new SurrogateGB18030Test();
1879N/A
1879N/A test.roundtripTest();
1879N/A
1879N/A /**
1879N/A * Valid Surrogate pair and 4 byte GB18030 representation
1879N/A */
1879N/A
1879N/A String inputString = "\uD800\uDC00";
1879N/A
0N/A byte[] expectedBytes = { (byte)0x90,
0N/A (byte)0x30,
0N/A (byte)0x81,
0N/A (byte)0x30
0N/A };
0N/A test.encodeTest(inputString, expectedBytes);
0N/A
0N/A /**
0N/A * Vice-versa : check that 4 byte GB18030 value encodes correctly
0N/A */
0N/A
0N/A String expectedStr = "\uDBFF\uDFFF";
0N/A
0N/A byte[] inputBytes = { (byte)0xe3,
0N/A (byte)0x32,
0N/A (byte)0x9a,
0N/A (byte)0x35
0N/A };
0N/A
0N/A
0N/A test.decodeTest(inputBytes, expectedStr);
0N/A
0N/A }
0N/A
0N/A private void roundtripTest() throws Exception
0N/A {
0N/A byte[] ba;
0N/A char[] pair = new char[2];
0N/A for (char high = '\ud800'; high <= '\udbff'; high++) {
0N/A for (char low = '\udc00'; low <= '\udfff'; low++) {
63N/A pair[0] = high;
63N/A pair[1] = low;
0N/A String s = new String(pair);
63N/A if (!s.equals(new String(s.getBytes("gb18030"), "gb18030")))
63N/A throw new Exception ("GB18030 roundtrip failure");
460N/A }
63N/A }
63N/A
63N/A }
63N/A
2958N/A private void encodeTest(String inputString, byte[] expectedBytes)
63N/A throws Exception
63N/A {
63N/A byte[] encoded = inputString.getBytes("GB18030");
63N/A
63N/A CharBuffer cb = CharBuffer.wrap(inputString.toCharArray());
0N/A ByteBuffer bb = ByteBuffer.allocate(4);
63N/A
0N/A CharsetEncoder encoder = Charset.forName("GB18030").newEncoder();
0N/A encoder.encode(cb, bb, true);
0N/A
0N/A bb.flip();
0N/A for (int i = 0 ; i < expectedBytes.length; i++) {
0N/A if (encoded[i] != expectedBytes[i]
63N/A || bb.get() != expectedBytes[i])
0N/A throw new Exception ("GB18030 encode failure");
0N/A }
0N/A }
0N/A
420N/A private void decodeTest(byte[] inputBytes, String expectedStr)
420N/A throws Exception
420N/A {
420N/A String s2 = new String(inputBytes, "GB18030");
420N/A
420N/A CharsetDecoder decoder = Charset.forName("GB18030").newDecoder();
420N/A
420N/A ByteBuffer bb = ByteBuffer.wrap(inputBytes);
420N/A CharBuffer cb = CharBuffer.allocate(2);
420N/A decoder.decode(bb, cb, true);
420N/A
0N/A cb.flip();
420N/A for (int i = 0 ; i < expectedStr.length(); i++) {
420N/A if (expectedStr.charAt(i) != cb.get()
0N/A || s2.charAt(i) != expectedStr.charAt(i))
0N/A throw new Exception ("GB18030 encode failure");
420N/A }
420N/A }
420N/A}
420N/A