You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by th...@apache.org on 2017/07/07 02:06:24 UTC
[02/58] [abbrv] commons-dbutils git commit: Changed the package names
so dbutils and dbutils2 won't conflict if both loaded
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/DbUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/DbUtilsTest.java b/src/test/java/org/apache/commons/dbutils2/DbUtilsTest.java
new file mode 100644
index 0000000..3623689
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/DbUtilsTest.java
@@ -0,0 +1,273 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.apache.commons.dbutils2.DbUtils;
+import org.junit.Test;
+
+public class DbUtilsTest {
+
+ @Test
+ public void closeNullConnection() throws Exception {
+ DbUtils.close((Connection) null);
+ }
+
+ @Test
+ public void closeConnection() throws Exception {
+ Connection mockCon = mock(Connection.class);
+ DbUtils.close(mockCon);
+ verify(mockCon).close();
+ }
+
+ @Test
+ public void closeNullResultSet() throws Exception {
+ DbUtils.close((ResultSet) null);
+ }
+
+ @Test
+ public void closeResultSet() throws Exception {
+ ResultSet mockResultSet = mock(ResultSet.class);
+ DbUtils.close(mockResultSet);
+ verify(mockResultSet).close();
+ }
+
+ @Test
+ public void closeNullStatement() throws Exception {
+ DbUtils.close((Statement) null);
+ }
+
+ @Test
+ public void closeStatement() throws Exception {
+ Statement mockStatement = mock(Statement.class);
+ DbUtils.close(mockStatement);
+ verify(mockStatement).close();
+ }
+
+ @Test
+ public void closeQuietlyNullConnection() throws Exception {
+ DbUtils.closeQuietly((Connection) null);
+ }
+
+ @Test
+ public void closeQuietlyConnection() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ DbUtils.closeQuietly(mockConnection);
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void closeQuietlyConnectionThrowingException() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ doThrow(SQLException.class).when(mockConnection).close();
+ DbUtils.closeQuietly(mockConnection);
+ }
+
+ @Test
+ public void closeQuietlyNullResultSet() throws Exception {
+ DbUtils.closeQuietly((ResultSet) null);
+ }
+
+ @Test
+ public void closeQuietlyResultSet() throws Exception {
+ ResultSet mockResultSet = mock(ResultSet.class);
+ DbUtils.closeQuietly(mockResultSet);
+ verify(mockResultSet).close();
+ }
+
+ @Test
+ public void closeQuietlyResultSetThrowingException() throws Exception {
+ ResultSet mockResultSet = mock(ResultSet.class);
+ doThrow(SQLException.class).when(mockResultSet).close();
+ DbUtils.closeQuietly(mockResultSet);
+ }
+
+ @Test
+ public void closeQuietlyNullStatement() throws Exception {
+ DbUtils.closeQuietly((Statement) null);
+ }
+
+ @Test
+ public void closeQuietlyStatement() throws Exception {
+ Statement mockStatement = mock(Statement.class);
+ DbUtils.closeQuietly(mockStatement);
+ verify(mockStatement).close();
+ }
+
+ @Test
+ public void closeQuietlyStatementThrowingException() throws Exception {
+ Statement mockStatement = mock(Statement.class);
+ doThrow(SQLException.class).when(mockStatement).close();
+ DbUtils.closeQuietly(mockStatement);
+ }
+
+ @Test
+ public void closeQuietlyConnectionResultSetStatement() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ ResultSet mockResultSet = mock(ResultSet.class);
+ Statement mockStatement = mock(Statement.class);
+ DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
+ verify(mockConnection).close();
+ verify(mockResultSet).close();
+ verify(mockStatement).close();
+ }
+
+ @Test
+ public void closeQuietlyConnectionThrowingExceptionResultSetStatement() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ doThrow(SQLException.class).when(mockConnection).close();
+ ResultSet mockResultSet = mock(ResultSet.class);
+ Statement mockStatement = mock(Statement.class);
+ DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
+ verify(mockConnection).close();
+ verify(mockResultSet).close();
+ verify(mockStatement).close();
+ }
+
+ @Test
+ public void closeQuietlyConnectionResultSetThrowingExceptionStatement() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ ResultSet mockResultSet = mock(ResultSet.class);
+ doThrow(SQLException.class).when(mockResultSet).close();
+ Statement mockStatement = mock(Statement.class);
+ DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
+ verify(mockConnection).close();
+ verify(mockResultSet).close();
+ verify(mockStatement).close();
+ }
+
+ @Test
+ public void closeQuietlyConnectionResultSetStatementThrowingException() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ ResultSet mockResultSet = mock(ResultSet.class);
+ Statement mockStatement = mock(Statement.class);
+ doThrow(SQLException.class).when(mockStatement).close();
+ DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
+ verify(mockConnection).close();
+ verify(mockResultSet).close();
+ verify(mockStatement).close();
+ }
+
+ @Test
+ public void commitAndClose() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ DbUtils.commitAndClose(mockConnection);
+ verify(mockConnection).commit();
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void commitAndCloseWithException() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ doThrow(SQLException.class).when(mockConnection).commit();
+ try {
+ DbUtils.commitAndClose(mockConnection);
+ fail("DbUtils.commitAndClose() swallowed SQLEception!");
+ } catch (SQLException e) {
+ // we expect this exception
+ }
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void commitAndCloseQuietly() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ DbUtils.commitAndClose(mockConnection);
+ verify(mockConnection).commit();
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void commitAndCloseQuietlyWithException() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ doThrow(SQLException.class).when(mockConnection).close();
+ DbUtils.commitAndCloseQuietly(mockConnection);
+ verify(mockConnection).commit();
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void rollbackNull() throws Exception {
+ DbUtils.rollback(null);
+ }
+
+ @Test
+ public void rollback() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ DbUtils.rollback(mockConnection);
+ verify(mockConnection).rollback();
+ }
+
+ @Test
+ public void rollbackAndCloseNull() throws Exception {
+ DbUtils.rollbackAndClose(null);
+ }
+
+ @Test
+ public void rollbackAndClose() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ DbUtils.rollbackAndClose(mockConnection);
+ verify(mockConnection).rollback();
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void rollbackAndCloseWithException() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ doThrow(SQLException.class).when(mockConnection).rollback();
+ try {
+ DbUtils.rollbackAndClose(mockConnection);
+ fail("DbUtils.rollbackAndClose() swallowed SQLException!");
+ } catch (SQLException e) {
+ // we expect this exeption
+ }
+ verify(mockConnection).rollback();
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void rollbackAndCloseQuietlyNull() throws Exception {
+ DbUtils.rollbackAndCloseQuietly(null);
+ }
+
+ @Test
+ public void rollbackAndCloseQuietly() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ DbUtils.rollbackAndCloseQuietly(mockConnection);
+ verify(mockConnection).rollback();
+ verify(mockConnection).close();
+ }
+
+ @Test
+ public void rollbackAndCloseQuietlyWithException() throws Exception {
+ Connection mockConnection = mock(Connection.class);
+ doThrow(SQLException.class).when(mockConnection).rollback();
+ DbUtils.rollbackAndCloseQuietly(mockConnection);
+ verify(mockConnection).rollback();
+ verify(mockConnection).close();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java b/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java
new file mode 100644
index 0000000..11d0394
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.when;
+import java.beans.PropertyDescriptor;
+import java.sql.ResultSetMetaData;
+
+import org.apache.commons.dbutils2.GenerousBeanProcessor;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class GenerousBeanProcessorTest {
+
+ GenerousBeanProcessor processor = new GenerousBeanProcessor();
+ @Mock ResultSetMetaData metaData;
+ PropertyDescriptor[] propDescriptors;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ propDescriptors = new PropertyDescriptor[3];
+
+ propDescriptors[0] = new PropertyDescriptor("one", TestBean.class);
+ propDescriptors[1] = new PropertyDescriptor("two", TestBean.class);
+ propDescriptors[2] = new PropertyDescriptor("three", TestBean.class);
+ }
+
+ @Test
+ public void testMapColumnsToPropertiesWithOutUnderscores() throws Exception {
+ when(metaData.getColumnCount()).thenReturn(3);
+
+ when(metaData.getColumnLabel(1)).thenReturn("three");
+ when(metaData.getColumnLabel(2)).thenReturn("one");
+ when(metaData.getColumnLabel(3)).thenReturn("two");
+
+ int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
+
+ assertNotNull(ret);
+ assertEquals(4, ret.length);
+ assertEquals(-1, ret[0]);
+ assertEquals(2, ret[1]);
+ assertEquals(0, ret[2]);
+ assertEquals(1, ret[3]);
+ }
+
+ @Test
+ public void testMapColumnsToPropertiesWithUnderscores() throws Exception {
+ when(metaData.getColumnCount()).thenReturn(3);
+
+ when(metaData.getColumnLabel(1)).thenReturn("t_h_r_e_e");
+ when(metaData.getColumnLabel(2)).thenReturn("o_n_e");
+ when(metaData.getColumnLabel(3)).thenReturn("t_w_o");
+
+ int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors);
+
+ assertNotNull(ret);
+ assertEquals(4, ret.length);
+ assertEquals(-1, ret[0]);
+ assertEquals(2, ret[1]);
+ assertEquals(0, ret[2]);
+ assertEquals(1, ret[3]);
+ }
+
+ static class TestBean {
+ private String one;
+ private int two;
+ private long three;
+
+ public String getOne() {
+ return one;
+ }
+
+ public void setOne(String one) {
+ this.one = one;
+ }
+
+ public int getTwo() {
+ return two;
+ }
+
+ public void setTwo(int two) {
+ this.two = two;
+ }
+
+ public long getThree() {
+ return three;
+ }
+
+ public void setThree(long three) {
+ this.three = three;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/InsertExecutorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/InsertExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/InsertExecutorTest.java
new file mode 100644
index 0000000..ee7bf11
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/InsertExecutorTest.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.InsertExecutor;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class InsertExecutorTest {
+
+ private InsertExecutor executor;
+
+ @Mock private ResultSetHandler<Object> handler;
+ @Mock private Connection conn;
+ @Mock private PreparedStatement stmt;
+ @Mock private ResultSet resultSet;
+
+ @Before
+ public void setup() throws SQLException {
+ MockitoAnnotations.initMocks(this);
+
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.getGeneratedKeys()).thenReturn(resultSet);
+ when(handler.handle(any(ResultSet.class))).thenReturn(new Object());
+ }
+
+ protected void createExecutor(String sql) throws Exception {
+ executor = new InsertExecutor(conn, sql, true);
+ }
+
+ @Test
+ public void testGoodSQL() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.execute(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUnmappedParams() throws Exception {
+ createExecutor("insert into blah (:something)");
+
+ Object ret = executor.execute(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullHandler() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.execute(null);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/MockResultSet.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/MockResultSet.java b/src/test/java/org/apache/commons/dbutils2/MockResultSet.java
new file mode 100644
index 0000000..6944dd1
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/MockResultSet.java
@@ -0,0 +1,365 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.dbutils2.ProxyFactory;
+
+/**
+ * MockResultSet dynamically implements the ResultSet interface.
+ */
+public class MockResultSet implements InvocationHandler {
+
+ /**
+ * Create a <code>MockResultSet</code> proxy object. This is equivalent to:
+ * <pre>
+ * ProxyFactory.instance().createResultSet(new MockResultSet(metaData, rows));
+ * </pre>
+ *
+ * @param metaData
+ * @param rows A null value indicates an empty <code>ResultSet</code>.
+ */
+ public static ResultSet create(ResultSetMetaData metaData,
+ Object[][] rows) {
+ return ProxyFactory.instance().createResultSet(
+ new MockResultSet(metaData, rows));
+ }
+
+ private Object[] currentRow = null;
+
+ private Iterator<Object[]> iter = null;
+
+ private ResultSetMetaData metaData = null;
+
+ private Boolean wasNull = Boolean.FALSE;
+
+ /**
+ * MockResultSet constructor.
+ * @param metaData
+ * @param rows A null value indicates an empty <code>ResultSet</code>.
+ */
+ public MockResultSet(ResultSetMetaData metaData, Object[][] rows) {
+ super();
+ this.metaData = metaData;
+ if (rows == null) {
+ List<Object[]> empty = Collections.emptyList();
+ this.iter = empty.iterator();
+ } else {
+ this.iter = Arrays.asList(rows).iterator();
+ }
+ }
+
+ /**
+ * The get* methods can have an int column index or a String column name as
+ * the parameter. This method handles both cases and returns the column
+ * index that the client is trying to get at.
+ * @param args
+ * @return A column index.
+ * @throws SQLException if a database access error occurs
+ */
+ private int columnIndex(Object[] args) throws SQLException {
+
+ if (args[0] instanceof Integer) {
+ return ((Integer) args[0]).intValue();
+
+ } else if (args[0] instanceof String) {
+ return this.columnNameToIndex((String) args[0]);
+
+ } else {
+ throw new SQLException(args[0] + " must be Integer or String");
+ }
+ }
+
+ /**
+ * Returns the column index for the given column name.
+ * @return A 1 based index
+ * @throws SQLException if the column name is invalid
+ */
+ private int columnNameToIndex(String columnName) throws SQLException {
+ for (int i = 0; i < this.currentRow.length; i++) {
+ int c = i + 1;
+ if (this.metaData.getColumnName(c).equalsIgnoreCase(columnName)) {
+ return c;
+ }
+ }
+
+ throw new SQLException(columnName + " is not a valid column name.");
+ }
+
+ /**
+ * Gets the boolean value at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getBoolean(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+
+ try {
+ return (obj == null)
+ ? Boolean.FALSE
+ : Boolean.valueOf(obj.toString());
+
+ } catch (NumberFormatException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+
+ /**
+ * Gets the byte value at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getByte(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+
+ try {
+ return (obj == null)
+ ? Byte.valueOf((byte) 0)
+ : Byte.valueOf(obj.toString());
+
+ } catch (NumberFormatException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+
+ /**
+ * Gets the double value at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getDouble(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+
+ try {
+ return (obj == null)
+ ? new Double(0)
+ : Double.valueOf(obj.toString());
+
+ } catch (NumberFormatException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+
+ /**
+ * Gets the float value at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getFloat(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+
+ try {
+ return (obj == null) ? new Float(0) : Float.valueOf(obj.toString());
+
+ } catch (NumberFormatException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+
+ /**
+ * Gets the int value at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getInt(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+
+ try {
+ return (obj == null)
+ ? Integer.valueOf(0)
+ : Integer.valueOf(obj.toString());
+
+ } catch (NumberFormatException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+
+ /**
+ * Gets the long value at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getLong(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+
+ try {
+ return (obj == null) ? Long.valueOf(0) : Long.valueOf(obj.toString());
+
+ } catch (NumberFormatException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+
+ /**
+ * @throws SQLException
+ */
+ protected ResultSetMetaData getMetaData() throws SQLException {
+ return this.metaData;
+ }
+
+ /**
+ * Gets the object at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getObject(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+ return obj;
+ }
+
+ /**
+ * Gets the short value at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected Object getShort(int columnIndex) throws SQLException {
+ Object obj = this.currentRow[columnIndex - 1];
+ this.setWasNull(obj);
+
+ try {
+ return (obj == null)
+ ? Short.valueOf((short) 0)
+ : Short.valueOf(obj.toString());
+
+ } catch (NumberFormatException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+
+ /**
+ * Gets the String at the given column index.
+ * @param columnIndex A 1 based index.
+ * @throws SQLException if a database access error occurs
+ */
+ protected String getString(int columnIndex) throws SQLException {
+ Object obj = this.getObject(columnIndex);
+ this.setWasNull(obj);
+ return (obj == null) ? null : obj.toString();
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+
+ String methodName = method.getName();
+
+ if (methodName.equals("getMetaData")) {
+ return this.getMetaData();
+
+ } else if (methodName.equals("next")) {
+ return this.next();
+
+ } else if (methodName.equals("previous")) {
+
+ } else if (methodName.equals("close")) {
+
+ } else if (methodName.equals("getBoolean")) {
+ return this.getBoolean(columnIndex(args));
+
+ } else if (methodName.equals("getByte")) {
+ return this.getByte(columnIndex(args));
+
+ } else if (methodName.equals("getDouble")) {
+ return this.getDouble(columnIndex(args));
+
+ } else if (methodName.equals("getFloat")) {
+ return this.getFloat(columnIndex(args));
+
+ } else if (methodName.equals("getInt")) {
+ return this.getInt(columnIndex(args));
+
+ } else if (methodName.equals("getLong")) {
+ return this.getLong(columnIndex(args));
+
+ } else if (methodName.equals("getObject")) {
+ return this.getObject(columnIndex(args));
+
+ } else if (methodName.equals("getShort")) {
+ return this.getShort(columnIndex(args));
+
+ } else if (methodName.equals("getString")) {
+ return this.getString(columnIndex(args));
+
+ } else if (methodName.equals("wasNull")) {
+ return this.wasNull();
+
+ } else if (methodName.equals("isLast")) {
+ return this.isLast();
+
+ } else if (methodName.equals("hashCode")) {
+ return Integer.valueOf(System.identityHashCode(proxy));
+
+ } else if (methodName.equals("toString")) {
+ return "MockResultSet " + System.identityHashCode(proxy);
+
+ } else if (methodName.equals("equals")) {
+ return Boolean.valueOf(proxy == args[0]);
+ }
+
+ throw new UnsupportedOperationException("Unsupported method: " + methodName);
+ }
+
+ /**
+ * @throws SQLException
+ */
+ protected Boolean isLast() throws SQLException {
+ return this.iter.hasNext() ? Boolean.FALSE : Boolean.TRUE;
+ }
+
+ /**
+ * @throws SQLException
+ */
+ protected Boolean next() throws SQLException {
+ if (!this.iter.hasNext()) {
+ return Boolean.FALSE;
+ } else {
+ this.currentRow = iter.next();
+ return Boolean.TRUE;
+ }
+ }
+
+ /**
+ * Assigns this.wasNull a Boolean value based on the object passed in.
+ * @param isNull
+ */
+ private void setWasNull(Object isNull) {
+ this.wasNull = (isNull == null) ? Boolean.TRUE : Boolean.FALSE;
+ }
+
+ /**
+ * @throws SQLException
+ */
+ protected Boolean wasNull() throws SQLException {
+ return this.wasNull;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/MockResultSetMetaData.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/MockResultSetMetaData.java b/src/test/java/org/apache/commons/dbutils2/MockResultSetMetaData.java
new file mode 100644
index 0000000..d67a19e
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/MockResultSetMetaData.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.sql.ResultSetMetaData;
+
+import org.apache.commons.dbutils2.ProxyFactory;
+
+/**
+ * MockResultSetMetaData dynamically implements the ResultSetMetaData
+ * interface.
+ */
+public class MockResultSetMetaData implements InvocationHandler {
+
+ private String[] columnNames = null;
+ private String[] columnLabels = null;
+
+ /**
+ * Create a <code>MockResultSetMetaData</code> proxy object. This is
+ * equivalent to:
+ * <pre>
+ * ProxyFactory.instance().createResultSetMetaData(new MockResultSetMetaData(columnNames));
+ * </pre>
+ *
+ * @param columnNames
+ * @return the proxy object
+ */
+ public static ResultSetMetaData create(String[] columnNames) {
+ return ProxyFactory.instance().createResultSetMetaData(
+ new MockResultSetMetaData(columnNames));
+ }
+
+ public MockResultSetMetaData(String[] columnNames) {
+ super();
+ this.columnNames = columnNames;
+ this.columnLabels = new String[columnNames.length];
+
+ }
+
+ public MockResultSetMetaData(String[] columnNames, String[] columnLabels) {
+ super();
+ this.columnNames = columnNames;
+ this.columnLabels = columnLabels;
+
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+
+ String methodName = method.getName();
+
+ if (methodName.equals("getColumnCount")) {
+ return Integer.valueOf(this.columnNames.length);
+
+ } else if (
+ methodName.equals("getColumnName")) {
+
+ int col = ((Integer) args[0]).intValue() - 1;
+ return this.columnNames[col];
+
+ } else if (
+ methodName.equals("getColumnLabel")) {
+
+ int col = ((Integer) args[0]).intValue() - 1;
+ return this.columnLabels[col];
+
+ } else if (methodName.equals("hashCode")) {
+ return Integer.valueOf(System.identityHashCode(proxy));
+
+ } else if (methodName.equals("toString")) {
+ return "MockResultSetMetaData " + System.identityHashCode(proxy);
+
+ } else if (methodName.equals("equals")) {
+ return Boolean.valueOf(proxy == args[0]);
+
+ } else {
+ throw new UnsupportedOperationException("Unsupported method: " + methodName);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/ProxyFactoryTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/ProxyFactoryTest.java b/src/test/java/org/apache/commons/dbutils2/ProxyFactoryTest.java
new file mode 100644
index 0000000..f4125f2
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/ProxyFactoryTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+import org.apache.commons.dbutils2.ProxyFactory;
+
+/**
+ * ProxyFactoryTest performs simple type checking on proxy objects returned
+ * from a ProxyFactory.
+ */
+public class ProxyFactoryTest extends BaseTestCase {
+
+ private static final InvocationHandler stub = new InvocationHandler() {
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+
+ return null;
+ }
+ };
+
+ public void testCreateConnection() {
+ assertNotNull(ProxyFactory.instance().createConnection(stub));
+ }
+
+ public void testCreateDriver() {
+ assertNotNull(ProxyFactory.instance().createDriver(stub));
+ }
+
+ public void testCreatePreparedStatement() {
+ assertNotNull(ProxyFactory.instance().createPreparedStatement(stub));
+ }
+
+ public void testCreateResultSet() {
+ assertNotNull(ProxyFactory.instance().createResultSet(stub));
+ }
+
+ public void testCreateResultSetMetaData() {
+ assertNotNull(ProxyFactory.instance().createResultSetMetaData(stub));
+ }
+
+ public void testCreateStatement() {
+ assertNotNull(ProxyFactory.instance().createStatement(stub));
+ }
+
+ public void testCreateCallableStatement() {
+ assertNotNull(ProxyFactory.instance().createCallableStatement(stub));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/QueryExecutorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/QueryExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/QueryExecutorTest.java
new file mode 100644
index 0000000..9b7b1e8
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/QueryExecutorTest.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.QueryExecutor;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class QueryExecutorTest {
+
+ private QueryExecutor executor;
+
+ @Mock private ResultSetHandler<Object> handler;
+ @Mock private Connection conn;
+ @Mock private PreparedStatement stmt;
+ @Mock private ResultSet resultSet;
+
+ @Before
+ public void setup() throws SQLException {
+ MockitoAnnotations.initMocks(this);
+
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.executeQuery()).thenReturn(resultSet);
+ when(handler.handle(any(ResultSet.class))).thenReturn(new Object());
+ }
+
+ protected void createExecutor(String sql) throws Exception {
+ executor = new QueryExecutor(conn, sql, true);
+ }
+
+ @Test
+ public void testGoodSQL() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.execute(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUnmappedParams() throws Exception {
+ createExecutor("insert into blah (:something)");
+
+ Object ret = executor.execute(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullHandler() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.execute(null);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/QueryLoaderTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/QueryLoaderTest.java b/src/test/java/org/apache/commons/dbutils2/QueryLoaderTest.java
new file mode 100644
index 0000000..ff09a2c
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/QueryLoaderTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.QueryLoader;
+
+/**
+ * QueryLoaderTest
+ */
+public class QueryLoaderTest extends BaseTestCase {
+
+ private static final String QUERIES =
+ "/org/apache/commons/dbutils/TestQueries.properties";
+
+ public void testLoad() throws IOException {
+ try {
+ QueryLoader loader = QueryLoader.instance();
+ Map<String,String> q = loader.load(QUERIES);
+ Map<String,String> q2 = loader.load(QUERIES);
+ assertTrue(q == q2); // pointer comparison should return true
+ assertEquals("SELECT * FROM SomeTable", q.get("test.query"));
+
+ loader.unload(QUERIES);
+ Map<String,String> q3 = loader.load(QUERIES);
+ assertTrue(q != q3); // pointer comparison should return false
+
+ } catch (IllegalArgumentException e) {
+ // TODO Figure out why the Maven build can't find the properties
+ // file. The tests run fine in Eclipse so just catch this
+ // exception for now.
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/QueryRunnerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/QueryRunnerTest.java b/src/test/java/org/apache/commons/dbutils2/QueryRunnerTest.java
new file mode 100644
index 0000000..9944972
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/QueryRunnerTest.java
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.SQLException;
+import javax.sql.DataSource;
+
+import org.apache.commons.dbutils2.QueryRunner;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@SuppressWarnings("boxing") // test code
+public class QueryRunnerTest {
+ QueryRunner runner;
+
+ @Mock DataSource dataSource;
+ @Mock Connection conn;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this); // init the mocks
+
+ when(dataSource.getConnection()).thenReturn(conn);
+ runner = new QueryRunner(dataSource);
+ }
+
+ // batch tests
+
+ @Test
+ public void testBatchSQL() throws SQLException {
+ assertNotNull(runner.batch("select * from blah where :first=first"));
+ verify(dataSource, times(1)).getConnection();
+ }
+
+ @Test
+ public void testBatchConnSQL() throws SQLException {
+ assertNotNull(runner.batch(conn, "select * from blah where :first=first"));
+ }
+
+ @Test
+ public void testBatchConnSQLBoolean() throws SQLException {
+ assertNotNull(runner.batch(conn, true, "select * from blah where :first=first"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testBatchNullConn() throws SQLException {
+ assertNotNull(runner.batch(null, true, "select"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testBatchNullSQL() throws SQLException {
+ assertNotNull(runner.batch(conn, true, null));
+ }
+
+ // query tests
+
+ @Test
+ public void testQuerySQL() throws SQLException {
+ assertNotNull(runner.query("select * from blah where :first=first"));
+ verify(dataSource, times(1)).getConnection();
+ }
+
+ @Test
+ public void testQueryConnSQL() throws SQLException {
+ assertNotNull(runner.query(conn, "select * from blah where :first=first"));
+ }
+
+ @Test
+ public void testQueryConnSQLBoolean() throws SQLException {
+ assertNotNull(runner.query(conn, true, "select * from blah where :first=first"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testQueryNullConn() throws SQLException {
+ assertNotNull(runner.query(null, true, "select"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testQueryNullSQL() throws SQLException {
+ assertNotNull(runner.query(conn, true, null));
+ }
+
+ // insert tests
+
+ @Test
+ public void testInsertSQL() throws SQLException {
+ assertNotNull(runner.insert("insert * from blah where :first=first"));
+ verify(dataSource, times(1)).getConnection();
+ }
+
+ @Test
+ public void testInsertConnSQL() throws SQLException {
+ assertNotNull(runner.insert(conn, "insert * from blah where :first=first"));
+ }
+
+ @Test
+ public void testInsertConnSQLBoolean() throws SQLException {
+ assertNotNull(runner.insert(conn, true, "insert * from blah where :first=first"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testInsertNullConn() throws SQLException {
+ assertNotNull(runner.insert(null, true, "select"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testInsertNullSQL() throws SQLException {
+ assertNotNull(runner.insert(conn, true, null));
+ }
+
+ // update tests
+
+ @Test
+ public void testUpdateSQL() throws SQLException {
+ assertNotNull(runner.update("select * from blah where :first=first"));
+ verify(dataSource, times(1)).getConnection();
+ }
+
+ @Test
+ public void testUpdateConnSQL() throws SQLException {
+ assertNotNull(runner.update(conn, "select * from blah where :first=first"));
+ }
+
+ @Test
+ public void testUpdateConnSQLBoolean() throws SQLException {
+ assertNotNull(runner.update(conn, true, "select * from blah where :first=first"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUpdateNullConn() throws SQLException {
+ assertNotNull(runner.update(null, true, "select"));
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUpdateNullSQL() throws SQLException {
+ assertNotNull(runner.update(conn, true, null));
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/ResultSetIteratorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/ResultSetIteratorTest.java b/src/test/java/org/apache/commons/dbutils2/ResultSetIteratorTest.java
new file mode 100644
index 0000000..89070a9
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/ResultSetIteratorTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import java.util.Iterator;
+
+import org.apache.commons.dbutils2.ResultSetIterator;
+
+/**
+ * ResultSetIteratorTest
+ */
+public class ResultSetIteratorTest extends BaseTestCase {
+
+ public void testNext() {
+
+ Iterator<Object[]> iter = new ResultSetIterator(this.rs);
+
+ Object[] row = null;
+ assertTrue(iter.hasNext());
+ row = iter.next();
+ assertEquals(COLS, row.length);
+ assertEquals("1", row[0]);
+ assertEquals("2", row[1]);
+ assertEquals("3", row[2]);
+
+ assertTrue(iter.hasNext());
+ row = iter.next();
+ assertEquals(COLS, row.length);
+
+ assertEquals("4", row[0]);
+ assertEquals("5", row[1]);
+ assertEquals("6", row[2]);
+
+ assertFalse(iter.hasNext());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/TestBean.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/TestBean.java b/src/test/java/org/apache/commons/dbutils2/TestBean.java
new file mode 100644
index 0000000..fc3f8e6
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/TestBean.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+/**
+ * A bean to use in testing toBean() and toBeanList().
+ */
+public class TestBean {
+
+ private String one = null;
+
+ private String two = null;
+
+ private String three = null;
+
+ private int intTest = 0;
+
+ private Integer integerTest = Integer.valueOf(0);
+
+ private String doNotSet = "not set";
+
+ /**
+ * toBean() should set primitive fields to their defaults (ie. 0) when
+ * null is returned from the ResultSet.
+ */
+ private int nullPrimitiveTest = 7;
+
+ /**
+ * toBean() should set Object fields to null when null is returned from the
+ * ResultSet
+ */
+ private Object nullObjectTest = "overwrite";
+
+ /**
+ * A Date will be returned from the ResultSet but the property is a String.
+ * BeanProcessor should create a String from the Date and set this property.
+ */
+ private String notDate = "not a date";
+
+ /**
+ * The ResultSet will have a BigDecimal in this column and the
+ * BasicColumnProcessor should convert that to a double and store the value
+ * in this property.
+ */
+ private double columnProcessorDoubleTest = -1;
+
+ /**
+ * 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;
+ }
+
+ public Integer getIntegerTest() {
+ return integerTest;
+ }
+
+ public int getIntTest() {
+ return intTest;
+ }
+
+ public void setIntegerTest(Integer integer) {
+ integerTest = integer;
+ }
+
+ public void setIntTest(int i) {
+ intTest = i;
+ }
+
+ public Object getNullObjectTest() {
+ return nullObjectTest;
+ }
+
+ public int getNullPrimitiveTest() {
+ return nullPrimitiveTest;
+ }
+
+ public void setNullObjectTest(Object object) {
+ nullObjectTest = object;
+ }
+
+ public void setNullPrimitiveTest(int i) {
+ nullPrimitiveTest = i;
+ }
+
+ public String getNotDate() {
+ return notDate;
+ }
+
+ public void setNotDate(String string) {
+ notDate = string;
+ }
+
+ public double getColumnProcessorDoubleTest() {
+ return columnProcessorDoubleTest;
+ }
+
+ public void setColumnProcessorDoubleTest(double d) {
+ columnProcessorDoubleTest = d;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/UpdateExecutorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/UpdateExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/UpdateExecutorTest.java
new file mode 100644
index 0000000..462fa4d
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/UpdateExecutorTest.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2;
+
+import static org.junit.Assert.*;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.UpdateExecutor;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class UpdateExecutorTest {
+
+ private UpdateExecutor executor;
+
+ @Mock private Connection conn;
+ @Mock private PreparedStatement stmt;
+
+ @Before
+ public void setup() throws SQLException {
+ MockitoAnnotations.initMocks(this);
+
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.executeUpdate()).thenReturn(20);
+ }
+
+ protected void createExecutor(String sql) throws Exception {
+ executor = new UpdateExecutor(conn, sql, true);
+ }
+
+ @Test
+ public void testGoodSQL() throws Exception {
+ createExecutor("insert into blah");
+
+ int ret = executor.execute();
+
+ assertEquals(20, ret);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUnmappedParams() throws Exception {
+ createExecutor("insert into blah (:something)");
+
+ int ret = executor.execute();
+
+ assertEquals(20, ret);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/ArrayHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/ArrayHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/ArrayHandlerTest.java
new file mode 100644
index 0000000..b893b89
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/ArrayHandlerTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.handlers.ArrayHandler;
+
+/**
+ * ArrayHandlerTest
+ */
+public class ArrayHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<Object[]> h = new ArrayHandler();
+ Object[] results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(COLS, results.length);
+ assertEquals("1", results[0]);
+ assertEquals("2", results[1]);
+ assertEquals("3", results[2]);
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<Object[]> h = new ArrayHandler();
+ Object[] results = h.handle(this.emptyResultSet);
+
+ assertNull(results);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/ArrayListHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/ArrayListHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/ArrayListHandlerTest.java
new file mode 100644
index 0000000..e252acd
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/ArrayListHandlerTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.handlers.ArrayListHandler;
+
+/**
+ * ArrayListHandlerTest
+ */
+public class ArrayListHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<List<Object[]>> h = new ArrayListHandler();
+ List<Object[]> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ Iterator<Object[]> iter = results.iterator();
+ Object[] row = null;
+ assertTrue(iter.hasNext());
+ row = iter.next();
+ assertEquals(COLS, row.length);
+ assertEquals("1", row[0]);
+ assertEquals("2", row[1]);
+ assertEquals("3", row[2]);
+
+ assertTrue(iter.hasNext());
+ row = iter.next();
+ assertEquals(COLS, row.length);
+
+ assertEquals("4", row[0]);
+ assertEquals("5", row[1]);
+ assertEquals("6", row[2]);
+
+ assertFalse(iter.hasNext());
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<List<Object[]>> h = new ArrayListHandler();
+ List<Object[]> results = h.handle(this.emptyResultSet);
+
+ assertNotNull(results);
+ assertTrue(results.isEmpty());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/BeanHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/BeanHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/BeanHandlerTest.java
new file mode 100644
index 0000000..feaab77
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/BeanHandlerTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.TestBean;
+import org.apache.commons.dbutils2.handlers.BeanHandler;
+
+/**
+ * BeanHandlerTest
+ */
+public class BeanHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<TestBean> h = new BeanHandler<TestBean>(TestBean.class);
+ TestBean results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals("1", results.getOne());
+ assertEquals("2", results.getTwo());
+ assertEquals("3", results.getThree());
+ assertEquals("not set", results.getDoNotSet());
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<TestBean> h = new BeanHandler<TestBean>(TestBean.class);
+ TestBean results = h.handle(this.emptyResultSet);
+
+ assertNull(results);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/BeanListHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/BeanListHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/BeanListHandlerTest.java
new file mode 100644
index 0000000..1c12b1d
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/BeanListHandlerTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.TestBean;
+import org.apache.commons.dbutils2.handlers.BeanListHandler;
+
+/**
+ * BeanListHandlerTest
+ */
+public class BeanListHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<List<TestBean>> h = new BeanListHandler<TestBean>(TestBean.class);
+ List<TestBean> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ Iterator<TestBean> iter = results.iterator();
+ TestBean row = null;
+ assertTrue(iter.hasNext());
+ row = iter.next();
+ assertEquals("1", row.getOne());
+ assertEquals("2", row.getTwo());
+ assertEquals("3", row.getThree());
+ assertEquals("not set", row.getDoNotSet());
+
+ assertTrue(iter.hasNext());
+ row = iter.next();
+
+ assertEquals("4", row.getOne());
+ assertEquals("5", row.getTwo());
+ assertEquals("6", row.getThree());
+ assertEquals("not set", row.getDoNotSet());
+
+ assertFalse(iter.hasNext());
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<List<TestBean>> h = new BeanListHandler<TestBean>(TestBean.class);
+ List<TestBean> results = h.handle(this.emptyResultSet);
+
+ assertNotNull(results);
+ assertTrue(results.isEmpty());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/BeanMapHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/BeanMapHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/BeanMapHandlerTest.java
new file mode 100644
index 0000000..c14951c
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/BeanMapHandlerTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.when;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.RowProcessor;
+import org.apache.commons.dbutils2.TestBean;
+import org.apache.commons.dbutils2.handlers.BeanMapHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class BeanMapHandlerTest {
+
+ private BeanMapHandler<Long, TestBean> bmh;
+ private Map<Long, TestBean> res;
+ @Mock private ResultSet rs;
+ @Mock private ResultSetMetaData rsmd;
+ @Mock private RowProcessor rp;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ when(Boolean.valueOf(rs.next())).thenReturn(Boolean.TRUE, Boolean.FALSE);
+ when(rs.getObject(1)).thenReturn(Long.valueOf(23L));
+ when(rs.getObject(2)).thenReturn(Long.valueOf(23L));
+ when(rs.getObject("id")).thenReturn(Long.valueOf(23L));
+ when(rs.getMetaData()).thenReturn(rsmd);
+ when(rp.toBean(rs, TestBean.class)).thenReturn(new TestBean());
+ }
+
+ private void handle() throws Exception {
+ res = bmh.handle(rs);
+ assertNotNull(res.get(Long.valueOf(23L)));
+ }
+
+ @Test
+ public void testBeanMapHandlerClassOfV() throws Exception {
+ bmh = new BeanMapHandler<Long, TestBean>(TestBean.class);
+ handle();
+ }
+
+ @Test
+ public void testBeanMapHandlerClassOfVRowProcessor() throws Exception {
+ bmh = new BeanMapHandler<Long, TestBean>(TestBean.class, rp);
+ handle();
+ }
+
+ @Test
+ public void testBeanMapHandlerClassOfVInt() throws Exception {
+ bmh = new BeanMapHandler<Long, TestBean>(TestBean.class, 2);
+ handle();
+ }
+
+ @Test
+ public void testBeanMapHandlerClassOfVString() throws Exception {
+ bmh = new BeanMapHandler<Long, TestBean>(TestBean.class, "id");
+ handle();
+ }
+
+ @Test
+ public void testEmptyResultSet() throws Exception {
+ when(Boolean.valueOf(rs.next())).thenReturn(Boolean.FALSE);
+ bmh = new BeanMapHandler<Long, TestBean>(TestBean.class);
+ res = bmh.handle(rs);
+ assertNull(res.get(Long.valueOf(23L)));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/ColumnListHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/ColumnListHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/ColumnListHandlerTest.java
new file mode 100644
index 0000000..4686e53
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/ColumnListHandlerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.handlers.ColumnListHandler;
+
+/**
+ * ColumnListHandlerTest
+ */
+public class ColumnListHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<List<String>> h = new ColumnListHandler<String>();
+ List<String> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ assertEquals("1", results.get(0));
+ assertEquals("4", results.get(1));
+ }
+
+ public void testColumnIndexHandle() throws SQLException {
+ ResultSetHandler<List<String>> h = new ColumnListHandler<String>(2);
+ List<String> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ assertEquals("2", results.get(0));
+ assertEquals("5", results.get(1));
+ }
+
+ public void testColumnNameHandle() throws SQLException {
+ ResultSetHandler<List<Integer>> h = new ColumnListHandler<Integer>("intTest");
+ List<Integer> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ assertEquals(new Integer(1), results.get(0));
+ assertEquals(new Integer(3), results.get(1));
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<List<String>> h = new ColumnListHandler<String>();
+ List<String> results = h.handle(this.emptyResultSet);
+
+ assertNotNull(results);
+ assertTrue(results.isEmpty());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/KeyedHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/KeyedHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/KeyedHandlerTest.java
new file mode 100644
index 0000000..f0cedc0
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/KeyedHandlerTest.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.handlers.KeyedHandler;
+
+public class KeyedHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<String>();
+
+ Map<String,Map<String,Object>> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ Map<String,Object> row = null;
+ for(Entry<String, Map<String, Object>> entry : results.entrySet())
+ {
+ Object key = entry.getKey();
+ assertNotNull(key);
+ row = entry.getValue();
+ assertNotNull(row);
+ assertEquals(COLS, row.keySet().size());
+ }
+ row = results.get("1");
+ assertEquals("1", row.get("one"));
+ assertEquals("2", row.get("TWO"));
+ assertEquals("3", row.get("Three"));
+ }
+
+ public void testColumnIndexHandle() throws SQLException {
+ ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<String>(2);
+ Map<String,Map<String,Object>> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ Map<String,Object> row = null;
+ for(Entry<String, Map<String, Object>> entry : results.entrySet())
+ {
+ Object key = entry.getKey();
+ assertNotNull(key);
+ row = entry.getValue();
+ assertNotNull(row);
+ assertEquals(COLS, row.keySet().size());
+ }
+ row = results.get("5");
+ assertEquals("4", row.get("one"));
+ assertEquals("5", row.get("TWO"));
+ assertEquals("6", row.get("Three"));
+ }
+
+ public void testColumnNameHandle() throws SQLException {
+ ResultSetHandler<Map<Integer,Map<String,Object>>> h = new KeyedHandler<Integer>("intTest");
+ Map<Integer,Map<String,Object>> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ Map<String,Object> row = null;
+ for(Entry<Integer, Map<String, Object>> entry : results.entrySet())
+ {
+ Object key = entry.getKey();
+ assertNotNull(key);
+ row = entry.getValue();
+ assertNotNull(row);
+ assertEquals(COLS, row.keySet().size());
+ }
+ row = results.get(Integer.valueOf(3));
+ assertEquals("4", row.get("one"));
+ assertEquals("5", row.get("TWO"));
+ assertEquals("6", row.get("Three"));
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<String>();
+ Map<String,Map<String,Object>> results = h.handle(this.emptyResultSet);
+ assertNotNull(results);
+ assertTrue(results.isEmpty());
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/MapHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/MapHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/MapHandlerTest.java
new file mode 100644
index 0000000..d8dbf33
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/MapHandlerTest.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.handlers.MapHandler;
+
+/**
+ * MapHandlerTest
+ */
+public class MapHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<Map<String,Object>> h = new MapHandler();
+ Map<String,Object> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(COLS, results.keySet().size());
+ assertEquals("1", results.get("ONE"));
+ assertEquals("2", results.get("two"));
+ assertEquals("3", results.get("Three"));
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<Map<String,Object>> h = new MapHandler();
+ Map<String,Object> results = h.handle(this.emptyResultSet);
+
+ assertNull(results);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/MapListHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/MapListHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/MapListHandlerTest.java
new file mode 100644
index 0000000..d14a7c3
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/MapListHandlerTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.handlers.MapListHandler;
+
+/**
+ * MapListHandlerTest
+ */
+public class MapListHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<List<Map<String,Object>>> h = new MapListHandler();
+ List<Map<String,Object>> results = h.handle(this.rs);
+
+ assertNotNull(results);
+ assertEquals(ROWS, results.size());
+
+ Iterator<Map<String,Object>> iter = results.iterator();
+ Map<String,Object> row = null;
+ assertTrue(iter.hasNext());
+ row = iter.next();
+ assertEquals(COLS, row.keySet().size());
+ assertEquals("1", row.get("one"));
+ assertEquals("2", row.get("TWO"));
+ assertEquals("3", row.get("Three"));
+
+ assertTrue(iter.hasNext());
+ row = iter.next();
+ assertEquals(COLS, row.keySet().size());
+
+ assertEquals("4", row.get("one"));
+ assertEquals("5", row.get("TWO"));
+ assertEquals("6", row.get("Three"));
+
+ assertFalse(iter.hasNext());
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<List<Map<String,Object>>> h = new MapListHandler();
+ List<Map<String,Object>> results = h.handle(this.emptyResultSet);
+
+ assertNotNull(results);
+ assertTrue(results.isEmpty());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/handlers/ScalarHandlerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/handlers/ScalarHandlerTest.java b/src/test/java/org/apache/commons/dbutils2/handlers/ScalarHandlerTest.java
new file mode 100644
index 0000000..f44592f
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/handlers/ScalarHandlerTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.BaseTestCase;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.handlers.ScalarHandler;
+
+public class ScalarHandlerTest extends BaseTestCase {
+
+ public void testHandle() throws SQLException {
+ ResultSetHandler<String> h = new ScalarHandler<String>();
+ Object results = h.handle(this.rs);
+ assertNotNull(results);
+ assertEquals("1", results);
+ }
+
+ public void testColumnIndexHandle() throws SQLException {
+ ResultSetHandler<String> h = new ScalarHandler<String>(2);
+ Object results = h.handle(this.rs);
+ assertNotNull(results);
+ assertEquals("2", results);
+ }
+
+ public void testColumnNameHandle() throws SQLException {
+ ResultSetHandler<Integer> h = new ScalarHandler<Integer>("intTest");
+ Object results = h.handle(this.rs);
+ assertNotNull(results);
+ assertEquals(Integer.valueOf(1), results);
+ }
+
+ public void testEmptyResultSetHandle() throws SQLException {
+ ResultSetHandler<String> h = new ScalarHandler<String>();
+ Object results = h.handle(this.emptyResultSet);
+ assertNull(results);
+ }
+
+}