1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/*
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary test Bug 4199484
* @modules jdk.charsets
* @run main ConverterTest
* @bug 4199484 4199599 4199601 4199602 4159519 4201529 4199604 4201532 4947038 6217210
*/
import java.util.*;
import java.io.*;
/*
* (C) Copyright IBM Corp. 1999 - All Rights Reserved
*
* The original version of this source code and documentation is
* copyrighted and owned by IBM. These materials are provided
* under terms of a License Agreement between IBM and Sun.
* This technology is protected by multiple US and International
* patents. This notice and attribution to IBM may not be removed.
*/
public class ConverterTest extends TestFmwk {
public static void main(String[] args) throws Exception {
new ConverterTest().run(args);
}
public void test4199484() throws Exception {
checkPages(new String[] { "Cp33722" });
}
public void test4199599() throws Exception {
checkPages(new String[] { "Cp930", "Cp939" });
}
public void test4199601() throws Exception {
checkPages(new String[] { "Cp942" });
}
public void test4199602() throws Exception {
checkPages(new String[] { "Cp943" });
}
public void test6217210() throws Exception {
checkPages(new String[] { "Cp833" });
}
public void test4159519() throws Exception {
checkPages(new String[] { "Cp037", "Cp1025", "Cp1026", "Cp1112" });
checkPages(new String[] { "Cp1122", "Cp1123", "Cp273", "Cp277" });
checkPages(new String[] { "Cp278", "Cp280", "Cp284", "Cp285" });
checkPages(new String[] { "Cp297", "Cp420", "Cp424", "Cp500" });
checkPages(new String[] { "Cp838", "Cp870", "Cp871", "Cp875" });
checkPages(new String[] { "Cp918", "Cp930", "Cp935", "Cp937" });
checkPages(new String[] { "Cp939" });
}
public void test4201529() throws Exception {
// Test fallback mapping for U+00B7
byte[] b = new String("\u00B7").getBytes("Cp1381");
int b1 = b[0] & 0xff;
int b2 = b[1] & 0xff;
if (b.length != 2 || b1 != 0xa1 || b2 != 0xa4)
errln("Error in Converter: Cp1381");
}
public void test4199604() throws Exception {
checkPages(new String[] { "Cp970" });
}
public void test4201532() throws Exception {
checkPages(new String[] { "Cp1383" });
}
private static class Parameter {
public final String value;
public final boolean statefull;
private final Vector nativeValues = new Vector();
private final Vector unicodeValues = new Vector();
public Parameter(final String param) throws IOException {
final int ndx = param.indexOf(":");
if (ndx >= 0) {
value = param.substring(0, ndx);
} else {
value = param;
}
final String args = (ndx < 0) ? "" : param.substring(ndx+1);
boolean isStatefull = false;
for (int i = 0; i < args.length(); i++) {
final char flag = args.charAt(i);
switch (flag) {
case 's':
isStatefull = true;
break;
default:
}
}
final String fileName = value+".b2c";
final FileReader f = new FileReader(new File(System.getProperty("test.src", "."), fileName));
final BufferedReader in = new BufferedReader(f);
String line = in.readLine();
while (line != null) {
if (line.startsWith("#")) {
//ignore all comments except ones that indicate this is a
//statefull conversion.
if (line.indexOf("STATEFULL") > 0) {
isStatefull = true;
}
} else {
final StringTokenizer tokenizer = new StringTokenizer(line);
String key = tokenizer.nextToken();
String value = tokenizer.nextToken();
if (key.startsWith("0x")) key = key.substring(2);
if (value.startsWith("0x")) value = value.substring(2);
final char c = (char)Integer.parseInt(value, 16);
final String unicodeValue = String.valueOf(c);
final long keyValue = Long.parseLong(key, 16);
if (isStatefull) {
final int keyLength = key.length();
if (keyLength == 2) {
byte[] nativeValue = new byte[1];
nativeValue[0] = (byte)((keyValue) & 0x00FF);
nativeValues.addElement(nativeValue);
unicodeValues.addElement(unicodeValue);
} else if (keyLength == 8) {
byte[] nativeValue = new byte[4];
nativeValue[0] = 0x0E;
nativeValue[1] = (byte)((keyValue >> 16) & 0x00FF);
nativeValue[2] = (byte)((keyValue >> 8) & 0x00FF);
nativeValue[3] = 0x0F;
nativeValues.addElement(nativeValue);
unicodeValues.addElement(unicodeValue);
} else {
System.err.println("Agh!");
}
} else {
if (key.length() == 2) {
byte[] nativeValue = new byte[1];
nativeValue[0] = (byte)(keyValue & 0x00FF);
nativeValues.addElement(nativeValue);
unicodeValues.addElement(unicodeValue);
} else if (key.length() == 4) {
byte[] nativeValue = new byte[2];
nativeValue[0] = (byte)((keyValue >> 8) & 0x00FF);
nativeValue[1] = (byte)((keyValue) & 0x00FF);
nativeValues.addElement(nativeValue);
unicodeValues.addElement(unicodeValue);
} else {
byte[] nativeValue = new byte[3];
nativeValue[0] = (byte)((keyValue >> 16) & 0x00FF);
nativeValue[1] = (byte)((keyValue >> 8) & 0x00FF);
nativeValue[2] = (byte)((keyValue) & 0x00FF);
nativeValues.addElement(nativeValue);
unicodeValues.addElement(unicodeValue);
}
}
}
line = in.readLine();
}
statefull = isStatefull;
}
public String toString() {
return value;
}
public void getMapping(final Vector keys, final Vector values, final boolean toUnicode)
throws IOException {
final Hashtable temp = new Hashtable();
for (int i = nativeValues.size() - 1; i >= 0; --i) {
final byte[] key = (byte[])nativeValues.elementAt(i);
final String value = (String)unicodeValues.elementAt(i);
if (toUnicode) {
final String keyString = printable(key);
if (temp.get(keyString) == null) {
temp.put(keyString, keyString);
keys.addElement(key);
values.addElement(value);
}
} else {
if (temp.get(value) == null) {
temp.put(value, value);
keys.addElement(value);
values.addElement(key);
}
}
}
}
}
public void checkPages(String[] args) {
for (int j = 0; j < args.length; j++) {
logln("Checking converter: "+args[j]);
boolean err = false;
try {
final Parameter param = new Parameter(args[j]);
final Vector keys = new Vector();
final Vector values = new Vector();
param.getMapping(keys, values, true);
for (int i = 0; i < keys.size(); i++) {
final byte[] key = (byte[])keys.elementAt(i);
final String value = (String)values.elementAt(i);
try {
final String actualValue = new String(key, param.value);
if (!value.equals(actualValue)) {
logln(printable(key)+" ==> "+printable(value)+" produced "+printable(actualValue));
err = true;
}
} catch (UnsupportedEncodingException e) {
logln(param.value+" encoding not supported: "+e);
err = true;
break;
}
}
keys.removeAllElements();
values.removeAllElements();
param.getMapping(keys, values, false);
for (int i = 0; i < keys.size(); i++) {
final String key = (String)keys.elementAt(i);
final byte[] value = (byte[])values.elementAt(i);
try {
final byte[] actualValue = key.getBytes(param.value);
boolean diff = false;
if (value.length != actualValue.length) {
logln(printable(key)+" ==> "+printable(value)+" produced "+printable(actualValue));
err = true;
} else {
for (int k = 0; k < value.length; k++) {
if (value[k] != actualValue[k]) {
logln(printable(key)+" ==> "+printable(value)+" produced "+printable(actualValue));
err = true;
break;
}
}
}
} catch (UnsupportedEncodingException e) {
logln(param.value+" encoding not supported: "+e);
err = true;
break;
}
}
} catch (IOException e) {
logln("Could not load table: "+e);
err = true;
}
if (err) {
errln("Error in converter: "+args[j]);
} else {
logln(" passed.");
}
}
}
protected static String printable(String c) {
final StringBuffer buffer = new StringBuffer();
for (int i = 0; i < c.length(); i++) {
buffer.append(printable(c.charAt(i)));
}
return buffer.toString();
}
protected static String printable(byte c) {
final StringBuffer buffer = new StringBuffer("0x");
final int value = ((int)c) & 0x00FF;
buffer.append(HEX_DIGIT[(value & 0x00F0) >> 4]);
buffer.append(HEX_DIGIT[(value & 0x000F)]);
return buffer.toString();
}
protected static String printable(byte[] c) {
final StringBuffer buffer = new StringBuffer("[");
for (int i = 0; i < c.length; i++) {
final int value = ((int)c[i]) & 0x00FF;
buffer.append(HEX_DIGIT[(value & 0x00F0) >> 4]);
buffer.append(HEX_DIGIT[(value & 0x000F)]);
buffer.append(" ");
}
buffer.append("]");
return buffer.toString();
}
protected static String printable(char[] c) {
final StringBuffer buffer = new StringBuffer("[");
for (int i = 0; i < c.length; i++) {
buffer.append(printable(c[i]));
}
buffer.append("]");
return buffer.toString();
}
protected static String printable(char c) {
final StringBuffer buffer = new StringBuffer("\\u");
final int value = ((int)c) & 0xFFFF;
buffer.append(HEX_DIGIT[(value & 0xF000) >> 12]);
buffer.append(HEX_DIGIT[(value & 0x0F00) >> 8]);
buffer.append(HEX_DIGIT[(value & 0x00F0) >> 4]);
buffer.append(HEX_DIGIT[(value & 0x000F)]);
return buffer.toString();
}
static final char[] HEX_DIGIT = {'0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F'};
}
|