mock refactor

This commit is contained in:
温少
2012-06-06 08:40:01 +08:00
parent c509b2db6a
commit ffeb629f0d
7 changed files with 43 additions and 89 deletions
@@ -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);
}
}
@@ -11,7 +11,6 @@ import junit.framework.TestCase;
import com.alibaba.druid.mock.MockConnection;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.mock.MockPreparedStatement;
import com.alibaba.druid.mock.MockPreparedStatementFactory;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
@@ -21,15 +20,13 @@ public class TestGetUpdateCount extends TestCase {
private MockDriver driver;
protected void setUp() throws Exception {
driver = new MockDriver();
driver.setPreparedStatementFactory(new MockPreparedStatementFactory() {
driver = new MockDriver() {
@Override
public MockPreparedStatement createMockPreparedStatement(MockConnection conn, String sql) {
return new MyPreparedStatement(conn, sql);
}
});
};
// /////////////////////////////
@@ -108,20 +105,20 @@ public class TestGetUpdateCount extends TestCase {
}
{
Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement("update t set id = ?");
myStmtB = stmt.unwrap(MyPreparedStatement.class);
Assert.assertSame(myStmtA, myStmtB);
Assert.assertNotNull(myStmtB.updateCount);
stmt.setString(1, "xxx");
stmt.execute();
Assert.assertNotNull(myStmtB.updateCount);
Assert.assertEquals(1, stmt.getUpdateCount());
stmt.close();
conn.close();
}
@@ -134,12 +131,12 @@ public class TestGetUpdateCount extends TestCase {
public MyPreparedStatement(MockConnection conn, String sql){
super(conn, sql);
}
public boolean execute() throws SQLException {
updateCount = null;
return false;
}
public ResultSet executeQuery() throws SQLException {
updateCount = -1;
return super.executeQuery();