From 68b735c7f80e137949db8c4fc69f3c4af8ceab56 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 25 Aug 2012 20:00:12 +0800 Subject: [PATCH] add testcase & bug fixed --- .../druid/pool/DruidPooledConnection.java | 6 +- .../bvt/pool/DruidPooledConnectionTest1.java | 111 ++++++++ ...ruidPooledConnectionTest_prepareError.java | 251 ++++++++++++++++++ 3 files changed, 363 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest1.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest_prepareError.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java b/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java index 390e04215a..701a06e19e 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledConnection.java @@ -115,11 +115,7 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec return holder.getDataSource().isOracle(); } - void closePoolableStatement(DruidPooledPreparedStatement stmt) throws SQLException { - if (this.holder == null) { - return; - } - + public void closePoolableStatement(DruidPooledPreparedStatement stmt) throws SQLException { PreparedStatement rawStatement = stmt.getRawPreparedStatement(); try { diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest1.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest1.java new file mode 100644 index 0000000000..8716878c75 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest1.java @@ -0,0 +1,111 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.SQLException; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.DruidPooledConnection; +import com.alibaba.druid.pool.DruidPooledPreparedStatement; + +public class DruidPooledConnectionTest1 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setFilters("stat"); + dataSource.setPoolPreparedStatements(true); + + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_conn() throws Exception { + DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection(); + + conn.close(); + + Exception error = null; + try { + conn.handleException(new RuntimeException()); + } catch (SQLException e) { + error = e; + } + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getRecycleCount()); + Assert.assertEquals(1, dataSource.getCloseCount()); + Assert.assertEquals(1, dataSource.getPoolingCount()); + Assert.assertEquals(0, dataSource.getActiveCount()); + } + + public void test_handleException_1() throws Exception { + DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection(); + + Exception error = null; + try { + conn.handleException(new RuntimeException()); + } catch (SQLException e) { + error = e; + } + Assert.assertNotNull(error); + + conn.close(); + + Assert.assertEquals(1, dataSource.getRecycleCount()); + Assert.assertEquals(1, dataSource.getCloseCount()); + Assert.assertEquals(1, dataSource.getPoolingCount()); + Assert.assertEquals(0, dataSource.getActiveCount()); + } + + public void test_closePoolableStatement() throws Exception { + DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection(); + DruidPooledPreparedStatement stmt = (DruidPooledPreparedStatement) conn.prepareStatement("select 1"); + conn.close(); + conn.closePoolableStatement(stmt); + } + + public void test_dup_close() throws Exception { + DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection(); + + conn.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getRecycleCount()); + Assert.assertEquals(1, dataSource.getCloseCount()); + Assert.assertEquals(1, dataSource.getPoolingCount()); + Assert.assertEquals(0, dataSource.getActiveCount()); + } + + public void test_recycle() throws Exception { + DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection(); + + conn.close(); + conn.recycle(); + + Assert.assertEquals(1, dataSource.getRecycleCount()); + Assert.assertEquals(1, dataSource.getCloseCount()); + Assert.assertEquals(1, dataSource.getPoolingCount()); + Assert.assertEquals(0, dataSource.getActiveCount()); + } + + public void test_recycle_2() throws Exception { + DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection(); + + conn.recycle(); + conn.recycle(); + conn.close(); + + Assert.assertEquals(1, dataSource.getRecycleCount()); + Assert.assertEquals(1, dataSource.getCloseCount()); + Assert.assertEquals(1, dataSource.getPoolingCount()); + Assert.assertEquals(0, dataSource.getActiveCount()); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest_prepareError.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest_prepareError.java new file mode 100644 index 0000000000..f12d54889a --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledConnectionTest_prepareError.java @@ -0,0 +1,251 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.sql.SQLException; + +import org.junit.Assert; + +import junit.framework.TestCase; + +import com.alibaba.druid.filter.FilterAdapter; +import com.alibaba.druid.filter.FilterChain; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.CallableStatementProxy; +import com.alibaba.druid.proxy.jdbc.ConnectionProxy; +import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy; + +public class DruidPooledConnectionTest_prepareError extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setFilters("stat"); + dataSource.setPoolPreparedStatements(true); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, + String sql) throws SQLException { + throw new SQLException(); + } + + @Override + public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, + String sql, int autoGeneratedKeys) + throws SQLException { + throw new SQLException(); + } + + @Override + public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, + String sql, int resultSetType, + int resultSetConcurrency) throws SQLException { + throw new SQLException(); + } + + @Override + public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, + String sql, int resultSetType, + int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + throw new SQLException(); + } + + @Override + public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, + String sql, int[] columnIndexes) + throws SQLException { + throw new SQLException(); + } + + @Override + public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, + String sql, String[] columnNames) + throws SQLException { + throw new SQLException(); + } + + @Override + public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection, + String sql) throws SQLException { + throw new SQLException(); + } + + @Override + public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection, + String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { + throw new SQLException(); + } + + @Override + public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection, + String sql, int resultSetType, + int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + throw new SQLException(); + } + + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_prepare_error() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareStatement("select 1"); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepare_error_1() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareStatement("select 1", 0, 0); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepare_error_2() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareStatement("select 1", 0, 0, 0); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepare_error_3() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareStatement("select 1", 0); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepare_error_4() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareStatement("select 1", new int[0]); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepare_error_5() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareStatement("select 1", new String[0]); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepareCall_error_1() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareCall("select 1"); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepareCall_error_2() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareCall("select 1", 0, 0); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } + + public void test_prepareCall_error_3() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + conn.prepareCall("select 1", 0, 0, 0); + } catch (Exception e) { + error = e; + } + + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + } +}