mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
fixed testcase
This commit is contained in:
@@ -36,6 +36,8 @@ public interface Log {
|
||||
void warn(String msg);
|
||||
|
||||
void warn(String msg, Throwable e);
|
||||
|
||||
boolean isErrorEnabled();
|
||||
|
||||
int getErrorCount();
|
||||
|
||||
|
||||
@@ -109,6 +109,10 @@ public class Log4j2Impl implements Log {
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isEnabled(Level.WARN);
|
||||
}
|
||||
|
||||
public boolean isErrorEnabled() {
|
||||
return log.isErrorEnabled();
|
||||
}
|
||||
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
|
||||
@@ -110,6 +110,10 @@ public class Log4jImpl implements Log {
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isEnabledFor(Level.WARN);
|
||||
}
|
||||
|
||||
public boolean isErrorEnabled() {
|
||||
return log.isEnabledFor(Level.ERROR);
|
||||
}
|
||||
|
||||
public int getInfoCount() {
|
||||
return infoCount;
|
||||
|
||||
@@ -22,6 +22,11 @@ public class NoLoggingImpl implements Log {
|
||||
private int warnCount;
|
||||
private int debugCount;
|
||||
private String loggerName;
|
||||
|
||||
private boolean debugEnable = false;
|
||||
private boolean infoEnable = true;
|
||||
private boolean warnEnable = true;
|
||||
private boolean errorEnable = true;
|
||||
|
||||
public NoLoggingImpl(String loggerName){
|
||||
this.loggerName = loggerName;
|
||||
@@ -32,10 +37,14 @@ public class NoLoggingImpl implements Log {
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return false;
|
||||
return debugEnable;
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
if (!errorEnable) {
|
||||
return;
|
||||
}
|
||||
|
||||
error(s);
|
||||
|
||||
if (e != null) {
|
||||
@@ -86,7 +95,7 @@ public class NoLoggingImpl implements Log {
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return false;
|
||||
return infoEnable;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -96,7 +105,7 @@ public class NoLoggingImpl implements Log {
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return false;
|
||||
return warnEnable;
|
||||
}
|
||||
|
||||
public int getInfoCount() {
|
||||
@@ -107,4 +116,11 @@ public class NoLoggingImpl implements Log {
|
||||
return debugCount;
|
||||
}
|
||||
|
||||
public boolean isErrorEnabled() {
|
||||
return errorEnable;
|
||||
}
|
||||
|
||||
public void setErrorEnabled(boolean value) {
|
||||
this.errorEnable = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,11 @@ public class SLF4JImpl implements Log {
|
||||
public boolean isWarnEnabled() {
|
||||
return log.isWarnEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isErrorEnabled() {
|
||||
return log.isErrorEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String msg) {
|
||||
|
||||
@@ -357,6 +357,10 @@ public final class JdbcUtils implements JdbcConstants {
|
||||
}
|
||||
|
||||
public static String getDriverClassName(String rawUrl) throws SQLException {
|
||||
if (rawUrl == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (rawUrl.startsWith("jdbc:derby:")) {
|
||||
return "org.apache.derby.jdbc.EmbeddedDriver";
|
||||
} else if (rawUrl.startsWith("jdbc:mysql:")) {
|
||||
|
||||
@@ -23,6 +23,7 @@ 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;
|
||||
|
||||
@@ -34,40 +35,44 @@ public class AsyncCloseTest3 extends TestCase {
|
||||
|
||||
final AtomicInteger errorCount = new AtomicInteger();
|
||||
|
||||
private Logger log;
|
||||
private Level oldLevel;
|
||||
|
||||
long xmx = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax() / (1000 * 1000); // m
|
||||
private Logger log4jLog;
|
||||
private Level log4jOldLevel;
|
||||
|
||||
private NoLoggingImpl noLoggingImpl;
|
||||
|
||||
long xmx;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
xmx = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax() / (1000 * 1000); // m;
|
||||
|
||||
System.gc();
|
||||
|
||||
LogFactory.selectLog4JLogging();
|
||||
log = ((Log4jImpl) LogFactory.getLog(DruidDataSource.class)).getLog();
|
||||
oldLevel = log.getLevel();
|
||||
log.setLevel(Level.FATAL);
|
||||
|
||||
|
||||
log4jLog = ((Log4jImpl) LogFactory.getLog(DruidDataSource.class)).getLog();
|
||||
|
||||
Field logField = DruidDataSource.class.getDeclaredField("LOG");
|
||||
logField.setAccessible(true);
|
||||
Log dataSourceLog = (Log) logField.get(null);
|
||||
if (dataSourceLog instanceof Log4jImpl) {
|
||||
this.log = ((Log4jImpl) dataSourceLog).getLog();
|
||||
this.oldLevel = this.log.getLevel();
|
||||
this.log.setLevel(Level.FATAL);
|
||||
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);
|
||||
// dataSource.setAsyncCloseConnectionEnable(true);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setMaxActive(16);
|
||||
|
||||
dataSource.getProxyFilters().add(new FilterAdapter() {
|
||||
|
||||
@Override
|
||||
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql)
|
||||
throws SQLException {
|
||||
public boolean statement_execute(FilterChain chain, StatementProxy statement,
|
||||
String sql) throws SQLException {
|
||||
throw new SQLException();
|
||||
}
|
||||
});
|
||||
@@ -79,11 +84,13 @@ public class AsyncCloseTest3 extends TestCase {
|
||||
closeExecutor = Executors.newFixedThreadPool(2);
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
if (log != null) {
|
||||
log.setLevel(oldLevel);
|
||||
if (log4jLog != null) {
|
||||
log4jLog.setLevel(log4jOldLevel);
|
||||
} else if (noLoggingImpl != null) {
|
||||
noLoggingImpl.setErrorEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +138,7 @@ public class AsyncCloseTest3 extends TestCase {
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
|
||||
final int COUNT;
|
||||
|
||||
|
||||
if (xmx <= 256) {
|
||||
COUNT = 1024 * 8;
|
||||
} else if (xmx <= 512) {
|
||||
@@ -143,7 +150,7 @@ public class AsyncCloseTest3 extends TestCase {
|
||||
} else {
|
||||
COUNT = 1024 * 128;
|
||||
}
|
||||
|
||||
|
||||
final CountDownLatch closeLatch = new CountDownLatch(COUNT * 2);
|
||||
final CountDownLatch execLatch = new CountDownLatch(COUNT);
|
||||
|
||||
@@ -162,14 +169,14 @@ public class AsyncCloseTest3 extends TestCase {
|
||||
} finally {
|
||||
closeExecutor.submit(closeTask);
|
||||
closeExecutor.submit(closeTask); // dup close
|
||||
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
errorCount.incrementAndGet();
|
||||
} finally {
|
||||
execLatch.countDown();
|
||||
execLatch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -10,6 +10,10 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class JdbcUtils_driver extends TestCase {
|
||||
|
||||
public void test_null() throws Exception {
|
||||
Assert.assertNull(JdbcUtils.getDriverClassName(null));
|
||||
}
|
||||
|
||||
public void test_driver() throws Exception {
|
||||
String url = "jdbc:odps:xxx";
|
||||
|
||||
Reference in New Issue
Block a user