mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #4 from sandzhang/master
get and recycle connection filter testcase
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
package com.alibaba.druid.bvt.filter;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.filter.FilterAdapter;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
|
||||
public class FilterDatasourceConnectAndRecycleFilterTest extends TestCase {
|
||||
|
||||
TestFilter filter = new TestFilter();
|
||||
List<Filter> filterList = new ArrayList<Filter>();
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
filterList.add(filter);
|
||||
dataSource.setProxyFilters(filterList);
|
||||
dataSource.setUrl("jdbc:mock:");
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
|
||||
Assert.assertEquals(0, filter.getDataSourceConnectCount());
|
||||
Assert.assertEquals(0, filter.getDataSourceRecycleCount());
|
||||
Connection conn = dataSource.getConnection();
|
||||
Assert.assertEquals(1, filter.getDataSourceConnectCount());
|
||||
Assert.assertEquals(0, filter.getDataSourceRecycleCount());
|
||||
conn.close();
|
||||
Assert.assertEquals(1, filter.getDataSourceConnectCount());
|
||||
Assert.assertEquals(1, filter.getDataSourceRecycleCount());
|
||||
}
|
||||
|
||||
public static class TestFilter extends FilterAdapter {
|
||||
|
||||
private AtomicLong dataSourceConnectCount = new AtomicLong();
|
||||
private AtomicLong dataSourceRecycleCount = new AtomicLong();
|
||||
|
||||
@Override
|
||||
public void dataSource_recycle(FilterChain chain, DruidPooledConnection connection) throws SQLException {
|
||||
chain.dataSource_recycle(connection);
|
||||
dataSourceRecycleCount.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DruidPooledConnection dataSource_connect(FilterChain chain, DruidDataSource dataSource,
|
||||
long maxWaitMillis) throws SQLException {
|
||||
dataSourceConnectCount.incrementAndGet();
|
||||
return chain.dataSource_connect(dataSource, maxWaitMillis);
|
||||
}
|
||||
|
||||
public long getDataSourceConnectCount() {
|
||||
return dataSourceConnectCount.get();
|
||||
}
|
||||
|
||||
public long getDataSourceRecycleCount() {
|
||||
return dataSourceRecycleCount.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@ import junit.framework.TestCase;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.filter.FilterChainImpl;
|
||||
import com.alibaba.druid.filter.FilterEventAdapter;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
@@ -783,5 +785,115 @@ public class JdbcFilterEventAdapterTest extends TestCase {
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
|
||||
// //////////////////////////
|
||||
|
||||
{
|
||||
FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {
|
||||
|
||||
public void dataSource_recycle(DruidPooledConnection connection) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
};
|
||||
|
||||
Throwable error = null;
|
||||
try {
|
||||
filter.dataSource_recycle(chain, null);
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
{
|
||||
FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {
|
||||
|
||||
public void dataSource_recycle(DruidPooledConnection connection) throws SQLException {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
};
|
||||
|
||||
Throwable error = null;
|
||||
try {
|
||||
filter.dataSource_recycle(chain, null);
|
||||
} catch (RuntimeException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
{
|
||||
FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {
|
||||
|
||||
public void dataSource_recycle(DruidPooledConnection connection) throws SQLException {
|
||||
throw new Error();
|
||||
}
|
||||
};
|
||||
|
||||
Throwable error = null;
|
||||
try {
|
||||
filter.dataSource_recycle(chain, null);
|
||||
} catch (Error ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
// //////////////////////////
|
||||
|
||||
{
|
||||
FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {
|
||||
|
||||
public DruidPooledConnection dataSource_connect(DruidDataSource dataSource, long maxWaitMillis)
|
||||
throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
};
|
||||
|
||||
Throwable error = null;
|
||||
try {
|
||||
filter.dataSource_connect(chain, null, 0L);
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
{
|
||||
FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {
|
||||
|
||||
public DruidPooledConnection dataSource_connect(DruidDataSource dataSource, long maxWaitMillis)
|
||||
throws SQLException {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
};
|
||||
|
||||
Throwable error = null;
|
||||
try {
|
||||
filter.dataSource_connect(chain, null, 0L);
|
||||
} catch (RuntimeException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
{
|
||||
FilterChain chain = new FilterChainImpl(new DataSourceProxyImpl(null, config)) {
|
||||
|
||||
public DruidPooledConnection dataSource_connect(DruidDataSource dataSource, long maxWaitMillis)
|
||||
throws SQLException {
|
||||
throw new Error();
|
||||
}
|
||||
};
|
||||
|
||||
Throwable error = null;
|
||||
try {
|
||||
filter.dataSource_connect(chain, null, 0L);
|
||||
} catch (Error ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user