diff --git a/src/main/java/com/alibaba/druid/mock/MockStatement.java b/src/main/java/com/alibaba/druid/mock/MockStatement.java index 34750c6708..0f7b9b44ed 100644 --- a/src/main/java/com/alibaba/druid/mock/MockStatement.java +++ b/src/main/java/com/alibaba/druid/mock/MockStatement.java @@ -29,6 +29,10 @@ public class MockStatement implements Statement { private Connection connection; protected MockConnection mockConnection; + private int maxFieldSize; + private int maxRows; + private int queryTimeout; + private boolean escapeProcessing; public MockStatement(Connection connection){ super(); @@ -63,7 +67,7 @@ public class MockStatement implements Statement { if (closed) { throw new SQLException("stmt closed."); } - + if (mockConnection != null && mockConnection.getDriver() != null) { return mockConnection.getDriver().executeQuery(this, sql); } @@ -76,7 +80,7 @@ public class MockStatement implements Statement { if (closed) { throw new SQLException("stmt closed."); } - + return 0; } @@ -87,41 +91,74 @@ public class MockStatement implements Statement { @Override public int getMaxFieldSize() throws SQLException { - return 0; + if (closed) { + throw new SQLException("stmt closed."); + } + + return maxFieldSize; } @Override public void setMaxFieldSize(int max) throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + + this.maxFieldSize = max; } @Override public int getMaxRows() throws SQLException { - return 0; + if (closed) { + throw new SQLException("stmt closed."); + } + + return maxRows; } @Override public void setMaxRows(int max) throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + this.maxRows = max; } @Override public void setEscapeProcessing(boolean enable) throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + this.escapeProcessing = enable; + } + + public boolean isEscapeProcessing() { + return escapeProcessing; } @Override public int getQueryTimeout() throws SQLException { - return 0; + if (closed) { + throw new SQLException("stmt closed."); + } + return queryTimeout; } @Override public void setQueryTimeout(int seconds) throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } + this.queryTimeout = seconds; } @Override public void cancel() throws SQLException { - + if (closed) { + throw new SQLException("stmt closed."); + } } @Override @@ -144,7 +181,7 @@ public class MockStatement implements Statement { if (closed) { throw new SQLException("stmt closed."); } - + if (ERROR_SQL.equals(sql)) { throw new SQLException(); } 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 a5e491554d..dd4bffe85e 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 @@ -126,4 +126,91 @@ public class PoolableStatementTest2 extends TestCase { 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 { + stmt.setEscapeProcessing(true); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.getMaxFieldSize(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + { + SQLException error = null; + try { + stmt.setMaxFieldSize(23); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.getQueryTimeout(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + { + SQLException error = null; + try { + stmt.setQueryTimeout(23); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } }