You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/10/22 06:16:04 UTC
cvs commit: jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils BasicRowProcessorTest.java MockResultSet.java TestBean.java MockResultSetMetaData.java
dgraham 2003/10/21 21:16:04
Added: dbutils/src/test/org/apache/commons/dbutils
BasicRowProcessorTest.java MockResultSet.java
TestBean.java MockResultSetMetaData.java
Log:
Added test case for BasicRowProcessor. The two Mock* classes in this
package should probably replace the other mock classes in the main
distro for a fully dynamic solution to the JDBC API problem.
Revision Changes Path
1.1 jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java
Index: BasicRowProcessorTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/BasicRowProcessorTest.java,v 1.1 2003/10/22 04:16:04 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2003/10/22 04:16:04 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.dbutils;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
/**
* Test the BasicRowProcessor class.
*
* @author David Graham
*/
public class BasicRowProcessorTest extends TestCase {
private static final RowProcessor processor = BasicRowProcessor.instance();
private static ResultSetMetaData metaData = null;
private static Object[][] rows = null;
/**
* Constructor for BasicRowProcessorTest.
* @param name
*/
public BasicRowProcessorTest(String name) {
super(name);
}
/**
* @throws java.lang.Exception
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
String[] columnNames = new String[] { "one", "two", "three", "notInBean" };
metaData = MockResultSetMetaData.create(columnNames);
rows = new Object[][] { { "1", "2", "3", "notInBean1" }, {
"4", "5", "6", "notInBean2" }
};
}
public void testToArray() throws SQLException {
ResultSet rs = MockResultSet.create(metaData, rows);
int rowCount = 0;
Object[] a = null;
while (rs.next()) {
a = processor.toArray(rs);
assertEquals(4, a.length);
rowCount++;
}
assertEquals(2, rowCount);
assertEquals("4", a[0]);
assertEquals("5", a[1]);
assertEquals("6", a[2]);
}
public void testToBean() throws SQLException {
ResultSet rs = MockResultSet.create(metaData, rows);
int rowCount = 0;
TestBean b = null;
while (rs.next()) {
b = (TestBean) processor.toBean(rs, TestBean.class);
assertNotNull(b);
rowCount++;
}
assertEquals(2, rowCount);
assertEquals("4", b.getOne());
assertEquals("5", b.getTwo());
assertEquals("6", b.getThree());
assertEquals("not set", b.getDoNotSet());
}
public void testToBeanList() throws SQLException {
ResultSet rs = MockResultSet.create(metaData, rows);
List list = processor.toBeanList(rs, TestBean.class);
assertNotNull(list);
assertEquals(2, list.size());
TestBean b = (TestBean) list.get(1);
assertEquals("4", b.getOne());
assertEquals("5", b.getTwo());
assertEquals("6", b.getThree());
assertEquals("not set", b.getDoNotSet());
}
public void testToMap() throws SQLException {
ResultSet rs = MockResultSet.create(metaData, rows);
int rowCount = 0;
Map m = null;
while (rs.next()) {
m = processor.toMap(rs);
assertNotNull(m);
assertEquals(4, m.keySet().size());
rowCount++;
}
assertEquals(2, rowCount);
assertEquals("4", m.get("One")); // case shouldn't matter
assertEquals("5", m.get("two"));
assertEquals("6", m.get("THREE"));
}
}
1.1 jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSet.java
Index: MockResultSet.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSet.java,v 1.1 2003/10/22 04:16:04 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2003/10/22 04:16:04 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.dbutils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Arrays;
import java.util.Iterator;
/**
* MockResultSet dynamically implements the ResultSet interface.
*
* @author David Graham
*/
class MockResultSet implements InvocationHandler {
private Object[] rows = null;
private ResultSetMetaData metaData = null;
private Iterator iter = null;
private Object[] currentRow = null;
private Boolean wasNull = Boolean.FALSE;
/**
* Create a <code>MockResultSet</code> proxy object. This is equivalent to:
* <pre>
* ProxyFactory.instance().createResultSet(new MockResultSet(metaData, rows));
* </pre>
*
* @param metaData
* @param rows
* @return
*/
public static ResultSet create(ResultSetMetaData metaData, Object[][] rows) {
return ProxyFactory.instance().createResultSet(
new MockResultSet(metaData, rows));
}
public MockResultSet(ResultSetMetaData metaData, Object[][] rows) {
super();
this.metaData = metaData;
this.rows = rows;
this.iter = Arrays.asList(rows).iterator();
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
if (methodName.equals("getMetaData")) {
return this.metaData;
} else if (methodName.equals("next")) {
if (!iter.hasNext()) {
return Boolean.FALSE;
} else {
this.currentRow = (Object[]) iter.next();
return Boolean.TRUE;
}
} else if (methodName.equals("previous")) {
} else if (methodName.equals("close")) {
} else if (methodName.equals("getObject")) {
int col = ((Integer) args[0]).intValue() - 1;
Object obj = this.currentRow[col];
if (obj == null) {
this.wasNull = (obj == null) ? Boolean.TRUE : Boolean.FALSE;
}
return obj;
} else if (methodName.equals("wasNull")) {
return this.wasNull;
}
return null;
}
}
1.1 jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/TestBean.java
Index: TestBean.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/TestBean.java,v 1.1 2003/10/22 04:16:04 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2003/10/22 04:16:04 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.dbutils;
/**
* A bean to use in testing toBean() and toBeanList().
*
* @author David Graham
*/
class TestBean {
private String one = null;
private String two = null;
private String three = null;
private String doNotSet = "not set";
/**
* Constructor for TestBean.
*/
public TestBean() {
super();
}
public String getOne() {
return one;
}
public String getThree() {
return three;
}
public String getTwo() {
return two;
}
public void setOne(String string) {
one = string;
}
public void setThree(String string) {
three = string;
}
public void setTwo(String string) {
two = string;
}
public String getDoNotSet() {
return doNotSet;
}
public void setDoNotSet(String string) {
doNotSet = string;
}
}
1.1 jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSetMetaData.java
Index: MockResultSetMetaData.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/MockResultSetMetaData.java,v 1.1 2003/10/22 04:16:04 dgraham Exp $
* $Revision: 1.1 $
* $Date: 2003/10/22 04:16:04 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.dbutils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.ResultSetMetaData;
/**
* MockResultSetMetaData dynamically implements the ResultSetMetaData
* interface.
*
* @author David Graham
*/
class MockResultSetMetaData implements InvocationHandler {
private String[] columnNames = null;
/**
* Create a <code>MockResultSetMetaData</code> proxy object. This is
* equivalent to:
* <pre>
* ProxyFactory.instance().createResultSetMetaData(new MockResultSetMetaData(columnNames));
* </pre>
*
* @param columnNames
* @return
*/
public static ResultSetMetaData create(String[] columnNames) {
return ProxyFactory.instance().createResultSetMetaData(
new MockResultSetMetaData(columnNames));
}
public MockResultSetMetaData(String[] columnNames) {
super();
this.columnNames = columnNames;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
if (methodName.equals("getColumnCount")) {
return new Integer(this.columnNames.length);
} else if (
methodName.equals("getColumnName")
|| methodName.equals("getColumnLabel")) {
int col = ((Integer) args[0]).intValue() - 1;
return this.columnNames[col];
// stub out other methods for now
} else {
Class returnType = method.getReturnType();
if (returnType.equals(String.class)) {
return "";
} else if (returnType.equals(Integer.TYPE)) {
return new Integer(0);
} else {
return Boolean.FALSE;
}
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org