Path: blob/master/test/jdk/sun/nio/cs/JISAutoDetectTest.java
41149 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* @modules jdk.charsets29*/3031import java.nio.charset.*;32import java.nio.*;3334public class JISAutoDetectTest {3536class TestData {37byte[] input;38byte[] input2; // for second call39String expectedCharset;40}41TestData[] data = new TestData[50];4243public static void main(String[] argv) throws Exception {44JISAutoDetectTest test = new JISAutoDetectTest();45test.execute();46}4748void execute() throws Exception {49CharBuffer output = CharBuffer.allocate(128);50CharBuffer expectedOutput = CharBuffer.allocate(128);5152for (int i = 0; i < data.length; i++) {53if (data[i] == null)54break;5556CharsetDecoder autoDetect = Charset.forName("JISAutoDetect").newDecoder();57CharsetDecoder dec = Charset.forName(data[i].expectedCharset).newDecoder();58CoderResult ncr, mcr;59output.clear();60expectedOutput.clear();61ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input),62output,63true);64mcr = dec.decode(ByteBuffer.wrap(data[i].input),65expectedOutput,66true);6768if (data[i].input2 != null) {69ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input2),70output,71true);72mcr = dec.decode(ByteBuffer.wrap(data[i].input2),73expectedOutput,74true);75}76String testNumber = " (test#: " + i + ")";77if (ncr != mcr)78throw new Exception("JISAutoDetect returned a wrong result");79output.flip();80expectedOutput.flip();81if (output.limit() != expectedOutput.limit())82throw new Exception("JISAutoDetect returned a wrong length"+testNumber);8384for (int x = 0; x < output.limit(); x++) {85if (expectedOutput.charAt(x) != output.charAt(x))86throw new Exception("JISAutoDetect returned a wrong string"+testNumber);87}88}89}9091public JISAutoDetectTest() {92int i = 0;9394// 095data[i] = new TestData();96data[i].input = new byte[] { (byte)'C', (byte)'o', (byte)'p', (byte)'y',97(byte)'r', (byte)'i', (byte)'g', (byte)'h',98(byte)'t', (byte)' ', (byte)0xa9, (byte)' ',99(byte)'1', (byte)'9', (byte)'9', (byte)'8' };100data[i].expectedCharset = "SJIS";101102// 1103i++;104data[i] = new TestData();105data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,106(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,107(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,108(byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 };109data[i].expectedCharset = "SJIS";110111// 2112i++;113data[i] = new TestData();114data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,115(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,116(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde};117data[i].expectedCharset = "SJIS";118119// 3120i++;121data[i] = new TestData();122data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,123(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,124(byte)0xc3, (byte)0xd1, (byte)0xbd };125data[i].expectedCharset = "SJIS";126127// 4128i++;129data[i] = new TestData();130data[i].input = new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa };131data[i].expectedCharset = "SJIS";132133// 5134i++;135data[i] = new TestData();136data[i].input = new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,137(byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca };138data[i].expectedCharset = "EUC_JP";139140// 6141i++;142data[i] = new TestData();143data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,144(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,145(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,146(byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 };147data[i].expectedCharset = "EUC_JP";148149// 7 (for 4184592)150i++;151data[i] = new TestData();152data[i].input = new byte[] { (byte)'a', (byte)'b', (byte)'c' };153data[i].input2 = new byte[] { (byte)0x1b, (byte)'$', (byte)'B',154(byte)'#', (byte)'4', (byte)'$', (byte)'5',155(byte)0x1b, (byte)'(', (byte)'B' };156data[i].expectedCharset = "ISO2022JP";157}158}159160161