完善mock和测试

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@784 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-09-16 00:20:56 +00:00
parent fd94af5b57
commit 09c328aa9c
3 changed files with 256 additions and 10 deletions
@@ -41,13 +41,15 @@ import java.util.Map;
public class MockResultSet implements ResultSet {
private int rowIndex = -1;
private int rowIndex = -1;
private Statement statement;
private List<Object[]> rows = new ArrayList<Object[]>();
private List<Object[]> rows = new ArrayList<Object[]>();
private boolean wasNull = false;
private boolean wasNull = false;
private MockResultSetMetaData metaData = new MockResultSetMetaData();
private MockResultSetMetaData metaData = new MockResultSetMetaData();
private boolean closed = false;
private int fetchSize = 0;
public MockResultSet(Statement statement){
super();
@@ -58,15 +60,20 @@ public class MockResultSet implements ResultSet {
return rows;
}
@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == MockResultSet.class || iface == ResultSet.class) {
return (T) this;
}
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
if (iface == MockResultSet.class || iface == ResultSet.class) {
return true;
}
return false;
}
@@ -81,7 +88,7 @@ public class MockResultSet implements ResultSet {
@Override
public void close() throws SQLException {
this.closed = true;
}
@Override
@@ -358,6 +365,9 @@ public class MockResultSet implements ResultSet {
@Override
public boolean isBeforeFirst() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@@ -370,52 +380,79 @@ public class MockResultSet implements ResultSet {
@Override
public boolean isFirst() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@Override
public boolean isLast() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@Override
public void beforeFirst() throws SQLException {
if (closed) {
throw new SQLException();
}
}
@Override
public void afterLast() throws SQLException {
if (closed) {
throw new SQLException();
}
}
@Override
public boolean first() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@Override
public boolean last() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@Override
public int getRow() throws SQLException {
if (closed) {
throw new SQLException();
}
return 0;
}
@Override
public boolean absolute(int row) throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@Override
public boolean relative(int rows) throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@@ -431,52 +468,80 @@ public class MockResultSet implements ResultSet {
@Override
public void setFetchDirection(int direction) throws SQLException {
if (closed) {
throw new SQLException();
}
}
@Override
public int getFetchDirection() throws SQLException {
if (closed) {
throw new SQLException();
}
return 0;
}
@Override
public void setFetchSize(int rows) throws SQLException {
if (closed) {
throw new SQLException();
}
this.fetchSize = rows;
}
@Override
public int getFetchSize() throws SQLException {
if (closed) {
throw new SQLException();
}
return 0;
return fetchSize;
}
@Override
public int getType() throws SQLException {
if (closed) {
throw new SQLException();
}
return 0;
}
@Override
public int getConcurrency() throws SQLException {
if (closed) {
throw new SQLException();
}
return 0;
}
@Override
public boolean rowUpdated() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@Override
public boolean rowInserted() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@Override
public boolean rowDeleted() throws SQLException {
if (closed) {
throw new SQLException();
}
return false;
}
@@ -765,7 +765,7 @@ public class ResultSetTest extends TestCase {
{
SQLException error = null;
try {
resultSet.updateBlob(0, (InputStream) null, 1);
resultSet.updateAsciiStream(0, (InputStream) null, 1);
} catch (SQLException ex) {
error = ex;
}
@@ -1872,4 +1872,58 @@ public class ResultSetTest extends TestCase {
Assert.assertNotNull(error);
}
}
public void test_updateObject() throws Exception {
resultSet.next();
resultSet.updateObject(1, null);
resultSet.updateObject("1", null);
{
SQLException error = null;
try {
resultSet.updateObject(0, null);
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
{
SQLException error = null;
try {
resultSet.updateObject("0", null);
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
public void test_updateObject_1() throws Exception {
resultSet.next();
resultSet.updateObject(1, null, 1);
resultSet.updateObject("1", null, 1);
{
SQLException error = null;
try {
resultSet.updateObject(0, null, 1);
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
{
SQLException error = null;
try {
resultSet.updateObject("0", null, 1);
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
}
@@ -0,0 +1,127 @@
package com.alibaba.druid.bvt.pool.basic;
import java.sql.SQLException;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.mock.MockResultSet;
import com.alibaba.druid.pool.PoolableResultSet;
import com.alibaba.druid.pool.PoolableStatement;
public class ResultSetTest2 extends TestCase {
private PoolableStatement stmt;
private MockResultSet raw;
private PoolableResultSet resultSet;
protected void setUp() throws Exception {
stmt = new PoolableStatement(null, null) {
protected SQLException checkException(Throwable error) throws SQLException {
if (error instanceof SQLException) {
return (SQLException) error;
}
return new SQLException(error);
}
};
raw = new MockResultSet(null);
raw.getRows().add(new Object[] { null });
resultSet = new PoolableResultSet(stmt, raw);
}
public void test_rowDeleted() throws Exception {
resultSet.rowDeleted();
raw.close();
{
SQLException error = null;
try {
resultSet.rowDeleted();
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
public void test_rowInserted() throws Exception {
resultSet.rowInserted();
raw.close();
{
SQLException error = null;
try {
resultSet.rowInserted();
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
public void test_rowUpdated() throws Exception {
resultSet.rowInserted();
raw.close();
{
SQLException error = null;
try {
resultSet.rowUpdated();
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
public void test_getConcurrency() throws Exception {
resultSet.getConcurrency();
raw.close();
{
SQLException error = null;
try {
resultSet.getConcurrency();
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
public void test_getType() throws Exception {
resultSet.getType();
raw.close();
{
SQLException error = null;
try {
resultSet.getType();
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
public void test_FetchSize() throws Exception {
resultSet.setFetchSize(10);
Assert.assertEquals(10, resultSet.getFetchSize());
raw.close();
{
SQLException error = null;
try {
resultSet.getFetchSize();
} catch (SQLException ex) {
error = ex;
}
Assert.assertNotNull(error);
}
}
}