From bc0672cf62b6f6f0e37a59083cc81d85572e72dd Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 26 Aug 2012 19:58:36 +0800 Subject: [PATCH] add testcase & bug fixed --- .../druid/pool/DruidAbstractDataSource.java | 39 +++--- .../druid/bvt/pool/DruidDataSourceTest5.java | 92 ++++++++++++++ .../druid/bvt/pool/DruidDataSourceTest6.java | 113 ++++++++++++++++++ .../druid/bvt/pool/DruidDataSourceTest7.java | 50 ++++++++ .../druid/bvt/pool/DruidDataSourceTest8.java | 48 ++++++++ .../druid/bvt/pool/DruidDataSourceTest9.java | 38 ++++++ 6 files changed, 360 insertions(+), 20 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest5.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest6.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest7.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest9.java diff --git a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java index e86956c2d2..844211f849 100644 --- a/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java @@ -1127,7 +1127,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements } return true; - } catch (SQLException ex) { + } catch (Exception ex) { // skip return false; } @@ -1137,10 +1137,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements return this.activeConnections.keySet(); } - void removeActiveConnection(DruidPooledConnection conn) { - activeConnections.remove(conn); - } - public List getActiveConnectionStackTrace() { List list = new ArrayList(); for (ActiveConnectionTraceInfo traceInfo : this.activeConnections.values()) { @@ -1227,11 +1223,13 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements String password = getPassword(); PasswordCallback passwordCallback = getPasswordCallback(); - if (passwordCallback instanceof DruidPasswordCallback) { - DruidPasswordCallback druidPasswordCallback = (DruidPasswordCallback) passwordCallback; + if (passwordCallback != null) { + if (passwordCallback instanceof DruidPasswordCallback) { + DruidPasswordCallback druidPasswordCallback = (DruidPasswordCallback) passwordCallback; - druidPasswordCallback.setUrl(url); - druidPasswordCallback.setProperties(connectProperties); + druidPasswordCallback.setUrl(url); + druidPasswordCallback.setProperties(connectProperties); + } char[] chars = passwordCallback.getPassword(); if (chars != null) { @@ -1312,6 +1310,8 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements } } + public abstract int getActivePeak(); + public CompositeDataSupport getCompositeData() throws JMException { JdbcDataSourceStat stat = this.getDataSourceStat(); @@ -1330,20 +1330,19 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements map.put("Properties", getProperties()); // 0 - 4 - map.put("ConnectionActiveCount", stat.getConnectionActiveCount()); - map.put("ConnectionActiveCountMax", stat.getConnectionStat().getActiveMax()); - map.put("ConnectionCloseCount", stat.getConnectionStat().getCloseCount()); - map.put("ConnectionCommitCount", stat.getConnectionStat().getCommitCount()); - map.put("ConnectionRollbackCount", stat.getConnectionStat().getRollbackCount()); + map.put("ConnectionActiveCount", (long) getActiveCount()); + map.put("ConnectionActiveCountMax", getActivePeak()); + map.put("ConnectionCloseCount", getCloseCount()); + map.put("ConnectionCommitCount", getCommitCount()); + map.put("ConnectionRollbackCount", getRollbackCount()); // 5 - 9 map.put("ConnectionConnectLastTime", stat.getConnectionStat().getConnectLastTime()); - map.put("ConnectionConnectErrorCount", stat.getConnectionStat().getConnectErrorCount()); - Throwable lastConnectionConnectError = stat.getConnectionStat().getConnectErrorLast(); - if (lastConnectionConnectError != null) { - map.put("ConnectionConnectErrorLastTime", stat.getConnectionStat().getErrorLastTime()); - map.put("ConnectionConnectErrorLastMessage", lastConnectionConnectError.getMessage()); - map.put("ConnectionConnectErrorLastStackTrace", IOUtils.getStackTrace(lastConnectionConnectError)); + map.put("ConnectionConnectErrorCount", this.getCreateCount()); + if (createError != null) { + map.put("ConnectionConnectErrorLastTime", getLastCreateErrorTime()); + map.put("ConnectionConnectErrorLastMessage", createError.getMessage()); + map.put("ConnectionConnectErrorLastStackTrace", IOUtils.getStackTrace(createError)); } else { map.put("ConnectionConnectErrorLastTime", null); map.put("ConnectionConnectErrorLastMessage", null); diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest5.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest5.java new file mode 100644 index 0000000000..fdc56cc0ac --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest5.java @@ -0,0 +1,92 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.Assert; + +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.ValidConnectionChecker; + +public class DruidDataSourceTest5 extends TestCase { + + private DruidDataSource dataSource; + + private final AtomicBoolean validate = new AtomicBoolean(true); + + protected void setUp() throws Exception { + validate.set(true); + + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setInitialSize(1); + + dataSource.setValidConnectionChecker(new ValidConnectionChecker() { + + @Override + public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) { + return validate.get(); + } + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void testValidate() throws Exception { + validate.set(false); + + Exception error = null; + try { + dataSource.getConnection(); + } catch (SQLException e) { + error = e; + } + Assert.assertNotNull(error); + + validate.set(true); + + Connection conn = dataSource.getConnection(); + conn.close(); + } + + public void testValidate_1() throws Exception { + validate.set(false); + + Exception error = null; + try { + dataSource.init(); + } catch (SQLException e) { + error = e; + } + Assert.assertNotNull(error); + + validate.set(true); + + Connection conn = dataSource.getConnection(); + conn.close(); + } + + public void testValidate_3() throws Exception { + validate.set(false); + + Exception error = null; + try { + dataSource.init(); + } catch (SQLException e) { + error = e; + } + Assert.assertNotNull(error); + + validate.set(true); + + Connection conn = dataSource.getConnection(); + conn.close(); + } + +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest6.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest6.java new file mode 100644 index 0000000000..dfc32456f0 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest6.java @@ -0,0 +1,113 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.concurrent.atomic.AtomicInteger; + +import javax.security.auth.callback.NameCallback; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.filter.FilterAdapter; +import com.alibaba.druid.filter.FilterChain; +import com.alibaba.druid.mock.MockResultSet; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl; +import com.alibaba.druid.proxy.jdbc.StatementProxy; +import com.alibaba.druid.util.DruidPasswordCallback; + +public class DruidDataSourceTest6 extends TestCase { + + private DruidDataSource dataSource; + + private final AtomicInteger errorCount = new AtomicInteger(); + private final AtomicInteger returnEmptyCount = new AtomicInteger(); + + protected void setUp() throws Exception { + returnEmptyCount.set(0); + + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(true); + dataSource.setInitialSize(1); + dataSource.setValidationQuery("select 1"); + dataSource.setValidationQueryTimeout(10); + dataSource.setQueryTimeout(100); + + dataSource.setUserCallback(new NameCallback("xx") { + + }); + + dataSource.setPasswordCallback(new DruidPasswordCallback() { + @Override + public char[] getPassword() { + return "xx".toCharArray(); + } + }); + + dataSource.getProxyFilters().add(new FilterAdapter() { + + public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql) + throws SQLException { + if (errorCount.get() > 0) { + errorCount.decrementAndGet(); + throw new RuntimeException(); + } + + if (returnEmptyCount.get() > 0) { + returnEmptyCount.decrementAndGet(); + return new ResultSetProxyImpl(statement, new MockResultSet(statement), 0, sql); + } + + return chain.statement_executeQuery(statement, sql); + } + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void testValidate() throws Exception { + returnEmptyCount.set(1); + + Exception error = null; + try { + dataSource.init(); + } catch (SQLException e) { + error = e; + } + Assert.assertNotNull(error); + + { + returnEmptyCount.set(1); + Connection conn = dataSource.getConnection(); + conn.close(); + } + + { + returnEmptyCount.set(1); + Connection conn = dataSource.getConnection(); + conn.close(); + } + + { + errorCount.set(1); + Connection conn = dataSource.getConnection(); + conn.close(); + } + + Connection conn = dataSource.getConnection(); + + Statement stmt = conn.createStatement(); + Assert.assertEquals(100, stmt.getQueryTimeout()); + stmt.close(); + + conn.close(); + } + +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest7.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest7.java new file mode 100644 index 0000000000..ea3cbecc14 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest7.java @@ -0,0 +1,50 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.SQLException; +import java.util.Properties; + +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.ConnectionProxy; + +public class DruidDataSourceTest7 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setInitialSize(1); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException { + throw new RuntimeException(); + } + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void testInitError() throws Exception { + Assert.assertEquals(0, dataSource.getCreateErrorCount()); + Throwable error = null; + try { + dataSource.init(); + } catch (RuntimeException e) { + error = e; + } + Assert.assertNotNull(error); + Assert.assertTrue(dataSource.getCreateErrorCount() > 0); + + dataSource.getCompositeData(); + } + +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java new file mode 100644 index 0000000000..0ca88cbd78 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest8.java @@ -0,0 +1,48 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.SQLException; +import java.util.Properties; + +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.ConnectionProxy; + +public class DruidDataSourceTest8 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setInitialSize(1); + dataSource.getProxyFilters().add(new FilterAdapter() { + + @Override + public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException { + throw new Error(); + } + }); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void testInitError() throws Exception { + Assert.assertEquals(0, dataSource.getCreateErrorCount()); + Throwable error = null; + try { + dataSource.init(); + } catch (Throwable e) { + error = e; + } + Assert.assertNotNull(error); + Assert.assertTrue(dataSource.getCreateErrorCount() > 0); + } + +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest9.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest9.java new file mode 100644 index 0000000000..b7e76f9137 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest9.java @@ -0,0 +1,38 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.pool.DruidDataSource; + +public class DruidDataSourceTest9 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setInitialSize(1); + dataSource.setDefaultReadOnly(true); + dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); + dataSource.setDefaultCatalog("c123"); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void testDefault() throws Exception { + Connection conn = dataSource.getConnection(); + + Assert.assertEquals(true, conn.isReadOnly()); + Assert.assertEquals(Connection.TRANSACTION_SERIALIZABLE, conn.getTransactionIsolation()); + Assert.assertEquals("c123", conn.getCatalog()); + + conn.close(); + } + +}