mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add testcase & bug fixed
This commit is contained in:
@@ -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<String> getActiveConnectionStackTrace() {
|
||||
List<String> list = new ArrayList<String>();
|
||||
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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user