add testcase & bug fixed

This commit is contained in:
wenshao
2012-08-25 11:46:54 +08:00
parent a658091f59
commit 2ca722227f
12 changed files with 397 additions and 79 deletions
@@ -0,0 +1,52 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.vendor.OracleValidConnectionChecker;
/**
* 这个场景测试minIdle > maxActive
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_clearCache extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setPoolPreparedStatements(true);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_clearStatementCache() throws Exception {
{
Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement("select 1");
stmt.close();
conn.close();
}
{
Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement("select 1, 2");
stmt.close();
conn.close();
}
Assert.assertEquals(2, dataSource.getCachedPreparedStatementCount());
dataSource.clearStatementCache();
Assert.assertEquals(0, dataSource.getCachedPreparedStatementCount());
}
}
@@ -0,0 +1,71 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.pool.DataSourceDisableException;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.vendor.OracleValidConnectionChecker;
/**
* 这个场景测试minIdle > maxActive
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_enable extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setMaxWait(1000);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_disable() throws Exception {
{
Connection conn = dataSource.getConnection();
conn.close();
}
Assert.assertTrue(dataSource.isEnable());
dataSource.setEnable(false);
Assert.assertFalse(dataSource.isEnable());
dataSource.shrink();
Exception error = null;
try {
Connection conn = dataSource.getConnection();
conn.close();
} catch (DataSourceDisableException e) {
error = e;
}
Assert.assertNotNull(error);
}
public void test_disable_() throws Exception {
dataSource.setEnable(false);
Assert.assertFalse(dataSource.isEnable());
Exception error = null;
try {
Connection conn = dataSource.getConnection();
conn.close();
} catch (DataSourceDisableException e) {
error = e;
}
Assert.assertNotNull(error);
}
}
@@ -0,0 +1,39 @@
package com.alibaba.druid.bvt.pool;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 这个场景测试defaultAutoCommit
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_getConnection extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_conn_error() throws Exception {
Exception error = null;
try {
dataSource.getConnection(null, null);
} catch (UnsupportedOperationException e) {
error = e;
}
Assert.assertNotNull(error);
}
}
@@ -34,5 +34,6 @@ public class DruidDataSourceTest_getPoolingPeakTime extends TestCase {
conn.close();
Assert.assertNotNull(dataSource.getPoolingPeakTime());
Assert.assertNotNull(dataSource.getActivePeakTime());
}
}
@@ -82,6 +82,7 @@ public class DruidDataSourceTest_notEmptyWait2 extends TestCase {
errorThreadEndLatch.await(100, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, maxWaitErrorCount.get());
Assert.assertTrue(dataSource.getNotEmptySignalCount() > 0);
conn.close();
@@ -0,0 +1,41 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Statement;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
/**
* 这个场景测试initialSize > maxActive
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_recycle extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setMaxActive(1);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_recycle() throws Exception {
DruidPooledConnection conn = dataSource.getConnection();
conn.setAutoCommit(false);
conn.setReadOnly(false);
Statement stmt = conn.createStatement();
stmt.execute("select 1");
conn.close();
}
}
@@ -0,0 +1,78 @@
package com.alibaba.druid.bvt.pool;
import java.sql.SQLException;
import java.sql.Statement;
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.pool.DruidPooledConnection;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
/**
* 这个场景测试initialSize > maxActive
*
* @author wenshao<szujobs@hotmail.com>
*/
public class DruidDataSourceTest_recycle2 extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.getProxyFilters().add(new FilterAdapter() {
public void connection_rollback(FilterChain chain, ConnectionProxy connection) throws SQLException {
throw new SQLException();
}
});
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_recycle() throws Exception {
DruidPooledConnection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("select 1");
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(1, dataSource.getActiveCount());
conn.close();
Assert.assertEquals(1, dataSource.getPoolingCount());
Assert.assertEquals(0, dataSource.getActiveCount());
}
public void test_recycle_error() throws Exception {
DruidPooledConnection conn = dataSource.getConnection();
conn.setAutoCommit(false);
conn.setReadOnly(false);
Statement stmt = conn.createStatement();
stmt.execute("select 1");
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(1, dataSource.getActiveCount());
Exception error = null;
try {
conn.close();
} catch (Exception e) {
error = e;
}
Assert.assertNotNull(error);
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(0, dataSource.getActiveCount());
}
}
@@ -0,0 +1,83 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
public class DruidPooledConnectionTest extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setTestOnBorrow(false);
dataSource.setFilters("stat");
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_rollback() throws Exception {
Connection conn = dataSource.getConnection();
conn.rollback();
conn.rollback(null);
conn.close();
}
public void test_rollback_1() throws Exception {
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
conn.close();
conn.rollback();
conn.rollback(null);
}
public void test_getOwnerThread() throws Exception {
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
Assert.assertEquals(Thread.currentThread(), conn.getOwnerThread());
conn.close();
}
public void test_isDiable() throws Exception {
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
Assert.assertEquals(false, conn.isDisable());
conn.close();
Assert.assertEquals(true, conn.isDisable());
}
public void test_dupClose() throws Exception {
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
conn.close();
conn.close();
}
public void test_disable() throws Exception {
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(0, dataSource.getActiveCount());
DruidPooledConnection conn = (DruidPooledConnection) dataSource.getConnection();
conn.disable();
Assert.assertEquals(true, conn.isDisable());
Assert.assertEquals(1, dataSource.getActiveCount());
conn.close();
Assert.assertEquals(0, dataSource.getPoolingCount());
Assert.assertEquals(1, dataSource.getActiveCount());
}
}