mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
完善mock和测试
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@808 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -70,6 +70,10 @@ public class MockCallableStatement extends MockPreparedStatement implements Call
|
||||
|
||||
@Override
|
||||
public boolean wasNull() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
return wasNull;
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,10 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
|
||||
|
||||
@Override
|
||||
public ResultSet executeQuery() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
if (mockConnection != null && mockConnection.getDriver() != null) {
|
||||
return mockConnection.getDriver().createResultSet(this);
|
||||
}
|
||||
@@ -73,6 +77,10 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
|
||||
|
||||
@Override
|
||||
public int executeUpdate() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -163,6 +171,10 @@ public class MockPreparedStatement extends MockStatement implements PreparedStat
|
||||
|
||||
@Override
|
||||
public void clearParameters() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
}
|
||||
|
||||
parameters.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -81,8 +81,12 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
void closePoolableStatement(PoolablePreparedStatement stmt) throws SQLException {
|
||||
PreparedStatement rawStatement = stmt.getRawPreparedStatement();
|
||||
|
||||
rawStatement.clearParameters();
|
||||
|
||||
try {
|
||||
rawStatement.clearParameters();
|
||||
} catch (SQLException ex) {
|
||||
LOG.error("clear parameter error", ex);
|
||||
}
|
||||
|
||||
if (holder.isPoolPreparedStatements()) {
|
||||
this.holder.getStatementPool().put(stmt);
|
||||
stmt.clearResultSet();
|
||||
|
||||
@@ -323,4 +323,33 @@ public class PoolablePreparedStatementTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void test_setRef() throws Exception {
|
||||
|
||||
stmt.setRef(1, null);
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.setRef(0, null);
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
}
|
||||
|
||||
public void test_setArray() throws Exception {
|
||||
|
||||
stmt.setArray(1, null);
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.setArray(0, null);
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.bvt.pool.basic;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
@@ -804,5 +805,130 @@ public class PoolableStatementTest2 extends TestCase {
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void test_wasNull() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
CallableStatement stmt = conn.prepareCall("SELELCT 1");
|
||||
|
||||
stmt.wasNull();
|
||||
((PoolableStatement) stmt).getStatement().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.wasNull();
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void test_executeQuery() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELELCT 1");
|
||||
|
||||
stmt.executeQuery();
|
||||
((PoolableStatement) stmt).getStatement().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.executeQuery();
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void test_executeUpdate_4() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELELCT 1");
|
||||
|
||||
stmt.executeQuery();
|
||||
((PoolableStatement) stmt).getStatement().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void test_execute_3() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELELCT 1");
|
||||
|
||||
stmt.execute();
|
||||
((PoolableStatement) stmt).getStatement().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.execute();
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
|
||||
public void test_clearParameters() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELELCT 1");
|
||||
|
||||
stmt.clearParameters();
|
||||
((PoolableStatement) stmt).getStatement().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.clearParameters();
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void test_addBatch_1() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELELCT 1");
|
||||
|
||||
stmt.addBatch();
|
||||
((PoolableStatement) stmt).getStatement().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
stmt.addBatch();
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user