796
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
|
796
|
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 |
*
|
5506
|
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.
|
796
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4087261 4184592
|
|
27 |
* @summary Make sure to determine Japanese text encoding as correctly
|
|
28 |
* as possible.
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.nio.charset.*;
|
|
32 |
import java.nio.*;
|
|
33 |
|
|
34 |
public class JISAutoDetectTest {
|
|
35 |
|
|
36 |
class TestData {
|
|
37 |
byte[] input;
|
|
38 |
byte[] input2; // for second call
|
|
39 |
String expectedCharset;
|
|
40 |
}
|
|
41 |
TestData[] data = new TestData[50];
|
|
42 |
|
|
43 |
public static void main(String[] argv) throws Exception {
|
|
44 |
JISAutoDetectTest test = new JISAutoDetectTest();
|
|
45 |
test.execute();
|
|
46 |
}
|
|
47 |
|
|
48 |
void execute() throws Exception {
|
|
49 |
CharBuffer output = CharBuffer.allocate(128);
|
|
50 |
CharBuffer expectedOutput = CharBuffer.allocate(128);
|
|
51 |
|
|
52 |
for (int i = 0; i < data.length; i++) {
|
|
53 |
if (data[i] == null)
|
|
54 |
break;
|
|
55 |
|
|
56 |
CharsetDecoder autoDetect = Charset.forName("JISAutoDetect").newDecoder();
|
|
57 |
CharsetDecoder dec = Charset.forName(data[i].expectedCharset).newDecoder();
|
|
58 |
CoderResult ncr, mcr;
|
|
59 |
output.clear();
|
|
60 |
expectedOutput.clear();
|
|
61 |
ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input),
|
|
62 |
output,
|
|
63 |
true);
|
|
64 |
mcr = dec.decode(ByteBuffer.wrap(data[i].input),
|
|
65 |
expectedOutput,
|
|
66 |
true);
|
|
67 |
|
|
68 |
if (data[i].input2 != null) {
|
|
69 |
ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input2),
|
|
70 |
output,
|
|
71 |
true);
|
|
72 |
mcr = dec.decode(ByteBuffer.wrap(data[i].input2),
|
|
73 |
expectedOutput,
|
|
74 |
true);
|
|
75 |
}
|
|
76 |
String testNumber = " (test#: " + i + ")";
|
|
77 |
if (ncr != mcr)
|
|
78 |
throw new Exception("JISAutoDetect returned a wrong result");
|
|
79 |
output.flip();
|
|
80 |
expectedOutput.flip();
|
|
81 |
if (output.limit() != expectedOutput.limit())
|
|
82 |
throw new Exception("JISAutoDetect returned a wrong length"+testNumber);
|
|
83 |
|
|
84 |
for (int x = 0; x < output.limit(); x++) {
|
|
85 |
if (expectedOutput.charAt(x) != output.charAt(x))
|
|
86 |
throw new Exception("JISAutoDetect returned a wrong string"+testNumber);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
public JISAutoDetectTest() {
|
|
92 |
int i = 0;
|
|
93 |
|
|
94 |
// 0
|
|
95 |
data[i] = new TestData();
|
|
96 |
data[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' };
|
|
100 |
data[i].expectedCharset = "SJIS";
|
|
101 |
|
|
102 |
// 1
|
|
103 |
i++;
|
|
104 |
data[i] = new TestData();
|
|
105 |
data[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 };
|
|
109 |
data[i].expectedCharset = "SJIS";
|
|
110 |
|
|
111 |
// 2
|
|
112 |
i++;
|
|
113 |
data[i] = new TestData();
|
|
114 |
data[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};
|
|
117 |
data[i].expectedCharset = "SJIS";
|
|
118 |
|
|
119 |
// 3
|
|
120 |
i++;
|
|
121 |
data[i] = new TestData();
|
|
122 |
data[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 };
|
|
125 |
data[i].expectedCharset = "SJIS";
|
|
126 |
|
|
127 |
// 4
|
|
128 |
i++;
|
|
129 |
data[i] = new TestData();
|
|
130 |
data[i].input = new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa };
|
|
131 |
data[i].expectedCharset = "SJIS";
|
|
132 |
|
|
133 |
// 5
|
|
134 |
i++;
|
|
135 |
data[i] = new TestData();
|
|
136 |
data[i].input = new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
|
|
137 |
(byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca };
|
|
138 |
data[i].expectedCharset = "EUC_JP";
|
|
139 |
|
|
140 |
// 6
|
|
141 |
i++;
|
|
142 |
data[i] = new TestData();
|
|
143 |
data[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 };
|
|
147 |
data[i].expectedCharset = "EUC_JP";
|
|
148 |
|
|
149 |
// 7 (for 4184592)
|
|
150 |
i++;
|
|
151 |
data[i] = new TestData();
|
|
152 |
data[i].input = new byte[] { (byte)'a', (byte)'b', (byte)'c' };
|
|
153 |
data[i].input2 = new byte[] { (byte)0x1b, (byte)'$', (byte)'B',
|
|
154 |
(byte)'#', (byte)'4', (byte)'$', (byte)'5',
|
|
155 |
(byte)0x1b, (byte)'(', (byte)'B' };
|
|
156 |
data[i].expectedCharset = "ISO2022JP";
|
|
157 |
}
|
|
158 |
}
|