From d2a397de892d10dba76f0925669e244aea59ae66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Tue, 29 Mar 2016 16:50:34 +0800 Subject: [PATCH] fixed testcase --- .../druid/bvt/pool/AsyncCloseTest1.java | 34 ++++++++++++----- .../druid/bvt/pool/AsyncCloseTest2.java | 37 ++++++++++++++----- .../druid/bvt/proxy/filter/ClobTest.java | 6 ++- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest1.java b/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest1.java index 600c9f765f..eef9c54c64 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest1.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest1.java @@ -1,5 +1,6 @@ package com.alibaba.druid.bvt.pool; +import java.lang.reflect.Field; import java.sql.Connection; import java.sql.SQLException; import java.util.concurrent.CountDownLatch; @@ -7,15 +8,16 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; -import junit.framework.TestCase; - import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.junit.Assert; import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.Log4jImpl; -import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.support.logging.NoLoggingImpl; + +import junit.framework.TestCase; public class AsyncCloseTest1 extends TestCase { @@ -25,13 +27,23 @@ public class AsyncCloseTest1 extends TestCase { final AtomicInteger errorCount = new AtomicInteger(); - private Logger log; - private Level oldLevel; + private Logger log4jLog; + private Level log4jOldLevel; + + private NoLoggingImpl noLoggingImpl; protected void setUp() throws Exception { - log = ((Log4jImpl) LogFactory.getLog(DruidDataSource.class)).getLog(); - oldLevel = log.getLevel(); - log.setLevel(Level.FATAL); + Field logField = DruidDataSource.class.getDeclaredField("LOG"); + logField.setAccessible(true); + Log dataSourceLog = (Log) logField.get(null); + if (dataSourceLog instanceof Log4jImpl) { + this.log4jLog = ((Log4jImpl) dataSourceLog).getLog(); + this.log4jOldLevel = this.log4jLog.getLevel(); + this.log4jLog.setLevel(Level.FATAL); + } else if (dataSourceLog instanceof NoLoggingImpl) { + noLoggingImpl = (NoLoggingImpl) dataSourceLog; + noLoggingImpl.setErrorEnabled(false); + } dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:"); @@ -46,7 +58,11 @@ public class AsyncCloseTest1 extends TestCase { protected void tearDown() throws Exception { dataSource.close(); - log.setLevel(oldLevel); + if (log4jLog != null) { + log4jLog.setLevel(log4jOldLevel); + } else if (noLoggingImpl != null) { + noLoggingImpl.setErrorEnabled(true); + } } public void test_0() throws Exception { diff --git a/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest2.java b/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest2.java index de38aaf1be..25cb65394b 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest2.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/AsyncCloseTest2.java @@ -1,5 +1,6 @@ package com.alibaba.druid.bvt.pool; +import java.lang.reflect.Field; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; @@ -8,8 +9,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; -import junit.framework.TestCase; - import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.junit.Assert; @@ -19,8 +18,11 @@ import com.alibaba.druid.filter.FilterChain; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.vendor.MockExceptionSorter; import com.alibaba.druid.proxy.jdbc.StatementProxy; +import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.Log4jImpl; -import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.support.logging.NoLoggingImpl; + +import junit.framework.TestCase; public class AsyncCloseTest2 extends TestCase { @@ -30,14 +32,25 @@ public class AsyncCloseTest2 extends TestCase { final AtomicInteger errorCount = new AtomicInteger(); - private Logger log; - private Level oldLevel; + private Logger log4jLog; + private Level log4jOldLevel; + + private NoLoggingImpl noLoggingImpl; protected void setUp() throws Exception { - log = ((Log4jImpl) LogFactory.getLog(DruidDataSource.class)).getLog(); - oldLevel = log.getLevel(); - log.setLevel(Level.FATAL); - + Field logField = DruidDataSource.class.getDeclaredField("LOG"); + logField.setAccessible(true); + Log dataSourceLog = (Log) logField.get(null); + if (dataSourceLog instanceof Log4jImpl) { + this.log4jLog = ((Log4jImpl) dataSourceLog).getLog(); + this.log4jOldLevel = this.log4jLog.getLevel(); + this.log4jLog.setLevel(Level.FATAL); + } else if (dataSourceLog instanceof NoLoggingImpl) { + noLoggingImpl = (NoLoggingImpl) dataSourceLog; + noLoggingImpl.setErrorEnabled(false); + } + + dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:"); dataSource.setAsyncCloseConnectionEnable(true); @@ -63,7 +76,11 @@ public class AsyncCloseTest2 extends TestCase { protected void tearDown() throws Exception { dataSource.close(); - log.setLevel(oldLevel); + if (log4jLog != null) { + log4jLog.setLevel(log4jOldLevel); + } else if (noLoggingImpl != null) { + noLoggingImpl.setErrorEnabled(true); + } } public void test_0() throws Exception { diff --git a/src/test/java/com/alibaba/druid/bvt/proxy/filter/ClobTest.java b/src/test/java/com/alibaba/druid/bvt/proxy/filter/ClobTest.java index 1e3c2a3e43..45a7aee6c9 100644 --- a/src/test/java/com/alibaba/druid/bvt/proxy/filter/ClobTest.java +++ b/src/test/java/com/alibaba/druid/bvt/proxy/filter/ClobTest.java @@ -32,12 +32,14 @@ import com.alibaba.druid.stat.DruidDataSourceStatManager; public class ClobTest extends TestCase { + protected int dataSourceListSize; + protected void setUp() throws Exception { - Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size()); + dataSourceListSize = DruidDataSourceStatManager.getInstance().getDataSourceList().size(); } protected void tearDown() throws Exception { - Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size()); + Assert.assertEquals(dataSourceListSize, DruidDataSourceStatManager.getInstance().getDataSourceList().size()); } public void test_clob() throws Exception {