📄 charsettests.java
字号:
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; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -