From a474f82c34684db47e758a9bcd2d2f39e39a0f20 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 26 Aug 2012 11:59:08 +0800 Subject: [PATCH] add testcase & bug fixed --- .../pool/DruidPooledPreparedStatement.java | 2 +- .../DruidPooledPreparedStatementTest1.java | 202 ++++++++++++++++++ 2 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest1.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java index 082765acb0..f9139cad2e 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java +++ b/src/main/java/com/alibaba/druid/pool/DruidPooledPreparedStatement.java @@ -505,7 +505,7 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement conn.beforeExecute(); try { - return super.executeBatch(); + return stmt.executeBatch(); } catch (Throwable t) { throw checkException(t); } finally { diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest1.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest1.java new file mode 100644 index 0000000000..58eeb51ae7 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest1.java @@ -0,0 +1,202 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.sql.ParameterMetaData; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.filter.FilterAdapter; +import com.alibaba.druid.filter.FilterChain; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.proxy.jdbc.StatementProxy; + +public class DruidPooledPreparedStatementTest1 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setInitialSize(1); + + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public boolean preparedStatement_execute(FilterChain chain, PreparedStatementProxy statement) + throws SQLException { + throw new SQLException(); + } + + @Override + public int preparedStatement_executeUpdate(FilterChain chain, PreparedStatementProxy statement) + throws SQLException { + throw new SQLException(); + } + + public ResultSetProxy preparedStatement_executeQuery(FilterChain chain, PreparedStatementProxy statement) + throws SQLException { + throw new SQLException(); + } + + public void preparedStatement_clearParameters(FilterChain chain, PreparedStatementProxy statement) + throws SQLException { + throw new SQLException(); + } + + @Override + public int[] statement_executeBatch(FilterChain chain, StatementProxy statement) throws SQLException { + throw new SQLException(); + } + + @Override + public ParameterMetaData preparedStatement_getParameterMetaData(FilterChain chain, PreparedStatementProxy statement) + throws SQLException { + throw new SQLException(); + } + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_execute_error() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select 1"); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + stmt.execute(); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + + stmt.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getPoolingCount()); + } + + public void test_executeQuery_error() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select 1"); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + stmt.executeQuery(); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + + stmt.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getPoolingCount()); + } + + public void test_executeUpdate_error() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select 1"); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + stmt.executeUpdate(); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + + stmt.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getPoolingCount()); + } + + public void test_clearParameter_error() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select 1"); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + stmt.clearParameters(); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + + stmt.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getPoolingCount()); + } + + public void test_executeBatch_error() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select 1"); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + stmt.executeBatch(); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + + stmt.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getPoolingCount()); + } + + public void test_getParameterMetaData_error() throws Exception { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("select 1"); + + Assert.assertEquals(0, dataSource.getErrorCount()); + + Exception error = null; + try { + stmt.getParameterMetaData(); + } catch (Exception e) { + error = e; + } + Assert.assertNotNull(error); + + Assert.assertEquals(1, dataSource.getErrorCount()); + + stmt.close(); + conn.close(); + + Assert.assertEquals(1, dataSource.getPoolingCount()); + } +}