|
1 /* |
|
2 * Copyright (c) 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 4087261 4184592 |
|
27 * @summary Make sure to determine Japanese text encoding as correctly |
|
28 * as possible. |
|
29 * @modules jdk.charsets |
|
30 */ |
|
31 |
|
32 import java.nio.charset.*; |
|
33 import java.nio.*; |
|
34 |
|
35 public class JISAutoDetectTest { |
|
36 |
|
37 class TestData { |
|
38 byte[] input; |
|
39 byte[] input2; // for second call |
|
40 String expectedCharset; |
|
41 } |
|
42 TestData[] data = new TestData[50]; |
|
43 |
|
44 public static void main(String[] argv) throws Exception { |
|
45 JISAutoDetectTest test = new JISAutoDetectTest(); |
|
46 test.execute(); |
|
47 } |
|
48 |
|
49 void execute() throws Exception { |
|
50 CharBuffer output = CharBuffer.allocate(128); |
|
51 CharBuffer expectedOutput = CharBuffer.allocate(128); |
|
52 |
|
53 for (int i = 0; i < data.length; i++) { |
|
54 if (data[i] == null) |
|
55 break; |
|
56 |
|
57 CharsetDecoder autoDetect = Charset.forName("JISAutoDetect").newDecoder(); |
|
58 CharsetDecoder dec = Charset.forName(data[i].expectedCharset).newDecoder(); |
|
59 CoderResult ncr, mcr; |
|
60 output.clear(); |
|
61 expectedOutput.clear(); |
|
62 ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input), |
|
63 output, |
|
64 true); |
|
65 mcr = dec.decode(ByteBuffer.wrap(data[i].input), |
|
66 expectedOutput, |
|
67 true); |
|
68 |
|
69 if (data[i].input2 != null) { |
|
70 ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input2), |
|
71 output, |
|
72 true); |
|
73 mcr = dec.decode(ByteBuffer.wrap(data[i].input2), |
|
74 expectedOutput, |
|
75 true); |
|
76 } |
|
77 String testNumber = " (test#: " + i + ")"; |
|
78 if (ncr != mcr) |
|
79 throw new Exception("JISAutoDetect returned a wrong result"); |
|
80 output.flip(); |
|
81 expectedOutput.flip(); |
|
82 if (output.limit() != expectedOutput.limit()) |
|
83 throw new Exception("JISAutoDetect returned a wrong length"+testNumber); |
|
84 |
|
85 for (int x = 0; x < output.limit(); x++) { |
|
86 if (expectedOutput.charAt(x) != output.charAt(x)) |
|
87 throw new Exception("JISAutoDetect returned a wrong string"+testNumber); |
|
88 } |
|
89 } |
|
90 } |
|
91 |
|
92 public JISAutoDetectTest() { |
|
93 int i = 0; |
|
94 |
|
95 // 0 |
|
96 data[i] = new TestData(); |
|
97 data[i].input = new byte[] { (byte)'C', (byte)'o', (byte)'p', (byte)'y', |
|
98 (byte)'r', (byte)'i', (byte)'g', (byte)'h', |
|
99 (byte)'t', (byte)' ', (byte)0xa9, (byte)' ', |
|
100 (byte)'1', (byte)'9', (byte)'9', (byte)'8' }; |
|
101 data[i].expectedCharset = "SJIS"; |
|
102 |
|
103 // 1 |
|
104 i++; |
|
105 data[i] = new TestData(); |
|
106 data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2, |
|
107 (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd, |
|
108 (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde, |
|
109 (byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 }; |
|
110 data[i].expectedCharset = "SJIS"; |
|
111 |
|
112 // 2 |
|
113 i++; |
|
114 data[i] = new TestData(); |
|
115 data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2, |
|
116 (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd, |
|
117 (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde}; |
|
118 data[i].expectedCharset = "SJIS"; |
|
119 |
|
120 // 3 |
|
121 i++; |
|
122 data[i] = new TestData(); |
|
123 data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2, |
|
124 (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd, |
|
125 (byte)0xc3, (byte)0xd1, (byte)0xbd }; |
|
126 data[i].expectedCharset = "SJIS"; |
|
127 |
|
128 // 4 |
|
129 i++; |
|
130 data[i] = new TestData(); |
|
131 data[i].input = new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa }; |
|
132 data[i].expectedCharset = "SJIS"; |
|
133 |
|
134 // 5 |
|
135 i++; |
|
136 data[i] = new TestData(); |
|
137 data[i].input = new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9, |
|
138 (byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca }; |
|
139 data[i].expectedCharset = "EUC_JP"; |
|
140 |
|
141 // 6 |
|
142 i++; |
|
143 data[i] = new TestData(); |
|
144 data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2, |
|
145 (byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd, |
|
146 (byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde, |
|
147 (byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 }; |
|
148 data[i].expectedCharset = "EUC_JP"; |
|
149 |
|
150 // 7 (for 4184592) |
|
151 i++; |
|
152 data[i] = new TestData(); |
|
153 data[i].input = new byte[] { (byte)'a', (byte)'b', (byte)'c' }; |
|
154 data[i].input2 = new byte[] { (byte)0x1b, (byte)'$', (byte)'B', |
|
155 (byte)'#', (byte)'4', (byte)'$', (byte)'5', |
|
156 (byte)0x1b, (byte)'(', (byte)'B' }; |
|
157 data[i].expectedCharset = "ISO2022JP"; |
|
158 } |
|
159 } |