enhance testcase

This commit is contained in:
温少
2012-06-06 08:22:05 +08:00
parent d70b5b21cd
commit c509b2db6a
@@ -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;
}