You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2017/10/16 02:41:50 UTC
[1/5] commons-dbutils git commit: Remove trailing white spaces on all
lines.
Repository: commons-dbutils
Updated Branches:
refs/heads/master 062a9ed46 -> 084f286a0
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java b/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java
index a872521..79ccba2 100644
--- a/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java
+++ b/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java
@@ -1,1022 +1,1022 @@
-/*
- * 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.dbutils.wrappers;
-
-import java.io.ByteArrayInputStream;
-import java.io.CharArrayReader;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.io.Writer;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.math.BigDecimal;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.sql.Blob;
-import java.sql.Clob;
-import java.sql.Ref;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Time;
-import java.sql.Timestamp;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.Map;
-
-import org.apache.commons.dbutils.BaseTestCase;
-import org.apache.commons.dbutils.ProxyFactory;
-
-/**
- * Test cases for <code>SqlNullCheckedResultSet</code> class.
- */
-public class SqlNullCheckedResultSetTest extends BaseTestCase {
-
- private SqlNullCheckedResultSet rs2 = null;
-
- /**
- * Sets up instance variables required by this test case.
- */
- @Override
- public void setUp() throws Exception {
- super.setUp();
-
- rs2 =
- new SqlNullCheckedResultSet(
- ProxyFactory.instance().createResultSet(
- new SqlNullUncheckedMockResultSet()));
-
- rs = ProxyFactory.instance().createResultSet(rs2); // Override superclass field
- }
-
- /**
- * Tests the getAsciiStream implementation.
- */
- public void testGetAsciiStream() throws SQLException {
-
- assertNull(rs.getAsciiStream(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getAsciiStream("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- InputStream stream = new ByteArrayInputStream(new byte[0]);
- rs2.setNullAsciiStream(stream);
- assertNotNull(rs.getAsciiStream(1));
- assertEquals(stream, rs.getAsciiStream(1));
- assertNotNull(rs.getAsciiStream("column"));
- assertEquals(stream, rs.getAsciiStream("column"));
-
- }
-
- /**
- * Tests the getBigDecimal implementation.
- */
- public void testGetBigDecimal() throws SQLException {
-
- assertNull(rs.getBigDecimal(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getBigDecimal("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- BigDecimal bd = new BigDecimal(5.0);
- rs2.setNullBigDecimal(bd);
- assertNotNull(rs.getBigDecimal(1));
- assertEquals(bd, rs.getBigDecimal(1));
- assertNotNull(rs.getBigDecimal("column"));
- assertEquals(bd, rs.getBigDecimal("column"));
-
- }
-
- /**
- * Tests the getBinaryStream implementation.
- */
- public void testGetBinaryStream() throws SQLException {
-
- assertNull(rs.getBinaryStream(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getBinaryStream("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- InputStream stream = new ByteArrayInputStream(new byte[0]);
- rs2.setNullBinaryStream(stream);
- assertNotNull(rs.getBinaryStream(1));
- assertEquals(stream, rs.getBinaryStream(1));
- assertNotNull(rs.getBinaryStream("column"));
- assertEquals(stream, rs.getBinaryStream("column"));
-
- }
-
- /**
- * Tests the getBlob implementation.
- */
- public void testGetBlob() throws SQLException {
-
- assertNull(rs.getBlob(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getBlob("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- Blob blob = new SqlNullCheckedResultSetMockBlob();
- rs2.setNullBlob(blob);
- assertNotNull(rs.getBlob(1));
- assertEquals(blob, rs.getBlob(1));
- assertNotNull(rs.getBlob("column"));
- assertEquals(blob, rs.getBlob("column"));
-
- }
-
- /**
- * Tests the getBoolean implementation.
- */
- public void testGetBoolean() throws SQLException {
-
- assertEquals(false, rs.getBoolean(1));
- assertTrue(rs.wasNull());
- assertEquals(false, rs.getBoolean("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- rs2.setNullBoolean(true);
- assertEquals(true, rs.getBoolean(1));
- assertEquals(true, rs.getBoolean("column"));
-
- }
-
- /**
- * Tests the getByte implementation.
- */
- public void testGetByte() throws SQLException {
-
- assertEquals((byte) 0, rs.getByte(1));
- assertTrue(rs.wasNull());
- assertEquals((byte) 0, rs.getByte("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- byte b = (byte) 10;
- rs2.setNullByte(b);
- assertEquals(b, rs.getByte(1));
- assertEquals(b, rs.getByte("column"));
-
- }
-
- /**
- * Tests the getByte implementation.
- */
- public void testGetBytes() throws SQLException {
-
- assertNull(rs.getBytes(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getBytes("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- byte[] b = new byte[5];
- for (int i = 0; i < 5; i++) {
- b[0] = (byte) i;
- }
- rs2.setNullBytes(b);
- assertNotNull(rs.getBytes(1));
- assertArrayEquals(b, rs.getBytes(1));
- assertNotNull(rs.getBytes("column"));
- assertArrayEquals(b, rs.getBytes("column"));
-
- }
-
- private static void assertArrayEquals(byte[] expected, byte[] actual) {
- if (expected == actual) {
- return;
- }
- if (expected.length != actual.length) {
- failNotEquals(null, Arrays.toString(expected), Arrays.toString(actual));
- }
- for (int i = 0; i < expected.length; i++) {
- byte expectedItem = expected[i];
- byte actualItem = actual[i];
- assertEquals("Array not equal at index " + i, expectedItem, actualItem);
- }
- }
-
- /**
- * Tests the getCharacterStream implementation.
- */
- public void testGetCharacterStream() throws SQLException {
-
- assertNull(rs.getCharacterStream(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getCharacterStream("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- Reader reader = new CharArrayReader("this is a string".toCharArray());
- rs2.setNullCharacterStream(reader);
- assertNotNull(rs.getCharacterStream(1));
- assertEquals(reader, rs.getCharacterStream(1));
- assertNotNull(rs.getCharacterStream("column"));
- assertEquals(reader, rs.getCharacterStream("column"));
-
- }
-
- /**
- * Tests the getClob implementation.
- */
- public void testGetClob() throws SQLException {
-
- assertNull(rs.getClob(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getClob("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- Clob clob = new SqlNullCheckedResultSetMockClob();
- rs2.setNullClob(clob);
- assertNotNull(rs.getClob(1));
- assertEquals(clob, rs.getClob(1));
- assertNotNull(rs.getClob("column"));
- assertEquals(clob, rs.getClob("column"));
-
- }
-
- /**
- * Tests the getDate implementation.
- */
- public void testGetDate() throws SQLException {
-
- assertNull(rs.getDate(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getDate("column"));
- assertTrue(rs.wasNull());
- assertNull(rs.getDate(1, Calendar.getInstance()));
- assertTrue(rs.wasNull());
- assertNull(rs.getDate("column", Calendar.getInstance()));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
- rs2.setNullDate(date);
- assertNotNull(rs.getDate(1));
- assertEquals(date, rs.getDate(1));
- assertNotNull(rs.getDate("column"));
- assertEquals(date, rs.getDate("column"));
- assertNotNull(rs.getDate(1, Calendar.getInstance()));
- assertEquals(date, rs.getDate(1, Calendar.getInstance()));
- assertNotNull(rs.getDate("column", Calendar.getInstance()));
- assertEquals(date, rs.getDate("column", Calendar.getInstance()));
-
- }
-
- /**
- * Tests the getDouble implementation.
- */
- public void testGetDouble() throws SQLException {
-
- assertEquals(0.0, rs.getDouble(1), 0.0);
- assertTrue(rs.wasNull());
- assertEquals(0.0, rs.getDouble("column"), 0.0);
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- double d = 10.0;
- rs2.setNullDouble(d);
- assertEquals(d, rs.getDouble(1), 0.0);
- assertEquals(d, rs.getDouble("column"), 0.0);
-
- }
-
- /**
- * Tests the getFloat implementation.
- */
- public void testGetFloat() throws SQLException {
- assertEquals(0, rs.getFloat(1), 0.0);
- assertTrue(rs.wasNull());
- assertEquals(0, rs.getFloat("column"), 0.0);
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- float f = 10;
- rs2.setNullFloat(f);
- assertEquals(f, rs.getFloat(1), 0.0);
- assertEquals(f, rs.getFloat("column"), 0.0);
- }
-
- /**
- * Tests the getInt implementation.
- */
- public void testGetInt() throws SQLException {
- assertEquals(0, rs.getInt(1));
- assertTrue(rs.wasNull());
- assertEquals(0, rs.getInt("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- int i = 10;
- rs2.setNullInt(i);
- assertEquals(i, rs.getInt(1));
- assertEquals(i, rs.getInt("column"));
- }
-
- /**
- * Tests the getLong implementation.
- */
- public void testGetLong() throws SQLException {
- assertEquals(0, rs.getLong(1));
- assertTrue(rs.wasNull());
- assertEquals(0, rs.getLong("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- long l = 10;
- rs2.setNullLong(l);
- assertEquals(l, rs.getLong(1));
- assertEquals(l, rs.getLong("column"));
- }
-
- /**
- * Tests the getObject implementation.
- */
- public void testGetObject() throws SQLException {
-
- assertNull(rs.getObject(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getObject("column"));
- assertTrue(rs.wasNull());
- assertNull(rs.getObject(1, (Map<String, Class<?>>) null));
- assertTrue(rs.wasNull());
- assertNull(rs.getObject("column", (Map<String, Class<?>>) null));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- Object o = new Object();
- rs2.setNullObject(o);
- assertNotNull(rs.getObject(1));
- assertEquals(o, rs.getObject(1));
- assertNotNull(rs.getObject("column"));
- assertEquals(o, rs.getObject("column"));
- assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
- assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
- assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
- assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
-
- }
-
- /**
- * Tests the getRef implementation.
- */
- public void testGetRef() throws SQLException {
-
- assertNull(rs.getRef(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getRef("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- Ref ref = new SqlNullCheckedResultSetMockRef();
- rs2.setNullRef(ref);
- assertNotNull(rs.getRef(1));
- assertEquals(ref, rs.getRef(1));
- assertNotNull(rs.getRef("column"));
- assertEquals(ref, rs.getRef("column"));
-
- }
-
- /**
- * Tests the getShort implementation.
- */
- public void testGetShort() throws SQLException {
-
- assertEquals((short) 0, rs.getShort(1));
- assertTrue(rs.wasNull());
- assertEquals((short) 0, rs.getShort("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- short s = (short) 10;
- rs2.setNullShort(s);
- assertEquals(s, rs.getShort(1));
- assertEquals(s, rs.getShort("column"));
- }
-
- /**
- * Tests the getString implementation.
- */
- public void testGetString() throws SQLException {
- assertEquals(null, rs.getString(1));
- assertTrue(rs.wasNull());
- assertEquals(null, rs.getString("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- String s = "hello, world";
- rs2.setNullString(s);
- assertEquals(s, rs.getString(1));
- assertEquals(s, rs.getString("column"));
- }
-
- /**
- * Tests the getTime implementation.
- */
- public void testGetTime() throws SQLException {
-
- assertNull(rs.getTime(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getTime("column"));
- assertTrue(rs.wasNull());
- assertNull(rs.getTime(1, Calendar.getInstance()));
- assertTrue(rs.wasNull());
- assertNull(rs.getTime("column", Calendar.getInstance()));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- Time time = new Time(new java.util.Date().getTime());
- rs2.setNullTime(time);
- assertNotNull(rs.getTime(1));
- assertEquals(time, rs.getTime(1));
- assertNotNull(rs.getTime("column"));
- assertEquals(time, rs.getTime("column"));
- assertNotNull(rs.getTime(1, Calendar.getInstance()));
- assertEquals(time, rs.getTime(1, Calendar.getInstance()));
- assertNotNull(rs.getTime("column", Calendar.getInstance()));
- assertEquals(time, rs.getTime("column", Calendar.getInstance()));
-
- }
-
- /**
- * Tests the getTimestamp implementation.
- */
- public void testGetTimestamp() throws SQLException {
-
- assertNull(rs.getTimestamp(1));
- assertTrue(rs.wasNull());
- assertNull(rs.getTimestamp("column"));
- assertTrue(rs.wasNull());
- assertNull(rs.getTimestamp(1, Calendar.getInstance()));
- assertTrue(rs.wasNull());
- assertNull(rs.getTimestamp("column", Calendar.getInstance()));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- Timestamp ts = new Timestamp(new java.util.Date().getTime());
- rs2.setNullTimestamp(ts);
- assertNotNull(rs.getTimestamp(1));
- assertEquals(ts, rs.getTimestamp(1));
- assertNotNull(rs.getTimestamp("column"));
- assertEquals(ts, rs.getTimestamp("column"));
- assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
- assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
- assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
- assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
- }
-
- /**
- * Tests the getURL and setNullURL implementations.
- *
- * Uses reflection to allow for building under JDK 1.3.
- */
- public void testURL() throws SQLException, MalformedURLException,
- IllegalAccessException, IllegalArgumentException,
- java.lang.reflect.InvocationTargetException
- {
- Method getUrlInt = null;
- Method getUrlString = null;
- try {
- getUrlInt = ResultSet.class.getMethod("getURL",
- new Class[] { Integer.TYPE } );
- getUrlString = ResultSet.class.getMethod("getURL",
- new Class[] { String.class } );
- } catch(NoSuchMethodException e) {
- // ignore
- } catch(SecurityException e) {
- // ignore
- }
- if (getUrlInt != null && getUrlString != null) {
- assertEquals(null, getUrlInt.invoke(rs,
- new Object[] { Integer.valueOf(1) } ) );
- assertTrue(rs.wasNull());
- assertEquals(null, getUrlString.invoke(rs,
- new Object[] { "column" } ) );
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- URL u = new URL("http://www.apache.org");
- rs2.setNullURL(u);
- assertEquals(u, getUrlInt.invoke(rs,
- new Object[] { Integer.valueOf(1) } ) );
- assertEquals(u, getUrlString.invoke(rs,
- new Object[] { "column" } ) );
- }
- }
-
- /**
- * Tests the setNullAsciiStream implementation.
- */
- public void testSetNullAsciiStream() throws SQLException {
-
- assertNull(rs2.getNullAsciiStream());
- // Set what gets returned to something other than the default
- InputStream stream = new ByteArrayInputStream(new byte[0]);
- rs2.setNullAsciiStream(stream);
- assertNotNull(rs.getAsciiStream(1));
- assertEquals(stream, rs.getAsciiStream(1));
- assertNotNull(rs.getAsciiStream("column"));
- assertEquals(stream, rs.getAsciiStream("column"));
-
- }
-
- /**
- * Tests the setNullBigDecimal implementation.
- */
- public void testSetNullBigDecimal() throws SQLException {
-
- assertNull(rs2.getNullBigDecimal());
- // Set what gets returned to something other than the default
- BigDecimal bd = new BigDecimal(5.0);
- rs2.setNullBigDecimal(bd);
- assertNotNull(rs.getBigDecimal(1));
- assertEquals(bd, rs.getBigDecimal(1));
- assertNotNull(rs.getBigDecimal("column"));
- assertEquals(bd, rs.getBigDecimal("column"));
-
- }
-
- /**
- * Tests the setNullBinaryStream implementation.
- */
- public void testSetNullBinaryStream() throws SQLException {
-
- assertNull(rs2.getNullBinaryStream());
- // Set what gets returned to something other than the default
- InputStream stream = new ByteArrayInputStream(new byte[0]);
- rs2.setNullBinaryStream(stream);
- assertNotNull(rs.getBinaryStream(1));
- assertEquals(stream, rs.getBinaryStream(1));
- assertNotNull(rs.getBinaryStream("column"));
- assertEquals(stream, rs.getBinaryStream("column"));
-
- }
-
- /**
- * Tests the setNullBlob implementation.
- */
- public void testSetNullBlob() throws SQLException {
-
- assertNull(rs2.getNullBlob());
- // Set what gets returned to something other than the default
- Blob blob = new SqlNullCheckedResultSetMockBlob();
- rs2.setNullBlob(blob);
- assertNotNull(rs.getBlob(1));
- assertEquals(blob, rs.getBlob(1));
- assertNotNull(rs.getBlob("column"));
- assertEquals(blob, rs.getBlob("column"));
-
- }
-
- /**
- * Tests the setNullBoolean implementation.
- */
- public void testSetNullBoolean() throws SQLException {
-
- assertEquals(false, rs2.getNullBoolean());
- // Set what gets returned to something other than the default
- rs2.setNullBoolean(true);
- assertEquals(true, rs.getBoolean(1));
- assertEquals(true, rs.getBoolean("column"));
-
- }
-
- /**
- * Tests the setNullByte implementation.
- */
- public void testSetNullByte() throws SQLException {
-
- assertEquals((byte) 0, rs2.getNullByte());
- // Set what gets returned to something other than the default
- byte b = (byte) 10;
- rs2.setNullByte(b);
- assertEquals(b, rs.getByte(1));
- assertEquals(b, rs.getByte("column"));
-
- }
-
- /**
- * Tests the setNullByte implementation.
- */
- public void testSetNullBytes() throws SQLException {
-
- assertNull(rs2.getNullBytes());
- // Set what gets returned to something other than the default
- byte[] b = new byte[5];
- for (int i = 0; i < 5; i++) {
- b[0] = (byte) i;
- }
- rs2.setNullBytes(b);
- assertNotNull(rs.getBytes(1));
- assertArrayEquals(b, rs.getBytes(1));
- assertNotNull(rs.getBytes("column"));
- assertArrayEquals(b, rs.getBytes("column"));
-
- }
-
- /**
- * Tests the setNullCharacterStream implementation.
- */
- public void testSetNullCharacterStream() throws SQLException {
-
- assertNull(rs2.getNullCharacterStream());
- // Set what gets returned to something other than the default
- Reader reader = new CharArrayReader("this is a string".toCharArray());
- rs2.setNullCharacterStream(reader);
- assertNotNull(rs.getCharacterStream(1));
- assertEquals(reader, rs.getCharacterStream(1));
- assertNotNull(rs.getCharacterStream("column"));
- assertEquals(reader, rs.getCharacterStream("column"));
-
- }
-
- /**
- * Tests the setNullClob implementation.
- */
- public void testSetNullClob() throws SQLException {
-
- assertNull(rs2.getNullClob());
- // Set what gets returned to something other than the default
- Clob clob = new SqlNullCheckedResultSetMockClob();
- rs2.setNullClob(clob);
- assertNotNull(rs.getClob(1));
- assertEquals(clob, rs.getClob(1));
- assertNotNull(rs.getClob("column"));
- assertEquals(clob, rs.getClob("column"));
-
- }
-
- /**
- * Tests the setNullDate implementation.
- */
- public void testSetNullDate() throws SQLException {
-
- assertNull(rs2.getNullDate());
- // Set what gets returned to something other than the default
- java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
- rs2.setNullDate(date);
- assertNotNull(rs.getDate(1));
- assertEquals(date, rs.getDate(1));
- assertNotNull(rs.getDate("column"));
- assertEquals(date, rs.getDate("column"));
- assertNotNull(rs.getDate(1, Calendar.getInstance()));
- assertEquals(date, rs.getDate(1, Calendar.getInstance()));
- assertNotNull(rs.getDate("column", Calendar.getInstance()));
- assertEquals(date, rs.getDate("column", Calendar.getInstance()));
-
- }
-
- /**
- * Tests the setNullDouble implementation.
- */
- public void testSetNullDouble() throws SQLException {
- assertEquals(0.0, rs2.getNullDouble(), 0.0);
- // Set what gets returned to something other than the default
- double d = 10.0;
- rs2.setNullDouble(d);
- assertEquals(d, rs.getDouble(1), 0.0);
- assertEquals(d, rs.getDouble("column"), 0.0);
- }
-
- /**
- * Tests the setNullFloat implementation.
- */
- public void testSetNullFloat() throws SQLException {
- assertEquals((float) 0.0, rs2.getNullFloat(), 0.0);
- // Set what gets returned to something other than the default
- float f = (float) 10.0;
- rs2.setNullFloat(f);
- assertEquals(f, rs.getFloat(1), 0.0);
- assertEquals(f, rs.getFloat("column"), 0.0);
- }
-
- /**
- * Tests the setNullInt implementation.
- */
- public void testSetNullInt() throws SQLException {
- assertEquals(0, rs2.getNullInt());
- assertEquals(0, rs.getInt(1));
- assertTrue(rs.wasNull());
- assertEquals(0, rs.getInt("column"));
- assertTrue(rs.wasNull());
- // Set what gets returned to something other than the default
- int i = 10;
- rs2.setNullInt(i);
- assertEquals(i, rs.getInt(1));
- assertEquals(i, rs.getInt("column"));
- }
-
- /**
- * Tests the setNullLong implementation.
- */
- public void testSetNullLong() throws SQLException {
- assertEquals(0, rs2.getNullLong());
- // Set what gets returned to something other than the default
- long l = 10;
- rs2.setNullLong(l);
- assertEquals(l, rs.getLong(1));
- assertEquals(l, rs.getLong("column"));
- }
-
- /**
- * Tests the setNullObject implementation.
- */
- public void testSetNullObject() throws SQLException {
- assertNull(rs2.getNullObject());
- // Set what gets returned to something other than the default
- Object o = new Object();
- rs2.setNullObject(o);
- assertNotNull(rs.getObject(1));
- assertEquals(o, rs.getObject(1));
- assertNotNull(rs.getObject("column"));
- assertEquals(o, rs.getObject("column"));
- assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
- assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
- assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
- assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
- }
-
- /**
- * Tests the setNullShort implementation.
- */
- public void testSetNullShort() throws SQLException {
-
- assertEquals((short) 0, rs2.getNullShort());
- // Set what gets returned to something other than the default
- short s = (short) 10;
- rs2.setNullShort(s);
- assertEquals(s, rs.getShort(1));
- assertEquals(s, rs.getShort("column"));
-
- }
-
- /**
- * Tests the setNullString implementation.
- */
- public void testSetNullString() throws SQLException {
- assertEquals(null, rs2.getNullString());
- // Set what gets returned to something other than the default
- String s = "hello, world";
- rs2.setNullString(s);
- assertEquals(s, rs.getString(1));
- assertEquals(s, rs.getString("column"));
- }
-
- /**
- * Tests the setNullRef implementation.
- */
- public void testSetNullRef() throws SQLException {
- assertNull(rs2.getNullRef());
- // Set what gets returned to something other than the default
- Ref ref = new SqlNullCheckedResultSetMockRef();
- rs2.setNullRef(ref);
- assertNotNull(rs.getRef(1));
- assertEquals(ref, rs.getRef(1));
- assertNotNull(rs.getRef("column"));
- assertEquals(ref, rs.getRef("column"));
- }
-
- /**
- * Tests the setNullTime implementation.
- */
- public void testSetNullTime() throws SQLException {
- assertEquals(null, rs2.getNullTime());
- // Set what gets returned to something other than the default
- Time time = new Time(new java.util.Date().getTime());
- rs2.setNullTime(time);
- assertNotNull(rs.getTime(1));
- assertEquals(time, rs.getTime(1));
- assertNotNull(rs.getTime("column"));
- assertEquals(time, rs.getTime("column"));
- assertNotNull(rs.getTime(1, Calendar.getInstance()));
- assertEquals(time, rs.getTime(1, Calendar.getInstance()));
- assertNotNull(rs.getTime("column", Calendar.getInstance()));
- assertEquals(time, rs.getTime("column", Calendar.getInstance()));
- }
-
- /**
- * Tests the setNullTimestamp implementation.
- */
- public void testSetNullTimestamp() throws SQLException {
- assertEquals(null, rs2.getNullTimestamp());
- // Set what gets returned to something other than the default
- Timestamp ts = new Timestamp(new java.util.Date().getTime());
- rs2.setNullTimestamp(ts);
- assertNotNull(rs.getTimestamp(1));
- assertEquals(ts, rs.getTimestamp(1));
- assertNotNull(rs.getTimestamp("column"));
- assertEquals(ts, rs.getTimestamp("column"));
- assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
- assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
- assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
- assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
- }
-
-}
-
-class SqlNullUncheckedMockResultSet implements InvocationHandler {
-
- /**
- * Always return false for booleans, 0 for numerics, and null for Objects.
- * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
- */
- @Override
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
-
- Class<?> returnType = method.getReturnType();
-
- if (method.getName().equals("wasNull")) {
- return Boolean.TRUE;
-
- } else if (returnType.equals(Boolean.TYPE)) {
- return Boolean.FALSE;
-
- } else if (returnType.equals(Integer.TYPE)) {
- return Integer.valueOf(0);
-
- } else if (returnType.equals(Short.TYPE)) {
- return Short.valueOf((short) 0);
-
- } else if (returnType.equals(Double.TYPE)) {
- return new Double(0);
-
- } else if (returnType.equals(Long.TYPE)) {
- return Long.valueOf(0);
-
- } else if (returnType.equals(Byte.TYPE)) {
- return Byte.valueOf((byte) 0);
-
- } else if (returnType.equals(Float.TYPE)) {
- return new Float(0);
-
- } else {
- return null;
- }
- }
-}
-
-class SqlNullCheckedResultSetMockBlob implements Blob {
-
- @Override
- public InputStream getBinaryStream() throws SQLException {
- return new ByteArrayInputStream(new byte[0]);
- }
-
- @Override
- public byte[] getBytes(long param, int param1) throws SQLException {
- return new byte[0];
- }
-
- @Override
- public long length() throws SQLException {
- return 0;
- }
-
- @Override
- public long position(byte[] values, long param) throws SQLException {
- return 0;
- }
-
- @Override
- public long position(Blob blob, long param) throws SQLException {
- return 0;
- }
-
- @Override
- public void truncate(long len) throws SQLException {
-
- }
-
- @Override
- public int setBytes(long pos, byte[] bytes) throws SQLException {
- return 0;
- }
-
- @Override
- public int setBytes(long pos, byte[] bytes, int offset, int len)
- throws SQLException {
- return 0;
- }
-
- @Override
- public OutputStream setBinaryStream(long pos) throws SQLException {
- return null;
- }
-
- /**
- * @throws SQLException
- */
- @Override
- public void free() throws SQLException {
-
- }
-
- /**
- * @throws SQLException
- */
- @Override
- public InputStream getBinaryStream(long pos, long length) throws SQLException {
- return null;
- }
-
-}
-
-class SqlNullCheckedResultSetMockClob implements Clob {
-
- @Override
- public InputStream getAsciiStream() throws SQLException {
- return null;
- }
-
- @Override
- public Reader getCharacterStream() throws SQLException {
- return null;
- }
-
- @Override
- public String getSubString(long param, int param1) throws SQLException {
- return "";
- }
-
- @Override
- public long length() throws SQLException {
- return 0;
- }
-
- @Override
- public long position(Clob clob, long param) throws SQLException {
- return 0;
- }
-
- @Override
- public long position(String str, long param) throws SQLException {
- return 0;
- }
-
- @Override
- public void truncate(long len) throws SQLException {
-
- }
-
- @Override
- public OutputStream setAsciiStream(long pos) throws SQLException {
- return null;
- }
-
- @Override
- public Writer setCharacterStream(long pos) throws SQLException {
- return null;
- }
-
- @Override
- public int setString(long pos, String str) throws SQLException {
- return 0;
- }
-
- @Override
- public int setString(long pos, String str, int offset, int len)
- throws SQLException {
- return 0;
- }
-
- /**
- * @throws SQLException
- */
- @Override
- public void free() throws SQLException {
-
- }
-
- /**
- * @throws SQLException
- */
- @Override
- public Reader getCharacterStream(long pos, long length) throws SQLException {
- return null;
- }
-
-}
-
-class SqlNullCheckedResultSetMockRef implements Ref {
-
- @Override
- public String getBaseTypeName() throws SQLException {
- return "";
- }
-
- @Override
- public Object getObject() throws SQLException {
- return null;
- }
-
- @Override
- public void setObject(Object value) throws SQLException {
-
- }
-
- @Override
- public Object getObject(Map<String,Class<?>> map) throws SQLException {
- return null;
- }
-
-}
+/*
+ * 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.dbutils.wrappers;
+
+import java.io.ByteArrayInputStream;
+import java.io.CharArrayReader;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.Ref;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Map;
+
+import org.apache.commons.dbutils.BaseTestCase;
+import org.apache.commons.dbutils.ProxyFactory;
+
+/**
+ * Test cases for <code>SqlNullCheckedResultSet</code> class.
+ */
+public class SqlNullCheckedResultSetTest extends BaseTestCase {
+
+ private SqlNullCheckedResultSet rs2 = null;
+
+ /**
+ * Sets up instance variables required by this test case.
+ */
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ rs2 =
+ new SqlNullCheckedResultSet(
+ ProxyFactory.instance().createResultSet(
+ new SqlNullUncheckedMockResultSet()));
+
+ rs = ProxyFactory.instance().createResultSet(rs2); // Override superclass field
+ }
+
+ /**
+ * Tests the getAsciiStream implementation.
+ */
+ public void testGetAsciiStream() throws SQLException {
+
+ assertNull(rs.getAsciiStream(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getAsciiStream("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ InputStream stream = new ByteArrayInputStream(new byte[0]);
+ rs2.setNullAsciiStream(stream);
+ assertNotNull(rs.getAsciiStream(1));
+ assertEquals(stream, rs.getAsciiStream(1));
+ assertNotNull(rs.getAsciiStream("column"));
+ assertEquals(stream, rs.getAsciiStream("column"));
+
+ }
+
+ /**
+ * Tests the getBigDecimal implementation.
+ */
+ public void testGetBigDecimal() throws SQLException {
+
+ assertNull(rs.getBigDecimal(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getBigDecimal("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ BigDecimal bd = new BigDecimal(5.0);
+ rs2.setNullBigDecimal(bd);
+ assertNotNull(rs.getBigDecimal(1));
+ assertEquals(bd, rs.getBigDecimal(1));
+ assertNotNull(rs.getBigDecimal("column"));
+ assertEquals(bd, rs.getBigDecimal("column"));
+
+ }
+
+ /**
+ * Tests the getBinaryStream implementation.
+ */
+ public void testGetBinaryStream() throws SQLException {
+
+ assertNull(rs.getBinaryStream(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getBinaryStream("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ InputStream stream = new ByteArrayInputStream(new byte[0]);
+ rs2.setNullBinaryStream(stream);
+ assertNotNull(rs.getBinaryStream(1));
+ assertEquals(stream, rs.getBinaryStream(1));
+ assertNotNull(rs.getBinaryStream("column"));
+ assertEquals(stream, rs.getBinaryStream("column"));
+
+ }
+
+ /**
+ * Tests the getBlob implementation.
+ */
+ public void testGetBlob() throws SQLException {
+
+ assertNull(rs.getBlob(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getBlob("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ Blob blob = new SqlNullCheckedResultSetMockBlob();
+ rs2.setNullBlob(blob);
+ assertNotNull(rs.getBlob(1));
+ assertEquals(blob, rs.getBlob(1));
+ assertNotNull(rs.getBlob("column"));
+ assertEquals(blob, rs.getBlob("column"));
+
+ }
+
+ /**
+ * Tests the getBoolean implementation.
+ */
+ public void testGetBoolean() throws SQLException {
+
+ assertEquals(false, rs.getBoolean(1));
+ assertTrue(rs.wasNull());
+ assertEquals(false, rs.getBoolean("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ rs2.setNullBoolean(true);
+ assertEquals(true, rs.getBoolean(1));
+ assertEquals(true, rs.getBoolean("column"));
+
+ }
+
+ /**
+ * Tests the getByte implementation.
+ */
+ public void testGetByte() throws SQLException {
+
+ assertEquals((byte) 0, rs.getByte(1));
+ assertTrue(rs.wasNull());
+ assertEquals((byte) 0, rs.getByte("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ byte b = (byte) 10;
+ rs2.setNullByte(b);
+ assertEquals(b, rs.getByte(1));
+ assertEquals(b, rs.getByte("column"));
+
+ }
+
+ /**
+ * Tests the getByte implementation.
+ */
+ public void testGetBytes() throws SQLException {
+
+ assertNull(rs.getBytes(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getBytes("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ byte[] b = new byte[5];
+ for (int i = 0; i < 5; i++) {
+ b[0] = (byte) i;
+ }
+ rs2.setNullBytes(b);
+ assertNotNull(rs.getBytes(1));
+ assertArrayEquals(b, rs.getBytes(1));
+ assertNotNull(rs.getBytes("column"));
+ assertArrayEquals(b, rs.getBytes("column"));
+
+ }
+
+ private static void assertArrayEquals(byte[] expected, byte[] actual) {
+ if (expected == actual) {
+ return;
+ }
+ if (expected.length != actual.length) {
+ failNotEquals(null, Arrays.toString(expected), Arrays.toString(actual));
+ }
+ for (int i = 0; i < expected.length; i++) {
+ byte expectedItem = expected[i];
+ byte actualItem = actual[i];
+ assertEquals("Array not equal at index " + i, expectedItem, actualItem);
+ }
+ }
+
+ /**
+ * Tests the getCharacterStream implementation.
+ */
+ public void testGetCharacterStream() throws SQLException {
+
+ assertNull(rs.getCharacterStream(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getCharacterStream("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ Reader reader = new CharArrayReader("this is a string".toCharArray());
+ rs2.setNullCharacterStream(reader);
+ assertNotNull(rs.getCharacterStream(1));
+ assertEquals(reader, rs.getCharacterStream(1));
+ assertNotNull(rs.getCharacterStream("column"));
+ assertEquals(reader, rs.getCharacterStream("column"));
+
+ }
+
+ /**
+ * Tests the getClob implementation.
+ */
+ public void testGetClob() throws SQLException {
+
+ assertNull(rs.getClob(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getClob("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ Clob clob = new SqlNullCheckedResultSetMockClob();
+ rs2.setNullClob(clob);
+ assertNotNull(rs.getClob(1));
+ assertEquals(clob, rs.getClob(1));
+ assertNotNull(rs.getClob("column"));
+ assertEquals(clob, rs.getClob("column"));
+
+ }
+
+ /**
+ * Tests the getDate implementation.
+ */
+ public void testGetDate() throws SQLException {
+
+ assertNull(rs.getDate(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getDate("column"));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getDate(1, Calendar.getInstance()));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getDate("column", Calendar.getInstance()));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
+ rs2.setNullDate(date);
+ assertNotNull(rs.getDate(1));
+ assertEquals(date, rs.getDate(1));
+ assertNotNull(rs.getDate("column"));
+ assertEquals(date, rs.getDate("column"));
+ assertNotNull(rs.getDate(1, Calendar.getInstance()));
+ assertEquals(date, rs.getDate(1, Calendar.getInstance()));
+ assertNotNull(rs.getDate("column", Calendar.getInstance()));
+ assertEquals(date, rs.getDate("column", Calendar.getInstance()));
+
+ }
+
+ /**
+ * Tests the getDouble implementation.
+ */
+ public void testGetDouble() throws SQLException {
+
+ assertEquals(0.0, rs.getDouble(1), 0.0);
+ assertTrue(rs.wasNull());
+ assertEquals(0.0, rs.getDouble("column"), 0.0);
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ double d = 10.0;
+ rs2.setNullDouble(d);
+ assertEquals(d, rs.getDouble(1), 0.0);
+ assertEquals(d, rs.getDouble("column"), 0.0);
+
+ }
+
+ /**
+ * Tests the getFloat implementation.
+ */
+ public void testGetFloat() throws SQLException {
+ assertEquals(0, rs.getFloat(1), 0.0);
+ assertTrue(rs.wasNull());
+ assertEquals(0, rs.getFloat("column"), 0.0);
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ float f = 10;
+ rs2.setNullFloat(f);
+ assertEquals(f, rs.getFloat(1), 0.0);
+ assertEquals(f, rs.getFloat("column"), 0.0);
+ }
+
+ /**
+ * Tests the getInt implementation.
+ */
+ public void testGetInt() throws SQLException {
+ assertEquals(0, rs.getInt(1));
+ assertTrue(rs.wasNull());
+ assertEquals(0, rs.getInt("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ int i = 10;
+ rs2.setNullInt(i);
+ assertEquals(i, rs.getInt(1));
+ assertEquals(i, rs.getInt("column"));
+ }
+
+ /**
+ * Tests the getLong implementation.
+ */
+ public void testGetLong() throws SQLException {
+ assertEquals(0, rs.getLong(1));
+ assertTrue(rs.wasNull());
+ assertEquals(0, rs.getLong("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ long l = 10;
+ rs2.setNullLong(l);
+ assertEquals(l, rs.getLong(1));
+ assertEquals(l, rs.getLong("column"));
+ }
+
+ /**
+ * Tests the getObject implementation.
+ */
+ public void testGetObject() throws SQLException {
+
+ assertNull(rs.getObject(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getObject("column"));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getObject(1, (Map<String, Class<?>>) null));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getObject("column", (Map<String, Class<?>>) null));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ Object o = new Object();
+ rs2.setNullObject(o);
+ assertNotNull(rs.getObject(1));
+ assertEquals(o, rs.getObject(1));
+ assertNotNull(rs.getObject("column"));
+ assertEquals(o, rs.getObject("column"));
+ assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
+ assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
+ assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
+ assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
+
+ }
+
+ /**
+ * Tests the getRef implementation.
+ */
+ public void testGetRef() throws SQLException {
+
+ assertNull(rs.getRef(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getRef("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ Ref ref = new SqlNullCheckedResultSetMockRef();
+ rs2.setNullRef(ref);
+ assertNotNull(rs.getRef(1));
+ assertEquals(ref, rs.getRef(1));
+ assertNotNull(rs.getRef("column"));
+ assertEquals(ref, rs.getRef("column"));
+
+ }
+
+ /**
+ * Tests the getShort implementation.
+ */
+ public void testGetShort() throws SQLException {
+
+ assertEquals((short) 0, rs.getShort(1));
+ assertTrue(rs.wasNull());
+ assertEquals((short) 0, rs.getShort("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ short s = (short) 10;
+ rs2.setNullShort(s);
+ assertEquals(s, rs.getShort(1));
+ assertEquals(s, rs.getShort("column"));
+ }
+
+ /**
+ * Tests the getString implementation.
+ */
+ public void testGetString() throws SQLException {
+ assertEquals(null, rs.getString(1));
+ assertTrue(rs.wasNull());
+ assertEquals(null, rs.getString("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ String s = "hello, world";
+ rs2.setNullString(s);
+ assertEquals(s, rs.getString(1));
+ assertEquals(s, rs.getString("column"));
+ }
+
+ /**
+ * Tests the getTime implementation.
+ */
+ public void testGetTime() throws SQLException {
+
+ assertNull(rs.getTime(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getTime("column"));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getTime(1, Calendar.getInstance()));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getTime("column", Calendar.getInstance()));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ Time time = new Time(new java.util.Date().getTime());
+ rs2.setNullTime(time);
+ assertNotNull(rs.getTime(1));
+ assertEquals(time, rs.getTime(1));
+ assertNotNull(rs.getTime("column"));
+ assertEquals(time, rs.getTime("column"));
+ assertNotNull(rs.getTime(1, Calendar.getInstance()));
+ assertEquals(time, rs.getTime(1, Calendar.getInstance()));
+ assertNotNull(rs.getTime("column", Calendar.getInstance()));
+ assertEquals(time, rs.getTime("column", Calendar.getInstance()));
+
+ }
+
+ /**
+ * Tests the getTimestamp implementation.
+ */
+ public void testGetTimestamp() throws SQLException {
+
+ assertNull(rs.getTimestamp(1));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getTimestamp("column"));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getTimestamp(1, Calendar.getInstance()));
+ assertTrue(rs.wasNull());
+ assertNull(rs.getTimestamp("column", Calendar.getInstance()));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ Timestamp ts = new Timestamp(new java.util.Date().getTime());
+ rs2.setNullTimestamp(ts);
+ assertNotNull(rs.getTimestamp(1));
+ assertEquals(ts, rs.getTimestamp(1));
+ assertNotNull(rs.getTimestamp("column"));
+ assertEquals(ts, rs.getTimestamp("column"));
+ assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
+ assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
+ assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
+ assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
+ }
+
+ /**
+ * Tests the getURL and setNullURL implementations.
+ *
+ * Uses reflection to allow for building under JDK 1.3.
+ */
+ public void testURL() throws SQLException, MalformedURLException,
+ IllegalAccessException, IllegalArgumentException,
+ java.lang.reflect.InvocationTargetException
+ {
+ Method getUrlInt = null;
+ Method getUrlString = null;
+ try {
+ getUrlInt = ResultSet.class.getMethod("getURL",
+ new Class[] { Integer.TYPE } );
+ getUrlString = ResultSet.class.getMethod("getURL",
+ new Class[] { String.class } );
+ } catch(NoSuchMethodException e) {
+ // ignore
+ } catch(SecurityException e) {
+ // ignore
+ }
+ if (getUrlInt != null && getUrlString != null) {
+ assertEquals(null, getUrlInt.invoke(rs,
+ new Object[] { Integer.valueOf(1) } ) );
+ assertTrue(rs.wasNull());
+ assertEquals(null, getUrlString.invoke(rs,
+ new Object[] { "column" } ) );
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ URL u = new URL("http://www.apache.org");
+ rs2.setNullURL(u);
+ assertEquals(u, getUrlInt.invoke(rs,
+ new Object[] { Integer.valueOf(1) } ) );
+ assertEquals(u, getUrlString.invoke(rs,
+ new Object[] { "column" } ) );
+ }
+ }
+
+ /**
+ * Tests the setNullAsciiStream implementation.
+ */
+ public void testSetNullAsciiStream() throws SQLException {
+
+ assertNull(rs2.getNullAsciiStream());
+ // Set what gets returned to something other than the default
+ InputStream stream = new ByteArrayInputStream(new byte[0]);
+ rs2.setNullAsciiStream(stream);
+ assertNotNull(rs.getAsciiStream(1));
+ assertEquals(stream, rs.getAsciiStream(1));
+ assertNotNull(rs.getAsciiStream("column"));
+ assertEquals(stream, rs.getAsciiStream("column"));
+
+ }
+
+ /**
+ * Tests the setNullBigDecimal implementation.
+ */
+ public void testSetNullBigDecimal() throws SQLException {
+
+ assertNull(rs2.getNullBigDecimal());
+ // Set what gets returned to something other than the default
+ BigDecimal bd = new BigDecimal(5.0);
+ rs2.setNullBigDecimal(bd);
+ assertNotNull(rs.getBigDecimal(1));
+ assertEquals(bd, rs.getBigDecimal(1));
+ assertNotNull(rs.getBigDecimal("column"));
+ assertEquals(bd, rs.getBigDecimal("column"));
+
+ }
+
+ /**
+ * Tests the setNullBinaryStream implementation.
+ */
+ public void testSetNullBinaryStream() throws SQLException {
+
+ assertNull(rs2.getNullBinaryStream());
+ // Set what gets returned to something other than the default
+ InputStream stream = new ByteArrayInputStream(new byte[0]);
+ rs2.setNullBinaryStream(stream);
+ assertNotNull(rs.getBinaryStream(1));
+ assertEquals(stream, rs.getBinaryStream(1));
+ assertNotNull(rs.getBinaryStream("column"));
+ assertEquals(stream, rs.getBinaryStream("column"));
+
+ }
+
+ /**
+ * Tests the setNullBlob implementation.
+ */
+ public void testSetNullBlob() throws SQLException {
+
+ assertNull(rs2.getNullBlob());
+ // Set what gets returned to something other than the default
+ Blob blob = new SqlNullCheckedResultSetMockBlob();
+ rs2.setNullBlob(blob);
+ assertNotNull(rs.getBlob(1));
+ assertEquals(blob, rs.getBlob(1));
+ assertNotNull(rs.getBlob("column"));
+ assertEquals(blob, rs.getBlob("column"));
+
+ }
+
+ /**
+ * Tests the setNullBoolean implementation.
+ */
+ public void testSetNullBoolean() throws SQLException {
+
+ assertEquals(false, rs2.getNullBoolean());
+ // Set what gets returned to something other than the default
+ rs2.setNullBoolean(true);
+ assertEquals(true, rs.getBoolean(1));
+ assertEquals(true, rs.getBoolean("column"));
+
+ }
+
+ /**
+ * Tests the setNullByte implementation.
+ */
+ public void testSetNullByte() throws SQLException {
+
+ assertEquals((byte) 0, rs2.getNullByte());
+ // Set what gets returned to something other than the default
+ byte b = (byte) 10;
+ rs2.setNullByte(b);
+ assertEquals(b, rs.getByte(1));
+ assertEquals(b, rs.getByte("column"));
+
+ }
+
+ /**
+ * Tests the setNullByte implementation.
+ */
+ public void testSetNullBytes() throws SQLException {
+
+ assertNull(rs2.getNullBytes());
+ // Set what gets returned to something other than the default
+ byte[] b = new byte[5];
+ for (int i = 0; i < 5; i++) {
+ b[0] = (byte) i;
+ }
+ rs2.setNullBytes(b);
+ assertNotNull(rs.getBytes(1));
+ assertArrayEquals(b, rs.getBytes(1));
+ assertNotNull(rs.getBytes("column"));
+ assertArrayEquals(b, rs.getBytes("column"));
+
+ }
+
+ /**
+ * Tests the setNullCharacterStream implementation.
+ */
+ public void testSetNullCharacterStream() throws SQLException {
+
+ assertNull(rs2.getNullCharacterStream());
+ // Set what gets returned to something other than the default
+ Reader reader = new CharArrayReader("this is a string".toCharArray());
+ rs2.setNullCharacterStream(reader);
+ assertNotNull(rs.getCharacterStream(1));
+ assertEquals(reader, rs.getCharacterStream(1));
+ assertNotNull(rs.getCharacterStream("column"));
+ assertEquals(reader, rs.getCharacterStream("column"));
+
+ }
+
+ /**
+ * Tests the setNullClob implementation.
+ */
+ public void testSetNullClob() throws SQLException {
+
+ assertNull(rs2.getNullClob());
+ // Set what gets returned to something other than the default
+ Clob clob = new SqlNullCheckedResultSetMockClob();
+ rs2.setNullClob(clob);
+ assertNotNull(rs.getClob(1));
+ assertEquals(clob, rs.getClob(1));
+ assertNotNull(rs.getClob("column"));
+ assertEquals(clob, rs.getClob("column"));
+
+ }
+
+ /**
+ * Tests the setNullDate implementation.
+ */
+ public void testSetNullDate() throws SQLException {
+
+ assertNull(rs2.getNullDate());
+ // Set what gets returned to something other than the default
+ java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
+ rs2.setNullDate(date);
+ assertNotNull(rs.getDate(1));
+ assertEquals(date, rs.getDate(1));
+ assertNotNull(rs.getDate("column"));
+ assertEquals(date, rs.getDate("column"));
+ assertNotNull(rs.getDate(1, Calendar.getInstance()));
+ assertEquals(date, rs.getDate(1, Calendar.getInstance()));
+ assertNotNull(rs.getDate("column", Calendar.getInstance()));
+ assertEquals(date, rs.getDate("column", Calendar.getInstance()));
+
+ }
+
+ /**
+ * Tests the setNullDouble implementation.
+ */
+ public void testSetNullDouble() throws SQLException {
+ assertEquals(0.0, rs2.getNullDouble(), 0.0);
+ // Set what gets returned to something other than the default
+ double d = 10.0;
+ rs2.setNullDouble(d);
+ assertEquals(d, rs.getDouble(1), 0.0);
+ assertEquals(d, rs.getDouble("column"), 0.0);
+ }
+
+ /**
+ * Tests the setNullFloat implementation.
+ */
+ public void testSetNullFloat() throws SQLException {
+ assertEquals((float) 0.0, rs2.getNullFloat(), 0.0);
+ // Set what gets returned to something other than the default
+ float f = (float) 10.0;
+ rs2.setNullFloat(f);
+ assertEquals(f, rs.getFloat(1), 0.0);
+ assertEquals(f, rs.getFloat("column"), 0.0);
+ }
+
+ /**
+ * Tests the setNullInt implementation.
+ */
+ public void testSetNullInt() throws SQLException {
+ assertEquals(0, rs2.getNullInt());
+ assertEquals(0, rs.getInt(1));
+ assertTrue(rs.wasNull());
+ assertEquals(0, rs.getInt("column"));
+ assertTrue(rs.wasNull());
+ // Set what gets returned to something other than the default
+ int i = 10;
+ rs2.setNullInt(i);
+ assertEquals(i, rs.getInt(1));
+ assertEquals(i, rs.getInt("column"));
+ }
+
+ /**
+ * Tests the setNullLong implementation.
+ */
+ public void testSetNullLong() throws SQLException {
+ assertEquals(0, rs2.getNullLong());
+ // Set what gets returned to something other than the default
+ long l = 10;
+ rs2.setNullLong(l);
+ assertEquals(l, rs.getLong(1));
+ assertEquals(l, rs.getLong("column"));
+ }
+
+ /**
+ * Tests the setNullObject implementation.
+ */
+ public void testSetNullObject() throws SQLException {
+ assertNull(rs2.getNullObject());
+ // Set what gets returned to something other than the default
+ Object o = new Object();
+ rs2.setNullObject(o);
+ assertNotNull(rs.getObject(1));
+ assertEquals(o, rs.getObject(1));
+ assertNotNull(rs.getObject("column"));
+ assertEquals(o, rs.getObject("column"));
+ assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
+ assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
+ assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
+ assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
+ }
+
+ /**
+ * Tests the setNullShort implementation.
+ */
+ public void testSetNullShort() throws SQLException {
+
+ assertEquals((short) 0, rs2.getNullShort());
+ // Set what gets returned to something other than the default
+ short s = (short) 10;
+ rs2.setNullShort(s);
+ assertEquals(s, rs.getShort(1));
+ assertEquals(s, rs.getShort("column"));
+
+ }
+
+ /**
+ * Tests the setNullString implementation.
+ */
+ public void testSetNullString() throws SQLException {
+ assertEquals(null, rs2.getNullString());
+ // Set what gets returned to something other than the default
+ String s = "hello, world";
+ rs2.setNullString(s);
+ assertEquals(s, rs.getString(1));
+ assertEquals(s, rs.getString("column"));
+ }
+
+ /**
+ * Tests the setNullRef implementation.
+ */
+ public void testSetNullRef() throws SQLException {
+ assertNull(rs2.getNullRef());
+ // Set what gets returned to something other than the default
+ Ref ref = new SqlNullCheckedResultSetMockRef();
+ rs2.setNullRef(ref);
+ assertNotNull(rs.getRef(1));
+ assertEquals(ref, rs.getRef(1));
+ assertNotNull(rs.getRef("column"));
+ assertEquals(ref, rs.getRef("column"));
+ }
+
+ /**
+ * Tests the setNullTime implementation.
+ */
+ public void testSetNullTime() throws SQLException {
+ assertEquals(null, rs2.getNullTime());
+ // Set what gets returned to something other than the default
+ Time time = new Time(new java.util.Date().getTime());
+ rs2.setNullTime(time);
+ assertNotNull(rs.getTime(1));
+ assertEquals(time, rs.getTime(1));
+ assertNotNull(rs.getTime("column"));
+ assertEquals(time, rs.getTime("column"));
+ assertNotNull(rs.getTime(1, Calendar.getInstance()));
+ assertEquals(time, rs.getTime(1, Calendar.getInstance()));
+ assertNotNull(rs.getTime("column", Calendar.getInstance()));
+ assertEquals(time, rs.getTime("column", Calendar.getInstance()));
+ }
+
+ /**
+ * Tests the setNullTimestamp implementation.
+ */
+ public void testSetNullTimestamp() throws SQLException {
+ assertEquals(null, rs2.getNullTimestamp());
+ // Set what gets returned to something other than the default
+ Timestamp ts = new Timestamp(new java.util.Date().getTime());
+ rs2.setNullTimestamp(ts);
+ assertNotNull(rs.getTimestamp(1));
+ assertEquals(ts, rs.getTimestamp(1));
+ assertNotNull(rs.getTimestamp("column"));
+ assertEquals(ts, rs.getTimestamp("column"));
+ assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
+ assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
+ assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
+ assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
+ }
+
+}
+
+class SqlNullUncheckedMockResultSet implements InvocationHandler {
+
+ /**
+ * Always return false for booleans, 0 for numerics, and null for Objects.
+ * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
+ */
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+
+ Class<?> returnType = method.getReturnType();
+
+ if (method.getName().equals("wasNull")) {
+ return Boolean.TRUE;
+
+ } else if (returnType.equals(Boolean.TYPE)) {
+ return Boolean.FALSE;
+
+ } else if (returnType.equals(Integer.TYPE)) {
+ return Integer.valueOf(0);
+
+ } else if (returnType.equals(Short.TYPE)) {
+ return Short.valueOf((short) 0);
+
+ } else if (returnType.equals(Double.TYPE)) {
+ return new Double(0);
+
+ } else if (returnType.equals(Long.TYPE)) {
+ return Long.valueOf(0);
+
+ } else if (returnType.equals(Byte.TYPE)) {
+ return Byte.valueOf((byte) 0);
+
+ } else if (returnType.equals(Float.TYPE)) {
+ return new Float(0);
+
+ } else {
+ return null;
+ }
+ }
+}
+
+class SqlNullCheckedResultSetMockBlob implements Blob {
+
+ @Override
+ public InputStream getBinaryStream() throws SQLException {
+ return new ByteArrayInputStream(new byte[0]);
+ }
+
+ @Override
+ public byte[] getBytes(long param, int param1) throws SQLException {
+ return new byte[0];
+ }
+
+ @Override
+ public long length() throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public long position(byte[] values, long param) throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public long position(Blob blob, long param) throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public void truncate(long len) throws SQLException {
+
+ }
+
+ @Override
+ public int setBytes(long pos, byte[] bytes) throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public int setBytes(long pos, byte[] bytes, int offset, int len)
+ throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public OutputStream setBinaryStream(long pos) throws SQLException {
+ return null;
+ }
+
+ /**
+ * @throws SQLException
+ */
+ @Override
+ public void free() throws SQLException {
+
+ }
+
+ /**
+ * @throws SQLException
+ */
+ @Override
+ public InputStream getBinaryStream(long pos, long length) throws SQLException {
+ return null;
+ }
+
+}
+
+class SqlNullCheckedResultSetMockClob implements Clob {
+
+ @Override
+ public InputStream getAsciiStream() throws SQLException {
+ return null;
+ }
+
+ @Override
+ public Reader getCharacterStream() throws SQLException {
+ return null;
+ }
+
+ @Override
+ public String getSubString(long param, int param1) throws SQLException {
+ return "";
+ }
+
+ @Override
+ public long length() throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public long position(Clob clob, long param) throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public long position(String str, long param) throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public void truncate(long len) throws SQLException {
+
+ }
+
+ @Override
+ public OutputStream setAsciiStream(long pos) throws SQLException {
+ return null;
+ }
+
+ @Override
+ public Writer setCharacterStream(long pos) throws SQLException {
+ return null;
+ }
+
+ @Override
+ public int setString(long pos, String str) throws SQLException {
+ return 0;
+ }
+
+ @Override
+ public int setString(long pos, String str, int offset, int len)
+ throws SQLException {
+ return 0;
+ }
+
+ /**
+ * @throws SQLException
+ */
+ @Override
+ public void free() throws SQLException {
+
+ }
+
+ /**
+ * @throws SQLException
+ */
+ @Override
+ public Reader getCharacterStream(long pos, long length) throws SQLException {
+ return null;
+ }
+
+}
+
+class SqlNullCheckedResultSetMockRef implements Ref {
+
+ @Override
+ public String getBaseTypeName() throws SQLException {
+ return "";
+ }
+
+ @Override
+ public Object getObject() throws SQLException {
+ return null;
+ }
+
+ @Override
+ public void setObject(Object value) throws SQLException {
+
+ }
+
+ @Override
+ public Object getObject(Map<String,Class<?>> map) throws SQLException {
+ return null;
+ }
+
+}
[3/5] commons-dbutils git commit: Remove trailing white spaces on all
lines.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/test/java/org/apache/commons/dbutils/DbUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/DbUtilsTest.java b/src/test/java/org/apache/commons/dbutils/DbUtilsTest.java
index 33660ef..78e9081 100644
--- a/src/test/java/org/apache/commons/dbutils/DbUtilsTest.java
+++ b/src/test/java/org/apache/commons/dbutils/DbUtilsTest.java
@@ -1,285 +1,285 @@
-/*
- * 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.dbutils;
-
-import org.junit.Test;
-
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-import static org.mockito.Mockito.*;
-
-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();
- }
-
- @Test
- public void testLoadDriverReturnsFalse() {
-
- assertFalse(DbUtils.loadDriver(""));
-
- }
-
- @Test
- public void testCommitAndCloseQuietlyWithNullDoesNotThrowAnSQLException() {
-
- DbUtils.commitAndCloseQuietly(null);
-
- }
-
-}
+/*
+ * 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.dbutils;
+
+import org.junit.Test;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.*;
+
+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();
+ }
+
+ @Test
+ public void testLoadDriverReturnsFalse() {
+
+ assertFalse(DbUtils.loadDriver(""));
+
+ }
+
+ @Test
+ public void testCommitAndCloseQuietlyWithNullDoesNotThrowAnSQLException() {
+
+ DbUtils.commitAndCloseQuietly(null);
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java b/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java
index 20aed1b..2013627 100644
--- a/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java
+++ b/src/test/java/org/apache/commons/dbutils/GenerousBeanProcessorTest.java
@@ -30,7 +30,7 @@ import static org.mockito.Mockito.when;
public class GenerousBeanProcessorTest {
-
+
GenerousBeanProcessor processor = new GenerousBeanProcessor();
@Mock ResultSetMetaData metaData;
PropertyDescriptor[] propDescriptors;
@@ -38,9 +38,9 @@ public class GenerousBeanProcessorTest {
@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);
@@ -50,13 +50,13 @@ public class GenerousBeanProcessorTest {
@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]);
@@ -88,13 +88,13 @@ public class GenerousBeanProcessorTest {
@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]);
@@ -127,27 +127,27 @@ public class GenerousBeanProcessorTest {
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/084f286a/src/test/java/org/apache/commons/dbutils/MockResultSet.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/MockResultSet.java b/src/test/java/org/apache/commons/dbutils/MockResultSet.java
index 39b8547..e1d2e49 100644
--- a/src/test/java/org/apache/commons/dbutils/MockResultSet.java
+++ b/src/test/java/org/apache/commons/dbutils/MockResultSet.java
@@ -218,7 +218,7 @@ public class MockResultSet implements InvocationHandler {
}
/**
- * @throws SQLException
+ * @throws SQLException
*/
protected ResultSetMetaData getMetaData() throws SQLException {
return this.metaData;
@@ -328,14 +328,14 @@ public class MockResultSet implements InvocationHandler {
}
/**
- * @throws SQLException
+ * @throws SQLException
*/
protected Boolean isLast() throws SQLException {
return this.iter.hasNext() ? Boolean.FALSE : Boolean.TRUE;
}
/**
- * @throws SQLException
+ * @throws SQLException
*/
protected Boolean next() throws SQLException {
if (!this.iter.hasNext()) {
@@ -355,7 +355,7 @@ public class MockResultSet implements InvocationHandler {
}
/**
- * @throws SQLException
+ * @throws SQLException
*/
protected Boolean wasNull() throws SQLException {
return this.wasNull;
[5/5] commons-dbutils git commit: Remove trailing white spaces on all
lines.
Posted by gg...@apache.org.
Remove trailing white spaces on all lines.
Project: http://git-wip-us.apache.org/repos/asf/commons-dbutils/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-dbutils/commit/084f286a
Tree: http://git-wip-us.apache.org/repos/asf/commons-dbutils/tree/084f286a
Diff: http://git-wip-us.apache.org/repos/asf/commons-dbutils/diff/084f286a
Branch: refs/heads/master
Commit: 084f286a0d7ffdde482ab5a9e4ada7affe2547f0
Parents: 062a9ed
Author: ggregory <gg...@US-L-GG05.rocketsoftware.com>
Authored: Sun Oct 15 20:41:26 2017 -0600
Committer: ggregory <gg...@US-L-GG05.rocketsoftware.com>
Committed: Sun Oct 15 20:41:26 2017 -0600
----------------------------------------------------------------------
.../commons/dbutils/AbstractQueryRunner.java | 1210 +++++-----
.../apache/commons/dbutils/BeanProcessor.java | 1068 ++++-----
.../commons/dbutils/AsyncQueryRunnerTest.java | 992 ++++-----
.../apache/commons/dbutils/BaseTestCase.java | 276 +--
.../org/apache/commons/dbutils/DbUtilsTest.java | 570 ++---
.../dbutils/GenerousBeanProcessorTest.java | 30 +-
.../apache/commons/dbutils/MockResultSet.java | 8 +-
.../apache/commons/dbutils/QueryRunnerTest.java | 2072 +++++++++---------
.../wrappers/SqlNullCheckedResultSetTest.java | 2044 ++++++++---------
9 files changed, 4135 insertions(+), 4135 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java b/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java
index 961aa68..4e3a56c 100644
--- a/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java
+++ b/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java
@@ -1,605 +1,605 @@
-/*
- * 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.dbutils;
-
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.CallableStatement;
-import java.sql.Connection;
-import java.sql.ParameterMetaData;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.SQLFeatureNotSupportedException;
-import java.sql.Statement;
-import java.sql.Types;
-import java.util.Arrays;
-
-import javax.sql.DataSource;
-
-/**
- * The base class for QueryRunner & AsyncQueryRunner. This class is thread safe.
- *
- * @since 1.4 (mostly extracted from QueryRunner)
- */
-public abstract class AbstractQueryRunner {
- /**
- * Is {@link ParameterMetaData#getParameterType(int)} broken (have we tried
- * it yet)?
- */
- private volatile boolean pmdKnownBroken = false;
-
- /**
- * The DataSource to retrieve connections from.
- * @deprecated Access to this field should be through {@link #getDataSource()}.
- */
- @Deprecated
- protected final DataSource ds;
-
- /**
- * Configuration to use when preparing statements.
- */
- private final StatementConfiguration stmtConfig;
-
- /**
- * Default constructor, sets pmdKnownBroken to false, ds to null and stmtConfig to null.
- */
- public AbstractQueryRunner() {
- ds = null;
- this.stmtConfig = null;
- }
-
- /**
- * Constructor to control the use of <code>ParameterMetaData</code>.
- *
- * @param pmdKnownBroken
- * Some drivers don't support
- * {@link ParameterMetaData#getParameterType(int) }; if
- * <code>pmdKnownBroken</code> is set to true, we won't even try
- * it; if false, we'll try it, and if it breaks, we'll remember
- * not to use it again.
- */
- public AbstractQueryRunner(boolean pmdKnownBroken) {
- this.pmdKnownBroken = pmdKnownBroken;
- ds = null;
- this.stmtConfig = null;
- }
-
- /**
- * Constructor to provide a <code>DataSource</code>. Methods that do not
- * take a <code>Connection</code> parameter will retrieve connections from
- * this <code>DataSource</code>.
- *
- * @param ds
- * The <code>DataSource</code> to retrieve connections from.
- */
- public AbstractQueryRunner(DataSource ds) {
- this.ds = ds;
- this.stmtConfig = null;
- }
-
- /**
- * Constructor for QueryRunner that takes a <code>StatementConfiguration</code> to configure statements when
- * preparing them.
- *
- * @param stmtConfig The configuration to apply to statements when they are prepared.
- */
- public AbstractQueryRunner(StatementConfiguration stmtConfig) {
- this.ds = null;
- this.stmtConfig = stmtConfig;
- }
-
- /**
- * Constructor to provide a <code>DataSource</code> and control the use of
- * <code>ParameterMetaData</code>. Methods that do not take a
- * <code>Connection</code> parameter will retrieve connections from this
- * <code>DataSource</code>.
- *
- * @param ds
- * The <code>DataSource</code> to retrieve connections from.
- * @param pmdKnownBroken
- * Some drivers don't support
- * {@link ParameterMetaData#getParameterType(int) }; if
- * <code>pmdKnownBroken</code> is set to true, we won't even try
- * it; if false, we'll try it, and if it breaks, we'll remember
- * not to use it again.
- */
- public AbstractQueryRunner(DataSource ds, boolean pmdKnownBroken) {
- this.pmdKnownBroken = pmdKnownBroken;
- this.ds = ds;
- this.stmtConfig = null;
- }
-
- /**
- * Constructor for QueryRunner that takes a <code>DataSource</code> to use and a <code>StatementConfiguration</code>.
- *
- * Methods that do not take a <code>Connection</code> parameter will retrieve connections from this
- * <code>DataSource</code>.
- *
- * @param ds The <code>DataSource</code> to retrieve connections from.
- * @param stmtConfig The configuration to apply to statements when they are prepared.
- */
- public AbstractQueryRunner(DataSource ds, StatementConfiguration stmtConfig) {
- this.ds = ds;
- this.stmtConfig = stmtConfig;
- }
-
- /**
- * Constructor for QueryRunner that takes a <code>DataSource</code>, a <code>StatementConfiguration</code>, and
- * controls the use of <code>ParameterMetaData</code>. Methods that do not take a <code>Connection</code> parameter
- * will retrieve connections from this <code>DataSource</code>.
- *
- * @param ds The <code>DataSource</code> to retrieve connections from.
- * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) };
- * if <code>pmdKnownBroken</code> is set to true, we won't even try it; if false, we'll try it,
- * and if it breaks, we'll remember not to use it again.
- * @param stmtConfig The configuration to apply to statements when they are prepared.
- */
- public AbstractQueryRunner(DataSource ds, boolean pmdKnownBroken, StatementConfiguration stmtConfig) {
- this.pmdKnownBroken = pmdKnownBroken;
- this.ds = ds;
- this.stmtConfig = stmtConfig;
- }
-
- /**
- * Returns the <code>DataSource</code> this runner is using.
- * <code>QueryRunner</code> methods always call this method to get the
- * <code>DataSource</code> so subclasses can provide specialized behavior.
- *
- * @return DataSource the runner is using
- */
- public DataSource getDataSource() {
- return this.ds;
- }
-
- /**
- * Some drivers don't support
- * {@link ParameterMetaData#getParameterType(int) }; if
- * <code>pmdKnownBroken</code> is set to true, we won't even try it; if
- * false, we'll try it, and if it breaks, we'll remember not to use it
- * again.
- *
- * @return the flag to skip (or not)
- * {@link ParameterMetaData#getParameterType(int) }
- * @since 1.4
- */
- public boolean isPmdKnownBroken() {
- return pmdKnownBroken;
- }
-
- /**
- * Factory method that creates and initializes a
- * <code>PreparedStatement</code> object for the given SQL.
- * <code>QueryRunner</code> methods always call this method to prepare
- * statements for them. Subclasses can override this method to provide
- * special PreparedStatement configuration if needed. This implementation
- * simply calls <code>conn.prepareStatement(sql)</code>.
- *
- * @param conn
- * The <code>Connection</code> used to create the
- * <code>PreparedStatement</code>
- * @param sql
- * The SQL statement to prepare.
- * @return An initialized <code>PreparedStatement</code>.
- * @throws SQLException
- * if a database access error occurs
- */
- protected PreparedStatement prepareStatement(Connection conn, String sql)
- throws SQLException {
-
- PreparedStatement ps = conn.prepareStatement(sql);
- try {
- configureStatement(ps);
- } catch (SQLException e) {
- ps.close();
- throw e;
- }
- return ps;
- }
-
- /**
- * Factory method that creates and initializes a
- * <code>PreparedStatement</code> object for the given SQL.
- * <code>QueryRunner</code> methods always call this method to prepare
- * statements for them. Subclasses can override this method to provide
- * special PreparedStatement configuration if needed. This implementation
- * simply calls <code>conn.prepareStatement(sql, returnedKeys)</code>
- * which will result in the ability to retrieve the automatically-generated
- * keys from an auto_increment column.
- *
- * @param conn
- * The <code>Connection</code> used to create the
- * <code>PreparedStatement</code>
- * @param sql
- * The SQL statement to prepare.
- * @param returnedKeys
- * Flag indicating whether to return generated keys or not.
- *
- * @return An initialized <code>PreparedStatement</code>.
- * @throws SQLException
- * if a database access error occurs
- * @since 1.6
- */
- protected PreparedStatement prepareStatement(Connection conn, String sql, int returnedKeys)
- throws SQLException {
-
- PreparedStatement ps = conn.prepareStatement(sql, returnedKeys);
- try {
- configureStatement(ps);
- } catch (SQLException e) {
- ps.close();
- throw e;
- }
- return ps;
- }
-
- private void configureStatement(Statement stmt) throws SQLException {
-
- if (stmtConfig != null) {
- if (stmtConfig.isFetchDirectionSet()) {
- stmt.setFetchDirection(stmtConfig.getFetchDirection());
- }
-
- if (stmtConfig.isFetchSizeSet()) {
- stmt.setFetchSize(stmtConfig.getFetchSize());
- }
-
- if (stmtConfig.isMaxFieldSizeSet()) {
- stmt.setMaxFieldSize(stmtConfig.getMaxFieldSize());
- }
-
- if (stmtConfig.isMaxRowsSet()) {
- stmt.setMaxRows(stmtConfig.getMaxRows());
- }
-
- if (stmtConfig.isQueryTimeoutSet()) {
- stmt.setQueryTimeout(stmtConfig.getQueryTimeout());
- }
- }
- }
-
- /**
- * Factory method that creates and initializes a
- * <code>CallableStatement</code> object for the given SQL.
- * <code>QueryRunner</code> methods always call this method to prepare
- * callable statements for them. Subclasses can override this method to
- * provide special CallableStatement configuration if needed. This
- * implementation simply calls <code>conn.prepareCall(sql)</code>.
- *
- * @param conn
- * The <code>Connection</code> used to create the
- * <code>CallableStatement</code>
- * @param sql
- * The SQL statement to prepare.
- * @return An initialized <code>CallableStatement</code>.
- * @throws SQLException
- * if a database access error occurs
- */
- protected CallableStatement prepareCall(Connection conn, String sql)
- throws SQLException {
-
- return conn.prepareCall(sql);
- }
-
- /**
- * Factory method that creates and initializes a <code>Connection</code>
- * object. <code>QueryRunner</code> methods always call this method to
- * retrieve connections from its DataSource. Subclasses can override this
- * method to provide special <code>Connection</code> configuration if
- * needed. This implementation simply calls <code>ds.getConnection()</code>.
- *
- * @return An initialized <code>Connection</code>.
- * @throws SQLException
- * if a database access error occurs
- * @since DbUtils 1.1
- */
- protected Connection prepareConnection() throws SQLException {
- if (this.getDataSource() == null) {
- throw new SQLException(
- "QueryRunner requires a DataSource to be "
- + "invoked in this way, or a Connection should be passed in");
- }
- return this.getDataSource().getConnection();
- }
-
- /**
- * Fill the <code>PreparedStatement</code> replacement parameters with the
- * given objects.
- *
- * @param stmt
- * PreparedStatement to fill
- * @param params
- * Query replacement parameters; <code>null</code> is a valid
- * value to pass in.
- * @throws SQLException
- * if a database access error occurs
- */
- public void fillStatement(PreparedStatement stmt, Object... params)
- throws SQLException {
-
- // check the parameter count, if we can
- ParameterMetaData pmd = null;
- if (!pmdKnownBroken) {
- try {
- pmd = stmt.getParameterMetaData();
- if (pmd == null) { // can be returned by implementations that don't support the method
- pmdKnownBroken = true;
- } else {
- int stmtCount = pmd.getParameterCount();
- int paramsCount = params == null ? 0 : params.length;
-
- if (stmtCount != paramsCount) {
- throw new SQLException("Wrong number of parameters: expected "
- + stmtCount + ", was given " + paramsCount);
- }
- }
- } catch (SQLFeatureNotSupportedException ex) {
- pmdKnownBroken = true;
- }
- // TODO see DBUTILS-117: would it make sense to catch any other SQLEx types here?
- }
-
- // nothing to do here
- if (params == null) {
- return;
- }
-
- CallableStatement call = null;
- if (stmt instanceof CallableStatement) {
- call = (CallableStatement) stmt;
- }
-
- for (int i = 0; i < params.length; i++) {
- if (params[i] != null) {
- if (call != null && params[i] instanceof OutParameter) {
- ((OutParameter)params[i]).register(call, i + 1);
- } else {
- stmt.setObject(i + 1, params[i]);
- }
- } else {
- // VARCHAR works with many drivers regardless
- // of the actual column type. Oddly, NULL and
- // OTHER don't work with Oracle's drivers.
- int sqlType = Types.VARCHAR;
- if (!pmdKnownBroken) {
- // TODO see DBUTILS-117: does it make sense to catch SQLEx here?
- try {
- /*
- * It's not possible for pmdKnownBroken to change from
- * true to false, (once true, always true) so pmd cannot
- * be null here.
- */
- sqlType = pmd.getParameterType(i + 1);
- } catch (SQLException e) {
- pmdKnownBroken = true;
- }
- }
- stmt.setNull(i + 1, sqlType);
- }
- }
- }
-
- /**
- * Fill the <code>PreparedStatement</code> replacement parameters with the
- * given object's bean property values.
- *
- * @param stmt
- * PreparedStatement to fill
- * @param bean
- * a JavaBean object
- * @param properties
- * an ordered array of properties; this gives the order to insert
- * values in the statement
- * @throws SQLException
- * if a database access error occurs
- */
- public void fillStatementWithBean(PreparedStatement stmt, Object bean,
- PropertyDescriptor[] properties) throws SQLException {
- Object[] params = new Object[properties.length];
- for (int i = 0; i < properties.length; i++) {
- PropertyDescriptor property = properties[i];
- Object value = null;
- Method method = property.getReadMethod();
- if (method == null) {
- throw new RuntimeException("No read method for bean property "
- + bean.getClass() + " " + property.getName());
- }
- try {
- value = method.invoke(bean, new Object[0]);
- } catch (InvocationTargetException e) {
- throw new RuntimeException("Couldn't invoke method: " + method,
- e);
- } catch (IllegalArgumentException e) {
- throw new RuntimeException(
- "Couldn't invoke method with 0 arguments: " + method, e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException("Couldn't invoke method: " + method,
- e);
- }
- params[i] = value;
- }
- fillStatement(stmt, params);
- }
-
- /**
- * Fill the <code>PreparedStatement</code> replacement parameters with the
- * given object's bean property values.
- *
- * @param stmt
- * PreparedStatement to fill
- * @param bean
- * A JavaBean object
- * @param propertyNames
- * An ordered array of property names (these should match the
- * getters/setters); this gives the order to insert values in the
- * statement
- * @throws SQLException
- * If a database access error occurs
- */
- public void fillStatementWithBean(PreparedStatement stmt, Object bean,
- String... propertyNames) throws SQLException {
- PropertyDescriptor[] descriptors;
- try {
- descriptors = Introspector.getBeanInfo(bean.getClass())
- .getPropertyDescriptors();
- } catch (IntrospectionException e) {
- throw new RuntimeException("Couldn't introspect bean "
- + bean.getClass().toString(), e);
- }
- PropertyDescriptor[] sorted = new PropertyDescriptor[propertyNames.length];
- for (int i = 0; i < propertyNames.length; i++) {
- String propertyName = propertyNames[i];
- if (propertyName == null) {
- throw new NullPointerException("propertyName can't be null: "
- + i);
- }
- boolean found = false;
- for (int j = 0; j < descriptors.length; j++) {
- PropertyDescriptor descriptor = descriptors[j];
- if (propertyName.equals(descriptor.getName())) {
- sorted[i] = descriptor;
- found = true;
- break;
- }
- }
- if (!found) {
- throw new RuntimeException("Couldn't find bean property: "
- + bean.getClass() + " " + propertyName);
- }
- }
- fillStatementWithBean(stmt, bean, sorted);
- }
-
- /**
- * Throws a new exception with a more informative error message.
- *
- * @param cause
- * The original exception that will be chained to the new
- * exception when it's rethrown.
- *
- * @param sql
- * The query that was executing when the exception happened.
- *
- * @param params
- * The query replacement parameters; <code>null</code> is a valid
- * value to pass in.
- *
- * @throws SQLException
- * if a database access error occurs
- */
- protected void rethrow(SQLException cause, String sql, Object... params)
- throws SQLException {
-
- String causeMessage = cause.getMessage();
- if (causeMessage == null) {
- causeMessage = "";
- }
- StringBuffer msg = new StringBuffer(causeMessage);
-
- msg.append(" Query: ");
- msg.append(sql);
- msg.append(" Parameters: ");
-
- if (params == null) {
- msg.append("[]");
- } else {
- msg.append(Arrays.deepToString(params));
- }
-
- SQLException e = new SQLException(msg.toString(), cause.getSQLState(),
- cause.getErrorCode());
- e.setNextException(cause);
-
- throw e;
- }
-
- /**
- * Wrap the <code>ResultSet</code> in a decorator before processing it. This
- * implementation returns the <code>ResultSet</code> it is given without any
- * decoration.
- *
- * <p>
- * Often, the implementation of this method can be done in an anonymous
- * inner class like this:
- * </p>
- *
- * <pre>
- * QueryRunner run = new QueryRunner() {
- * protected ResultSet wrap(ResultSet rs) {
- * return StringTrimmedResultSet.wrap(rs);
- * }
- * };
- * </pre>
- *
- * @param rs
- * The <code>ResultSet</code> to decorate; never
- * <code>null</code>.
- * @return The <code>ResultSet</code> wrapped in some decorator.
- */
- protected ResultSet wrap(ResultSet rs) {
- return rs;
- }
-
- /**
- * Close a <code>Connection</code>. This implementation avoids closing if
- * null and does <strong>not</strong> suppress any exceptions. Subclasses
- * can override to provide special handling like logging.
- *
- * @param conn
- * Connection to close
- * @throws SQLException
- * if a database access error occurs
- * @since DbUtils 1.1
- */
- protected void close(Connection conn) throws SQLException {
- DbUtils.close(conn);
- }
-
- /**
- * Close a <code>Statement</code>. This implementation avoids closing if
- * null and does <strong>not</strong> suppress any exceptions. Subclasses
- * can override to provide special handling like logging.
- *
- * @param stmt
- * Statement to close
- * @throws SQLException
- * if a database access error occurs
- * @since DbUtils 1.1
- */
- protected void close(Statement stmt) throws SQLException {
- DbUtils.close(stmt);
- }
-
- /**
- * Close a <code>ResultSet</code>. This implementation avoids closing if
- * null and does <strong>not</strong> suppress any exceptions. Subclasses
- * can override to provide special handling like logging.
- *
- * @param rs
- * ResultSet to close
- * @throws SQLException
- * if a database access error occurs
- * @since DbUtils 1.1
- */
- protected void close(ResultSet rs) throws SQLException {
- DbUtils.close(rs);
- }
-
-}
+/*
+ * 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.dbutils;
+
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.ParameterMetaData;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.Statement;
+import java.sql.Types;
+import java.util.Arrays;
+
+import javax.sql.DataSource;
+
+/**
+ * The base class for QueryRunner & AsyncQueryRunner. This class is thread safe.
+ *
+ * @since 1.4 (mostly extracted from QueryRunner)
+ */
+public abstract class AbstractQueryRunner {
+ /**
+ * Is {@link ParameterMetaData#getParameterType(int)} broken (have we tried
+ * it yet)?
+ */
+ private volatile boolean pmdKnownBroken = false;
+
+ /**
+ * The DataSource to retrieve connections from.
+ * @deprecated Access to this field should be through {@link #getDataSource()}.
+ */
+ @Deprecated
+ protected final DataSource ds;
+
+ /**
+ * Configuration to use when preparing statements.
+ */
+ private final StatementConfiguration stmtConfig;
+
+ /**
+ * Default constructor, sets pmdKnownBroken to false, ds to null and stmtConfig to null.
+ */
+ public AbstractQueryRunner() {
+ ds = null;
+ this.stmtConfig = null;
+ }
+
+ /**
+ * Constructor to control the use of <code>ParameterMetaData</code>.
+ *
+ * @param pmdKnownBroken
+ * Some drivers don't support
+ * {@link ParameterMetaData#getParameterType(int) }; if
+ * <code>pmdKnownBroken</code> is set to true, we won't even try
+ * it; if false, we'll try it, and if it breaks, we'll remember
+ * not to use it again.
+ */
+ public AbstractQueryRunner(boolean pmdKnownBroken) {
+ this.pmdKnownBroken = pmdKnownBroken;
+ ds = null;
+ this.stmtConfig = null;
+ }
+
+ /**
+ * Constructor to provide a <code>DataSource</code>. Methods that do not
+ * take a <code>Connection</code> parameter will retrieve connections from
+ * this <code>DataSource</code>.
+ *
+ * @param ds
+ * The <code>DataSource</code> to retrieve connections from.
+ */
+ public AbstractQueryRunner(DataSource ds) {
+ this.ds = ds;
+ this.stmtConfig = null;
+ }
+
+ /**
+ * Constructor for QueryRunner that takes a <code>StatementConfiguration</code> to configure statements when
+ * preparing them.
+ *
+ * @param stmtConfig The configuration to apply to statements when they are prepared.
+ */
+ public AbstractQueryRunner(StatementConfiguration stmtConfig) {
+ this.ds = null;
+ this.stmtConfig = stmtConfig;
+ }
+
+ /**
+ * Constructor to provide a <code>DataSource</code> and control the use of
+ * <code>ParameterMetaData</code>. Methods that do not take a
+ * <code>Connection</code> parameter will retrieve connections from this
+ * <code>DataSource</code>.
+ *
+ * @param ds
+ * The <code>DataSource</code> to retrieve connections from.
+ * @param pmdKnownBroken
+ * Some drivers don't support
+ * {@link ParameterMetaData#getParameterType(int) }; if
+ * <code>pmdKnownBroken</code> is set to true, we won't even try
+ * it; if false, we'll try it, and if it breaks, we'll remember
+ * not to use it again.
+ */
+ public AbstractQueryRunner(DataSource ds, boolean pmdKnownBroken) {
+ this.pmdKnownBroken = pmdKnownBroken;
+ this.ds = ds;
+ this.stmtConfig = null;
+ }
+
+ /**
+ * Constructor for QueryRunner that takes a <code>DataSource</code> to use and a <code>StatementConfiguration</code>.
+ *
+ * Methods that do not take a <code>Connection</code> parameter will retrieve connections from this
+ * <code>DataSource</code>.
+ *
+ * @param ds The <code>DataSource</code> to retrieve connections from.
+ * @param stmtConfig The configuration to apply to statements when they are prepared.
+ */
+ public AbstractQueryRunner(DataSource ds, StatementConfiguration stmtConfig) {
+ this.ds = ds;
+ this.stmtConfig = stmtConfig;
+ }
+
+ /**
+ * Constructor for QueryRunner that takes a <code>DataSource</code>, a <code>StatementConfiguration</code>, and
+ * controls the use of <code>ParameterMetaData</code>. Methods that do not take a <code>Connection</code> parameter
+ * will retrieve connections from this <code>DataSource</code>.
+ *
+ * @param ds The <code>DataSource</code> to retrieve connections from.
+ * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) };
+ * if <code>pmdKnownBroken</code> is set to true, we won't even try it; if false, we'll try it,
+ * and if it breaks, we'll remember not to use it again.
+ * @param stmtConfig The configuration to apply to statements when they are prepared.
+ */
+ public AbstractQueryRunner(DataSource ds, boolean pmdKnownBroken, StatementConfiguration stmtConfig) {
+ this.pmdKnownBroken = pmdKnownBroken;
+ this.ds = ds;
+ this.stmtConfig = stmtConfig;
+ }
+
+ /**
+ * Returns the <code>DataSource</code> this runner is using.
+ * <code>QueryRunner</code> methods always call this method to get the
+ * <code>DataSource</code> so subclasses can provide specialized behavior.
+ *
+ * @return DataSource the runner is using
+ */
+ public DataSource getDataSource() {
+ return this.ds;
+ }
+
+ /**
+ * Some drivers don't support
+ * {@link ParameterMetaData#getParameterType(int) }; if
+ * <code>pmdKnownBroken</code> is set to true, we won't even try it; if
+ * false, we'll try it, and if it breaks, we'll remember not to use it
+ * again.
+ *
+ * @return the flag to skip (or not)
+ * {@link ParameterMetaData#getParameterType(int) }
+ * @since 1.4
+ */
+ public boolean isPmdKnownBroken() {
+ return pmdKnownBroken;
+ }
+
+ /**
+ * Factory method that creates and initializes a
+ * <code>PreparedStatement</code> object for the given SQL.
+ * <code>QueryRunner</code> methods always call this method to prepare
+ * statements for them. Subclasses can override this method to provide
+ * special PreparedStatement configuration if needed. This implementation
+ * simply calls <code>conn.prepareStatement(sql)</code>.
+ *
+ * @param conn
+ * The <code>Connection</code> used to create the
+ * <code>PreparedStatement</code>
+ * @param sql
+ * The SQL statement to prepare.
+ * @return An initialized <code>PreparedStatement</code>.
+ * @throws SQLException
+ * if a database access error occurs
+ */
+ protected PreparedStatement prepareStatement(Connection conn, String sql)
+ throws SQLException {
+
+ PreparedStatement ps = conn.prepareStatement(sql);
+ try {
+ configureStatement(ps);
+ } catch (SQLException e) {
+ ps.close();
+ throw e;
+ }
+ return ps;
+ }
+
+ /**
+ * Factory method that creates and initializes a
+ * <code>PreparedStatement</code> object for the given SQL.
+ * <code>QueryRunner</code> methods always call this method to prepare
+ * statements for them. Subclasses can override this method to provide
+ * special PreparedStatement configuration if needed. This implementation
+ * simply calls <code>conn.prepareStatement(sql, returnedKeys)</code>
+ * which will result in the ability to retrieve the automatically-generated
+ * keys from an auto_increment column.
+ *
+ * @param conn
+ * The <code>Connection</code> used to create the
+ * <code>PreparedStatement</code>
+ * @param sql
+ * The SQL statement to prepare.
+ * @param returnedKeys
+ * Flag indicating whether to return generated keys or not.
+ *
+ * @return An initialized <code>PreparedStatement</code>.
+ * @throws SQLException
+ * if a database access error occurs
+ * @since 1.6
+ */
+ protected PreparedStatement prepareStatement(Connection conn, String sql, int returnedKeys)
+ throws SQLException {
+
+ PreparedStatement ps = conn.prepareStatement(sql, returnedKeys);
+ try {
+ configureStatement(ps);
+ } catch (SQLException e) {
+ ps.close();
+ throw e;
+ }
+ return ps;
+ }
+
+ private void configureStatement(Statement stmt) throws SQLException {
+
+ if (stmtConfig != null) {
+ if (stmtConfig.isFetchDirectionSet()) {
+ stmt.setFetchDirection(stmtConfig.getFetchDirection());
+ }
+
+ if (stmtConfig.isFetchSizeSet()) {
+ stmt.setFetchSize(stmtConfig.getFetchSize());
+ }
+
+ if (stmtConfig.isMaxFieldSizeSet()) {
+ stmt.setMaxFieldSize(stmtConfig.getMaxFieldSize());
+ }
+
+ if (stmtConfig.isMaxRowsSet()) {
+ stmt.setMaxRows(stmtConfig.getMaxRows());
+ }
+
+ if (stmtConfig.isQueryTimeoutSet()) {
+ stmt.setQueryTimeout(stmtConfig.getQueryTimeout());
+ }
+ }
+ }
+
+ /**
+ * Factory method that creates and initializes a
+ * <code>CallableStatement</code> object for the given SQL.
+ * <code>QueryRunner</code> methods always call this method to prepare
+ * callable statements for them. Subclasses can override this method to
+ * provide special CallableStatement configuration if needed. This
+ * implementation simply calls <code>conn.prepareCall(sql)</code>.
+ *
+ * @param conn
+ * The <code>Connection</code> used to create the
+ * <code>CallableStatement</code>
+ * @param sql
+ * The SQL statement to prepare.
+ * @return An initialized <code>CallableStatement</code>.
+ * @throws SQLException
+ * if a database access error occurs
+ */
+ protected CallableStatement prepareCall(Connection conn, String sql)
+ throws SQLException {
+
+ return conn.prepareCall(sql);
+ }
+
+ /**
+ * Factory method that creates and initializes a <code>Connection</code>
+ * object. <code>QueryRunner</code> methods always call this method to
+ * retrieve connections from its DataSource. Subclasses can override this
+ * method to provide special <code>Connection</code> configuration if
+ * needed. This implementation simply calls <code>ds.getConnection()</code>.
+ *
+ * @return An initialized <code>Connection</code>.
+ * @throws SQLException
+ * if a database access error occurs
+ * @since DbUtils 1.1
+ */
+ protected Connection prepareConnection() throws SQLException {
+ if (this.getDataSource() == null) {
+ throw new SQLException(
+ "QueryRunner requires a DataSource to be "
+ + "invoked in this way, or a Connection should be passed in");
+ }
+ return this.getDataSource().getConnection();
+ }
+
+ /**
+ * Fill the <code>PreparedStatement</code> replacement parameters with the
+ * given objects.
+ *
+ * @param stmt
+ * PreparedStatement to fill
+ * @param params
+ * Query replacement parameters; <code>null</code> is a valid
+ * value to pass in.
+ * @throws SQLException
+ * if a database access error occurs
+ */
+ public void fillStatement(PreparedStatement stmt, Object... params)
+ throws SQLException {
+
+ // check the parameter count, if we can
+ ParameterMetaData pmd = null;
+ if (!pmdKnownBroken) {
+ try {
+ pmd = stmt.getParameterMetaData();
+ if (pmd == null) { // can be returned by implementations that don't support the method
+ pmdKnownBroken = true;
+ } else {
+ int stmtCount = pmd.getParameterCount();
+ int paramsCount = params == null ? 0 : params.length;
+
+ if (stmtCount != paramsCount) {
+ throw new SQLException("Wrong number of parameters: expected "
+ + stmtCount + ", was given " + paramsCount);
+ }
+ }
+ } catch (SQLFeatureNotSupportedException ex) {
+ pmdKnownBroken = true;
+ }
+ // TODO see DBUTILS-117: would it make sense to catch any other SQLEx types here?
+ }
+
+ // nothing to do here
+ if (params == null) {
+ return;
+ }
+
+ CallableStatement call = null;
+ if (stmt instanceof CallableStatement) {
+ call = (CallableStatement) stmt;
+ }
+
+ for (int i = 0; i < params.length; i++) {
+ if (params[i] != null) {
+ if (call != null && params[i] instanceof OutParameter) {
+ ((OutParameter)params[i]).register(call, i + 1);
+ } else {
+ stmt.setObject(i + 1, params[i]);
+ }
+ } else {
+ // VARCHAR works with many drivers regardless
+ // of the actual column type. Oddly, NULL and
+ // OTHER don't work with Oracle's drivers.
+ int sqlType = Types.VARCHAR;
+ if (!pmdKnownBroken) {
+ // TODO see DBUTILS-117: does it make sense to catch SQLEx here?
+ try {
+ /*
+ * It's not possible for pmdKnownBroken to change from
+ * true to false, (once true, always true) so pmd cannot
+ * be null here.
+ */
+ sqlType = pmd.getParameterType(i + 1);
+ } catch (SQLException e) {
+ pmdKnownBroken = true;
+ }
+ }
+ stmt.setNull(i + 1, sqlType);
+ }
+ }
+ }
+
+ /**
+ * Fill the <code>PreparedStatement</code> replacement parameters with the
+ * given object's bean property values.
+ *
+ * @param stmt
+ * PreparedStatement to fill
+ * @param bean
+ * a JavaBean object
+ * @param properties
+ * an ordered array of properties; this gives the order to insert
+ * values in the statement
+ * @throws SQLException
+ * if a database access error occurs
+ */
+ public void fillStatementWithBean(PreparedStatement stmt, Object bean,
+ PropertyDescriptor[] properties) throws SQLException {
+ Object[] params = new Object[properties.length];
+ for (int i = 0; i < properties.length; i++) {
+ PropertyDescriptor property = properties[i];
+ Object value = null;
+ Method method = property.getReadMethod();
+ if (method == null) {
+ throw new RuntimeException("No read method for bean property "
+ + bean.getClass() + " " + property.getName());
+ }
+ try {
+ value = method.invoke(bean, new Object[0]);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException("Couldn't invoke method: " + method,
+ e);
+ } catch (IllegalArgumentException e) {
+ throw new RuntimeException(
+ "Couldn't invoke method with 0 arguments: " + method, e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException("Couldn't invoke method: " + method,
+ e);
+ }
+ params[i] = value;
+ }
+ fillStatement(stmt, params);
+ }
+
+ /**
+ * Fill the <code>PreparedStatement</code> replacement parameters with the
+ * given object's bean property values.
+ *
+ * @param stmt
+ * PreparedStatement to fill
+ * @param bean
+ * A JavaBean object
+ * @param propertyNames
+ * An ordered array of property names (these should match the
+ * getters/setters); this gives the order to insert values in the
+ * statement
+ * @throws SQLException
+ * If a database access error occurs
+ */
+ public void fillStatementWithBean(PreparedStatement stmt, Object bean,
+ String... propertyNames) throws SQLException {
+ PropertyDescriptor[] descriptors;
+ try {
+ descriptors = Introspector.getBeanInfo(bean.getClass())
+ .getPropertyDescriptors();
+ } catch (IntrospectionException e) {
+ throw new RuntimeException("Couldn't introspect bean "
+ + bean.getClass().toString(), e);
+ }
+ PropertyDescriptor[] sorted = new PropertyDescriptor[propertyNames.length];
+ for (int i = 0; i < propertyNames.length; i++) {
+ String propertyName = propertyNames[i];
+ if (propertyName == null) {
+ throw new NullPointerException("propertyName can't be null: "
+ + i);
+ }
+ boolean found = false;
+ for (int j = 0; j < descriptors.length; j++) {
+ PropertyDescriptor descriptor = descriptors[j];
+ if (propertyName.equals(descriptor.getName())) {
+ sorted[i] = descriptor;
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ throw new RuntimeException("Couldn't find bean property: "
+ + bean.getClass() + " " + propertyName);
+ }
+ }
+ fillStatementWithBean(stmt, bean, sorted);
+ }
+
+ /**
+ * Throws a new exception with a more informative error message.
+ *
+ * @param cause
+ * The original exception that will be chained to the new
+ * exception when it's rethrown.
+ *
+ * @param sql
+ * The query that was executing when the exception happened.
+ *
+ * @param params
+ * The query replacement parameters; <code>null</code> is a valid
+ * value to pass in.
+ *
+ * @throws SQLException
+ * if a database access error occurs
+ */
+ protected void rethrow(SQLException cause, String sql, Object... params)
+ throws SQLException {
+
+ String causeMessage = cause.getMessage();
+ if (causeMessage == null) {
+ causeMessage = "";
+ }
+ StringBuffer msg = new StringBuffer(causeMessage);
+
+ msg.append(" Query: ");
+ msg.append(sql);
+ msg.append(" Parameters: ");
+
+ if (params == null) {
+ msg.append("[]");
+ } else {
+ msg.append(Arrays.deepToString(params));
+ }
+
+ SQLException e = new SQLException(msg.toString(), cause.getSQLState(),
+ cause.getErrorCode());
+ e.setNextException(cause);
+
+ throw e;
+ }
+
+ /**
+ * Wrap the <code>ResultSet</code> in a decorator before processing it. This
+ * implementation returns the <code>ResultSet</code> it is given without any
+ * decoration.
+ *
+ * <p>
+ * Often, the implementation of this method can be done in an anonymous
+ * inner class like this:
+ * </p>
+ *
+ * <pre>
+ * QueryRunner run = new QueryRunner() {
+ * protected ResultSet wrap(ResultSet rs) {
+ * return StringTrimmedResultSet.wrap(rs);
+ * }
+ * };
+ * </pre>
+ *
+ * @param rs
+ * The <code>ResultSet</code> to decorate; never
+ * <code>null</code>.
+ * @return The <code>ResultSet</code> wrapped in some decorator.
+ */
+ protected ResultSet wrap(ResultSet rs) {
+ return rs;
+ }
+
+ /**
+ * Close a <code>Connection</code>. This implementation avoids closing if
+ * null and does <strong>not</strong> suppress any exceptions. Subclasses
+ * can override to provide special handling like logging.
+ *
+ * @param conn
+ * Connection to close
+ * @throws SQLException
+ * if a database access error occurs
+ * @since DbUtils 1.1
+ */
+ protected void close(Connection conn) throws SQLException {
+ DbUtils.close(conn);
+ }
+
+ /**
+ * Close a <code>Statement</code>. This implementation avoids closing if
+ * null and does <strong>not</strong> suppress any exceptions. Subclasses
+ * can override to provide special handling like logging.
+ *
+ * @param stmt
+ * Statement to close
+ * @throws SQLException
+ * if a database access error occurs
+ * @since DbUtils 1.1
+ */
+ protected void close(Statement stmt) throws SQLException {
+ DbUtils.close(stmt);
+ }
+
+ /**
+ * Close a <code>ResultSet</code>. This implementation avoids closing if
+ * null and does <strong>not</strong> suppress any exceptions. Subclasses
+ * can override to provide special handling like logging.
+ *
+ * @param rs
+ * ResultSet to close
+ * @throws SQLException
+ * if a database access error occurs
+ * @since DbUtils 1.1
+ */
+ protected void close(ResultSet rs) throws SQLException {
+ DbUtils.close(rs);
+ }
+
+}
[4/5] commons-dbutils git commit: Remove trailing white spaces on all
lines.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/main/java/org/apache/commons/dbutils/BeanProcessor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils/BeanProcessor.java b/src/main/java/org/apache/commons/dbutils/BeanProcessor.java
index e18dc01..b15920f 100644
--- a/src/main/java/org/apache/commons/dbutils/BeanProcessor.java
+++ b/src/main/java/org/apache/commons/dbutils/BeanProcessor.java
@@ -1,534 +1,534 @@
-/*
- * 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.dbutils;
-
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.ServiceLoader;
-
-/**
- * <p>
- * <code>BeanProcessor</code> matches column names to bean property names
- * and converts <code>ResultSet</code> columns into objects for those bean
- * properties. Subclasses should override the methods in the processing chain
- * to customize behavior.
- * </p>
- *
- * <p>
- * This class is thread-safe.
- * </p>
- *
- * @see BasicRowProcessor
- *
- * @since DbUtils 1.1
- */
-public class BeanProcessor {
-
- /**
- * Special array value used by <code>mapColumnsToProperties</code> that
- * indicates there is no bean property that matches a column from a
- * <code>ResultSet</code>.
- */
- protected static final int PROPERTY_NOT_FOUND = -1;
-
- /**
- * Set a bean's primitive properties to these defaults when SQL NULL
- * is returned. These are the same as the defaults that ResultSet get*
- * methods return in the event of a NULL column.
- */
- private static final Map<Class<?>, Object> primitiveDefaults = new HashMap<Class<?>, Object>();
-
- private static final List<ColumnHandler> columnHandlers = new ArrayList<ColumnHandler>();
-
- private static final List<PropertyHandler> propertyHandlers = new ArrayList<PropertyHandler>();
-
- /**
- * ResultSet column to bean property name overrides.
- */
- private final Map<String, String> columnToPropertyOverrides;
-
- static {
- primitiveDefaults.put(Integer.TYPE, Integer.valueOf(0));
- primitiveDefaults.put(Short.TYPE, Short.valueOf((short) 0));
- primitiveDefaults.put(Byte.TYPE, Byte.valueOf((byte) 0));
- primitiveDefaults.put(Float.TYPE, Float.valueOf(0f));
- primitiveDefaults.put(Double.TYPE, Double.valueOf(0d));
- primitiveDefaults.put(Long.TYPE, Long.valueOf(0L));
- primitiveDefaults.put(Boolean.TYPE, Boolean.FALSE);
- primitiveDefaults.put(Character.TYPE, Character.valueOf((char) 0));
-
- // Use a ServiceLoader to find implementations
- for (ColumnHandler handler : ServiceLoader.load(ColumnHandler.class)) {
- columnHandlers.add(handler);
- }
-
- // Use a ServiceLoader to find implementations
- for (PropertyHandler handler : ServiceLoader.load(PropertyHandler.class)) {
- propertyHandlers.add(handler);
- }
- }
-
- /**
- * Constructor for BeanProcessor.
- */
- public BeanProcessor() {
- this(new HashMap<String, String>());
- }
-
- /**
- * Constructor for BeanProcessor configured with column to property name overrides.
- *
- * @param columnToPropertyOverrides ResultSet column to bean property name overrides
- * @since 1.5
- */
- public BeanProcessor(Map<String, String> columnToPropertyOverrides) {
- super();
- if (columnToPropertyOverrides == null) {
- throw new IllegalArgumentException("columnToPropertyOverrides map cannot be null");
- }
- this.columnToPropertyOverrides = columnToPropertyOverrides;
- }
-
- /**
- * Convert a <code>ResultSet</code> row into a JavaBean. This
- * implementation uses reflection and <code>BeanInfo</code> classes to
- * match column names to bean property names. Properties are matched to
- * columns based on several factors:
- * <br/>
- * <ol>
- * <li>
- * The class has a writable property with the same name as a column.
- * The name comparison is case insensitive.
- * </li>
- *
- * <li>
- * The column type can be converted to the property's set method
- * parameter type with a ResultSet.get* method. If the conversion fails
- * (ie. the property was an int and the column was a Timestamp) an
- * SQLException is thrown.
- * </li>
- * </ol>
- *
- * <p>
- * Primitive bean properties are set to their defaults when SQL NULL is
- * returned from the <code>ResultSet</code>. Numeric fields are set to 0
- * and booleans are set to false. Object bean properties are set to
- * <code>null</code> when SQL NULL is returned. This is the same behavior
- * as the <code>ResultSet</code> get* methods.
- * </p>
- * @param <T> The type of bean to create
- * @param rs ResultSet that supplies the bean data
- * @param type Class from which to create the bean instance
- * @throws SQLException if a database access error occurs
- * @return the newly created bean
- */
- public <T> T toBean(ResultSet rs, Class<? extends T> type) throws SQLException {
- T bean = this.newInstance(type);
- return this.populateBean(rs, bean);
- }
-
- /**
- * Convert a <code>ResultSet</code> into a <code>List</code> of JavaBeans.
- * This implementation uses reflection and <code>BeanInfo</code> classes to
- * match column names to bean property names. Properties are matched to
- * columns based on several factors:
- * <br/>
- * <ol>
- * <li>
- * The class has a writable property with the same name as a column.
- * The name comparison is case insensitive.
- * </li>
- *
- * <li>
- * The column type can be converted to the property's set method
- * parameter type with a ResultSet.get* method. If the conversion fails
- * (ie. the property was an int and the column was a Timestamp) an
- * SQLException is thrown.
- * </li>
- * </ol>
- *
- * <p>
- * Primitive bean properties are set to their defaults when SQL NULL is
- * returned from the <code>ResultSet</code>. Numeric fields are set to 0
- * and booleans are set to false. Object bean properties are set to
- * <code>null</code> when SQL NULL is returned. This is the same behavior
- * as the <code>ResultSet</code> get* methods.
- * </p>
- * @param <T> The type of bean to create
- * @param rs ResultSet that supplies the bean data
- * @param type Class from which to create the bean instance
- * @throws SQLException if a database access error occurs
- * @return the newly created List of beans
- */
- public <T> List<T> toBeanList(ResultSet rs, Class<? extends T> type) throws SQLException {
- List<T> results = new ArrayList<T>();
-
- if (!rs.next()) {
- return results;
- }
-
- PropertyDescriptor[] props = this.propertyDescriptors(type);
- ResultSetMetaData rsmd = rs.getMetaData();
- int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
-
- do {
- results.add(this.createBean(rs, type, props, columnToProperty));
- } while (rs.next());
-
- return results;
- }
-
- /**
- * Creates a new object and initializes its fields from the ResultSet.
- * @param <T> The type of bean to create
- * @param rs The result set.
- * @param type The bean type (the return type of the object).
- * @param props The property descriptors.
- * @param columnToProperty The column indices in the result set.
- * @return An initialized object.
- * @throws SQLException if a database error occurs.
- */
- private <T> T createBean(ResultSet rs, Class<T> type,
- PropertyDescriptor[] props, int[] columnToProperty)
- throws SQLException {
-
- T bean = this.newInstance(type);
- return populateBean(rs, bean, props, columnToProperty);
- }
-
- /**
- * Initializes the fields of the provided bean from the ResultSet.
- * @param <T> The type of bean
- * @param rs The result set.
- * @param bean The bean to be populated.
- * @return An initialized object.
- * @throws SQLException if a database error occurs.
- */
- public <T> T populateBean(ResultSet rs, T bean) throws SQLException {
- PropertyDescriptor[] props = this.propertyDescriptors(bean.getClass());
- ResultSetMetaData rsmd = rs.getMetaData();
- int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
-
- return populateBean(rs, bean, props, columnToProperty);
- }
-
- /**
- * This method populates a bean from the ResultSet based upon the underlying meta-data.
- *
- * @param <T> The type of bean
- * @param rs The result set.
- * @param bean The bean to be populated.
- * @param props The property descriptors.
- * @param columnToProperty The column indices in the result set.
- * @return An initialized object.
- * @throws SQLException if a database error occurs.
- */
- private <T> T populateBean(ResultSet rs, T bean,
- PropertyDescriptor[] props, int[] columnToProperty)
- throws SQLException {
-
- for (int i = 1; i < columnToProperty.length; i++) {
-
- if (columnToProperty[i] == PROPERTY_NOT_FOUND) {
- continue;
- }
-
- PropertyDescriptor prop = props[columnToProperty[i]];
- Class<?> propType = prop.getPropertyType();
-
- Object value = null;
- if(propType != null) {
- value = this.processColumn(rs, i, propType);
-
- if (value == null && propType.isPrimitive()) {
- value = primitiveDefaults.get(propType);
- }
- }
-
- this.callSetter(bean, prop, value);
- }
-
- return bean;
- }
-
- /**
- * Calls the setter method on the target object for the given property.
- * If no setter method exists for the property, this method does nothing.
- * @param target The object to set the property on.
- * @param prop The property to set.
- * @param value The value to pass into the setter.
- * @throws SQLException if an error occurs setting the property.
- */
- private void callSetter(Object target, PropertyDescriptor prop, Object value)
- throws SQLException {
-
- Method setter = getWriteMethod(target, prop, value);
-
- if (setter == null || setter.getParameterTypes().length != 1) {
- return;
- }
-
- try {
- Class<?> firstParam = setter.getParameterTypes()[0];
- for (PropertyHandler handler : propertyHandlers) {
- if (handler.match(firstParam, value)) {
- value = handler.apply(firstParam, value);
- break;
- }
- }
-
- // Don't call setter if the value object isn't the right type
- if (this.isCompatibleType(value, firstParam)) {
- setter.invoke(target, new Object[]{value});
- } else {
- throw new SQLException(
- "Cannot set " + prop.getName() + ": incompatible types, cannot convert "
- + value.getClass().getName() + " to " + firstParam.getName());
- // value cannot be null here because isCompatibleType allows null
- }
-
- } catch (IllegalArgumentException e) {
- throw new SQLException(
- "Cannot set " + prop.getName() + ": " + e.getMessage());
-
- } catch (IllegalAccessException e) {
- throw new SQLException(
- "Cannot set " + prop.getName() + ": " + e.getMessage());
-
- } catch (InvocationTargetException e) {
- throw new SQLException(
- "Cannot set " + prop.getName() + ": " + e.getMessage());
- }
- }
-
- /**
- * ResultSet.getObject() returns an Integer object for an INT column. The
- * setter method for the property might take an Integer or a primitive int.
- * This method returns true if the value can be successfully passed into
- * the setter method. Remember, Method.invoke() handles the unwrapping
- * of Integer into an int.
- *
- * @param value The value to be passed into the setter method.
- * @param type The setter's parameter type (non-null)
- * @return boolean True if the value is compatible (null => true)
- */
- private boolean isCompatibleType(Object value, Class<?> type) {
- // Do object check first, then primitives
- if (value == null || type.isInstance(value) || matchesPrimitive(type, value.getClass())) {
- return true;
-
- }
- return false;
-
- }
-
- /**
- * Check whether a value is of the same primitive type as <code>targetType</code>.
- *
- * @param targetType The primitive type to target.
- * @param valueType The value to match to the primitive type.
- * @return Whether <code>valueType</code> can be coerced (e.g. autoboxed) into <code>targetType</code>.
- */
- private boolean matchesPrimitive(Class<?> targetType, Class<?> valueType) {
- if (!targetType.isPrimitive()) {
- return false;
- }
-
- try {
- // see if there is a "TYPE" field. This is present for primitive wrappers.
- Field typeField = valueType.getField("TYPE");
- Object primitiveValueType = typeField.get(valueType);
-
- if (targetType == primitiveValueType) {
- return true;
- }
- } catch (NoSuchFieldException e) {
- // lacking the TYPE field is a good sign that we're not working with a primitive wrapper.
- // we can't match for compatibility
- } catch (IllegalAccessException e) {
- // an inaccessible TYPE field is a good sign that we're not working with a primitive wrapper.
- // nothing to do. we can't match for compatibility
- }
- return false;
- }
-
- /**
- * Get the write method to use when setting {@code value} to the {@code target}.
- *
- * @param target Object where the write method will be called.
- * @param prop BeanUtils information.
- * @param value The value that will be passed to the write method.
- * @return The {@link java.lang.reflect.Method} to call on {@code target} to write {@code value} or {@code null} if
- * there is no suitable write method.
- */
- protected Method getWriteMethod(Object target, PropertyDescriptor prop, Object value) {
- Method method = prop.getWriteMethod();
- return method;
- }
-
- /**
- * Factory method that returns a new instance of the given Class. This
- * is called at the start of the bean creation process and may be
- * overridden to provide custom behavior like returning a cached bean
- * instance.
- * @param <T> The type of object to create
- * @param c The Class to create an object from.
- * @return A newly created object of the Class.
- * @throws SQLException if creation failed.
- */
- protected <T> T newInstance(Class<T> c) throws SQLException {
- try {
- return c.newInstance();
-
- } catch (InstantiationException e) {
- throw new SQLException(
- "Cannot create " + c.getName() + ": " + e.getMessage());
-
- } catch (IllegalAccessException e) {
- throw new SQLException(
- "Cannot create " + c.getName() + ": " + e.getMessage());
- }
- }
-
- /**
- * Returns a PropertyDescriptor[] for the given Class.
- *
- * @param c The Class to retrieve PropertyDescriptors for.
- * @return A PropertyDescriptor[] describing the Class.
- * @throws SQLException if introspection failed.
- */
- private PropertyDescriptor[] propertyDescriptors(Class<?> c)
- throws SQLException {
- // Introspector caches BeanInfo classes for better performance
- BeanInfo beanInfo = null;
- try {
- beanInfo = Introspector.getBeanInfo(c);
-
- } catch (IntrospectionException e) {
- throw new SQLException(
- "Bean introspection failed: " + e.getMessage());
- }
-
- return beanInfo.getPropertyDescriptors();
- }
-
- /**
- * The positions in the returned array represent column numbers. The
- * values stored at each position represent the index in the
- * <code>PropertyDescriptor[]</code> for the bean property that matches
- * the column name. If no bean property was found for a column, the
- * position is set to <code>PROPERTY_NOT_FOUND</code>.
- *
- * @param rsmd The <code>ResultSetMetaData</code> containing column
- * information.
- *
- * @param props The bean property descriptors.
- *
- * @throws SQLException if a database access error occurs
- *
- * @return An int[] with column index to property index mappings. The 0th
- * element is meaningless because JDBC column indexing starts at 1.
- */
- protected int[] mapColumnsToProperties(ResultSetMetaData rsmd,
- PropertyDescriptor[] props) throws SQLException {
-
- int cols = rsmd.getColumnCount();
- int[] columnToProperty = new int[cols + 1];
- Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);
-
- for (int col = 1; col <= cols; col++) {
- String columnName = rsmd.getColumnLabel(col);
- if (null == columnName || 0 == columnName.length()) {
- columnName = rsmd.getColumnName(col);
- }
- String propertyName = columnToPropertyOverrides.get(columnName);
- if (propertyName == null) {
- propertyName = columnName;
- }
- for (int i = 0; i < props.length; i++) {
-
- if (propertyName.equalsIgnoreCase(props[i].getName())) {
- columnToProperty[col] = i;
- break;
- }
- }
- }
-
- return columnToProperty;
- }
-
- /**
- * Convert a <code>ResultSet</code> column into an object. Simple
- * implementations could just call <code>rs.getObject(index)</code> while
- * more complex implementations could perform type manipulation to match
- * the column's type to the bean property type.
- *
- * <p>
- * This implementation calls the appropriate <code>ResultSet</code> getter
- * method for the given property type to perform the type conversion. If
- * the property type doesn't match one of the supported
- * <code>ResultSet</code> types, <code>getObject</code> is called.
- * </p>
- *
- * @param rs The <code>ResultSet</code> currently being processed. It is
- * positioned on a valid row before being passed into this method.
- *
- * @param index The current column index being processed.
- *
- * @param propType The bean property type that this column needs to be
- * converted into.
- *
- * @throws SQLException if a database access error occurs
- *
- * @return The object from the <code>ResultSet</code> at the given column
- * index after optional type processing or <code>null</code> if the column
- * value was SQL NULL.
- */
- protected Object processColumn(ResultSet rs, int index, Class<?> propType)
- throws SQLException {
-
- Object retval = rs.getObject(index);
-
- if ( !propType.isPrimitive() && retval == null ) {
- return null;
- }
-
- for (ColumnHandler handler : columnHandlers) {
- if (handler.match(propType)) {
- retval = handler.apply(rs, index);
- break;
- }
- }
-
- return retval;
-
- }
-
-}
+/*
+ * 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.dbutils;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ServiceLoader;
+
+/**
+ * <p>
+ * <code>BeanProcessor</code> matches column names to bean property names
+ * and converts <code>ResultSet</code> columns into objects for those bean
+ * properties. Subclasses should override the methods in the processing chain
+ * to customize behavior.
+ * </p>
+ *
+ * <p>
+ * This class is thread-safe.
+ * </p>
+ *
+ * @see BasicRowProcessor
+ *
+ * @since DbUtils 1.1
+ */
+public class BeanProcessor {
+
+ /**
+ * Special array value used by <code>mapColumnsToProperties</code> that
+ * indicates there is no bean property that matches a column from a
+ * <code>ResultSet</code>.
+ */
+ protected static final int PROPERTY_NOT_FOUND = -1;
+
+ /**
+ * Set a bean's primitive properties to these defaults when SQL NULL
+ * is returned. These are the same as the defaults that ResultSet get*
+ * methods return in the event of a NULL column.
+ */
+ private static final Map<Class<?>, Object> primitiveDefaults = new HashMap<Class<?>, Object>();
+
+ private static final List<ColumnHandler> columnHandlers = new ArrayList<ColumnHandler>();
+
+ private static final List<PropertyHandler> propertyHandlers = new ArrayList<PropertyHandler>();
+
+ /**
+ * ResultSet column to bean property name overrides.
+ */
+ private final Map<String, String> columnToPropertyOverrides;
+
+ static {
+ primitiveDefaults.put(Integer.TYPE, Integer.valueOf(0));
+ primitiveDefaults.put(Short.TYPE, Short.valueOf((short) 0));
+ primitiveDefaults.put(Byte.TYPE, Byte.valueOf((byte) 0));
+ primitiveDefaults.put(Float.TYPE, Float.valueOf(0f));
+ primitiveDefaults.put(Double.TYPE, Double.valueOf(0d));
+ primitiveDefaults.put(Long.TYPE, Long.valueOf(0L));
+ primitiveDefaults.put(Boolean.TYPE, Boolean.FALSE);
+ primitiveDefaults.put(Character.TYPE, Character.valueOf((char) 0));
+
+ // Use a ServiceLoader to find implementations
+ for (ColumnHandler handler : ServiceLoader.load(ColumnHandler.class)) {
+ columnHandlers.add(handler);
+ }
+
+ // Use a ServiceLoader to find implementations
+ for (PropertyHandler handler : ServiceLoader.load(PropertyHandler.class)) {
+ propertyHandlers.add(handler);
+ }
+ }
+
+ /**
+ * Constructor for BeanProcessor.
+ */
+ public BeanProcessor() {
+ this(new HashMap<String, String>());
+ }
+
+ /**
+ * Constructor for BeanProcessor configured with column to property name overrides.
+ *
+ * @param columnToPropertyOverrides ResultSet column to bean property name overrides
+ * @since 1.5
+ */
+ public BeanProcessor(Map<String, String> columnToPropertyOverrides) {
+ super();
+ if (columnToPropertyOverrides == null) {
+ throw new IllegalArgumentException("columnToPropertyOverrides map cannot be null");
+ }
+ this.columnToPropertyOverrides = columnToPropertyOverrides;
+ }
+
+ /**
+ * Convert a <code>ResultSet</code> row into a JavaBean. This
+ * implementation uses reflection and <code>BeanInfo</code> classes to
+ * match column names to bean property names. Properties are matched to
+ * columns based on several factors:
+ * <br/>
+ * <ol>
+ * <li>
+ * The class has a writable property with the same name as a column.
+ * The name comparison is case insensitive.
+ * </li>
+ *
+ * <li>
+ * The column type can be converted to the property's set method
+ * parameter type with a ResultSet.get* method. If the conversion fails
+ * (ie. the property was an int and the column was a Timestamp) an
+ * SQLException is thrown.
+ * </li>
+ * </ol>
+ *
+ * <p>
+ * Primitive bean properties are set to their defaults when SQL NULL is
+ * returned from the <code>ResultSet</code>. Numeric fields are set to 0
+ * and booleans are set to false. Object bean properties are set to
+ * <code>null</code> when SQL NULL is returned. This is the same behavior
+ * as the <code>ResultSet</code> get* methods.
+ * </p>
+ * @param <T> The type of bean to create
+ * @param rs ResultSet that supplies the bean data
+ * @param type Class from which to create the bean instance
+ * @throws SQLException if a database access error occurs
+ * @return the newly created bean
+ */
+ public <T> T toBean(ResultSet rs, Class<? extends T> type) throws SQLException {
+ T bean = this.newInstance(type);
+ return this.populateBean(rs, bean);
+ }
+
+ /**
+ * Convert a <code>ResultSet</code> into a <code>List</code> of JavaBeans.
+ * This implementation uses reflection and <code>BeanInfo</code> classes to
+ * match column names to bean property names. Properties are matched to
+ * columns based on several factors:
+ * <br/>
+ * <ol>
+ * <li>
+ * The class has a writable property with the same name as a column.
+ * The name comparison is case insensitive.
+ * </li>
+ *
+ * <li>
+ * The column type can be converted to the property's set method
+ * parameter type with a ResultSet.get* method. If the conversion fails
+ * (ie. the property was an int and the column was a Timestamp) an
+ * SQLException is thrown.
+ * </li>
+ * </ol>
+ *
+ * <p>
+ * Primitive bean properties are set to their defaults when SQL NULL is
+ * returned from the <code>ResultSet</code>. Numeric fields are set to 0
+ * and booleans are set to false. Object bean properties are set to
+ * <code>null</code> when SQL NULL is returned. This is the same behavior
+ * as the <code>ResultSet</code> get* methods.
+ * </p>
+ * @param <T> The type of bean to create
+ * @param rs ResultSet that supplies the bean data
+ * @param type Class from which to create the bean instance
+ * @throws SQLException if a database access error occurs
+ * @return the newly created List of beans
+ */
+ public <T> List<T> toBeanList(ResultSet rs, Class<? extends T> type) throws SQLException {
+ List<T> results = new ArrayList<T>();
+
+ if (!rs.next()) {
+ return results;
+ }
+
+ PropertyDescriptor[] props = this.propertyDescriptors(type);
+ ResultSetMetaData rsmd = rs.getMetaData();
+ int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
+
+ do {
+ results.add(this.createBean(rs, type, props, columnToProperty));
+ } while (rs.next());
+
+ return results;
+ }
+
+ /**
+ * Creates a new object and initializes its fields from the ResultSet.
+ * @param <T> The type of bean to create
+ * @param rs The result set.
+ * @param type The bean type (the return type of the object).
+ * @param props The property descriptors.
+ * @param columnToProperty The column indices in the result set.
+ * @return An initialized object.
+ * @throws SQLException if a database error occurs.
+ */
+ private <T> T createBean(ResultSet rs, Class<T> type,
+ PropertyDescriptor[] props, int[] columnToProperty)
+ throws SQLException {
+
+ T bean = this.newInstance(type);
+ return populateBean(rs, bean, props, columnToProperty);
+ }
+
+ /**
+ * Initializes the fields of the provided bean from the ResultSet.
+ * @param <T> The type of bean
+ * @param rs The result set.
+ * @param bean The bean to be populated.
+ * @return An initialized object.
+ * @throws SQLException if a database error occurs.
+ */
+ public <T> T populateBean(ResultSet rs, T bean) throws SQLException {
+ PropertyDescriptor[] props = this.propertyDescriptors(bean.getClass());
+ ResultSetMetaData rsmd = rs.getMetaData();
+ int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);
+
+ return populateBean(rs, bean, props, columnToProperty);
+ }
+
+ /**
+ * This method populates a bean from the ResultSet based upon the underlying meta-data.
+ *
+ * @param <T> The type of bean
+ * @param rs The result set.
+ * @param bean The bean to be populated.
+ * @param props The property descriptors.
+ * @param columnToProperty The column indices in the result set.
+ * @return An initialized object.
+ * @throws SQLException if a database error occurs.
+ */
+ private <T> T populateBean(ResultSet rs, T bean,
+ PropertyDescriptor[] props, int[] columnToProperty)
+ throws SQLException {
+
+ for (int i = 1; i < columnToProperty.length; i++) {
+
+ if (columnToProperty[i] == PROPERTY_NOT_FOUND) {
+ continue;
+ }
+
+ PropertyDescriptor prop = props[columnToProperty[i]];
+ Class<?> propType = prop.getPropertyType();
+
+ Object value = null;
+ if(propType != null) {
+ value = this.processColumn(rs, i, propType);
+
+ if (value == null && propType.isPrimitive()) {
+ value = primitiveDefaults.get(propType);
+ }
+ }
+
+ this.callSetter(bean, prop, value);
+ }
+
+ return bean;
+ }
+
+ /**
+ * Calls the setter method on the target object for the given property.
+ * If no setter method exists for the property, this method does nothing.
+ * @param target The object to set the property on.
+ * @param prop The property to set.
+ * @param value The value to pass into the setter.
+ * @throws SQLException if an error occurs setting the property.
+ */
+ private void callSetter(Object target, PropertyDescriptor prop, Object value)
+ throws SQLException {
+
+ Method setter = getWriteMethod(target, prop, value);
+
+ if (setter == null || setter.getParameterTypes().length != 1) {
+ return;
+ }
+
+ try {
+ Class<?> firstParam = setter.getParameterTypes()[0];
+ for (PropertyHandler handler : propertyHandlers) {
+ if (handler.match(firstParam, value)) {
+ value = handler.apply(firstParam, value);
+ break;
+ }
+ }
+
+ // Don't call setter if the value object isn't the right type
+ if (this.isCompatibleType(value, firstParam)) {
+ setter.invoke(target, new Object[]{value});
+ } else {
+ throw new SQLException(
+ "Cannot set " + prop.getName() + ": incompatible types, cannot convert "
+ + value.getClass().getName() + " to " + firstParam.getName());
+ // value cannot be null here because isCompatibleType allows null
+ }
+
+ } catch (IllegalArgumentException e) {
+ throw new SQLException(
+ "Cannot set " + prop.getName() + ": " + e.getMessage());
+
+ } catch (IllegalAccessException e) {
+ throw new SQLException(
+ "Cannot set " + prop.getName() + ": " + e.getMessage());
+
+ } catch (InvocationTargetException e) {
+ throw new SQLException(
+ "Cannot set " + prop.getName() + ": " + e.getMessage());
+ }
+ }
+
+ /**
+ * ResultSet.getObject() returns an Integer object for an INT column. The
+ * setter method for the property might take an Integer or a primitive int.
+ * This method returns true if the value can be successfully passed into
+ * the setter method. Remember, Method.invoke() handles the unwrapping
+ * of Integer into an int.
+ *
+ * @param value The value to be passed into the setter method.
+ * @param type The setter's parameter type (non-null)
+ * @return boolean True if the value is compatible (null => true)
+ */
+ private boolean isCompatibleType(Object value, Class<?> type) {
+ // Do object check first, then primitives
+ if (value == null || type.isInstance(value) || matchesPrimitive(type, value.getClass())) {
+ return true;
+
+ }
+ return false;
+
+ }
+
+ /**
+ * Check whether a value is of the same primitive type as <code>targetType</code>.
+ *
+ * @param targetType The primitive type to target.
+ * @param valueType The value to match to the primitive type.
+ * @return Whether <code>valueType</code> can be coerced (e.g. autoboxed) into <code>targetType</code>.
+ */
+ private boolean matchesPrimitive(Class<?> targetType, Class<?> valueType) {
+ if (!targetType.isPrimitive()) {
+ return false;
+ }
+
+ try {
+ // see if there is a "TYPE" field. This is present for primitive wrappers.
+ Field typeField = valueType.getField("TYPE");
+ Object primitiveValueType = typeField.get(valueType);
+
+ if (targetType == primitiveValueType) {
+ return true;
+ }
+ } catch (NoSuchFieldException e) {
+ // lacking the TYPE field is a good sign that we're not working with a primitive wrapper.
+ // we can't match for compatibility
+ } catch (IllegalAccessException e) {
+ // an inaccessible TYPE field is a good sign that we're not working with a primitive wrapper.
+ // nothing to do. we can't match for compatibility
+ }
+ return false;
+ }
+
+ /**
+ * Get the write method to use when setting {@code value} to the {@code target}.
+ *
+ * @param target Object where the write method will be called.
+ * @param prop BeanUtils information.
+ * @param value The value that will be passed to the write method.
+ * @return The {@link java.lang.reflect.Method} to call on {@code target} to write {@code value} or {@code null} if
+ * there is no suitable write method.
+ */
+ protected Method getWriteMethod(Object target, PropertyDescriptor prop, Object value) {
+ Method method = prop.getWriteMethod();
+ return method;
+ }
+
+ /**
+ * Factory method that returns a new instance of the given Class. This
+ * is called at the start of the bean creation process and may be
+ * overridden to provide custom behavior like returning a cached bean
+ * instance.
+ * @param <T> The type of object to create
+ * @param c The Class to create an object from.
+ * @return A newly created object of the Class.
+ * @throws SQLException if creation failed.
+ */
+ protected <T> T newInstance(Class<T> c) throws SQLException {
+ try {
+ return c.newInstance();
+
+ } catch (InstantiationException e) {
+ throw new SQLException(
+ "Cannot create " + c.getName() + ": " + e.getMessage());
+
+ } catch (IllegalAccessException e) {
+ throw new SQLException(
+ "Cannot create " + c.getName() + ": " + e.getMessage());
+ }
+ }
+
+ /**
+ * Returns a PropertyDescriptor[] for the given Class.
+ *
+ * @param c The Class to retrieve PropertyDescriptors for.
+ * @return A PropertyDescriptor[] describing the Class.
+ * @throws SQLException if introspection failed.
+ */
+ private PropertyDescriptor[] propertyDescriptors(Class<?> c)
+ throws SQLException {
+ // Introspector caches BeanInfo classes for better performance
+ BeanInfo beanInfo = null;
+ try {
+ beanInfo = Introspector.getBeanInfo(c);
+
+ } catch (IntrospectionException e) {
+ throw new SQLException(
+ "Bean introspection failed: " + e.getMessage());
+ }
+
+ return beanInfo.getPropertyDescriptors();
+ }
+
+ /**
+ * The positions in the returned array represent column numbers. The
+ * values stored at each position represent the index in the
+ * <code>PropertyDescriptor[]</code> for the bean property that matches
+ * the column name. If no bean property was found for a column, the
+ * position is set to <code>PROPERTY_NOT_FOUND</code>.
+ *
+ * @param rsmd The <code>ResultSetMetaData</code> containing column
+ * information.
+ *
+ * @param props The bean property descriptors.
+ *
+ * @throws SQLException if a database access error occurs
+ *
+ * @return An int[] with column index to property index mappings. The 0th
+ * element is meaningless because JDBC column indexing starts at 1.
+ */
+ protected int[] mapColumnsToProperties(ResultSetMetaData rsmd,
+ PropertyDescriptor[] props) throws SQLException {
+
+ int cols = rsmd.getColumnCount();
+ int[] columnToProperty = new int[cols + 1];
+ Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);
+
+ for (int col = 1; col <= cols; col++) {
+ String columnName = rsmd.getColumnLabel(col);
+ if (null == columnName || 0 == columnName.length()) {
+ columnName = rsmd.getColumnName(col);
+ }
+ String propertyName = columnToPropertyOverrides.get(columnName);
+ if (propertyName == null) {
+ propertyName = columnName;
+ }
+ for (int i = 0; i < props.length; i++) {
+
+ if (propertyName.equalsIgnoreCase(props[i].getName())) {
+ columnToProperty[col] = i;
+ break;
+ }
+ }
+ }
+
+ return columnToProperty;
+ }
+
+ /**
+ * Convert a <code>ResultSet</code> column into an object. Simple
+ * implementations could just call <code>rs.getObject(index)</code> while
+ * more complex implementations could perform type manipulation to match
+ * the column's type to the bean property type.
+ *
+ * <p>
+ * This implementation calls the appropriate <code>ResultSet</code> getter
+ * method for the given property type to perform the type conversion. If
+ * the property type doesn't match one of the supported
+ * <code>ResultSet</code> types, <code>getObject</code> is called.
+ * </p>
+ *
+ * @param rs The <code>ResultSet</code> currently being processed. It is
+ * positioned on a valid row before being passed into this method.
+ *
+ * @param index The current column index being processed.
+ *
+ * @param propType The bean property type that this column needs to be
+ * converted into.
+ *
+ * @throws SQLException if a database access error occurs
+ *
+ * @return The object from the <code>ResultSet</code> at the given column
+ * index after optional type processing or <code>null</code> if the column
+ * value was SQL NULL.
+ */
+ protected Object processColumn(ResultSet rs, int index, Class<?> propType)
+ throws SQLException {
+
+ Object retval = rs.getObject(index);
+
+ if ( !propType.isPrimitive() && retval == null ) {
+ return null;
+ }
+
+ for (ColumnHandler handler : columnHandlers) {
+ if (handler.match(propType)) {
+ retval = handler.apply(rs, index);
+ break;
+ }
+ }
+
+ return retval;
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java b/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
index 24f3900..3a00aa9 100644
--- a/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
+++ b/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java
@@ -1,496 +1,496 @@
-/*
- * 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.dbutils;
-
-import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.mock;
-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.ParameterMetaData;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-
-import javax.sql.DataSource;
-
-import org.apache.commons.dbutils.handlers.ArrayHandler;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-
-@SuppressWarnings("boxing") // test code
-public class AsyncQueryRunnerTest {
- AsyncQueryRunner runner;
- ArrayHandler handler;
-
- @Mock DataSource dataSource;
- @Mock Connection conn;
- @Mock PreparedStatement stmt;
- @Mock ParameterMetaData meta;
- @Mock ResultSet results;
-
- @Before
- public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
-
- when(dataSource.getConnection()).thenReturn(conn);
- when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
- when(stmt.getParameterMetaData()).thenReturn(meta);
- when(stmt.getResultSet()).thenReturn(results);
- when(stmt.executeQuery()).thenReturn(results);
- when(results.next()).thenReturn(false);
-
- handler = new ArrayHandler();
- runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1), new QueryRunner(dataSource));
- }
-
- //
- // Batch test cases
- //
- private void callGoodBatch(Connection conn, Object[][] params) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- Future<int[]> future = runner.batch(conn, "select * from blah where ? = ?", params);
-
- future.get();
-
- verify(stmt, times(2)).addBatch();
- verify(stmt, times(1)).executeBatch();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we closed the connection
- }
-
- private void callGoodBatch(Object[][] params) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- Future<int[]> future = runner.batch("select * from blah where ? = ?", params);
-
- future.get();
-
- verify(stmt, times(2)).addBatch();
- verify(stmt, times(1)).executeBatch();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
- }
-
- @Test
- public void testGoodBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- callGoodBatch(params);
- }
-
- @SuppressWarnings("deprecation") // deliberate test of deprecated code
- @Test
- public void testGoodBatchPmdTrue() throws Exception {
- runner = new AsyncQueryRunner(dataSource, true, Executors.newFixedThreadPool(1));
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- callGoodBatch(params);
- }
-
- @Test
- public void testGoodBatchDefaultConstructor() throws Exception {
- runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- callGoodBatch(conn, params);
- }
-
- @Test
- public void testNullParamsBatch() throws Exception {
- String[][] params = new String[][] { { null, "unit" }, { "test", null } };
-
- callGoodBatch(params);
- }
-
-
-
- // helper method for calling batch when an exception is expected
- private void callBatchWithException(String sql, Object[][] params) throws Exception {
- Future<int[]> future = null;
- boolean caught = false;
-
- try {
- future = runner.batch(sql, params);
-
- future.get();
-
- verify(stmt, times(2)).addBatch();
- verify(stmt, times(1)).executeBatch();
- verify(stmt, times(1)).close(); // make sure the statement is closed
- verify(conn, times(1)).close(); // make sure the connection is closed
- } catch(Exception e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testTooFewParamsBatch() throws Exception {
- String[][] params = new String[][] { { "unit" }, { "test" } };
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
- @Test
- public void testTooManyParamsBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit", "unit" }, { "test", "test", "test" } };
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullConnectionBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.batch("select * from blah where ? = ?", params).get();
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullSqlBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.batch(null, params).get();
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullParamsArgBatch() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.batch("select * from blah where ? = ?", null).get();
- }
-
- @Test
- public void testAddBatchException() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- doThrow(new SQLException()).when(stmt).addBatch();
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
- @Test
- public void testExecuteBatchException() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- doThrow(new SQLException()).when(stmt).executeBatch();
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
-
- //
- // Query test cases
- //
- private void callGoodQuery(Connection conn) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.query(conn, "select * from blah where ? = ?", handler, "unit", "test").get();
-
- verify(stmt, times(1)).executeQuery();
- verify(results, times(1)).close();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we closed the connection
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- runner.query(conn, "select * from blah", handler).get();
-
- verify(stmt, times(2)).executeQuery();
- verify(results, times(2)).close();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we closed the connection
- }
-
- private void callGoodQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.query("select * from blah where ? = ?", handler, "unit", "test").get();
-
- verify(stmt, times(1)).executeQuery();
- verify(results, times(1)).close();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- runner.query("select * from blah", handler).get();
-
- verify(stmt, times(2)).executeQuery();
- verify(results, times(2)).close();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(2)).close(); // make sure we closed the connection
- }
-
- @Test
- public void testGoodQuery() throws Exception {
- callGoodQuery();
- }
-
- @SuppressWarnings("deprecation") // deliberate test of deprecated code
- @Test
- public void testGoodQueryPmdTrue() throws Exception {
- runner = new AsyncQueryRunner(true, Executors.newFixedThreadPool(1));
- callGoodQuery(conn);
- }
-
- @Test
- public void testGoodQueryDefaultConstructor() throws Exception {
- runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
- callGoodQuery(conn);
- }
-
-
- // helper method for calling batch when an exception is expected
- private void callQueryWithException(Object... params) throws Exception {
- boolean caught = false;
-
- try {
- when(meta.getParameterCount()).thenReturn(2);
- runner.query("select * from blah where ? = ?", handler, params).get();
-
- verify(stmt, times(1)).executeQuery();
- verify(results, times(1)).close();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
- } catch(Exception e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testNoParamsQuery() throws Exception {
- callQueryWithException();
- }
-
- @Test
- public void testTooFewParamsQuery() throws Exception {
- callQueryWithException("unit");
- }
-
- @Test
- public void testTooManyParamsQuery() throws Exception {
- callQueryWithException("unit", "test", "fail");
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullConnectionQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.query("select * from blah where ? = ?", handler, "unit", "test").get();
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullSqlQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.query(null, handler).get();
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullHandlerQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.query("select * from blah where ? = ?", null).get();
- }
-
- @Test
- public void testExecuteQueryException() throws Exception {
- doThrow(new SQLException()).when(stmt).executeQuery();
-
- callQueryWithException(handler, "unit", "test");
- }
-
-
- //
- // Update test cases
- //
- private void callGoodUpdate(Connection conn) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.update(conn, "update blah set ? = ?", "unit", "test").get();
-
- verify(stmt, times(1)).executeUpdate();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we closed the connection
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(0);
- runner.update(conn, "update blah set unit = test").get();
-
- verify(stmt, times(2)).executeUpdate();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we closed the connection
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(1);
- runner.update(conn, "update blah set unit = ?", "test").get();
-
- verify(stmt, times(3)).executeUpdate();
- verify(stmt, times(3)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we closed the connection
- }
-
- private void callGoodUpdate() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.update("update blah set ? = ?", "unit", "test").get();
-
- verify(stmt, times(1)).executeUpdate();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(0);
- runner.update("update blah set unit = test").get();
-
- verify(stmt, times(2)).executeUpdate();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(2)).close(); // make sure we closed the connection
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(1);
- runner.update("update blah set unit = ?", "test").get();
-
- verify(stmt, times(3)).executeUpdate();
- verify(stmt, times(3)).close(); // make sure we closed the statement
- verify(conn, times(3)).close(); // make sure we closed the connection
- }
-
- @Test
- public void testGoodUpdate() throws Exception {
- callGoodUpdate();
- }
-
- @SuppressWarnings("deprecation") // deliberate test of deprecated code
- @Test
- public void testGoodUpdatePmdTrue() throws Exception {
- runner = new AsyncQueryRunner(true, Executors.newFixedThreadPool(1));
- callGoodUpdate(conn);
- }
-
- @Test
- public void testGoodUpdateDefaultConstructor() throws Exception {
- runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
- callGoodUpdate(conn);
- }
-
- // helper method for calling batch when an exception is expected
- private void callUpdateWithException(Object... params) throws Exception {
- boolean caught = false;
-
- try {
- when(meta.getParameterCount()).thenReturn(2);
- runner.update("select * from blah where ? = ?", params).get();
-
- verify(stmt, times(1)).executeUpdate();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
- } catch(Exception e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testNoParamsUpdate() throws Exception {
- callUpdateWithException();
- }
-
- @Test
- public void testTooFewParamsUpdate() throws Exception {
- callUpdateWithException("unit");
- }
-
- @Test
- public void testTooManyParamsUpdate() throws Exception {
- callUpdateWithException("unit", "test", "fail");
- }
-
- @Test
- public void testInsertUsesGivenQueryRunner() throws Exception {
- QueryRunner mockQueryRunner = mock(QueryRunner.class
- , org.mockito.Mockito.withSettings().verboseLogging() // debug for Continuum
- );
- runner = new AsyncQueryRunner(Executors.newSingleThreadExecutor(), mockQueryRunner);
-
- runner.insert("1", handler);
- runner.insert("2", handler, "param1");
- runner.insert(conn, "3", handler);
- runner.insert(conn, "4", handler, "param1");
-
- // give the Executor time to submit all insert statements. Otherwise the following verify statements will fail from time to time.
- TimeUnit.MILLISECONDS.sleep(50);
-
- verify(mockQueryRunner).insert("1", handler);
- verify(mockQueryRunner).insert("2", handler, "param1");
- verify(mockQueryRunner).insert(conn, "3", handler);
- verify(mockQueryRunner).insert(conn, "4", handler, "param1");
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullConnectionUpdate() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.update("select * from blah where ? = ?", "unit", "test").get();
- }
-
- @Test(expected=ExecutionException.class)
- public void testNullSqlUpdate() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.update(null).get();
- }
-
- @Test
- public void testExecuteUpdateException() throws Exception {
- doThrow(new SQLException()).when(stmt).executeUpdate();
-
- callUpdateWithException("unit", "test");
- }
-
- //
- // Random tests
- //
- @Test(expected=ExecutionException.class)
- public void testBadPrepareConnection() throws Exception {
- runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
- runner.update("update blah set unit = test").get();
- }
-}
+/*
+ * 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.dbutils;
+
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+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.ParameterMetaData;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import javax.sql.DataSource;
+
+import org.apache.commons.dbutils.handlers.ArrayHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@SuppressWarnings("boxing") // test code
+public class AsyncQueryRunnerTest {
+ AsyncQueryRunner runner;
+ ArrayHandler handler;
+
+ @Mock DataSource dataSource;
+ @Mock Connection conn;
+ @Mock PreparedStatement stmt;
+ @Mock ParameterMetaData meta;
+ @Mock ResultSet results;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ when(dataSource.getConnection()).thenReturn(conn);
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.getParameterMetaData()).thenReturn(meta);
+ when(stmt.getResultSet()).thenReturn(results);
+ when(stmt.executeQuery()).thenReturn(results);
+ when(results.next()).thenReturn(false);
+
+ handler = new ArrayHandler();
+ runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1), new QueryRunner(dataSource));
+ }
+
+ //
+ // Batch test cases
+ //
+ private void callGoodBatch(Connection conn, Object[][] params) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ Future<int[]> future = runner.batch(conn, "select * from blah where ? = ?", params);
+
+ future.get();
+
+ verify(stmt, times(2)).addBatch();
+ verify(stmt, times(1)).executeBatch();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we closed the connection
+ }
+
+ private void callGoodBatch(Object[][] params) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ Future<int[]> future = runner.batch("select * from blah where ? = ?", params);
+
+ future.get();
+
+ verify(stmt, times(2)).addBatch();
+ verify(stmt, times(1)).executeBatch();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+ }
+
+ @Test
+ public void testGoodBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ callGoodBatch(params);
+ }
+
+ @SuppressWarnings("deprecation") // deliberate test of deprecated code
+ @Test
+ public void testGoodBatchPmdTrue() throws Exception {
+ runner = new AsyncQueryRunner(dataSource, true, Executors.newFixedThreadPool(1));
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ callGoodBatch(params);
+ }
+
+ @Test
+ public void testGoodBatchDefaultConstructor() throws Exception {
+ runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ callGoodBatch(conn, params);
+ }
+
+ @Test
+ public void testNullParamsBatch() throws Exception {
+ String[][] params = new String[][] { { null, "unit" }, { "test", null } };
+
+ callGoodBatch(params);
+ }
+
+
+
+ // helper method for calling batch when an exception is expected
+ private void callBatchWithException(String sql, Object[][] params) throws Exception {
+ Future<int[]> future = null;
+ boolean caught = false;
+
+ try {
+ future = runner.batch(sql, params);
+
+ future.get();
+
+ verify(stmt, times(2)).addBatch();
+ verify(stmt, times(1)).executeBatch();
+ verify(stmt, times(1)).close(); // make sure the statement is closed
+ verify(conn, times(1)).close(); // make sure the connection is closed
+ } catch(Exception e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testTooFewParamsBatch() throws Exception {
+ String[][] params = new String[][] { { "unit" }, { "test" } };
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+ @Test
+ public void testTooManyParamsBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit", "unit" }, { "test", "test", "test" } };
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullConnectionBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.batch("select * from blah where ? = ?", params).get();
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullSqlBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.batch(null, params).get();
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullParamsArgBatch() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.batch("select * from blah where ? = ?", null).get();
+ }
+
+ @Test
+ public void testAddBatchException() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ doThrow(new SQLException()).when(stmt).addBatch();
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+ @Test
+ public void testExecuteBatchException() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ doThrow(new SQLException()).when(stmt).executeBatch();
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+
+ //
+ // Query test cases
+ //
+ private void callGoodQuery(Connection conn) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query(conn, "select * from blah where ? = ?", handler, "unit", "test").get();
+
+ verify(stmt, times(1)).executeQuery();
+ verify(results, times(1)).close();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we closed the connection
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.query(conn, "select * from blah", handler).get();
+
+ verify(stmt, times(2)).executeQuery();
+ verify(results, times(2)).close();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we closed the connection
+ }
+
+ private void callGoodQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query("select * from blah where ? = ?", handler, "unit", "test").get();
+
+ verify(stmt, times(1)).executeQuery();
+ verify(results, times(1)).close();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.query("select * from blah", handler).get();
+
+ verify(stmt, times(2)).executeQuery();
+ verify(results, times(2)).close();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(2)).close(); // make sure we closed the connection
+ }
+
+ @Test
+ public void testGoodQuery() throws Exception {
+ callGoodQuery();
+ }
+
+ @SuppressWarnings("deprecation") // deliberate test of deprecated code
+ @Test
+ public void testGoodQueryPmdTrue() throws Exception {
+ runner = new AsyncQueryRunner(true, Executors.newFixedThreadPool(1));
+ callGoodQuery(conn);
+ }
+
+ @Test
+ public void testGoodQueryDefaultConstructor() throws Exception {
+ runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
+ callGoodQuery(conn);
+ }
+
+
+ // helper method for calling batch when an exception is expected
+ private void callQueryWithException(Object... params) throws Exception {
+ boolean caught = false;
+
+ try {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query("select * from blah where ? = ?", handler, params).get();
+
+ verify(stmt, times(1)).executeQuery();
+ verify(results, times(1)).close();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+ } catch(Exception e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testNoParamsQuery() throws Exception {
+ callQueryWithException();
+ }
+
+ @Test
+ public void testTooFewParamsQuery() throws Exception {
+ callQueryWithException("unit");
+ }
+
+ @Test
+ public void testTooManyParamsQuery() throws Exception {
+ callQueryWithException("unit", "test", "fail");
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullConnectionQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.query("select * from blah where ? = ?", handler, "unit", "test").get();
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullSqlQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.query(null, handler).get();
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullHandlerQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.query("select * from blah where ? = ?", null).get();
+ }
+
+ @Test
+ public void testExecuteQueryException() throws Exception {
+ doThrow(new SQLException()).when(stmt).executeQuery();
+
+ callQueryWithException(handler, "unit", "test");
+ }
+
+
+ //
+ // Update test cases
+ //
+ private void callGoodUpdate(Connection conn) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.update(conn, "update blah set ? = ?", "unit", "test").get();
+
+ verify(stmt, times(1)).executeUpdate();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we closed the connection
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.update(conn, "update blah set unit = test").get();
+
+ verify(stmt, times(2)).executeUpdate();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we closed the connection
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(1);
+ runner.update(conn, "update blah set unit = ?", "test").get();
+
+ verify(stmt, times(3)).executeUpdate();
+ verify(stmt, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we closed the connection
+ }
+
+ private void callGoodUpdate() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.update("update blah set ? = ?", "unit", "test").get();
+
+ verify(stmt, times(1)).executeUpdate();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.update("update blah set unit = test").get();
+
+ verify(stmt, times(2)).executeUpdate();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(2)).close(); // make sure we closed the connection
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(1);
+ runner.update("update blah set unit = ?", "test").get();
+
+ verify(stmt, times(3)).executeUpdate();
+ verify(stmt, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(3)).close(); // make sure we closed the connection
+ }
+
+ @Test
+ public void testGoodUpdate() throws Exception {
+ callGoodUpdate();
+ }
+
+ @SuppressWarnings("deprecation") // deliberate test of deprecated code
+ @Test
+ public void testGoodUpdatePmdTrue() throws Exception {
+ runner = new AsyncQueryRunner(true, Executors.newFixedThreadPool(1));
+ callGoodUpdate(conn);
+ }
+
+ @Test
+ public void testGoodUpdateDefaultConstructor() throws Exception {
+ runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
+ callGoodUpdate(conn);
+ }
+
+ // helper method for calling batch when an exception is expected
+ private void callUpdateWithException(Object... params) throws Exception {
+ boolean caught = false;
+
+ try {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.update("select * from blah where ? = ?", params).get();
+
+ verify(stmt, times(1)).executeUpdate();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+ } catch(Exception e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testNoParamsUpdate() throws Exception {
+ callUpdateWithException();
+ }
+
+ @Test
+ public void testTooFewParamsUpdate() throws Exception {
+ callUpdateWithException("unit");
+ }
+
+ @Test
+ public void testTooManyParamsUpdate() throws Exception {
+ callUpdateWithException("unit", "test", "fail");
+ }
+
+ @Test
+ public void testInsertUsesGivenQueryRunner() throws Exception {
+ QueryRunner mockQueryRunner = mock(QueryRunner.class
+ , org.mockito.Mockito.withSettings().verboseLogging() // debug for Continuum
+ );
+ runner = new AsyncQueryRunner(Executors.newSingleThreadExecutor(), mockQueryRunner);
+
+ runner.insert("1", handler);
+ runner.insert("2", handler, "param1");
+ runner.insert(conn, "3", handler);
+ runner.insert(conn, "4", handler, "param1");
+
+ // give the Executor time to submit all insert statements. Otherwise the following verify statements will fail from time to time.
+ TimeUnit.MILLISECONDS.sleep(50);
+
+ verify(mockQueryRunner).insert("1", handler);
+ verify(mockQueryRunner).insert("2", handler, "param1");
+ verify(mockQueryRunner).insert(conn, "3", handler);
+ verify(mockQueryRunner).insert(conn, "4", handler, "param1");
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullConnectionUpdate() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.update("select * from blah where ? = ?", "unit", "test").get();
+ }
+
+ @Test(expected=ExecutionException.class)
+ public void testNullSqlUpdate() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.update(null).get();
+ }
+
+ @Test
+ public void testExecuteUpdateException() throws Exception {
+ doThrow(new SQLException()).when(stmt).executeUpdate();
+
+ callUpdateWithException("unit", "test");
+ }
+
+ //
+ // Random tests
+ //
+ @Test(expected=ExecutionException.class)
+ public void testBadPrepareConnection() throws Exception {
+ runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1));
+ runner.update("update blah set unit = test").get();
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/test/java/org/apache/commons/dbutils/BaseTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/BaseTestCase.java b/src/test/java/org/apache/commons/dbutils/BaseTestCase.java
index 913397f..dc5e479 100644
--- a/src/test/java/org/apache/commons/dbutils/BaseTestCase.java
+++ b/src/test/java/org/apache/commons/dbutils/BaseTestCase.java
@@ -1,138 +1,138 @@
-/*
- * 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.dbutils;
-
-import java.math.BigInteger;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.Timestamp;
-import java.util.Date;
-
-import junit.framework.TestCase;
-
-/**
- * BaseTestCase is the base class for all test cases as well as the "all tests"
- * runner.
- */
-public class BaseTestCase extends TestCase {
-
- private static final String[] columnNames =
- new String[] {
- "one",
- "two",
- "three",
- "notInBean",
- "intTest",
- "integerTest",
- "nullObjectTest",
- "nullPrimitiveTest",
- "notDate",
- "columnProcessorDoubleTest" };
-
- /**
- * The number of columns in the MockResultSet.
- */
- protected static final int COLS = columnNames.length;
-
- protected static final ResultSetMetaData metaData =
- MockResultSetMetaData.create(columnNames);
-
- /**
- * A Timestamp for test purposes having 9 decimals
- */
- static final Timestamp ts789456123;
-
- static {
- ts789456123 = new Timestamp(new Date().getTime());
- ts789456123.setNanos(789456123);
- }
-
- private static final Object[] row1 =
- new Object[] {
- "1",
- "2",
- "THREE",
- " notInBean ",
- Integer.valueOf(1),
- Integer.valueOf(2),
- null,
- null,
- new Date(),
- BigInteger.valueOf(13)};
-
- private static final Object[] row2 =
- new Object[] {
- "4",
- "5",
- "SIX",
- " notInBean ",
- Integer.valueOf(3),
- Integer.valueOf(4),
- null,
- null,
- ts789456123,
- BigInteger.valueOf(13)};
-
- private static final Object[][] rows = new Object[][] { row1, row2 };
-
- /**
- * The number of rows in the MockResultSet.
- */
- protected static final int ROWS = rows.length;
-
- /**
- * The ResultSet all test methods will use.
- */
- protected ResultSet rs = null;
-
- /**
- * A ResultSet with 0 rows.
- */
- protected ResultSet emptyResultSet = null;
-
- /**
- * This is called before each test method so ResultSet will be fresh each
- * time.
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- rs = this.createMockResultSet();
- emptyResultSet = MockResultSet.create(metaData, null);
- }
-
- /**
- * Creates a freshly initialized ResultSet.
- */
- protected ResultSet createMockResultSet() {
- return MockResultSet.create(metaData, rows);
- }
-
- // Test which allows Eclipse to be run on full project (avoids no tests found)
- // check that the rows are valid for the column definition
- public void testCheckDataSizes() {
- assertEquals("Row 1 must contain correct number of columns", columnNames.length, row1.length);
- assertEquals("Row 1 must contain correct number of columns", columnNames.length, row2.length);
- }
-
- public void testResultSets() throws Exception {
- assertFalse("emptyResultSet should be empty", emptyResultSet.next());
- // fails in SqlNullCheckedResultSetTest assertTrue("rs should not be empty", rs.next());
- }
-}
+/*
+ * 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.dbutils;
+
+import java.math.BigInteger;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.Timestamp;
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+/**
+ * BaseTestCase is the base class for all test cases as well as the "all tests"
+ * runner.
+ */
+public class BaseTestCase extends TestCase {
+
+ private static final String[] columnNames =
+ new String[] {
+ "one",
+ "two",
+ "three",
+ "notInBean",
+ "intTest",
+ "integerTest",
+ "nullObjectTest",
+ "nullPrimitiveTest",
+ "notDate",
+ "columnProcessorDoubleTest" };
+
+ /**
+ * The number of columns in the MockResultSet.
+ */
+ protected static final int COLS = columnNames.length;
+
+ protected static final ResultSetMetaData metaData =
+ MockResultSetMetaData.create(columnNames);
+
+ /**
+ * A Timestamp for test purposes having 9 decimals
+ */
+ static final Timestamp ts789456123;
+
+ static {
+ ts789456123 = new Timestamp(new Date().getTime());
+ ts789456123.setNanos(789456123);
+ }
+
+ private static final Object[] row1 =
+ new Object[] {
+ "1",
+ "2",
+ "THREE",
+ " notInBean ",
+ Integer.valueOf(1),
+ Integer.valueOf(2),
+ null,
+ null,
+ new Date(),
+ BigInteger.valueOf(13)};
+
+ private static final Object[] row2 =
+ new Object[] {
+ "4",
+ "5",
+ "SIX",
+ " notInBean ",
+ Integer.valueOf(3),
+ Integer.valueOf(4),
+ null,
+ null,
+ ts789456123,
+ BigInteger.valueOf(13)};
+
+ private static final Object[][] rows = new Object[][] { row1, row2 };
+
+ /**
+ * The number of rows in the MockResultSet.
+ */
+ protected static final int ROWS = rows.length;
+
+ /**
+ * The ResultSet all test methods will use.
+ */
+ protected ResultSet rs = null;
+
+ /**
+ * A ResultSet with 0 rows.
+ */
+ protected ResultSet emptyResultSet = null;
+
+ /**
+ * This is called before each test method so ResultSet will be fresh each
+ * time.
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ rs = this.createMockResultSet();
+ emptyResultSet = MockResultSet.create(metaData, null);
+ }
+
+ /**
+ * Creates a freshly initialized ResultSet.
+ */
+ protected ResultSet createMockResultSet() {
+ return MockResultSet.create(metaData, rows);
+ }
+
+ // Test which allows Eclipse to be run on full project (avoids no tests found)
+ // check that the rows are valid for the column definition
+ public void testCheckDataSizes() {
+ assertEquals("Row 1 must contain correct number of columns", columnNames.length, row1.length);
+ assertEquals("Row 1 must contain correct number of columns", columnNames.length, row2.length);
+ }
+
+ public void testResultSets() throws Exception {
+ assertFalse("emptyResultSet should be empty", emptyResultSet.next());
+ // fails in SqlNullCheckedResultSetTest assertTrue("rs should not be empty", rs.next());
+ }
+}
[2/5] commons-dbutils git commit: Remove trailing white spaces on all
lines.
Posted by gg...@apache.org.
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/084f286a/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java b/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
index 95e7662..7d60b0f 100644
--- a/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
+++ b/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
@@ -1,1036 +1,1036 @@
-/*
- * 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.dbutils;
-
-import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.sql.CallableStatement;
-import java.sql.Connection;
-import java.sql.ParameterMetaData;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.sql.Types;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.sql.DataSource;
-
-import org.apache.commons.dbutils.handlers.ArrayHandler;
-import org.apache.commons.dbutils.handlers.ScalarHandler;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-
-@SuppressWarnings("boxing") // test code
-public class QueryRunnerTest {
- QueryRunner runner;
- ArrayHandler handler;
-
- @Mock DataSource dataSource;
- @Mock Connection conn;
- @Mock PreparedStatement stmt;
- @Mock CallableStatement call;
- @Mock ParameterMetaData meta;
- @Mock ResultSet results;
- @Mock ResultSetMetaData resultsMeta;
-
- @Before
- public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this); // init the mocks
-
- when(dataSource.getConnection()).thenReturn(conn);
- when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
- when(stmt.getParameterMetaData()).thenReturn(meta);
- when(stmt.getResultSet()).thenReturn(results);
- when(stmt.executeQuery()).thenReturn(results);
- when(conn.prepareCall(any(String.class))).thenReturn(call);
- when(call.getParameterMetaData()).thenReturn(meta);
- when(call.getResultSet()).thenReturn(results);
- when(call.getMoreResults()).thenReturn(false);
- when(results.next()).thenReturn(false);
-
- handler = new ArrayHandler();
- runner = new QueryRunner(dataSource);
- }
-
- //
- // Batch test cases
- //
-
- private void callGoodBatch(Connection conn, Object[][] params) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.batch(conn, "select * from blah where ? = ?", params);
-
- verify(stmt, times(2)).addBatch();
- verify(stmt, times(1)).executeBatch();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection, since QueryRunner.batch(Connection, String, Object[][]) does not close connections
- }
-
- private void callGoodBatch(Object[][] params) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.batch("select * from blah where ? = ?", params);
-
- verify(stmt, times(2)).addBatch();
- verify(stmt, times(1)).executeBatch();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
- }
-
- @Test
- public void testGoodBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- callGoodBatch(params);
- }
-
- @Test
- public void testGoodBatchPmdTrue() throws Exception {
- runner = new QueryRunner(dataSource, true);
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- callGoodBatch(params);
- }
-
- @Test
- public void testGoodBatchDefaultConstructor() throws Exception {
- runner = new QueryRunner();
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- callGoodBatch(conn, params);
- }
-
- @Test
- public void testNullParamsBatch() throws Exception {
- String[][] params = new String[][] { { null, "unit" }, { "test", null } };
-
- callGoodBatch(params);
- }
-
-
-
- // helper method for calling batch when an exception is expected
- private void callBatchWithException(String sql, Object[][] params) throws Exception {
- boolean caught = false;
-
- try {
- runner.batch(sql, params);
-
- verify(stmt, times(2)).addBatch();
- verify(stmt, times(1)).executeBatch();
- verify(stmt, times(1)).close(); // make sure the statement is closed
- verify(conn, times(1)).close(); // make sure the connection is closed
- } catch(SQLException e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testTooFewParamsBatch() throws Exception {
- String[][] params = new String[][] { { "unit" }, { "test" } };
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
- @Test
- public void testTooManyParamsBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit", "unit" }, { "test", "test", "test" } };
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
- @Test(expected=SQLException.class)
- public void testNullConnectionBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.batch("select * from blah where ? = ?", params);
- }
-
- @Test(expected=SQLException.class)
- public void testNullSqlBatch() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.batch(null, params);
- }
-
- @Test(expected=SQLException.class)
- public void testNullParamsArgBatch() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.batch("select * from blah where ? = ?", null);
- }
-
- @Test
- public void testAddBatchException() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- doThrow(new SQLException()).when(stmt).addBatch();
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
- @Test
- public void testExecuteBatchException() throws Exception {
- String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
-
- doThrow(new SQLException()).when(stmt).executeBatch();
-
- callBatchWithException("select * from blah where ? = ?", params);
- }
-
-
- //
- // Query test cases
- //
- private void callGoodQuery(Connection conn) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.query(conn, "select * from blah where ? = ?", handler, "unit", "test");
-
- verify(stmt, times(1)).executeQuery();
- verify(results, times(1)).close();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection, since QueryRunner.query(Connection, String, ResultSetHandler<T>, Object...) does not close connections
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- runner.query(conn, "select * from blah", handler);
-
- verify(stmt, times(2)).executeQuery();
- verify(results, times(2)).close();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection, see above
- }
-
- private void callGoodQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.query("select * from blah where ? = ?", handler, "unit", "test");
-
- verify(stmt, times(1)).executeQuery();
- verify(results, times(1)).close();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- runner.query("select * from blah", handler);
-
- verify(stmt, times(2)).executeQuery();
- verify(results, times(2)).close();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(2)).close(); // make sure we closed the connection
- }
-
- @Test
- public void testGoodQuery() throws Exception {
- callGoodQuery();
- }
-
- @Test
- public void testGoodQueryPmdTrue() throws Exception {
- runner = new QueryRunner(true);
- callGoodQuery(conn);
- }
-
- @Test
- public void testGoodQueryDefaultConstructor() throws Exception {
- runner = new QueryRunner();
- callGoodQuery(conn);
- }
-
-
- // helper method for calling batch when an exception is expected
- private void callQueryWithException(Object... params) throws Exception {
- boolean caught = false;
-
- try {
- when(meta.getParameterCount()).thenReturn(2);
- runner.query("select * from blah where ? = ?", handler, params);
-
- verify(stmt, never()).close(); // make sure the statement is still open
- verify(stmt, times(1)).executeQuery();
- verify(results, times(1)).close();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
- } catch(SQLException e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testNoParamsQuery() throws Exception {
- callQueryWithException();
- }
-
- @Test
- public void testTooFewParamsQuery() throws Exception {
- callQueryWithException("unit");
- }
-
- @Test
- public void testTooManyParamsQuery() throws Exception {
- callQueryWithException("unit", "test", "fail");
- }
-
- @Test(expected=SQLException.class)
- public void testNullConnectionQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.query("select * from blah where ? = ?", handler, "unit", "test");
- }
-
- @Test(expected=SQLException.class)
- public void testNullSqlQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.query(null, handler);
- }
-
- @Test(expected=SQLException.class)
- public void testNullHandlerQuery() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.query("select * from blah where ? = ?", null);
- }
-
- @Test
- public void testExecuteQueryException() throws Exception {
- doThrow(new SQLException()).when(stmt).executeQuery();
-
- callQueryWithException(handler, "unit", "test");
- }
-
-
- //
- // Update test cases
- //
- private void callGoodUpdate(Connection conn) throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.update(conn, "update blah set ? = ?", "unit", "test");
-
- verify(stmt, times(1)).executeUpdate();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection, since QueryRunner.update(Connection, String, Object...) does not close connections
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(0);
- runner.update(conn, "update blah set unit = test");
-
- verify(stmt, times(2)).executeUpdate();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection, see above
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(1);
- runner.update(conn, "update blah set unit = ?", "test");
-
- verify(stmt, times(3)).executeUpdate();
- verify(stmt, times(3)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection, see above
- }
-
- private void callGoodUpdate() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- runner.update("update blah set ? = ?", "unit", "test");
-
- verify(stmt, times(1)).executeUpdate();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(0);
- runner.update("update blah set unit = test");
-
- verify(stmt, times(2)).executeUpdate();
- verify(stmt, times(2)).close(); // make sure we closed the statement
- verify(conn, times(2)).close(); // make sure we closed the connection
-
- // call the other variation
- when(meta.getParameterCount()).thenReturn(1);
- runner.update("update blah set unit = ?", "test");
-
- verify(stmt, times(3)).executeUpdate();
- verify(stmt, times(3)).close(); // make sure we closed the statement
- verify(conn, times(3)).close(); // make sure we closed the connection
- }
-
- @Test
- public void testGoodUpdate() throws Exception {
- callGoodUpdate();
- }
-
- @Test
- public void testGoodUpdatePmdTrue() throws Exception {
- runner = new QueryRunner(true);
- callGoodUpdate(conn);
- }
-
- @Test
- public void testGoodUpdateDefaultConstructor() throws Exception {
- runner = new QueryRunner();
- callGoodUpdate(conn);
- }
-
- @Test
- public void testGoodInsert() throws Exception {
- results = mock(ResultSet.class);
-
- when(meta.getParameterCount()).thenReturn(2);
- when(conn.prepareStatement(any(String.class), eq(Statement.RETURN_GENERATED_KEYS))).thenReturn(stmt);
- when(stmt.getGeneratedKeys()).thenReturn(results);
- when(results.next()).thenReturn(true).thenReturn(false);
- when(results.getObject(1)).thenReturn(1L);
-
- Long generatedKey = runner.insert("INSERT INTO blah(col1, col2) VALUES(?,?)", new ScalarHandler<Long>(), "unit", "test");
-
- verify(stmt, times(1)).executeUpdate();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
-
- Assert.assertEquals(1L, generatedKey.longValue());
- }
-
- @Test
- public void testGoodBatchInsert() throws Exception {
- results = mock(ResultSet.class);
- resultsMeta = mock(ResultSetMetaData.class);
-
- when(meta.getParameterCount()).thenReturn(2);
- when(conn.prepareStatement(any(String.class), eq(Statement.RETURN_GENERATED_KEYS))).thenReturn(stmt);
- when(stmt.getGeneratedKeys()).thenReturn(results);
- when(results.next()).thenReturn(true).thenReturn(true).thenReturn(false);
- when(results.getMetaData()).thenReturn(resultsMeta);
- when(resultsMeta.getColumnCount()).thenReturn(1);
-
- ResultSetHandler<List<Object>> handler = new ResultSetHandler<List<Object>>()
- {
- @Override
- public List<Object> handle(ResultSet rs) throws SQLException
- {
- List<Object> objects = new ArrayList<Object>();
- while (rs.next())
- {
- objects.add(new Object());
- }
- return objects;
- }
- };
-
- Object[][] params = new Object[2][2];
- params[0][0] = "Test";
- params[0][1] = "Blah";
- params[1][0] = "Test2";
- params[1][1] = "Blah2";
-
- List<Object> generatedKeys = runner.insertBatch("INSERT INTO blah(col1, col2) VALUES(?,?)", handler, params);
-
- verify(stmt, times(2)).addBatch();
- verify(stmt, times(1)).executeBatch();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
-
- Assert.assertEquals(2, generatedKeys.size());
- }
-
- // helper method for calling batch when an exception is expected
- private void callUpdateWithException(Object... params) throws Exception {
- boolean caught = false;
-
- try {
- when(meta.getParameterCount()).thenReturn(2);
- runner.update("select * from blah where ? = ?", params);
-
- verify(stmt, times(1)).executeUpdate();
- verify(stmt, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we closed the connection
- } catch(SQLException e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testNoParamsUpdate() throws Exception {
- callUpdateWithException();
- }
-
- @Test
- public void testTooFewParamsUpdate() throws Exception {
- callUpdateWithException("unit");
- }
-
- @Test
- public void testTooManyParamsUpdate() throws Exception {
- callUpdateWithException("unit", "test", "fail");
- }
-
- @Test(expected=SQLException.class)
- public void testNullConnectionUpdate() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.update("select * from blah where ? = ?", "unit", "test");
- }
-
- @Test(expected=SQLException.class)
- public void testNullSqlUpdate() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.update(null);
- }
-
- @Test
- public void testExecuteUpdateException() throws Exception {
- doThrow(new SQLException()).when(stmt).executeUpdate();
-
- callUpdateWithException("unit", "test");
- }
-
- @Test
- public void testStatementConfiguration() throws Exception {
- StatementConfiguration stmtConfig = new StatementConfiguration(1, 2, 3, 4, 5);
- QueryRunner queryRunner = new QueryRunner(stmtConfig);
- queryRunner.prepareStatement(conn, "select 1");
-
- verify(stmt).setFetchDirection(eq(1));
- verify(stmt).setFetchSize(eq(2));
- verify(stmt).setMaxFieldSize(eq(3));
- verify(stmt).setMaxRows(eq(4));
- verify(stmt).setQueryTimeout(eq(5));
- }
-
- //
- // Execute tests
- //
- private void callGoodExecute(Connection conn) throws Exception {
- when(call.execute()).thenReturn(false);
- when(call.getUpdateCount()).thenReturn(3);
-
- when(meta.getParameterCount()).thenReturn(2);
- int result = runner.execute(conn, "{call my_proc(?, ?)}", "unit", "test");
-
- Assert.assertEquals(3, result);
-
- verify(call, times(1)).execute();
- verify(call, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- result = runner.execute(conn, "{call my_proc()}");
-
- Assert.assertEquals(3, result);
-
- verify(call, times(2)).execute();
- verify(call, times(2)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // Test single OUT parameter
- when(meta.getParameterCount()).thenReturn(1);
- when(call.getObject(1)).thenReturn(42);
- OutParameter<Integer> intParam =
- new OutParameter<Integer>(Types.INTEGER, Integer.class);
- result = runner.execute(conn, "{?= call my_proc()}", intParam);
-
- Assert.assertEquals(42, intParam.getValue().intValue());
- Assert.assertEquals(3, result);
-
- verify(call, times(3)).execute();
- verify(call, times(3)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // Test OUT parameters with IN parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(4242);
- intParam.setValue(null);
- result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "unit", "test");
-
- Assert.assertEquals(4242, intParam.getValue().intValue());
- Assert.assertEquals(3, result);
-
- verify(call, times(4)).execute();
- verify(call, times(4)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // Test INOUT parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(24);
- when(call.getObject(3)).thenReturn("out");
- intParam.setValue(null);
- OutParameter<String> stringParam =
- new OutParameter<String>(Types.VARCHAR, String.class, "in");
- result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "test", stringParam);
-
- Assert.assertEquals(24, intParam.getValue().intValue());
- Assert.assertEquals("out", stringParam.getValue());
- Assert.assertEquals(3, result);
-
- verify(call, times(5)).execute();
- verify(call, times(5)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
- }
-
- private void callGoodExecute() throws Exception {
- when(call.execute()).thenReturn(false);
- when(call.getUpdateCount()).thenReturn(3);
-
- when(meta.getParameterCount()).thenReturn(2);
- int result = runner.execute("{call my_proc(?, ?)}", "unit", "test");
-
- Assert.assertEquals(3, result);
-
- verify(call, times(1)).execute();
- verify(call, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we do not close the connection
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- result = runner.execute("{call my_proc()}");
-
- Assert.assertEquals(3, result);
-
- verify(call, times(2)).execute();
- verify(call, times(2)).close(); // make sure we closed the statement
- verify(conn, times(2)).close(); // make sure we do not close the connection
-
- // Test single OUT parameter
- when(meta.getParameterCount()).thenReturn(1);
- when(call.getObject(1)).thenReturn(42);
- OutParameter<Integer> intParam =
- new OutParameter<Integer>(Types.INTEGER, Integer.class);
- result = runner.execute("{?= call my_proc()}", intParam);
-
- Assert.assertEquals(42, intParam.getValue().intValue());
- Assert.assertEquals(3, result);
-
- verify(call, times(3)).execute();
- verify(call, times(3)).close(); // make sure we closed the statement
- verify(conn, times(3)).close(); // make sure we do not close the connection
-
- // Test OUT parameters with IN parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(4242);
- intParam.setValue(null);
- result = runner.execute("{?= call my_proc(?, ?)}", intParam, "unit", "test");
-
- Assert.assertEquals(4242, intParam.getValue().intValue());
- Assert.assertEquals(3, result);
-
- verify(call, times(4)).execute();
- verify(call, times(4)).close(); // make sure we closed the statement
- verify(conn, times(4)).close(); // make sure we do not close the connection
-
- // Test INOUT parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(24);
- when(call.getObject(3)).thenReturn("out");
- intParam.setValue(null);
- OutParameter<String> stringParam =
- new OutParameter<String>(Types.VARCHAR, String.class, "in");
- result = runner.execute("{?= call my_proc(?, ?)}", intParam, "test", stringParam);
-
- Assert.assertEquals(24, intParam.getValue().intValue());
- Assert.assertEquals("out", stringParam.getValue());
- Assert.assertEquals(3, result);
-
- verify(call, times(5)).execute();
- verify(call, times(5)).close(); // make sure we closed the statement
- verify(conn, times(5)).close(); // make sure we do not close the connection
- }
-
- @Test
- public void testGoodExecute() throws Exception {
- callGoodExecute();
- }
-
- @Test
- public void testGoodExecutePmdTrue() throws Exception {
- runner = new QueryRunner(true);
- callGoodExecute(conn);
- }
-
- @Test
- public void testGoodExecuteDefaultConstructor() throws Exception {
- runner = new QueryRunner();
- callGoodExecute(conn);
- }
-
- // helper method for calling execute when an exception is expected
- private void callExecuteWithException(Object... params) throws Exception {
- boolean caught = false;
-
- try {
- when(call.execute()).thenReturn(false);
- when(meta.getParameterCount()).thenReturn(2);
- runner.query("{call my_proc(?, ?)}", handler, params);
-
- } catch(SQLException e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testNoParamsExecute() throws Exception {
- callExecuteWithException();
- }
-
- @Test
- public void testTooFewParamsExecute() throws Exception {
- callExecuteWithException("unit");
- }
-
- @Test
- public void testTooManyParamsExecute() throws Exception {
- callExecuteWithException("unit", "test", "fail");
- }
-
- @Test(expected=SQLException.class)
- public void testNullConnectionExecute() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.execute("{call my_proc(?, ?)}", "unit", "test");
- }
-
- @Test(expected=SQLException.class)
- public void testNullSqlExecute() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.execute(null);
- }
-
- @Test(expected=SQLException.class)
- public void testNullHandlerExecute() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.execute("{call my_proc(?, ?)}");
- }
-
- @Test
- public void testExecuteException() throws Exception {
- doThrow(new SQLException()).when(stmt).execute();
-
- callExecuteWithException(handler, "unit", "test");
- }
-
- //
- // Execute with ResultSetHandler
- //
-
- @Test
- public void testExecuteWithMultipleResultSets() throws Exception {
- when(call.execute()).thenReturn(true);
- when(call.getMoreResults()).thenAnswer(new Answer<Boolean>()
- {
- int count = 1;
- @Override
- public Boolean answer(InvocationOnMock invocation)
- {
- return ++count <= 3;
- }
- });
- when(meta.getParameterCount()).thenReturn(0);
- List<Object[]> objects = runner.execute("{call my_proc()}", handler);
-
- Assert.assertEquals(3, objects.size());
- verify(call, times(1)).execute();
- verify(results, times(3)).close();
- verify(call, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we close the connection
-
- }
-
- private void callGoodExecuteWithResultSet(Connection conn) throws Exception {
- when(call.execute()).thenReturn(true);
-
- when(meta.getParameterCount()).thenReturn(2);
- runner.execute(conn, "{call my_proc(?, ?)}", handler, "unit", "test");
-
- verify(call, times(1)).execute();
- verify(results, times(1)).close();
- verify(call, times(1)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- runner.execute(conn, "{call my_proc()}", handler);
-
- verify(call, times(2)).execute();
- verify(results, times(2)).close();
- verify(call, times(2)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // Test single OUT parameter
- when(meta.getParameterCount()).thenReturn(1);
- when(call.getObject(1)).thenReturn(42);
- OutParameter<Integer> intParam =
- new OutParameter<Integer>(Types.INTEGER, Integer.class);
- runner.execute(conn, "{?= call my_proc()}", handler, intParam);
-
- Assert.assertEquals(42, intParam.getValue().intValue());
-
- verify(call, times(3)).execute();
- verify(results, times(3)).close();
- verify(call, times(3)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // Test OUT parameters with IN parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(4242);
- intParam.setValue(null);
- runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "unit", "test");
-
- Assert.assertEquals(4242, intParam.getValue().intValue());
-
- verify(call, times(4)).execute();
- verify(results, times(4)).close();
- verify(call, times(4)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
-
- // Test INOUT parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(24);
- when(call.getObject(3)).thenReturn("out");
- intParam.setValue(null);
- OutParameter<String> stringParam =
- new OutParameter<String>(Types.VARCHAR, String.class, "in");
- runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);
-
- Assert.assertEquals(24, intParam.getValue().intValue());
- Assert.assertEquals("out", stringParam.getValue());
-
- verify(call, times(5)).execute();
- verify(results, times(5)).close();
- verify(call, times(5)).close(); // make sure we closed the statement
- verify(conn, times(0)).close(); // make sure we do not close the connection
- }
-
- private void callGoodExecuteWithResultSet() throws Exception {
- when(call.execute()).thenReturn(true);
-
- when(meta.getParameterCount()).thenReturn(2);
- runner.execute("{call my_proc(?, ?)}", handler, "unit", "test");
-
- verify(call, times(1)).execute();
- verify(results, times(1)).close();
- verify(call, times(1)).close(); // make sure we closed the statement
- verify(conn, times(1)).close(); // make sure we do not close the connection
-
- // call the other variation of query
- when(meta.getParameterCount()).thenReturn(0);
- runner.execute("{call my_proc()}", handler);
-
- verify(call, times(2)).execute();
- verify(results, times(2)).close();
- verify(call, times(2)).close(); // make sure we closed the statement
- verify(conn, times(2)).close(); // make sure we do not close the connection
-
- // Test single OUT parameter
- when(meta.getParameterCount()).thenReturn(1);
- when(call.getObject(1)).thenReturn(42);
- OutParameter<Integer> intParam =
- new OutParameter<Integer>(Types.INTEGER, Integer.class);
- runner.execute("{?= call my_proc()}", handler, intParam);
-
- Assert.assertEquals(42, intParam.getValue().intValue());
-
- verify(call, times(3)).execute();
- verify(results, times(3)).close();
- verify(call, times(3)).close(); // make sure we closed the statement
- verify(conn, times(3)).close(); // make sure we do not close the connection
-
- // Test OUT parameters with IN parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(4242);
- intParam.setValue(null);
- runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "unit", "test");
-
- Assert.assertEquals(4242, intParam.getValue().intValue());
-
- verify(call, times(4)).execute();
- verify(results, times(4)).close();
- verify(call, times(4)).close(); // make sure we closed the statement
- verify(conn, times(4)).close(); // make sure we do not close the connection
-
- // Test INOUT parameters
- when(meta.getParameterCount()).thenReturn(3);
- when(call.getObject(1)).thenReturn(24);
- when(call.getObject(3)).thenReturn("out");
- intParam.setValue(null);
- OutParameter<String> stringParam =
- new OutParameter<String>(Types.VARCHAR, String.class, "in");
- runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);
-
- Assert.assertEquals(24, intParam.getValue().intValue());
- Assert.assertEquals("out", stringParam.getValue());
-
- verify(call, times(5)).execute();
- verify(results, times(5)).close();
- verify(call, times(5)).close(); // make sure we closed the statement
- verify(conn, times(5)).close(); // make sure we do not close the connection
- }
-
- @Test
- public void testGoodExecuteWithResultSet() throws Exception {
- callGoodExecuteWithResultSet();
- }
-
- @Test
- public void testGoodExecuteWithResultSetPmdTrue() throws Exception {
- runner = new QueryRunner(true);
- callGoodExecuteWithResultSet(conn);
- }
-
- @Test
- public void testGoodExecuteWithResultSetDefaultConstructor() throws Exception {
- runner = new QueryRunner();
- callGoodExecuteWithResultSet(conn);
- }
-
- // helper method for calling execute when an exception is expected
- private void callExecuteWithResultSetWithException(Object... params) throws Exception {
- boolean caught = false;
-
- try {
- when(call.execute()).thenReturn(true);
- when(meta.getParameterCount()).thenReturn(2);
- runner.query("{call my_proc(?, ?)}", handler, params);
-
- } catch(SQLException e) {
- caught = true;
- }
-
- if(!caught) {
- fail("Exception never thrown, but expected");
- }
- }
-
- @Test
- public void testNoParamsExecuteWithResultSet() throws Exception {
- callExecuteWithResultSetWithException();
- }
-
- @Test
- public void testTooFewParamsExecuteWithResultSet() throws Exception {
- callExecuteWithResultSetWithException("unit");
- }
-
- @Test
- public void testTooManyParamsExecuteWithResultSet() throws Exception {
- callExecuteWithResultSetWithException("unit", "test", "fail");
- }
-
- @Test(expected=SQLException.class)
- public void testNullConnectionExecuteWithResultSet() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
- when(dataSource.getConnection()).thenReturn(null);
-
- runner.execute("{call my_proc(?, ?)}", handler, "unit", "test");
- }
-
- @Test(expected=SQLException.class)
- public void testNullSqlExecuteWithResultSet() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.execute(null, handler);
- }
-
- @Test(expected=SQLException.class)
- public void testNullHandlerExecuteWithResultSet() throws Exception {
- when(meta.getParameterCount()).thenReturn(2);
-
- runner.execute("{call my_proc(?, ?)}", (ResultSetHandler)null);
- }
-
- @Test
- public void testExecuteWithResultSetException() throws Exception {
- doThrow(new SQLException()).when(stmt).execute();
-
- callExecuteWithResultSetWithException(handler, "unit", "test");
- }
-
- //
- // Random tests
- //
- class MyBean {
- private int a;
- private double b;
- private String c;
-
- public int getA() { return a; }
- public void setA(int a) { this.a = a; }
- public double getB() { return b; }
- public void setB(double b) { this.b = b; }
- public String getC() { return c; }
- public void setC(String c) { this.c = c; }
- }
-
- @Test
- public void testFillStatementWithBean() throws Exception {
- MyBean bean = new MyBean();
- when(meta.getParameterCount()).thenReturn(3);
- runner.fillStatementWithBean(stmt, bean, new String[] { "a", "b", "c" });
- }
-
- @Test(expected=NullPointerException.class)
- public void testFillStatementWithBeanNullNames() throws Exception {
- MyBean bean = new MyBean();
- when(meta.getParameterCount()).thenReturn(3);
- runner.fillStatementWithBean(stmt, bean, new String[] { "a", "b", null });
- }
-
- @Test(expected=SQLException.class)
- public void testBadPrepareConnection() throws Exception {
- runner = new QueryRunner();
- runner.update("update blah set unit = test");
- }
-}
+/*
+ * 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.dbutils;
+
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.ParameterMetaData;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.sql.DataSource;
+
+import org.apache.commons.dbutils.handlers.ArrayHandler;
+import org.apache.commons.dbutils.handlers.ScalarHandler;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+@SuppressWarnings("boxing") // test code
+public class QueryRunnerTest {
+ QueryRunner runner;
+ ArrayHandler handler;
+
+ @Mock DataSource dataSource;
+ @Mock Connection conn;
+ @Mock PreparedStatement stmt;
+ @Mock CallableStatement call;
+ @Mock ParameterMetaData meta;
+ @Mock ResultSet results;
+ @Mock ResultSetMetaData resultsMeta;
+
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this); // init the mocks
+
+ when(dataSource.getConnection()).thenReturn(conn);
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.getParameterMetaData()).thenReturn(meta);
+ when(stmt.getResultSet()).thenReturn(results);
+ when(stmt.executeQuery()).thenReturn(results);
+ when(conn.prepareCall(any(String.class))).thenReturn(call);
+ when(call.getParameterMetaData()).thenReturn(meta);
+ when(call.getResultSet()).thenReturn(results);
+ when(call.getMoreResults()).thenReturn(false);
+ when(results.next()).thenReturn(false);
+
+ handler = new ArrayHandler();
+ runner = new QueryRunner(dataSource);
+ }
+
+ //
+ // Batch test cases
+ //
+
+ private void callGoodBatch(Connection conn, Object[][] params) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.batch(conn, "select * from blah where ? = ?", params);
+
+ verify(stmt, times(2)).addBatch();
+ verify(stmt, times(1)).executeBatch();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection, since QueryRunner.batch(Connection, String, Object[][]) does not close connections
+ }
+
+ private void callGoodBatch(Object[][] params) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.batch("select * from blah where ? = ?", params);
+
+ verify(stmt, times(2)).addBatch();
+ verify(stmt, times(1)).executeBatch();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+ }
+
+ @Test
+ public void testGoodBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ callGoodBatch(params);
+ }
+
+ @Test
+ public void testGoodBatchPmdTrue() throws Exception {
+ runner = new QueryRunner(dataSource, true);
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ callGoodBatch(params);
+ }
+
+ @Test
+ public void testGoodBatchDefaultConstructor() throws Exception {
+ runner = new QueryRunner();
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ callGoodBatch(conn, params);
+ }
+
+ @Test
+ public void testNullParamsBatch() throws Exception {
+ String[][] params = new String[][] { { null, "unit" }, { "test", null } };
+
+ callGoodBatch(params);
+ }
+
+
+
+ // helper method for calling batch when an exception is expected
+ private void callBatchWithException(String sql, Object[][] params) throws Exception {
+ boolean caught = false;
+
+ try {
+ runner.batch(sql, params);
+
+ verify(stmt, times(2)).addBatch();
+ verify(stmt, times(1)).executeBatch();
+ verify(stmt, times(1)).close(); // make sure the statement is closed
+ verify(conn, times(1)).close(); // make sure the connection is closed
+ } catch(SQLException e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testTooFewParamsBatch() throws Exception {
+ String[][] params = new String[][] { { "unit" }, { "test" } };
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+ @Test
+ public void testTooManyParamsBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit", "unit" }, { "test", "test", "test" } };
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullConnectionBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.batch("select * from blah where ? = ?", params);
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullSqlBatch() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.batch(null, params);
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullParamsArgBatch() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.batch("select * from blah where ? = ?", null);
+ }
+
+ @Test
+ public void testAddBatchException() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ doThrow(new SQLException()).when(stmt).addBatch();
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+ @Test
+ public void testExecuteBatchException() throws Exception {
+ String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } };
+
+ doThrow(new SQLException()).when(stmt).executeBatch();
+
+ callBatchWithException("select * from blah where ? = ?", params);
+ }
+
+
+ //
+ // Query test cases
+ //
+ private void callGoodQuery(Connection conn) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query(conn, "select * from blah where ? = ?", handler, "unit", "test");
+
+ verify(stmt, times(1)).executeQuery();
+ verify(results, times(1)).close();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection, since QueryRunner.query(Connection, String, ResultSetHandler<T>, Object...) does not close connections
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.query(conn, "select * from blah", handler);
+
+ verify(stmt, times(2)).executeQuery();
+ verify(results, times(2)).close();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection, see above
+ }
+
+ private void callGoodQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query("select * from blah where ? = ?", handler, "unit", "test");
+
+ verify(stmt, times(1)).executeQuery();
+ verify(results, times(1)).close();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.query("select * from blah", handler);
+
+ verify(stmt, times(2)).executeQuery();
+ verify(results, times(2)).close();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(2)).close(); // make sure we closed the connection
+ }
+
+ @Test
+ public void testGoodQuery() throws Exception {
+ callGoodQuery();
+ }
+
+ @Test
+ public void testGoodQueryPmdTrue() throws Exception {
+ runner = new QueryRunner(true);
+ callGoodQuery(conn);
+ }
+
+ @Test
+ public void testGoodQueryDefaultConstructor() throws Exception {
+ runner = new QueryRunner();
+ callGoodQuery(conn);
+ }
+
+
+ // helper method for calling batch when an exception is expected
+ private void callQueryWithException(Object... params) throws Exception {
+ boolean caught = false;
+
+ try {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query("select * from blah where ? = ?", handler, params);
+
+ verify(stmt, never()).close(); // make sure the statement is still open
+ verify(stmt, times(1)).executeQuery();
+ verify(results, times(1)).close();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+ } catch(SQLException e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testNoParamsQuery() throws Exception {
+ callQueryWithException();
+ }
+
+ @Test
+ public void testTooFewParamsQuery() throws Exception {
+ callQueryWithException("unit");
+ }
+
+ @Test
+ public void testTooManyParamsQuery() throws Exception {
+ callQueryWithException("unit", "test", "fail");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullConnectionQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.query("select * from blah where ? = ?", handler, "unit", "test");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullSqlQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.query(null, handler);
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullHandlerQuery() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.query("select * from blah where ? = ?", null);
+ }
+
+ @Test
+ public void testExecuteQueryException() throws Exception {
+ doThrow(new SQLException()).when(stmt).executeQuery();
+
+ callQueryWithException(handler, "unit", "test");
+ }
+
+
+ //
+ // Update test cases
+ //
+ private void callGoodUpdate(Connection conn) throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.update(conn, "update blah set ? = ?", "unit", "test");
+
+ verify(stmt, times(1)).executeUpdate();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection, since QueryRunner.update(Connection, String, Object...) does not close connections
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.update(conn, "update blah set unit = test");
+
+ verify(stmt, times(2)).executeUpdate();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection, see above
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(1);
+ runner.update(conn, "update blah set unit = ?", "test");
+
+ verify(stmt, times(3)).executeUpdate();
+ verify(stmt, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection, see above
+ }
+
+ private void callGoodUpdate() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.update("update blah set ? = ?", "unit", "test");
+
+ verify(stmt, times(1)).executeUpdate();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.update("update blah set unit = test");
+
+ verify(stmt, times(2)).executeUpdate();
+ verify(stmt, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(2)).close(); // make sure we closed the connection
+
+ // call the other variation
+ when(meta.getParameterCount()).thenReturn(1);
+ runner.update("update blah set unit = ?", "test");
+
+ verify(stmt, times(3)).executeUpdate();
+ verify(stmt, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(3)).close(); // make sure we closed the connection
+ }
+
+ @Test
+ public void testGoodUpdate() throws Exception {
+ callGoodUpdate();
+ }
+
+ @Test
+ public void testGoodUpdatePmdTrue() throws Exception {
+ runner = new QueryRunner(true);
+ callGoodUpdate(conn);
+ }
+
+ @Test
+ public void testGoodUpdateDefaultConstructor() throws Exception {
+ runner = new QueryRunner();
+ callGoodUpdate(conn);
+ }
+
+ @Test
+ public void testGoodInsert() throws Exception {
+ results = mock(ResultSet.class);
+
+ when(meta.getParameterCount()).thenReturn(2);
+ when(conn.prepareStatement(any(String.class), eq(Statement.RETURN_GENERATED_KEYS))).thenReturn(stmt);
+ when(stmt.getGeneratedKeys()).thenReturn(results);
+ when(results.next()).thenReturn(true).thenReturn(false);
+ when(results.getObject(1)).thenReturn(1L);
+
+ Long generatedKey = runner.insert("INSERT INTO blah(col1, col2) VALUES(?,?)", new ScalarHandler<Long>(), "unit", "test");
+
+ verify(stmt, times(1)).executeUpdate();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+
+ Assert.assertEquals(1L, generatedKey.longValue());
+ }
+
+ @Test
+ public void testGoodBatchInsert() throws Exception {
+ results = mock(ResultSet.class);
+ resultsMeta = mock(ResultSetMetaData.class);
+
+ when(meta.getParameterCount()).thenReturn(2);
+ when(conn.prepareStatement(any(String.class), eq(Statement.RETURN_GENERATED_KEYS))).thenReturn(stmt);
+ when(stmt.getGeneratedKeys()).thenReturn(results);
+ when(results.next()).thenReturn(true).thenReturn(true).thenReturn(false);
+ when(results.getMetaData()).thenReturn(resultsMeta);
+ when(resultsMeta.getColumnCount()).thenReturn(1);
+
+ ResultSetHandler<List<Object>> handler = new ResultSetHandler<List<Object>>()
+ {
+ @Override
+ public List<Object> handle(ResultSet rs) throws SQLException
+ {
+ List<Object> objects = new ArrayList<Object>();
+ while (rs.next())
+ {
+ objects.add(new Object());
+ }
+ return objects;
+ }
+ };
+
+ Object[][] params = new Object[2][2];
+ params[0][0] = "Test";
+ params[0][1] = "Blah";
+ params[1][0] = "Test2";
+ params[1][1] = "Blah2";
+
+ List<Object> generatedKeys = runner.insertBatch("INSERT INTO blah(col1, col2) VALUES(?,?)", handler, params);
+
+ verify(stmt, times(2)).addBatch();
+ verify(stmt, times(1)).executeBatch();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+
+ Assert.assertEquals(2, generatedKeys.size());
+ }
+
+ // helper method for calling batch when an exception is expected
+ private void callUpdateWithException(Object... params) throws Exception {
+ boolean caught = false;
+
+ try {
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.update("select * from blah where ? = ?", params);
+
+ verify(stmt, times(1)).executeUpdate();
+ verify(stmt, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we closed the connection
+ } catch(SQLException e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testNoParamsUpdate() throws Exception {
+ callUpdateWithException();
+ }
+
+ @Test
+ public void testTooFewParamsUpdate() throws Exception {
+ callUpdateWithException("unit");
+ }
+
+ @Test
+ public void testTooManyParamsUpdate() throws Exception {
+ callUpdateWithException("unit", "test", "fail");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullConnectionUpdate() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.update("select * from blah where ? = ?", "unit", "test");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullSqlUpdate() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.update(null);
+ }
+
+ @Test
+ public void testExecuteUpdateException() throws Exception {
+ doThrow(new SQLException()).when(stmt).executeUpdate();
+
+ callUpdateWithException("unit", "test");
+ }
+
+ @Test
+ public void testStatementConfiguration() throws Exception {
+ StatementConfiguration stmtConfig = new StatementConfiguration(1, 2, 3, 4, 5);
+ QueryRunner queryRunner = new QueryRunner(stmtConfig);
+ queryRunner.prepareStatement(conn, "select 1");
+
+ verify(stmt).setFetchDirection(eq(1));
+ verify(stmt).setFetchSize(eq(2));
+ verify(stmt).setMaxFieldSize(eq(3));
+ verify(stmt).setMaxRows(eq(4));
+ verify(stmt).setQueryTimeout(eq(5));
+ }
+
+ //
+ // Execute tests
+ //
+ private void callGoodExecute(Connection conn) throws Exception {
+ when(call.execute()).thenReturn(false);
+ when(call.getUpdateCount()).thenReturn(3);
+
+ when(meta.getParameterCount()).thenReturn(2);
+ int result = runner.execute(conn, "{call my_proc(?, ?)}", "unit", "test");
+
+ Assert.assertEquals(3, result);
+
+ verify(call, times(1)).execute();
+ verify(call, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ result = runner.execute(conn, "{call my_proc()}");
+
+ Assert.assertEquals(3, result);
+
+ verify(call, times(2)).execute();
+ verify(call, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // Test single OUT parameter
+ when(meta.getParameterCount()).thenReturn(1);
+ when(call.getObject(1)).thenReturn(42);
+ OutParameter<Integer> intParam =
+ new OutParameter<Integer>(Types.INTEGER, Integer.class);
+ result = runner.execute(conn, "{?= call my_proc()}", intParam);
+
+ Assert.assertEquals(42, intParam.getValue().intValue());
+ Assert.assertEquals(3, result);
+
+ verify(call, times(3)).execute();
+ verify(call, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // Test OUT parameters with IN parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(4242);
+ intParam.setValue(null);
+ result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "unit", "test");
+
+ Assert.assertEquals(4242, intParam.getValue().intValue());
+ Assert.assertEquals(3, result);
+
+ verify(call, times(4)).execute();
+ verify(call, times(4)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // Test INOUT parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(24);
+ when(call.getObject(3)).thenReturn("out");
+ intParam.setValue(null);
+ OutParameter<String> stringParam =
+ new OutParameter<String>(Types.VARCHAR, String.class, "in");
+ result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "test", stringParam);
+
+ Assert.assertEquals(24, intParam.getValue().intValue());
+ Assert.assertEquals("out", stringParam.getValue());
+ Assert.assertEquals(3, result);
+
+ verify(call, times(5)).execute();
+ verify(call, times(5)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+ }
+
+ private void callGoodExecute() throws Exception {
+ when(call.execute()).thenReturn(false);
+ when(call.getUpdateCount()).thenReturn(3);
+
+ when(meta.getParameterCount()).thenReturn(2);
+ int result = runner.execute("{call my_proc(?, ?)}", "unit", "test");
+
+ Assert.assertEquals(3, result);
+
+ verify(call, times(1)).execute();
+ verify(call, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we do not close the connection
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ result = runner.execute("{call my_proc()}");
+
+ Assert.assertEquals(3, result);
+
+ verify(call, times(2)).execute();
+ verify(call, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(2)).close(); // make sure we do not close the connection
+
+ // Test single OUT parameter
+ when(meta.getParameterCount()).thenReturn(1);
+ when(call.getObject(1)).thenReturn(42);
+ OutParameter<Integer> intParam =
+ new OutParameter<Integer>(Types.INTEGER, Integer.class);
+ result = runner.execute("{?= call my_proc()}", intParam);
+
+ Assert.assertEquals(42, intParam.getValue().intValue());
+ Assert.assertEquals(3, result);
+
+ verify(call, times(3)).execute();
+ verify(call, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(3)).close(); // make sure we do not close the connection
+
+ // Test OUT parameters with IN parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(4242);
+ intParam.setValue(null);
+ result = runner.execute("{?= call my_proc(?, ?)}", intParam, "unit", "test");
+
+ Assert.assertEquals(4242, intParam.getValue().intValue());
+ Assert.assertEquals(3, result);
+
+ verify(call, times(4)).execute();
+ verify(call, times(4)).close(); // make sure we closed the statement
+ verify(conn, times(4)).close(); // make sure we do not close the connection
+
+ // Test INOUT parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(24);
+ when(call.getObject(3)).thenReturn("out");
+ intParam.setValue(null);
+ OutParameter<String> stringParam =
+ new OutParameter<String>(Types.VARCHAR, String.class, "in");
+ result = runner.execute("{?= call my_proc(?, ?)}", intParam, "test", stringParam);
+
+ Assert.assertEquals(24, intParam.getValue().intValue());
+ Assert.assertEquals("out", stringParam.getValue());
+ Assert.assertEquals(3, result);
+
+ verify(call, times(5)).execute();
+ verify(call, times(5)).close(); // make sure we closed the statement
+ verify(conn, times(5)).close(); // make sure we do not close the connection
+ }
+
+ @Test
+ public void testGoodExecute() throws Exception {
+ callGoodExecute();
+ }
+
+ @Test
+ public void testGoodExecutePmdTrue() throws Exception {
+ runner = new QueryRunner(true);
+ callGoodExecute(conn);
+ }
+
+ @Test
+ public void testGoodExecuteDefaultConstructor() throws Exception {
+ runner = new QueryRunner();
+ callGoodExecute(conn);
+ }
+
+ // helper method for calling execute when an exception is expected
+ private void callExecuteWithException(Object... params) throws Exception {
+ boolean caught = false;
+
+ try {
+ when(call.execute()).thenReturn(false);
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query("{call my_proc(?, ?)}", handler, params);
+
+ } catch(SQLException e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testNoParamsExecute() throws Exception {
+ callExecuteWithException();
+ }
+
+ @Test
+ public void testTooFewParamsExecute() throws Exception {
+ callExecuteWithException("unit");
+ }
+
+ @Test
+ public void testTooManyParamsExecute() throws Exception {
+ callExecuteWithException("unit", "test", "fail");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullConnectionExecute() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.execute("{call my_proc(?, ?)}", "unit", "test");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullSqlExecute() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.execute(null);
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullHandlerExecute() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.execute("{call my_proc(?, ?)}");
+ }
+
+ @Test
+ public void testExecuteException() throws Exception {
+ doThrow(new SQLException()).when(stmt).execute();
+
+ callExecuteWithException(handler, "unit", "test");
+ }
+
+ //
+ // Execute with ResultSetHandler
+ //
+
+ @Test
+ public void testExecuteWithMultipleResultSets() throws Exception {
+ when(call.execute()).thenReturn(true);
+ when(call.getMoreResults()).thenAnswer(new Answer<Boolean>()
+ {
+ int count = 1;
+ @Override
+ public Boolean answer(InvocationOnMock invocation)
+ {
+ return ++count <= 3;
+ }
+ });
+ when(meta.getParameterCount()).thenReturn(0);
+ List<Object[]> objects = runner.execute("{call my_proc()}", handler);
+
+ Assert.assertEquals(3, objects.size());
+ verify(call, times(1)).execute();
+ verify(results, times(3)).close();
+ verify(call, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we close the connection
+
+ }
+
+ private void callGoodExecuteWithResultSet(Connection conn) throws Exception {
+ when(call.execute()).thenReturn(true);
+
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.execute(conn, "{call my_proc(?, ?)}", handler, "unit", "test");
+
+ verify(call, times(1)).execute();
+ verify(results, times(1)).close();
+ verify(call, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.execute(conn, "{call my_proc()}", handler);
+
+ verify(call, times(2)).execute();
+ verify(results, times(2)).close();
+ verify(call, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // Test single OUT parameter
+ when(meta.getParameterCount()).thenReturn(1);
+ when(call.getObject(1)).thenReturn(42);
+ OutParameter<Integer> intParam =
+ new OutParameter<Integer>(Types.INTEGER, Integer.class);
+ runner.execute(conn, "{?= call my_proc()}", handler, intParam);
+
+ Assert.assertEquals(42, intParam.getValue().intValue());
+
+ verify(call, times(3)).execute();
+ verify(results, times(3)).close();
+ verify(call, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // Test OUT parameters with IN parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(4242);
+ intParam.setValue(null);
+ runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "unit", "test");
+
+ Assert.assertEquals(4242, intParam.getValue().intValue());
+
+ verify(call, times(4)).execute();
+ verify(results, times(4)).close();
+ verify(call, times(4)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+
+ // Test INOUT parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(24);
+ when(call.getObject(3)).thenReturn("out");
+ intParam.setValue(null);
+ OutParameter<String> stringParam =
+ new OutParameter<String>(Types.VARCHAR, String.class, "in");
+ runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);
+
+ Assert.assertEquals(24, intParam.getValue().intValue());
+ Assert.assertEquals("out", stringParam.getValue());
+
+ verify(call, times(5)).execute();
+ verify(results, times(5)).close();
+ verify(call, times(5)).close(); // make sure we closed the statement
+ verify(conn, times(0)).close(); // make sure we do not close the connection
+ }
+
+ private void callGoodExecuteWithResultSet() throws Exception {
+ when(call.execute()).thenReturn(true);
+
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.execute("{call my_proc(?, ?)}", handler, "unit", "test");
+
+ verify(call, times(1)).execute();
+ verify(results, times(1)).close();
+ verify(call, times(1)).close(); // make sure we closed the statement
+ verify(conn, times(1)).close(); // make sure we do not close the connection
+
+ // call the other variation of query
+ when(meta.getParameterCount()).thenReturn(0);
+ runner.execute("{call my_proc()}", handler);
+
+ verify(call, times(2)).execute();
+ verify(results, times(2)).close();
+ verify(call, times(2)).close(); // make sure we closed the statement
+ verify(conn, times(2)).close(); // make sure we do not close the connection
+
+ // Test single OUT parameter
+ when(meta.getParameterCount()).thenReturn(1);
+ when(call.getObject(1)).thenReturn(42);
+ OutParameter<Integer> intParam =
+ new OutParameter<Integer>(Types.INTEGER, Integer.class);
+ runner.execute("{?= call my_proc()}", handler, intParam);
+
+ Assert.assertEquals(42, intParam.getValue().intValue());
+
+ verify(call, times(3)).execute();
+ verify(results, times(3)).close();
+ verify(call, times(3)).close(); // make sure we closed the statement
+ verify(conn, times(3)).close(); // make sure we do not close the connection
+
+ // Test OUT parameters with IN parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(4242);
+ intParam.setValue(null);
+ runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "unit", "test");
+
+ Assert.assertEquals(4242, intParam.getValue().intValue());
+
+ verify(call, times(4)).execute();
+ verify(results, times(4)).close();
+ verify(call, times(4)).close(); // make sure we closed the statement
+ verify(conn, times(4)).close(); // make sure we do not close the connection
+
+ // Test INOUT parameters
+ when(meta.getParameterCount()).thenReturn(3);
+ when(call.getObject(1)).thenReturn(24);
+ when(call.getObject(3)).thenReturn("out");
+ intParam.setValue(null);
+ OutParameter<String> stringParam =
+ new OutParameter<String>(Types.VARCHAR, String.class, "in");
+ runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);
+
+ Assert.assertEquals(24, intParam.getValue().intValue());
+ Assert.assertEquals("out", stringParam.getValue());
+
+ verify(call, times(5)).execute();
+ verify(results, times(5)).close();
+ verify(call, times(5)).close(); // make sure we closed the statement
+ verify(conn, times(5)).close(); // make sure we do not close the connection
+ }
+
+ @Test
+ public void testGoodExecuteWithResultSet() throws Exception {
+ callGoodExecuteWithResultSet();
+ }
+
+ @Test
+ public void testGoodExecuteWithResultSetPmdTrue() throws Exception {
+ runner = new QueryRunner(true);
+ callGoodExecuteWithResultSet(conn);
+ }
+
+ @Test
+ public void testGoodExecuteWithResultSetDefaultConstructor() throws Exception {
+ runner = new QueryRunner();
+ callGoodExecuteWithResultSet(conn);
+ }
+
+ // helper method for calling execute when an exception is expected
+ private void callExecuteWithResultSetWithException(Object... params) throws Exception {
+ boolean caught = false;
+
+ try {
+ when(call.execute()).thenReturn(true);
+ when(meta.getParameterCount()).thenReturn(2);
+ runner.query("{call my_proc(?, ?)}", handler, params);
+
+ } catch(SQLException e) {
+ caught = true;
+ }
+
+ if(!caught) {
+ fail("Exception never thrown, but expected");
+ }
+ }
+
+ @Test
+ public void testNoParamsExecuteWithResultSet() throws Exception {
+ callExecuteWithResultSetWithException();
+ }
+
+ @Test
+ public void testTooFewParamsExecuteWithResultSet() throws Exception {
+ callExecuteWithResultSetWithException("unit");
+ }
+
+ @Test
+ public void testTooManyParamsExecuteWithResultSet() throws Exception {
+ callExecuteWithResultSetWithException("unit", "test", "fail");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullConnectionExecuteWithResultSet() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+ when(dataSource.getConnection()).thenReturn(null);
+
+ runner.execute("{call my_proc(?, ?)}", handler, "unit", "test");
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullSqlExecuteWithResultSet() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.execute(null, handler);
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullHandlerExecuteWithResultSet() throws Exception {
+ when(meta.getParameterCount()).thenReturn(2);
+
+ runner.execute("{call my_proc(?, ?)}", (ResultSetHandler)null);
+ }
+
+ @Test
+ public void testExecuteWithResultSetException() throws Exception {
+ doThrow(new SQLException()).when(stmt).execute();
+
+ callExecuteWithResultSetWithException(handler, "unit", "test");
+ }
+
+ //
+ // Random tests
+ //
+ class MyBean {
+ private int a;
+ private double b;
+ private String c;
+
+ public int getA() { return a; }
+ public void setA(int a) { this.a = a; }
+ public double getB() { return b; }
+ public void setB(double b) { this.b = b; }
+ public String getC() { return c; }
+ public void setC(String c) { this.c = c; }
+ }
+
+ @Test
+ public void testFillStatementWithBean() throws Exception {
+ MyBean bean = new MyBean();
+ when(meta.getParameterCount()).thenReturn(3);
+ runner.fillStatementWithBean(stmt, bean, new String[] { "a", "b", "c" });
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testFillStatementWithBeanNullNames() throws Exception {
+ MyBean bean = new MyBean();
+ when(meta.getParameterCount()).thenReturn(3);
+ runner.fillStatementWithBean(stmt, bean, new String[] { "a", "b", null });
+ }
+
+ @Test(expected=SQLException.class)
+ public void testBadPrepareConnection() throws Exception {
+ runner = new QueryRunner();
+ runner.update("update blah set unit = test");
+ }
+}