From c509b2db6aea9fb6b32f97fee63853d01f9ba96e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E5=B0=91?= Date: Wed, 6 Jun 2012 08:22:05 +0800 Subject: [PATCH] enhance testcase --- .../druid/bvt/pool/TestGetUpdateCount.java | 82 +++++++++++++++---- 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/alibaba/druid/bvt/pool/TestGetUpdateCount.java b/src/test/java/com/alibaba/druid/bvt/pool/TestGetUpdateCount.java index 00f5a28ad0..14c0286b34 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/TestGetUpdateCount.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/TestGetUpdateCount.java @@ -38,6 +38,7 @@ public class TestGetUpdateCount extends TestCase { dataSource.setDriver(driver); dataSource.setUrl("jdbc:mock:xx"); dataSource.setFilters("stat"); + dataSource.setMaxOpenPreparedStatements(30); } protected void tearDown() throws Exception { @@ -48,41 +49,84 @@ public class TestGetUpdateCount extends TestCase { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("select ?"); - + MyPreparedStatement myStmt = stmt.unwrap(MyPreparedStatement.class); - + Assert.assertNull(myStmt.updateCount); - + stmt.setString(1, "xxx"); ResultSet rs = stmt.executeQuery(); - - Assert.assertNull(myStmt.updateCount); - + + Assert.assertEquals(-1, myStmt.updateCount.intValue()); + rs.close(); stmt.close(); conn.close(); } - + public void test_execute() throws Exception { Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("update t set id = ?"); - + MyPreparedStatement myStmt = stmt.unwrap(MyPreparedStatement.class); - + Assert.assertNull(myStmt.updateCount); - + stmt.setString(1, "xxx"); stmt.execute(); - + Assert.assertNotNull(myStmt.updateCount); - + Assert.assertEquals(1, stmt.getUpdateCount()); - + stmt.close(); conn.close(); } + public void test_execute_multi() throws Exception { + MyPreparedStatement myStmtA = null; + MyPreparedStatement myStmtB = null; + { + Connection conn = dataSource.getConnection(); + + PreparedStatement stmt = conn.prepareStatement("update t set id = ?"); + + myStmtA = stmt.unwrap(MyPreparedStatement.class); + + Assert.assertNull(myStmtA.updateCount); + + stmt.setString(1, "xxx"); + stmt.execute(); + + Assert.assertNotNull(myStmtA.updateCount); + + Assert.assertEquals(1, stmt.getUpdateCount()); + + stmt.close(); + conn.close(); + } + { + Connection conn = dataSource.getConnection(); + + PreparedStatement stmt = conn.prepareStatement("update t set id = ?"); + + myStmtB = stmt.unwrap(MyPreparedStatement.class); + Assert.assertSame(myStmtA, myStmtB); + Assert.assertNotNull(myStmtB.updateCount); + + stmt.setString(1, "xxx"); + stmt.execute(); + + Assert.assertNotNull(myStmtB.updateCount); + + Assert.assertEquals(1, stmt.getUpdateCount()); + + stmt.close(); + conn.close(); + } + } + public static class MyPreparedStatement extends MockPreparedStatement { Integer updateCount = null; @@ -90,13 +134,23 @@ public class TestGetUpdateCount extends TestCase { public MyPreparedStatement(MockConnection conn, String sql){ super(conn, sql); } + + public boolean execute() throws SQLException { + updateCount = null; + return false; + } + + public ResultSet executeQuery() throws SQLException { + updateCount = -1; + return super.executeQuery(); + } @Override public int getUpdateCount() throws SQLException { if (updateCount != null) { throw new SQLException("illegal state"); } - + updateCount = 1; return updateCount; }