mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge branch 'master' of https://github.com/alibaba/druid
This commit is contained in:
@@ -55,6 +55,8 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
|
||||
private SQLException error;
|
||||
|
||||
private String lastSql;
|
||||
|
||||
public MockConnection(){
|
||||
this(null, null, null);
|
||||
}
|
||||
@@ -69,6 +71,14 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
}
|
||||
}
|
||||
|
||||
public String getLastSql() {
|
||||
return lastSql;
|
||||
}
|
||||
|
||||
public void setLastSql(String lastSql) {
|
||||
this.lastSql = lastSql;
|
||||
}
|
||||
|
||||
public SQLException getError() {
|
||||
return error;
|
||||
}
|
||||
@@ -268,7 +278,7 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
if (error != null) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
if (closed) {
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
@@ -469,7 +479,7 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
public int getNetworkTimeout() throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setReadOnly(boolean readOnly) throws SQLException {
|
||||
checkState();
|
||||
|
||||
@@ -86,6 +86,7 @@ public class MockStatement extends StatementBase implements MockStatementBase, S
|
||||
}
|
||||
|
||||
if (mockConnection != null) {
|
||||
mockConnection.setLastSql(sql);
|
||||
mockConnection.handleSleep();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@ import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
@@ -598,20 +597,24 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setConnectionInitSqls(Collection<Object> connectionInitSqls) {
|
||||
public void setConnectionInitSqls(Collection<? extends Object> connectionInitSqls) {
|
||||
if ((connectionInitSqls != null) && (connectionInitSqls.size() > 0)) {
|
||||
ArrayList<String> newVal = null;
|
||||
for (Iterator<Object> iterator = connectionInitSqls.iterator(); iterator.hasNext();) {
|
||||
Object o = iterator.next();
|
||||
if (o != null) {
|
||||
String s = o.toString();
|
||||
if (s.trim().length() > 0) {
|
||||
if (newVal == null) {
|
||||
newVal = new ArrayList<String>();
|
||||
}
|
||||
newVal.add(s);
|
||||
}
|
||||
for (Object o : connectionInitSqls) {
|
||||
if (o == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String s = o.toString();
|
||||
s = s.trim();
|
||||
if (s.length() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (newVal == null) {
|
||||
newVal = new ArrayList<String>();
|
||||
}
|
||||
newVal.add(s);
|
||||
}
|
||||
this.connectionInitSqls = newVal;
|
||||
} else {
|
||||
@@ -1456,6 +1459,25 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
if (getDefaultCatalog() != null && getDefaultCatalog().length() != 0) {
|
||||
conn.setCatalog(getDefaultCatalog());
|
||||
}
|
||||
|
||||
Collection<String> initSqls = getConnectionInitSqls();
|
||||
if (initSqls.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
for (String sql : initSqls) {
|
||||
if (sql == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
stmt.execute(sql);
|
||||
}
|
||||
} finally {
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract int getActivePeak();
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidDataSourceTest_initSql extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
List<Object> sqlList = new ArrayList<Object>();
|
||||
sqlList.add("select 123");
|
||||
sqlList.add(null);
|
||||
sqlList.add("");
|
||||
|
||||
dataSource.setConnectionInitSqls(sqlList);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void testDefault() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
MockConnection mockConn = conn.unwrap(MockConnection.class);
|
||||
|
||||
Assert.assertEquals("select 123", mockConn.getLastSql());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
|
||||
public class DruidDataSourceTest_initSql_factory extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Properties properties = new Properties();
|
||||
properties.put(DruidDataSourceFactory.PROP_URL, "jdbc:mock:xxx");
|
||||
properties.put(DruidDataSourceFactory.PROP_INITCONNECTIONSQLS, ";;select 123");
|
||||
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void testDefault() throws Exception {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
MockConnection mockConn = conn.unwrap(MockConnection.class);
|
||||
|
||||
Assert.assertEquals("select 123", mockConn.getLastSql());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -8,7 +8,7 @@ import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidDataSourceTest9 extends TestCase {
|
||||
public class DruidDataSourceTest_initSqls extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
Reference in New Issue
Block a user