add testcase & bug fixed

This commit is contained in:
wenshao
2012-08-26 19:58:36 +08:00
parent c3f4b79de4
commit bc0672cf62
6 changed files with 360 additions and 20 deletions
@@ -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();
}
}
@@ -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();
}
}
@@ -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();
}
}
@@ -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);
}
}
@@ -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();
}
}