完善mock和测试

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@780 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-09-15 23:29:15 +00:00
parent 632695260b
commit 158db2907d
3 changed files with 184 additions and 0 deletions
@@ -60,6 +60,10 @@ public class MockStatement implements Statement {
@Override
public ResultSet executeQuery(String sql) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
if (mockConnection != null && mockConnection.getDriver() != null) {
return mockConnection.getDriver().executeQuery(this, sql);
}
@@ -69,6 +73,10 @@ public class MockStatement implements Statement {
@Override
public int executeUpdate(String sql) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
return 0;
}
@@ -133,6 +141,10 @@ public class MockStatement implements Statement {
@Override
public boolean execute(String sql) throws SQLException {
if (closed) {
throw new SQLException("stmt closed.");
}
if (ERROR_SQL.equals(sql)) {
throw new SQLException();
}
@@ -0,0 +1,43 @@
package com.alibaba.druid.bvt.pool.basic;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.mock.MockStatement;
import com.alibaba.druid.pool.PoolableStatement;
public class PoolableStatementTest extends TestCase {
protected Statement raw;
protected PoolableStatement stmt;
protected void setUp() throws Exception {
raw = new MockStatement(null);
stmt = new PoolableStatement(null, raw) {
protected SQLException checkException(Throwable error) throws SQLException {
if (error instanceof SQLException) {
return (SQLException) error;
}
return new SQLException(error);
}
};
}
protected void tearDown() throws Exception {
}
public void test_basic() throws Exception {
Assert.assertEquals(raw, stmt.getStatement());
Assert.assertEquals(null, stmt.getPoolableConnection());
Assert.assertEquals(null, stmt.getConnection());
Assert.assertEquals(false, stmt.isPoolable());
stmt.toString();
}
}
@@ -0,0 +1,129 @@
package com.alibaba.druid.bvt.pool.basic;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.PoolableStatement;
import com.alibaba.druid.pool.vendor.NullExceptionSorter;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
public class PoolableStatementTest2 extends TestCase {
private MockDriver driver;
private DruidDataSource dataSource;
protected void setUp() throws Exception {
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
driver = new MockDriver();
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setDriver(driver);
dataSource.setInitialSize(1);
dataSource.setMaxActive(2);
dataSource.setMaxIdle(2);
dataSource.setMinIdle(1);
dataSource.setMinEvictableIdleTimeMillis(300 * 1000); // 300 / 10
dataSource.setTimeBetweenEvictionRunsMillis(180 * 1000); // 180 / 10
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(false);
dataSource.setValidationQuery("SELECT 1");
dataSource.setFilters("stat");
dataSource.setRemoveAbandoned(true);
dataSource.setExceptionSorterClassName(null);
Assert.assertTrue(dataSource.getExceptionSoter() instanceof NullExceptionSorter);
dataSource.setExceptionSorterClassName("");
Assert.assertTrue(dataSource.getExceptionSoter() instanceof NullExceptionSorter);
}
protected void tearDown() throws Exception {
Assert.assertEquals(true, dataSource.getCreateTimespanNano() > 0);
dataSource.close();
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
}
public void test_dupClose() throws Exception {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
stmt.close();
stmt.close();
conn.close();
}
public void test_executeUpdate() throws Exception {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate("SET @VAR = 1");
stmt.close();
conn.close();
}
public void test_executeUpdate_error() throws Exception {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
((PoolableStatement) stmt).getStatement().close();
{
SQLException error = null;
try {
stmt.executeUpdate("SET @VAR = 1");
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
stmt.close();
conn.close();
}
public void test_execute_error() throws Exception {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
((PoolableStatement) stmt).getStatement().close();
{
SQLException error = null;
try {
stmt.execute("SET @VAR = 1");
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
stmt.close();
conn.close();
}
public void test_executeQuery_error() throws Exception {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
((PoolableStatement) stmt).getStatement().close();
{
SQLException error = null;
try {
stmt.executeQuery("SELECT 1");
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
stmt.close();
conn.close();
}
}