mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mock refactor
This commit is contained in:
@@ -108,6 +108,10 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
return createMockStatement();
|
||||
}
|
||||
|
||||
private MockStatement createMockStatement() {
|
||||
return new MockStatement(this);
|
||||
}
|
||||
|
||||
@@ -189,7 +193,7 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
MockStatement stmt = new MockStatement(this);
|
||||
MockStatement stmt = createMockStatement();
|
||||
|
||||
stmt.setResultSetType(resultSetType);
|
||||
stmt.setResultSetConcurrency(resultSetConcurrency);
|
||||
@@ -273,7 +277,7 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
throw new MockConnectionClosedException();
|
||||
}
|
||||
|
||||
MockStatement stmt = new MockStatement(this);
|
||||
MockStatement stmt = createMockStatement();
|
||||
|
||||
stmt.setResultSetType(resultSetType);
|
||||
stmt.setResultSetConcurrency(resultSetConcurrency);
|
||||
@@ -300,10 +304,7 @@ public class MockConnection extends ConnectionBase implements Connection {
|
||||
|
||||
private MockPreparedStatement createMockPreparedStatement(String sql) {
|
||||
if (driver != null) {
|
||||
MockPreparedStatementFactory preparedStatementFactory = driver.getPreparedStatementFactory();
|
||||
if (preparedStatementFactory != null) {
|
||||
return preparedStatementFactory.createMockPreparedStatement(this, sql);
|
||||
}
|
||||
return driver.createMockPreparedStatement(this, sql);
|
||||
}
|
||||
return new MockPreparedStatement(this, sql);
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.alibaba.druid.mock;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public interface MockConnectionFactory {
|
||||
|
||||
MockConnection createConnection(MockDriver driver, String url, Properties connectProperties);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.alibaba.druid.mock;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class MockConnectionFactoryImpl implements MockConnectionFactory {
|
||||
|
||||
@Override
|
||||
public MockConnection createConnection(MockDriver driver, String url, Properties connectProperties) {
|
||||
return new MockConnection(driver, url, connectProperties);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,53 +43,34 @@ import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
public class MockDriver implements Driver, MockDriverMBean {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(MockDriver.class);
|
||||
private final static Log LOG = LogFactory.getLog(MockDriver.class);
|
||||
|
||||
public final static MockExecuteHandler DEFAULT_HANDLER = new MySqlMockExecuteHandlerImpl();
|
||||
public final static MockExecuteHandler DEFAULT_HANDLER = new MySqlMockExecuteHandlerImpl();
|
||||
|
||||
private String prefix = "jdbc:fake:";
|
||||
private String mockPrefix = "jdbc:mock:";
|
||||
private String prefix = "jdbc:fake:";
|
||||
private String mockPrefix = "jdbc:mock:";
|
||||
|
||||
private MockExecuteHandler executeHandler = DEFAULT_HANDLER;
|
||||
private MockExecuteHandler executeHandler = DEFAULT_HANDLER;
|
||||
|
||||
public final static MockDriver instance = new MockDriver();
|
||||
public final static MockDriver instance = new MockDriver();
|
||||
|
||||
private final AtomicLong connectCount = new AtomicLong();
|
||||
private final AtomicLong connectionCloseCount = new AtomicLong();
|
||||
private final AtomicLong connectCount = new AtomicLong();
|
||||
private final AtomicLong connectionCloseCount = new AtomicLong();
|
||||
|
||||
private final AtomicLong connectionIdSeed = new AtomicLong(1000L);
|
||||
private final AtomicLong connectionIdSeed = new AtomicLong(1000L);
|
||||
|
||||
private final List<MockConnection> connections = new CopyOnWriteArrayList<MockConnection>();
|
||||
private final List<MockConnection> connections = new CopyOnWriteArrayList<MockConnection>();
|
||||
|
||||
private long idleTimeCount = 1000 * 60 * 3;
|
||||
private long idleTimeCount = 1000 * 60 * 3;
|
||||
|
||||
private boolean logExecuteQueryEnable = true;
|
||||
private boolean logExecuteQueryEnable = true;
|
||||
|
||||
private final static String MBEAN_NAME = "com.alibaba.druid:type=MockDriver";
|
||||
|
||||
private MockConnectionFactory connectionFactory = new MockConnectionFactoryImpl();
|
||||
private MockPreparedStatementFactory preparedStatementFactory = new MockPreparedStatementFactoryImpl();
|
||||
private final static String MBEAN_NAME = "com.alibaba.druid:type=MockDriver";
|
||||
|
||||
static {
|
||||
registerDriver(instance);
|
||||
}
|
||||
|
||||
public MockConnectionFactory getConnectionFactory() {
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
public void setConnectionFactory(MockConnectionFactory connectionFactory) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
public MockPreparedStatementFactory getPreparedStatementFactory() {
|
||||
return preparedStatementFactory;
|
||||
}
|
||||
|
||||
public void setPreparedStatementFactory(MockPreparedStatementFactory preparedStatementFactory) {
|
||||
this.preparedStatementFactory = preparedStatementFactory;
|
||||
}
|
||||
|
||||
public boolean isLogExecuteQueryEnable() {
|
||||
return logExecuteQueryEnable;
|
||||
}
|
||||
@@ -192,7 +173,7 @@ public class MockDriver implements Driver, MockDriverMBean {
|
||||
}
|
||||
}
|
||||
|
||||
MockConnection conn = connectionFactory.createConnection(this, url, info);
|
||||
MockConnection conn = createMockConnection(this, url, info);
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("connect, url " + url + ", id " + conn.getId());
|
||||
@@ -324,4 +305,16 @@ public class MockDriver implements Driver, MockDriverMBean {
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
}
|
||||
|
||||
public MockConnection createMockConnection(MockDriver driver, String url, Properties connectProperties) {
|
||||
return new MockConnection(this, url, connectProperties);
|
||||
}
|
||||
|
||||
public MockPreparedStatement createMockPreparedStatement(MockConnection conn, String sql) {
|
||||
return new MockPreparedStatement(conn, sql);
|
||||
}
|
||||
|
||||
public MockStatement createMockStatement(MockConnection conn) {
|
||||
return new MockStatement(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.alibaba.druid.mock;
|
||||
|
||||
|
||||
public interface MockPreparedStatementFactory {
|
||||
MockPreparedStatement createMockPreparedStatement(MockConnection conn, String sql);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.alibaba.druid.mock;
|
||||
|
||||
|
||||
public class MockPreparedStatementFactoryImpl implements MockPreparedStatementFactory {
|
||||
|
||||
@Override
|
||||
public MockPreparedStatement createMockPreparedStatement(MockConnection conn, String sql) {
|
||||
return new MockPreparedStatement(conn, sql);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user