diff --git a/src/main/java/com/alibaba/druid/mock/MockStatement.java b/src/main/java/com/alibaba/druid/mock/MockStatement.java index 9023c2fe8d..95d0bf298c 100644 --- a/src/main/java/com/alibaba/druid/mock/MockStatement.java +++ b/src/main/java/com/alibaba/druid/mock/MockStatement.java @@ -36,6 +36,8 @@ public class MockStatement implements Statement { private SQLWarning warnings; private String cursorName; private int updateCount; + private int fetchDirection; + private int fetchSize; public MockStatement(Connection connection){ super(); @@ -236,111 +238,161 @@ public class MockStatement implements Statement { @Override public boolean getMoreResults() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } return false; } @Override public void setFetchDirection(int direction) throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + this.fetchDirection = direction; } @Override public int getFetchDirection() throws SQLException { - return 0; + if (closed) { + throw new SQLException("stmt closed."); + } + return fetchDirection; } @Override public void setFetchSize(int rows) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } + this.fetchSize = rows; } @Override public int getFetchSize() throws SQLException { - return 0; + if (closed) { + throw new SQLException("stmt closed."); + } + return fetchSize; } @Override public int getResultSetConcurrency() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } return 0; } @Override public int getResultSetType() throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return 0; } @Override public void addBatch(String sql) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } } @Override public void clearBatch() throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } } @Override public int[] executeBatch() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } return new int[0]; } @Override public Connection getConnection() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } return connection; } @Override public boolean getMoreResults(int current) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return false; } @Override public ResultSet getGeneratedKeys() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } return new MockResultSet(this); } @Override public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return 0; } @Override public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return 0; } @Override public int executeUpdate(String sql, String[] columnNames) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return 0; } @Override public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return false; } @Override public boolean execute(String sql, int[] columnIndexes) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return false; } @Override public boolean execute(String sql, String[] columnNames) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return false; } @Override public int getResultSetHoldability() throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return 0; } @@ -351,12 +403,16 @@ public class MockStatement implements Statement { @Override public void setPoolable(boolean poolable) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } } @Override public boolean isPoolable() throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } return false; } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java index 4266ea2f3d..72817414ff 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java @@ -14,8 +14,8 @@ import com.alibaba.druid.pool.PoolableStatement; import com.alibaba.druid.pool.vendor.NullExceptionSorter; import com.alibaba.druid.stat.DruidDataSourceStatManager; - public class PoolableStatementTest2 extends TestCase { + private MockDriver driver; private DruidDataSource dataSource; @@ -39,7 +39,7 @@ public class PoolableStatementTest2 extends TestCase { dataSource.setFilters("stat"); dataSource.setRemoveAbandoned(true); dataSource.setExceptionSorterClassName(null); - + Assert.assertTrue(dataSource.getExceptionSoter() instanceof NullExceptionSorter); dataSource.setExceptionSorterClassName(""); Assert.assertTrue(dataSource.getExceptionSoter() instanceof NullExceptionSorter); @@ -50,29 +50,29 @@ public class PoolableStatementTest2 extends TestCase { dataSource.close(); Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size()); } - + public void test_dupClose() throws Exception { - Connection conn = dataSource.getConnection(); - Statement stmt = conn.createStatement(); - stmt.close(); - stmt.close(); - conn.close(); + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + stmt.close(); + stmt.close(); + conn.close(); } - + public void test_executeUpdate() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("SET @VAR = 1"); stmt.close(); conn.close(); - } - + } + public void test_executeUpdate_error() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -82,17 +82,17 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); - } - + } + public void test_execute_error() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -102,17 +102,17 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); - } - + } + public void test_executeQuery_error() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -122,18 +122,18 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); - } - + } + public void test_setEscapeProcessing() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.setEscapeProcessing(true); ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -143,20 +143,20 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_getMaxFieldSize() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.setMaxFieldSize(23); Assert.assertEquals(23, stmt.getMaxFieldSize()); - + ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -166,7 +166,7 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + { SQLException error = null; try { @@ -176,20 +176,20 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_QueryTimeout() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.setQueryTimeout(33); Assert.assertEquals(33, stmt.getQueryTimeout()); - + ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -199,7 +199,7 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + { SQLException error = null; try { @@ -209,20 +209,20 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_MaxRows() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.setMaxRows(44); Assert.assertEquals(44, stmt.getMaxRows()); - + ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -232,7 +232,7 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + { SQLException error = null; try { @@ -242,18 +242,84 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + + public void test_FetchDirection() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.setFetchDirection(144); + Assert.assertEquals(144, stmt.getFetchDirection()); + + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.getFetchDirection(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + { + SQLException error = null; + try { + stmt.setFetchDirection(23); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_FetchSize() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.setFetchSize(144); + Assert.assertEquals(144, stmt.getFetchSize()); + + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.getFetchSize(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + { + SQLException error = null; + try { + stmt.setFetchSize(23); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + public void test_cancel() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.cancel(); ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -263,18 +329,18 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_getWarnings() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.getWarnings(); ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -284,18 +350,18 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_clearWarnings() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.clearWarnings(); ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -305,18 +371,18 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_setCursorName() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.setCursorName("c_name"); ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -326,18 +392,18 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_getResultSet() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.getResultSet(); ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -347,18 +413,18 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } - + stmt.close(); conn.close(); } - + public void test_getUpdateCount() throws Exception { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); - + stmt.getUpdateCount(); ((PoolableStatement) stmt).getStatement().close(); - + { SQLException error = null; try { @@ -368,8 +434,177 @@ public class PoolableStatementTest2 extends TestCase { } Assert.assertNotNull(error); } + + stmt.close(); + conn.close(); + } + + public void test_getMoreResults() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.getMoreResults(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.getMoreResults(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_getResultSetConcurrency() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.getResultSetConcurrency(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.getResultSetConcurrency(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_getResultSetType() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.getResultSetType(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.getResultSetType(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_addBatch() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.addBatch("select 1"); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.addBatch("select 1"); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_clearBatch() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.clearBatch(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.clearBatch(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_executeBatch() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.executeBatch(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.executeBatch(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } stmt.close(); conn.close(); } + + public void test_getMoreResults_1() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.getMoreResults(1); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.getMoreResults(1); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + + public void test_getGeneratedKeys() throws Exception { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + + stmt.getGeneratedKeys(); + ((PoolableStatement) stmt).getStatement().close(); + + { + SQLException error = null; + try { + stmt.getGeneratedKeys(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } + }