mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
完善mock和测试
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@827 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -117,7 +117,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public Statement createStatement() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockStatement(this);
|
||||
@@ -126,7 +126,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockPreparedStatement(this, sql);
|
||||
@@ -135,7 +135,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockCallableStatement(this, sql);
|
||||
@@ -144,7 +144,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public String nativeSQL(String sql) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return sql;
|
||||
@@ -153,7 +153,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public void setAutoCommit(boolean autoCommit) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
this.autoCommit = autoCommit;
|
||||
@@ -167,7 +167,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public void commit() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -175,7 +175,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public void rollback() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -243,7 +243,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockStatement(this);
|
||||
@@ -253,7 +253,7 @@ public class MockConnection implements Connection {
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
|
||||
throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockPreparedStatement(this, sql);
|
||||
@@ -262,7 +262,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockCallableStatement(this, sql);
|
||||
@@ -291,7 +291,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public Savepoint setSavepoint() throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -299,7 +299,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public Savepoint setSavepoint(String name) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -307,14 +307,14 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public void rollback(Savepoint savepoint) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,7 +322,7 @@ public class MockConnection implements Connection {
|
||||
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
|
||||
throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockStatement(this);
|
||||
@@ -332,7 +332,7 @@ public class MockConnection implements Connection {
|
||||
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockPreparedStatement(this, sql);
|
||||
@@ -342,7 +342,7 @@ public class MockConnection implements Connection {
|
||||
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockCallableStatement(this, sql);
|
||||
@@ -351,7 +351,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockPreparedStatement(this, sql);
|
||||
@@ -360,7 +360,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockPreparedStatement(this, sql);
|
||||
@@ -369,7 +369,7 @@ public class MockConnection implements Connection {
|
||||
@Override
|
||||
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
|
||||
if (closed) {
|
||||
throw new SQLException();
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return new MockPreparedStatement(this, sql);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.alibaba.druid.mock;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class MockConnectionClosedException extends SQLException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public MockConnectionClosedException() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.vendor.InformixExceptionSorter;
|
||||
import com.alibaba.druid.pool.vendor.MSSQLValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.vendor.MockExceptionSorter;
|
||||
import com.alibaba.druid.pool.vendor.MySqlExceptionSorter;
|
||||
import com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.vendor.OracleExceptionSorter;
|
||||
@@ -247,6 +248,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
} else if (realDriverClassName.equals("com.sybase.jdbc2.jdbc.SybDriver")) {
|
||||
this.exceptionSoter = new SybaseExceptionSorter();
|
||||
|
||||
} else if (realDriverClassName.equals("com.alibaba.druid.mock.MockDriver")) {
|
||||
this.exceptionSoter = new MockExceptionSorter();
|
||||
}
|
||||
|
||||
for (Filter filter : filters) {
|
||||
|
||||
@@ -60,7 +60,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
this.holder = holder;
|
||||
}
|
||||
|
||||
protected SQLException handleException(Throwable t) throws SQLException {
|
||||
public SQLException handleException(Throwable t) throws SQLException {
|
||||
final ConnectionHolder holder = this.holder;
|
||||
|
||||
//
|
||||
@@ -766,7 +766,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
@Override
|
||||
public void setClientInfo(String name, String value) throws SQLClientInfoException {
|
||||
if (holder == null) {
|
||||
throw new IllegalStateException();
|
||||
throw new SQLClientInfoException();
|
||||
}
|
||||
|
||||
conn.setClientInfo(name, value);
|
||||
@@ -775,7 +775,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
@Override
|
||||
public void setClientInfo(Properties properties) throws SQLClientInfoException {
|
||||
if (holder == null) {
|
||||
throw new IllegalStateException();
|
||||
throw new SQLClientInfoException();
|
||||
}
|
||||
|
||||
conn.setClientInfo(properties);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alibaba.druid.pool.vendor;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnectionClosedException;
|
||||
import com.alibaba.druid.pool.ExceptionSorter;
|
||||
|
||||
public class MockExceptionSorter implements ExceptionSorter {
|
||||
|
||||
private final static MockExceptionSorter instance = new MockExceptionSorter();
|
||||
|
||||
public final static MockExceptionSorter getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExceptionFatal(SQLException e) {
|
||||
if (e instanceof MockConnectionClosedException) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@ import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
import com.mysql.jdbc.ResultSet;
|
||||
|
||||
public class ConnectionStatementTest extends TestCase {
|
||||
public class ConnectionTest extends TestCase {
|
||||
|
||||
private MockDriver driver;
|
||||
private DruidDataSource dataSource;
|
||||
+1
-1
@@ -12,7 +12,7 @@ import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
import com.mysql.jdbc.ResultSet;
|
||||
|
||||
public class ConnectionStatementTest2 extends TestCase {
|
||||
public class ConnectionTest2 extends TestCase {
|
||||
|
||||
private MockDriver driver;
|
||||
private DruidDataSource dataSource;
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.alibaba.druid.bvt.pool.basic;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -33,7 +36,7 @@ public class ConnectionTest5 extends TestCase {
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setValidationQuery("SELECT 1");
|
||||
dataSource.setFilters("stat,trace");
|
||||
|
||||
|
||||
JdbcStatContext context = new JdbcStatContext();
|
||||
context.setTraceEnable(true);
|
||||
JdbcStatManager.getInstance().setStatContext(context);
|
||||
@@ -42,26 +45,120 @@ public class ConnectionTest5 extends TestCase {
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
|
||||
|
||||
|
||||
JdbcStatManager.getInstance().setStatContext(null);
|
||||
}
|
||||
|
||||
public void test_basic() throws Exception {
|
||||
PoolableConnection conn = (PoolableConnection) dataSource.getConnection();
|
||||
conn.close();
|
||||
|
||||
|
||||
Assert.assertEquals(true, dataSource.isResetStatEnable());
|
||||
dataSource.setResetStatEnable(false);
|
||||
Assert.assertEquals(false, dataSource.isResetStatEnable());
|
||||
Assert.assertEquals(1, dataSource.getConnectCount());
|
||||
dataSource.resetStat();
|
||||
Assert.assertEquals(1, dataSource.getConnectCount());
|
||||
|
||||
|
||||
dataSource.setResetStatEnable(true);
|
||||
dataSource.resetStat();
|
||||
Assert.assertEquals(0, dataSource.getConnectCount());
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void test_handleException() throws Exception {
|
||||
PoolableConnection conn = (PoolableConnection) dataSource.getConnection();
|
||||
conn.close();
|
||||
|
||||
SQLException error = new SQLException();
|
||||
try {
|
||||
conn.handleException(error);
|
||||
} catch (SQLException ex) {
|
||||
Assert.assertEquals(error, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void test_handleException_2() throws Exception {
|
||||
PoolableConnection conn = dataSource.getConnection().unwrap(PoolableConnection.class);
|
||||
conn.getConnection().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
conn.handleException(new RuntimeException());
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
|
||||
public void test_handleException_3() throws Exception {
|
||||
PoolableConnection conn = dataSource.getConnection().unwrap(PoolableConnection.class);
|
||||
conn.getConnection().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
conn.handleException(new RuntimeException());
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
public void test_handleException_4() throws Exception {
|
||||
PoolableConnection conn = dataSource.getConnection().unwrap(PoolableConnection.class);
|
||||
conn.getConnection().close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
conn.prepareStatement("SELECT 1");
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
Assert.assertEquals(true, conn.isClosed());
|
||||
}
|
||||
|
||||
|
||||
public void test_setClientInfo() throws Exception {
|
||||
PoolableConnection conn = dataSource.getConnection().unwrap(PoolableConnection.class);
|
||||
conn.close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
conn.setClientInfo("name", "xxx");
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void test_setClientInfo_1() throws Exception {
|
||||
PoolableConnection conn = dataSource.getConnection().unwrap(PoolableConnection.class);
|
||||
conn.close();
|
||||
|
||||
{
|
||||
SQLException error = null;
|
||||
try {
|
||||
conn.setClientInfo(new Properties());
|
||||
} catch (SQLException ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user