From 158db2907d14d56076eb218bcfdfdfb087c77e95 Mon Sep 17 00:00:00 2001 From: wenshao Date: Thu, 15 Sep 2011 23:29: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@780 b9813039-fb51-4c41-a8b9-e21c2acb5095 --- .../com/alibaba/druid/mock/MockStatement.java | 12 ++ .../bvt/pool/basic/PoolableStatementTest.java | 43 ++++++ .../pool/basic/PoolableStatementTest2.java | 129 ++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java diff --git a/src/main/java/com/alibaba/druid/mock/MockStatement.java b/src/main/java/com/alibaba/druid/mock/MockStatement.java index 30c2d4df41..34750c6708 100644 --- a/src/main/java/com/alibaba/druid/mock/MockStatement.java +++ b/src/main/java/com/alibaba/druid/mock/MockStatement.java @@ -60,6 +60,10 @@ public class MockStatement implements Statement { @Override public ResultSet executeQuery(String sql) throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + if (mockConnection != null && mockConnection.getDriver() != null) { return mockConnection.getDriver().executeQuery(this, sql); } @@ -69,6 +73,10 @@ public class MockStatement implements Statement { @Override public int executeUpdate(String sql) throws SQLException { + if (closed) { + throw new SQLException("stmt closed."); + } + return 0; } @@ -133,6 +141,10 @@ public class MockStatement implements Statement { @Override public boolean execute(String sql) throws SQLException { + 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/PoolableStatementTest.java b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest.java new file mode 100644 index 0000000000..9b921f9e49 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest.java @@ -0,0 +1,43 @@ +package com.alibaba.druid.bvt.pool.basic; + +import java.sql.SQLException; +import java.sql.Statement; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.mock.MockStatement; +import com.alibaba.druid.pool.PoolableStatement; + + +public class PoolableStatementTest extends TestCase { + protected Statement raw; + protected PoolableStatement stmt; + + protected void setUp() throws Exception { + raw = new MockStatement(null); + stmt = new PoolableStatement(null, raw) { + protected SQLException checkException(Throwable error) throws SQLException { + if (error instanceof SQLException) { + return (SQLException) error; + } + + return new SQLException(error); + } + }; + } + + protected void tearDown() throws Exception { + + } + + public void test_basic() throws Exception { + Assert.assertEquals(raw, stmt.getStatement()); + Assert.assertEquals(null, stmt.getPoolableConnection()); + Assert.assertEquals(null, stmt.getConnection()); + Assert.assertEquals(false, stmt.isPoolable()); + stmt.toString(); + + } +} 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 new file mode 100644 index 0000000000..a5e491554d --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/basic/PoolableStatementTest2.java @@ -0,0 +1,129 @@ +package com.alibaba.druid.bvt.pool.basic; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.mock.MockDriver; +import com.alibaba.druid.pool.DruidDataSource; +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; + + protected void setUp() throws Exception { + Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size()); + + driver = new MockDriver(); + + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setDriver(driver); + dataSource.setInitialSize(1); + dataSource.setMaxActive(2); + dataSource.setMaxIdle(2); + dataSource.setMinIdle(1); + dataSource.setMinEvictableIdleTimeMillis(300 * 1000); // 300 / 10 + dataSource.setTimeBetweenEvictionRunsMillis(180 * 1000); // 180 / 10 + dataSource.setTestWhileIdle(true); + dataSource.setTestOnBorrow(false); + dataSource.setValidationQuery("SELECT 1"); + dataSource.setFilters("stat"); + dataSource.setRemoveAbandoned(true); + dataSource.setExceptionSorterClassName(null); + + Assert.assertTrue(dataSource.getExceptionSoter() instanceof NullExceptionSorter); + dataSource.setExceptionSorterClassName(""); + Assert.assertTrue(dataSource.getExceptionSoter() instanceof NullExceptionSorter); + } + + protected void tearDown() throws Exception { + Assert.assertEquals(true, dataSource.getCreateTimespanNano() > 0); + 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(); + } + + 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 { + stmt.executeUpdate("SET @VAR = 1"); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.execute("SET @VAR = 1"); + } catch (SQLException ex) { + error = ex; + } + 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 { + stmt.executeQuery("SELECT 1"); + } catch (SQLException ex) { + error = ex; + } + Assert.assertNotNull(error); + } + + stmt.close(); + conn.close(); + } +}