Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/nio/cs/JISAutoDetectTest.java
38839 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4087261 418459226* @summary Make sure to determine Japanese text encoding as correctly27* as possible.28*/2930import java.nio.charset.*;31import java.nio.*;3233public class JISAutoDetectTest {3435class TestData {36byte[] input;37byte[] input2; // for second call38String expectedCharset;39}40TestData[] data = new TestData[50];4142public static void main(String[] argv) throws Exception {43JISAutoDetectTest test = new JISAutoDetectTest();44test.execute();45}4647void execute() throws Exception {48CharBuffer output = CharBuffer.allocate(128);49CharBuffer expectedOutput = CharBuffer.allocate(128);5051for (int i = 0; i < data.length; i++) {52if (data[i] == null)53break;5455CharsetDecoder autoDetect = Charset.forName("JISAutoDetect").newDecoder();56CharsetDecoder dec = Charset.forName(data[i].expectedCharset).newDecoder();57CoderResult ncr, mcr;58output.clear();59expectedOutput.clear();60ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input),61output,62true);63mcr = dec.decode(ByteBuffer.wrap(data[i].input),64expectedOutput,65true);6667if (data[i].input2 != null) {68ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input2),69output,70true);71mcr = dec.decode(ByteBuffer.wrap(data[i].input2),72expectedOutput,73true);74}75String testNumber = " (test#: " + i + ")";76if (ncr != mcr)77throw new Exception("JISAutoDetect returned a wrong result");78output.flip();79expectedOutput.flip();80if (output.limit() != expectedOutput.limit())81throw new Exception("JISAutoDetect returned a wrong length"+testNumber);8283for (int x = 0; x < output.limit(); x++) {84if (expectedOutput.charAt(x) != output.charAt(x))85throw new Exception("JISAutoDetect returned a wrong string"+testNumber);86}87}88}8990public JISAutoDetectTest() {91int i = 0;9293// 094data[i] = new TestData();95data[i].input = new byte[] { (byte)'C', (byte)'o', (byte)'p', (byte)'y',96(byte)'r', (byte)'i', (byte)'g', (byte)'h',97(byte)'t', (byte)' ', (byte)0xa9, (byte)' ',98(byte)'1', (byte)'9', (byte)'9', (byte)'8' };99data[i].expectedCharset = "SJIS";100101// 1102i++;103data[i] = new TestData();104data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,105(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,106(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,107(byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 };108data[i].expectedCharset = "SJIS";109110// 2111i++;112data[i] = new TestData();113data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,114(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,115(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde};116data[i].expectedCharset = "SJIS";117118// 3119i++;120data[i] = new TestData();121data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,122(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,123(byte)0xc3, (byte)0xd1, (byte)0xbd };124data[i].expectedCharset = "SJIS";125126// 4127i++;128data[i] = new TestData();129data[i].input = new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa };130data[i].expectedCharset = "SJIS";131132// 5133i++;134data[i] = new TestData();135data[i].input = new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,136(byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca };137data[i].expectedCharset = "EUC_JP";138139// 6140i++;141data[i] = new TestData();142data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,143(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,144(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,145(byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 };146data[i].expectedCharset = "EUC_JP";147148// 7 (for 4184592)149i++;150data[i] = new TestData();151data[i].input = new byte[] { (byte)'a', (byte)'b', (byte)'c' };152data[i].input2 = new byte[] { (byte)0x1b, (byte)'$', (byte)'B',153(byte)'#', (byte)'4', (byte)'$', (byte)'5',154(byte)0x1b, (byte)'(', (byte)'B' };155data[i].expectedCharset = "ISO2022JP";156}157}158159160