📄 statementregressiontest.java
字号:
public void testBug11115() throws Exception { String tableName = "testBug11115"; if (versionMeetsMinimum(4, 1, 0)) { createTable(tableName, "(pwd VARBINARY(30)) TYPE=InnoDB DEFAULT CHARACTER SET utf8"); byte[] bytesToTest = new byte[] { 17, 120, -1, -73, -5 }; PreparedStatement insStmt = this.conn .prepareStatement("INSERT INTO " + tableName + " (pwd) VALUES (?)"); insStmt.setBytes(1, bytesToTest); insStmt.executeUpdate(); this.rs = this.stmt.executeQuery("SELECT pwd FROM " + tableName); this.rs.next(); byte[] fromDatabase = this.rs.getBytes(1); assertEquals(bytesToTest.length, fromDatabase.length); for (int i = 0; i < bytesToTest.length; i++) { assertEquals(bytesToTest[i], fromDatabase[i]); } this.rs = this.conn .prepareStatement("SELECT pwd FROM " + tableName) .executeQuery(); this.rs.next(); fromDatabase = this.rs.getBytes(1); assertEquals(bytesToTest.length, fromDatabase.length); for (int i = 0; i < bytesToTest.length; i++) { assertEquals(bytesToTest[i], fromDatabase[i]); } } } public void testBug11540() throws Exception { Locale originalLocale = Locale.getDefault(); Connection thaiConn = null; Statement thaiStmt = null; PreparedStatement thaiPrepStmt = null; try { createTable("testBug11540", "(field1 DATE, field2 TIMESTAMP)"); this.stmt .executeUpdate("INSERT INTO testBug11540 VALUES (NOW(), NOW())"); Locale.setDefault(new Locale("th", "TH")); Properties props = new Properties(); props.setProperty("jdbcCompliantTruncation", "false"); thaiConn = getConnectionWithProps(props); thaiStmt = thaiConn.createStatement(); this.rs = thaiStmt .executeQuery("SELECT field1, field2 FROM testBug11540"); this.rs.next(); Date origDate = this.rs.getDate(1); Timestamp origTimestamp = this.rs.getTimestamp(1); this.rs.close(); thaiStmt.executeUpdate("TRUNCATE TABLE testBug11540"); thaiPrepStmt = ((com.mysql.jdbc.Connection) thaiConn) .clientPrepareStatement("INSERT INTO testBug11540 VALUES (?,?)"); thaiPrepStmt.setDate(1, origDate); thaiPrepStmt.setTimestamp(2, origTimestamp); thaiPrepStmt.executeUpdate(); this.rs = thaiStmt .executeQuery("SELECT field1, field2 FROM testBug11540"); this.rs.next(); Date testDate = this.rs.getDate(1); Timestamp testTimestamp = this.rs.getTimestamp(1); this.rs.close(); assertEquals(origDate, testDate); assertEquals(origTimestamp, testTimestamp); } finally { Locale.setDefault(originalLocale); } } /** * Tests fix for BUG#11663, autoGenerateTestcaseScript uses bogus parameter * names for server-side prepared statements. * * @throws Exception * if the test fails. */ public void testBug11663() throws Exception { if (versionMeetsMinimum(4, 1, 0) && ((com.mysql.jdbc.Connection) this.conn) .getUseServerPreparedStmts()) { Connection testcaseGenCon = null; PrintStream oldErr = System.err; try { createTable("testBug11663", "(field1 int)"); Properties props = new Properties(); props.setProperty("autoGenerateTestcaseScript", "true"); testcaseGenCon = getConnectionWithProps(props); ByteArrayOutputStream testStream = new ByteArrayOutputStream(); PrintStream testErr = new PrintStream(testStream); System.setErr(testErr); this.pstmt = testcaseGenCon .prepareStatement("SELECT field1 FROM testBug11663 WHERE field1=?"); this.pstmt.setInt(1, 1); this.pstmt.execute(); System.setErr(oldErr); String testString = new String(testStream.toByteArray()); int setIndex = testString.indexOf("SET @debug_stmt_param"); int equalsIndex = testString.indexOf("=", setIndex); String paramName = testString.substring(setIndex + 4, equalsIndex); int usingIndex = testString.indexOf("USING " + paramName, equalsIndex); assertTrue(usingIndex != -1); } finally { System.setErr(oldErr); if (this.pstmt != null) { this.pstmt.close(); this.pstmt = null; } if (testcaseGenCon != null) { testcaseGenCon.close(); } } } } /** * Tests fix for BUG#11798 - Pstmt.setObject(...., Types.BOOLEAN) throws * exception. * * @throws Exception * if the test fails. */ public void testBug11798() throws Exception { if (isRunningOnJdk131()) { return; // test not valid on JDK-1.3.1 } try { this.pstmt = this.conn.prepareStatement("SELECT ?"); this.pstmt.setObject(1, Boolean.TRUE, Types.BOOLEAN); this.pstmt.setObject(1, new BigDecimal("1"), Types.BOOLEAN); this.pstmt.setObject(1, "true", Types.BOOLEAN); } finally { if (this.pstmt != null) { this.pstmt.close(); this.pstmt = null; } } } /** * Tests fix for BUG#13255 - Reconnect during middle of executeBatch() * should not happen. * * @throws Exception * if the test fails. */ public void testBug13255() throws Exception { createTable("testBug13255", "(field_1 int)"); Properties props = new Properties(); props.setProperty("autoReconnect", "true"); Connection reconnectConn = null; Statement reconnectStmt = null; PreparedStatement reconnectPStmt = null; try { reconnectConn = getConnectionWithProps(props); reconnectStmt = reconnectConn.createStatement(); String connectionId = getSingleIndexedValueWithQuery(reconnectConn, 1, "SELECT CONNECTION_ID()").toString(); reconnectStmt.addBatch("INSERT INTO testBug13255 VALUES (1)"); reconnectStmt.addBatch("INSERT INTO testBug13255 VALUES (2)"); reconnectStmt.addBatch("KILL " + connectionId); for (int i = 0; i < 100; i++) { reconnectStmt.addBatch("INSERT INTO testBug13255 VALUES (" + i + ")"); } try { reconnectStmt.executeBatch(); } catch (SQLException sqlEx) { // We expect this...we killed the connection } assertEquals(2, getRowCount("testBug13255")); this.stmt.executeUpdate("TRUNCATE TABLE testBug13255"); reconnectConn.close(); reconnectConn = getConnectionWithProps(props); connectionId = getSingleIndexedValueWithQuery(reconnectConn, 1, "SELECT CONNECTION_ID()").toString(); reconnectPStmt = reconnectConn .prepareStatement("INSERT INTO testBug13255 VALUES (?)"); reconnectPStmt.setInt(1, 1); reconnectPStmt.addBatch(); reconnectPStmt.setInt(1, 2); reconnectPStmt.addBatch(); reconnectPStmt.addBatch("KILL " + connectionId); for (int i = 3; i < 100; i++) { reconnectPStmt.setInt(1, i); reconnectPStmt.addBatch(); } try { reconnectPStmt.executeBatch(); } catch (SQLException sqlEx) { // We expect this...we killed the connection } assertEquals(2, getRowCount("testBug13255")); } finally { if (reconnectStmt != null) { reconnectStmt.close(); } if (reconnectConn != null) { reconnectConn.close(); } } } /** * Tests fix for BUG#15024 - Driver incorrectly closes streams passed as * arguments to PreparedStatements. * * @throws Exception * if the test fails. */ public void testBug15024() throws Exception { createTable("testBug15024", "(field1 BLOB)"); try { this.pstmt = this.conn .prepareStatement("INSERT INTO testBug15024 VALUES (?)"); testStreamsForBug15024(false, false); Properties props = new Properties(); props.setProperty("useConfigs", "3-0-Compat"); Connection compatConn = null; try { compatConn = getConnectionWithProps(props); this.pstmt = compatConn .prepareStatement("INSERT INTO testBug15024 VALUES (?)"); testStreamsForBug15024(true, false); } finally { if (compatConn != null) { compatConn.close(); } } } finally { if (this.pstmt != null) { PreparedStatement toClose = this.pstmt; this.pstmt = null; toClose.close(); } } } /** * PreparedStatement should call EscapeProcessor.escapeSQL? * * @throws Exception * if the test fails */ public void testBug15141() throws Exception { try { createTable("testBug15141", "(field1 VARCHAR(32))"); this.stmt.executeUpdate("INSERT INTO testBug15141 VALUES ('abc')"); this.pstmt = this.conn .prepareStatement("select {d '1997-05-24'} FROM testBug15141"); this.rs = this.pstmt.executeQuery(); assertTrue(this.rs.next()); assertEquals("1997-05-24", this.rs.getString(1)); this.rs.close(); this.rs = null; this.pstmt.close(); this.pstmt = null; this.pstmt = ((com.mysql.jdbc.Connection) this.conn) .clientPrepareStatement("select {d '1997-05-24'} FROM testBug15141"); this.rs = this.pstmt.executeQuery(); assertTrue(this.rs.next()); assertEquals("1997-05-24", this.rs.getString(1)); this.rs.close(); this.rs = null; this.pstmt.close(); this.pstmt = null; } finally { if (this.rs != null) { ResultSet toCloseRs = this.rs; this.rs = null; toCloseRs.close(); } if (this.pstmt != null) { PreparedStatement toClosePstmt = this.pstmt; this.pstmt = null; toClosePstmt.close(); } } } /** * Tests fix for BUG#18041 - Server-side prepared statements don't cause * truncation exceptions to be thrown. * * @throws Exception * if the test fails */ public void testBug18041() throws Exception { if (versionMeetsMinimum(4, 1)) { createTable("testBug18041", "(`a` tinyint(4) NOT NULL," + "`b` char(4) default NULL)"); Properties props = new Properties(); props.setProperty("jdbcCompliantTruncation", "true"); props.setProperty("useServerPrepStmts", "true"); Connection truncConn = null; PreparedStatement stm = null; try { truncConn = getConnectionWithProps(props); stm = truncConn .prepareStatement("insert into testBug18041 values (?,?)"); stm.setInt(1, 1000); stm.setString(2, "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"); stm.executeUpdate(); fail("Truncation exception should have been thrown"); } catch (DataTruncation truncEx) { // we expect this } finally { if (this.stmt != null) { this.stmt.close(); } if (truncConn != null) { truncConn.close(); } } } } private void testStreamsForBug15024(boolean shouldBeClosedStream, boolean shouldBeClosedReader) throws SQLException { IsClosedInputStream bIn = new IsClosedInputStream(new byte[4]); IsClosedReader readerIn = new IsClosedReader("abcdef"); this.pstmt.setBinaryStream(1, bIn, 4); this.pstmt.execute(); assertEquals(shouldBeClosedStream, bIn.isClosed()); this.pstmt.setCharacterStream(1, readerIn, 6); this.pstmt.execute(); assertEquals(shouldBeClosedReader, readerIn.isClosed()); this.pstmt.close(); } class IsClosedReader extends StringReader { boolean isClosed = false; public IsClosedReader(String arg0) { super(arg0); } public void close() { super.close(); this.isClosed = true; } public boolean isClosed() { return this.isClosed; } } class IsClosedInputStream extends ByteArrayInputStream { boolean isClosed = false; public IsClosedInputStream(byte[] arg0, int arg1, int arg2) { super(arg0, arg1, arg2); } public IsClosedInputStream(byte[] arg0) { super(arg0); } public void close() throws IOException { // TODO Auto-generated method stub super.close(); this.isClosed = true; } public boolean isClosed() { return this.isClosed; } } /** * Tests fix for BUG#1774 -- Truncated words after double quote * * @throws Exception * if the test fails. */ public void testBug1774() throws Exception { try { this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1774"); this.stmt .executeUpdate("CREATE TABLE testBug1774 (field1 VARCHAR(255))"); PreparedStatement pStmt = this.conn .prepareStatement("INSERT INTO testBug1774 VALUES (?)"); String testString = "The word contains \" character"; pStmt.setString(1, testString); pStmt.executeUpdate(); this.rs = this.stmt.executeQuery("SELECT * FROM testBug1774"); this.rs.next(); assertEquals(this.rs.getString(1), testString); } finally {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -