File size: 16,729 Bytes
2795186 |
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
/*
Copyright 2005 MySQL AB, 2008 Sun Microsystems
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
software distribution.
This program 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 for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package testsuite.simple;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import testsuite.BaseTestCase;
public class CharsetTests extends BaseTestCase {
public CharsetTests(String name) {
super(name);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(CharsetTests.class);
}
public void testCP932Backport() throws Exception {
if (versionMeetsMinimum(4, 1, 12)) {
if (versionMeetsMinimum(5, 0)) {
if (!versionMeetsMinimum(5, 0, 3)) {
return;
}
}
Properties props = new Properties();
props.put("useUnicode", "true");
props.put("characterEncoding", "WINDOWS-31J");
getConnectionWithProps(props).close();
}
}
public void testNECExtendedCharsByEUCJPSolaris() throws Exception {
if (!isRunningOnJdk131()) {
if (versionMeetsMinimum(5, 0, 5)) {
char necExtendedChar = 0x3231; // 0x878A of WINDOWS-31J, NEC
// special(row13).
String necExtendedCharString = String.valueOf(necExtendedChar);
Properties props = new Properties();
props.put("useUnicode", "true");
props.put("characterEncoding", "EUC_JP_Solaris");
Connection conn2 = getConnectionWithProps(props);
Statement stmt2 = conn2.createStatement();
stmt2.executeUpdate("DROP TABLE IF EXISTS t_eucjpms");
createTable("t_eucjpms", "(c1 char(1))"
+ " default character set = eucjpms");
stmt2.executeUpdate("INSERT INTO t_eucjpms VALUES ('"
+ necExtendedCharString + "')");
this.rs = stmt2.executeQuery("SELECT c1 FROM t_eucjpms");
this.rs.next();
assertEquals(necExtendedCharString, this.rs.getString("c1"));
this.rs.close();
stmt2.close();
conn2.close();
props.put("characterSetResults", "EUC_JP_Solaris");
conn2 = getConnectionWithProps(props);
stmt2 = conn.createStatement();
this.rs = stmt2.executeQuery("SELECT c1 FROM t_eucjpms");
this.rs.next();
assertEquals(necExtendedCharString, rs.getString("c1"));
stmt2.executeUpdate("DROP TABLE t_eucjpms");
this.rs.close();
stmt2.close();
conn2.close();
}
}
}
/**
* Test data of sjis. sjis consists of ASCII, JIS-Roman, JISX0201 and
* JISX0208.
*/
public static final char[] SJIS_CHARS = new char[] { 0xFF71, // halfwidth
// katakana
// letter A,
// 0xB100 of
// SJIS, one
// of
// JISX0201.
0x65E5, // CJK unified ideograph, 0x93FA of SJIS, one of JISX0208.
0x8868, // CJK unified ideograph, 0x955C of SJIS, one of '5c'
// character.
0x2016 // 0x8161 of SJIS/WINDOWS-31J, converted to differently
// to/from ucs2
};
/**
* Test data of cp932. WINDOWS-31J consists of ASCII, JIS-Roman, JISX0201,
* JISX0208, NEC special characters(row13), NEC selected IBM special
* characters, and IBM special characters.
*/
private static final char[] CP932_CHARS = new char[] { 0xFF71, // halfwidth
// katakana
// letter A,
// 0xB100 of
// WINDOWS-31J,
// one of
// JISX0201.
0x65E5, // CJK unified ideograph, 0x93FA of WINDOWS-31J, one of
// JISX0208.
0x3231, // parenthesized ideograph stok, 0x878B of WINDOWS-31J, one
// of NEC special characters(row13).
0x67BB, // CJK unified ideograph, 0xEDC6 of WINDOWS-31J, one of NEC
// selected IBM special characters.
0x6D6F, // CJK unified ideograph, 0xFAFC of WINDOWS-31J, one of IBM
// special characters.
0x8868, // one of CJK unified ideograph, 0x955C of WINDOWS-31J, one
// of '5c' characters.
0x2225 // 0x8161 of SJIS/WINDOWS-31J, converted to differently
// to/from ucs2
};
/**
* Test data of ujis. ujis consists of ASCII, JIS-Roman, JISX0201, JISX0208,
* JISX0212.
*/
public static final char[] UJIS_CHARS = new char[] { 0xFF71, // halfwidth
// katakana
// letter A,
// 0x8EB1 of
// ujis, one
// of
// JISX0201.
0x65E5, // CJK unified ideograph, 0xC6FC of ujis, one of JISX0208.
0x7B5D, // CJK unified ideograph, 0xE4B882 of ujis, one of JISX0212
0x301C // wave dash, 0xA1C1 of ujis, convertion rule is different
// from ujis
};
/**
* Test data of eucjpms. ujis consists of ASCII, JIS-Roman, JISX0201,
* JISX0208, JISX0212, NEC special characters(row13)
*/
public static final char[] EUCJPMS_CHARS = new char[] { 0xFF71, // halfwidth
// katakana
// letter A,
// 0x8EB1 of
// ujis, one
// of
// JISX0201.
0x65E5, // CJK unified ideograph, 0xC6FC of ujis, one of JISX0208.
0x7B5D, // CJK unified ideograph, 0xE4B882 of ujis, one of JISX0212
0x3231, // parenthesized ideograph stok, 0x878A of WINDOWS-31J, one
// of NEC special characters(row13).
0xFF5E // wave dash, 0xA1C1 of eucjpms, convertion rule is
// different from ujis
};
public void testInsertCharStatement() throws Exception {
if (!isRunningOnJdk131()) {
if (versionMeetsMinimum(4, 1, 12)) {
Map testDataMap = new HashMap();
List charsetList = new ArrayList();
Map connectionMap = new HashMap();
Map connectionWithResultMap = new HashMap();
Map statementMap = new HashMap();
Map statementWithResultMap = new HashMap();
Map javaToMysqlCharsetMap = new HashMap();
charsetList.add("SJIS");
testDataMap.put("SJIS", SJIS_CHARS);
javaToMysqlCharsetMap.put("SJIS", "sjis");
charsetList.add("Shift_JIS");
testDataMap.put("Shift_JIS", SJIS_CHARS);
javaToMysqlCharsetMap.put("Shift_JIS", "sjis");
charsetList.add("CP943");
testDataMap.put("CP943", SJIS_CHARS);
javaToMysqlCharsetMap.put("CP943", "sjis");
if (versionMeetsMinimum(5, 0, 3)) {
charsetList.add("WINDOWS-31J");
testDataMap.put("WINDOWS-31J", CP932_CHARS);
javaToMysqlCharsetMap.put("WINDOWS-31J", "cp932");
charsetList.add("MS932");
testDataMap.put("MS932", CP932_CHARS);
javaToMysqlCharsetMap.put("MS932", "cp932");
charsetList.add("EUC_JP");
testDataMap.put("EUC_JP", UJIS_CHARS);
// testDataHexMap.put("EUC_JP", UJIS_CHARS_HEX);
javaToMysqlCharsetMap.put("EUC_JP", "ujis");
charsetList.add("EUC_JP_Solaris");
testDataMap.put("EUC_JP_Solaris", EUCJPMS_CHARS);
// testDataHexMap.put("EUC_JP_Solaris", EUCJPMS_CHARS_HEX);
javaToMysqlCharsetMap.put("EUC_JP_Solaris", "eucjpms");
} else {
charsetList.add("EUC_JP");
testDataMap.put("EUC_JP", UJIS_CHARS);
javaToMysqlCharsetMap.put("EUC_JP", "ujis");
}
Iterator charsetIterator = charsetList.iterator();
while (charsetIterator.hasNext()) {
String charset = (String) charsetIterator.next();
Properties props = new Properties();
props.put("useUnicode", "true");
props.put("characterEncoding", charset);
Connection conn2 = getConnectionWithProps(props);
connectionMap.put(charset.toLowerCase(Locale.ENGLISH), conn2);
statementMap.put(charset.toLowerCase(Locale.ENGLISH), conn2
.createStatement());
props.put("characterSetResult", charset);
Connection connWithResult = getConnectionWithProps(props);
connectionWithResultMap.put(charset, connWithResult);
statementWithResultMap.put(charset, connWithResult
.createStatement());
}
charsetIterator = charsetList.iterator();
while (charsetIterator.hasNext()) {
String charset = (String) charsetIterator.next();
String mysqlCharset = (String) javaToMysqlCharsetMap
.get(charset);
Statement stmt2 = (Statement) statementMap.get(charset
.toLowerCase(Locale.ENGLISH));
String query1 = "DROP TABLE IF EXISTS t1";
String query2 = "CREATE TABLE t1 (c1 int, c2 char(1)) "
+ "DEFAULT CHARACTER SET = " + mysqlCharset;
stmt2.executeUpdate(query1);
stmt2.executeUpdate(query2);
char[] testData = (char[]) testDataMap.get(charset);
for (int i = 0; i < testData.length; i++) {
String query3 = "INSERT INTO t1 values(" + i + ", '"
+ testData[i] + "')";
stmt2.executeUpdate(query3);
String query4 = "SELECT c2 FROM t1 WHERE c1 = " + i;
this.rs = stmt2.executeQuery(query4);
this.rs.next();
String value = rs.getString(1);
assertEquals("For character set " + charset + "/ "
+ mysqlCharset, String.valueOf(testData[i]), value);
}
String query5 = "DROP TABLE t1";
stmt2.executeUpdate(query5);
}
}
}
}
public void testUtf8OutsideBMPInBlob() throws Exception {
createTable("utf8Test", "(include_blob BLOB, include_tinyblob TINYBLOB, include_longblob LONGBLOB, exclude_tinyblob TINYBLOB, exclude_blob BLOB, exclude_longblob LONGBLOB)");
// We know this gets truncated in MySQL currently, even though it's valid UTF-8, it's just 4 bytes encoded
String outsideBmp = new String(new byte[] {(byte) 0xF0, (byte) 0x90, (byte) 0x80, (byte) 0x80}, "UTF-8");
byte[] outsideBmpBytes = outsideBmp.getBytes("UTF-8");
System.out.println(outsideBmpBytes.length);
Connection utf8Conn = getConnectionWithProps("useBlobToStoreUTF8OutsideBMP=true, characterEncoding=UTF-8");
String insertStatement = "INSERT INTO utf8Test VALUES (?, ?, ?, ?, ?, ?)";
this.pstmt = utf8Conn.prepareStatement(insertStatement);
this.pstmt.setString(1, outsideBmp);
this.pstmt.setString(2, outsideBmp);
this.pstmt.setString(3, outsideBmp);
this.pstmt.setString(4, outsideBmp);
this.pstmt.setString(5, outsideBmp);
this.pstmt.setString(6, outsideBmp);
this.pstmt.executeUpdate();
String query = "SELECT include_blob, include_tinyblob, include_longblob, exclude_tinyblob, exclude_blob, exclude_longblob FROM utf8Test";
this.rs = utf8Conn.createStatement().executeQuery(query);
this.rs.next();
assertEquals(this.rs.getObject(1).toString(), outsideBmp);
assertEquals(this.rs.getObject(2).toString(), outsideBmp);
assertEquals(this.rs.getObject(3).toString(), outsideBmp);
assertEquals(this.rs.getObject(4).toString(), outsideBmp);
assertEquals(this.rs.getObject(5).toString(), outsideBmp);
assertEquals(this.rs.getObject(6).toString(), outsideBmp);
assertEquals("java.lang.String", this.rs.getObject(1).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(1));
assertEquals(Types.VARCHAR, this.rs.getMetaData().getColumnType(1));
assertEquals("java.lang.String", this.rs.getObject(2).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(2));
assertEquals(Types.VARCHAR, this.rs.getMetaData().getColumnType(2));
assertEquals("java.lang.String", this.rs.getObject(3).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(3));
assertEquals(Types.LONGVARCHAR, this.rs.getMetaData().getColumnType(3));
assertEquals("java.lang.String", this.rs.getObject(4).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(4));
assertEquals(Types.VARCHAR, this.rs.getMetaData().getColumnType(4));
assertEquals("java.lang.String", this.rs.getObject(5).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(5));
assertEquals(Types.VARCHAR, this.rs.getMetaData().getColumnType(5));
assertEquals("java.lang.String", this.rs.getObject(6).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(6));
assertEquals(Types.LONGVARCHAR, this.rs.getMetaData().getColumnType(6));
utf8Conn = getConnectionWithProps("useBlobToStoreUTF8OutsideBMP=true, characterEncoding=UTF-8,utf8OutsideBmpIncludedColumnNamePattern=.*include.*,utf8OutsideBmpExcludedColumnNamePattern=.*blob");
this.rs = utf8Conn.createStatement().executeQuery(query);
this.rs.next();
// Should walk/talk like a string, encoded in utf-8 on the server (4-byte)
assertEquals(this.rs.getObject(1).toString(), outsideBmp);
assertEquals(this.rs.getObject(2).toString(), outsideBmp);
assertEquals(this.rs.getObject(3).toString(), outsideBmp);
assertEquals("java.lang.String", this.rs.getObject(1).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(1));
assertEquals(Types.VARCHAR, this.rs.getMetaData().getColumnType(1));
assertEquals("java.lang.String", this.rs.getObject(2).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(2));
assertEquals(Types.VARCHAR, this.rs.getMetaData().getColumnType(2));
assertEquals("java.lang.String", this.rs.getObject(3).getClass().getName());
assertEquals("java.lang.String", this.rs.getMetaData().getColumnClassName(3));
assertEquals(Types.LONGVARCHAR, this.rs.getMetaData().getColumnType(3));
// These should be left as a blob, since it matches the exclusion regex
assertTrue(bytesAreSame(this.rs.getBytes(4), outsideBmpBytes));
assertEquals("[B", this.rs.getObject(4).getClass().getName());
assertEquals("[B", this.rs.getMetaData().getColumnClassName(4));
assertEquals(Types.VARBINARY, this.rs.getMetaData().getColumnType(4));
// Should behave types-wise just like BLOB, including LONGVARBINARY type mapping
assertTrue(bytesAreSame(this.rs.getBytes(5), outsideBmpBytes));
assertEquals("[B", this.rs.getObject(5).getClass().getName());
assertEquals("[B", this.rs.getMetaData().getColumnClassName(5));
assertEquals(Types.LONGVARBINARY, this.rs.getMetaData().getColumnType(5));
assertTrue(bytesAreSame(this.rs.getBytes(6), outsideBmpBytes));
assertEquals("[B", this.rs.getObject(6).getClass().getName());
assertEquals("[B", this.rs.getMetaData().getColumnClassName(6));
assertEquals(Types.LONGVARBINARY, this.rs.getMetaData().getColumnType(6));
//
// Check error handling
//
utf8Conn = getConnectionWithProps("useBlobToStoreUTF8OutsideBMP=true, characterEncoding=UTF-8,utf8OutsideBmpIncludedColumnNamePattern={{,utf8OutsideBmpExcludedColumnNamePattern={{");
try {
utf8Conn.createStatement().executeQuery(query);
fail("Expected an exception");
} catch (SQLException sqlEx) {
assertNotNull(sqlEx.getCause());
assertEquals("java.util.regex.PatternSyntaxException", sqlEx.getCause().getClass().getName());
}
utf8Conn = getConnectionWithProps("useBlobToStoreUTF8OutsideBMP=true, characterEncoding=UTF-8,utf8OutsideBmpIncludedColumnNamePattern={{,utf8OutsideBmpExcludedColumnNamePattern=.*");
try {
utf8Conn.createStatement().executeQuery(query);
fail("Expected an exception");
} catch (SQLException sqlEx) {
assertNotNull(sqlEx.getCause());
assertEquals("java.util.regex.PatternSyntaxException", sqlEx.getCause().getClass().getName());
}
utf8Conn = getConnectionWithProps("useBlobToStoreUTF8OutsideBMP=true, characterEncoding=UTF-8,utf8OutsideBmpIncludedColumnNamePattern={{,utf8OutsideBmpExcludedColumnNamePattern={{,paranoid=true");
try {
utf8Conn.createStatement().executeQuery(query);
fail("Expected an exception");
} catch (SQLException sqlEx) {
assertNull(sqlEx.getCause());
}
utf8Conn = getConnectionWithProps("useBlobToStoreUTF8OutsideBMP=true, characterEncoding=UTF-8,utf8OutsideBmpIncludedColumnNamePattern={{,utf8OutsideBmpExcludedColumnNamePattern=.*,paranoid=true");
try {
utf8Conn.createStatement().executeQuery(query);
fail("Expected an exception");
} catch (SQLException sqlEx) {
assertNull(sqlEx.getCause());
}
}
private boolean bytesAreSame(byte[] byte1, byte[] byte2) {
if (byte1.length != byte2.length) {
return false;
}
for (int i = 0; i < byte1.length; i++) {
if (byte1[i] != byte2[i]) {
return false;
}
}
return true;
}
}
|