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/*
395N/A * @test
395N/A * @bug 4087261 4184592
395N/A * @summary Make sure to determine Japanese text encoding as correctly
395N/A * as possible.
395N/A */
395N/A
395N/Aimport java.nio.charset.*;
395N/Aimport java.nio.*;
395N/A
395N/Apublic class JISAutoDetectTest {
395N/A
395N/A class TestData {
395N/A byte[] input;
395N/A byte[] input2; // for second call
395N/A String expectedCharset;
395N/A }
395N/A TestData[] data = new TestData[50];
395N/A
395N/A public static void main(String[] argv) throws Exception {
395N/A JISAutoDetectTest test = new JISAutoDetectTest();
395N/A test.execute();
395N/A }
395N/A
395N/A void execute() throws Exception {
395N/A CharBuffer output = CharBuffer.allocate(128);
395N/A CharBuffer expectedOutput = CharBuffer.allocate(128);
395N/A
395N/A for (int i = 0; i < data.length; i++) {
395N/A if (data[i] == null)
395N/A break;
395N/A
395N/A CharsetDecoder autoDetect = Charset.forName("JISAutoDetect").newDecoder();
395N/A CharsetDecoder dec = Charset.forName(data[i].expectedCharset).newDecoder();
395N/A CoderResult ncr, mcr;
395N/A output.clear();
395N/A expectedOutput.clear();
395N/A ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input),
395N/A output,
395N/A true);
395N/A mcr = dec.decode(ByteBuffer.wrap(data[i].input),
395N/A expectedOutput,
395N/A true);
395N/A
395N/A if (data[i].input2 != null) {
395N/A ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input2),
395N/A output,
395N/A true);
395N/A mcr = dec.decode(ByteBuffer.wrap(data[i].input2),
395N/A expectedOutput,
395N/A true);
395N/A }
395N/A String testNumber = " (test#: " + i + ")";
395N/A if (ncr != mcr)
395N/A throw new Exception("JISAutoDetect returned a wrong result");
395N/A output.flip();
395N/A expectedOutput.flip();
395N/A if (output.limit() != expectedOutput.limit())
395N/A throw new Exception("JISAutoDetect returned a wrong length"+testNumber);
395N/A
395N/A for (int x = 0; x < output.limit(); x++) {
395N/A if (expectedOutput.charAt(x) != output.charAt(x))
395N/A throw new Exception("JISAutoDetect returned a wrong string"+testNumber);
395N/A }
395N/A }
395N/A }
395N/A
395N/A public JISAutoDetectTest() {
395N/A int i = 0;
395N/A
395N/A // 0
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)'C', (byte)'o', (byte)'p', (byte)'y',
395N/A (byte)'r', (byte)'i', (byte)'g', (byte)'h',
395N/A (byte)'t', (byte)' ', (byte)0xa9, (byte)' ',
395N/A (byte)'1', (byte)'9', (byte)'9', (byte)'8' };
395N/A data[i].expectedCharset = "SJIS";
395N/A
395N/A // 1
395N/A i++;
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
395N/A (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
395N/A (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
395N/A (byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 };
395N/A data[i].expectedCharset = "SJIS";
395N/A
395N/A // 2
395N/A i++;
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
395N/A (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
395N/A (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde};
395N/A data[i].expectedCharset = "SJIS";
395N/A
395N/A // 3
395N/A i++;
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
395N/A (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
395N/A (byte)0xc3, (byte)0xd1, (byte)0xbd };
395N/A data[i].expectedCharset = "SJIS";
395N/A
395N/A // 4
395N/A i++;
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa };
395N/A data[i].expectedCharset = "SJIS";
395N/A
395N/A // 5
395N/A i++;
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
395N/A (byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca };
395N/A data[i].expectedCharset = "EUC_JP";
395N/A
395N/A // 6
395N/A i++;
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
395N/A (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
395N/A (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
395N/A (byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 };
395N/A data[i].expectedCharset = "EUC_JP";
395N/A
395N/A // 7 (for 4184592)
395N/A i++;
395N/A data[i] = new TestData();
395N/A data[i].input = new byte[] { (byte)'a', (byte)'b', (byte)'c' };
395N/A data[i].input2 = new byte[] { (byte)0x1b, (byte)'$', (byte)'B',
395N/A (byte)'#', (byte)'4', (byte)'$', (byte)'5',
395N/A (byte)0x1b, (byte)'(', (byte)'B' };
395N/A data[i].expectedCharset = "ISO2022JP";
395N/A }
395N/A}