From a90633b1762ae96b4395b9c8743f36ae38b45f13 Mon Sep 17 00:00:00 2001 From: wenshao Date: Fri, 16 Sep 2011 05:00:15 +0000 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84mock=E5=92=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.alibabatech.com/svn/druid/trunk@796 b9813039-fb51-4c41-a8b9-e21c2acb5095 --- .../alibaba/druid/filter/stat/StatFilter.java | 4 +- .../com/alibaba/druid/mock/MockStatement.java | 38 ++++- .../bvt/pool/basic/PoolableStatementTest.java | 3 + .../pool/basic/PoolableStatementTest2.java | 159 ++++++++++++++++++ 4 files changed, 201 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java index db11b163de..33922dcc3d 100644 --- a/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java +++ b/src/main/java/com/alibaba/druid/filter/stat/StatFilter.java @@ -375,7 +375,9 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean { statement.getAttributes().put(ATTR_UPDATE_COUNT, updateCount); final JdbcSqlStat sqlStat = getSqlStat(statement); - sqlStat.addUpdateCount(updateCount); + if (sqlStat != null) { + sqlStat.addUpdateCount(updateCount); + } } return updateCount; diff --git a/src/main/java/com/alibaba/druid/mock/MockStatement.java b/src/main/java/com/alibaba/druid/mock/MockStatement.java index 0f7b9b44ed..9023c2fe8d 100644 --- a/src/main/java/com/alibaba/druid/mock/MockStatement.java +++ b/src/main/java/com/alibaba/druid/mock/MockStatement.java @@ -33,6 +33,9 @@ public class MockStatement implements Statement { private int maxRows; private int queryTimeout; private boolean escapeProcessing; + private SQLWarning warnings; + private String cursorName; + private int updateCount; public MockStatement(Connection connection){ super(); @@ -163,17 +166,37 @@ public class MockStatement implements Statement { @Override public SQLWarning getWarnings() throws SQLException { - return null; + if (closed) { + throw new SQLException("stmt closed."); + } + + return warnings; } @Override public void clearWarnings() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + warnings = null; + } + + public void setWarning(SQLWarning warning) { + this.warnings = warning; } @Override public void setCursorName(String name) throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + cursorName = name; + } + + public String getCursorName() { + return cursorName; } @Override @@ -191,13 +214,24 @@ public class MockStatement implements Statement { @Override public ResultSet getResultSet() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + return new MockResultSet(this); } @Override public int getUpdateCount() throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } - return 0; + return updateCount; + } + + public void setUpdateCount(int updateCount) { + this.updateCount = updateCount; } @Override diff --git a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest.java index 9b921f9e49..d4313f7fd2 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest.java @@ -38,6 +38,9 @@ public class PoolableStatementTest extends TestCase { Assert.assertEquals(null, stmt.getConnection()); Assert.assertEquals(false, stmt.isPoolable()); stmt.toString(); + } + + public void test_MaxRows() throws Exception { } } 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 dd4bffe85e..4266ea2f3d 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 @@ -213,4 +213,163 @@ public class PoolableStatementTest2 extends TestCase { 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 { + stmt.getMaxRows(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + { + SQLException error = null; + try { + stmt.setMaxRows(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 { + stmt.cancel(); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.getWarnings(); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.clearWarnings(); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.setCursorName("c_name"); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.getResultSet(); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.getUpdateCount(); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } }