From a658091f59f1c06daaa9d7c171938ab9aafa6a73 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 25 Aug 2012 10:56:01 +0800 Subject: [PATCH] add testcase & bug fixed --- .../DruidPooledPreparedStatementTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest.java diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest.java new file mode 100644 index 0000000000..cde9d3ad9b --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidPooledPreparedStatementTest.java @@ -0,0 +1,84 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.sql.ResultSet; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.DruidPooledPreparedStatement; + +public class DruidPooledPreparedStatementTest 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_hitCount() throws Exception { + String sql = "select ?"; + { + Connection conn = dataSource.getConnection(); + DruidPooledPreparedStatement stmt = (DruidPooledPreparedStatement) conn.prepareStatement(sql); + + Assert.assertEquals(0, stmt.getFetchSize()); + Assert.assertEquals(0, stmt.getFetchDirection()); + Assert.assertEquals(0, stmt.getMaxRows()); + Assert.assertEquals(0, stmt.getMaxFieldSize()); + Assert.assertEquals(0, stmt.getQueryTimeout()); + Assert.assertEquals(sql, stmt.getSql()); + + stmt.setFetchSize(1); + stmt.setFetchDirection(2); + stmt.setMaxRows(3); + stmt.setMaxFieldSize(4); + stmt.setQueryTimeout(5); + + Assert.assertEquals(1, stmt.getFetchSize()); + Assert.assertEquals(2, stmt.getFetchDirection()); + Assert.assertEquals(3, stmt.getMaxRows()); + Assert.assertEquals(4, stmt.getMaxFieldSize()); + Assert.assertEquals(5, stmt.getQueryTimeout()); + + stmt.setString(1, "xx"); + ResultSet rs = stmt.executeQuery(); + Assert.assertTrue(rs.next()); + Assert.assertFalse(rs.next()); + rs.close(); + conn.close(); + + Assert.assertEquals(0, stmt.getHitCount()); + } + { + Connection conn = dataSource.getConnection(); + DruidPooledPreparedStatement stmt = (DruidPooledPreparedStatement) conn.prepareStatement(sql); + + Assert.assertEquals(0, stmt.getFetchSize()); + Assert.assertEquals(0, stmt.getFetchDirection()); + Assert.assertEquals(0, stmt.getMaxRows()); + Assert.assertEquals(0, stmt.getMaxFieldSize()); + Assert.assertEquals(0, stmt.getQueryTimeout()); + + stmt.setString(1, "xx"); + ResultSet rs = stmt.executeQuery(); + Assert.assertTrue(rs.next()); + Assert.assertFalse(rs.next()); + rs.close(); + conn.close(); + + Assert.assertEquals(1, stmt.getHitCount()); + Assert.assertNotNull(stmt.getKey()); + } + } +}