http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java deleted file mode 100644 index e259e42..0000000 --- a/src/test/java/org/apache/commons/dbutils/handlers/KeyedHandlerTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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.handlers; - -import java.sql.SQLException; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.commons.dbutils.BaseTestCase; -import org.apache.commons.dbutils.ResultSetHandler; - -public class KeyedHandlerTest extends BaseTestCase { - - public void testHandle() throws SQLException { - ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<String>(); - - Map<String,Map<String,Object>> results = h.handle(this.rs); - - assertNotNull(results); - assertEquals(ROWS, results.size()); - - Map<String,Object> row = null; - for(Entry<String, Map<String, Object>> entry : results.entrySet()) - { - Object key = entry.getKey(); - assertNotNull(key); - row = entry.getValue(); - assertNotNull(row); - assertEquals(COLS, row.keySet().size()); - } - row = results.get("1"); - assertEquals("1", row.get("one")); - assertEquals("2", row.get("TWO")); - assertEquals("3", row.get("Three")); - } - - public void testColumnIndexHandle() throws SQLException { - ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<String>(2); - Map<String,Map<String,Object>> results = h.handle(this.rs); - - assertNotNull(results); - assertEquals(ROWS, results.size()); - - Map<String,Object> row = null; - for(Entry<String, Map<String, Object>> entry : results.entrySet()) - { - Object key = entry.getKey(); - assertNotNull(key); - row = entry.getValue(); - assertNotNull(row); - assertEquals(COLS, row.keySet().size()); - } - row = results.get("5"); - assertEquals("4", row.get("one")); - assertEquals("5", row.get("TWO")); - assertEquals("6", row.get("Three")); - } - - public void testColumnNameHandle() throws SQLException { - ResultSetHandler<Map<Integer,Map<String,Object>>> h = new KeyedHandler<Integer>("intTest"); - Map<Integer,Map<String,Object>> results = h.handle(this.rs); - - assertNotNull(results); - assertEquals(ROWS, results.size()); - - Map<String,Object> row = null; - for(Entry<Integer, Map<String, Object>> entry : results.entrySet()) - { - Object key = entry.getKey(); - assertNotNull(key); - row = entry.getValue(); - assertNotNull(row); - assertEquals(COLS, row.keySet().size()); - } - row = results.get(Integer.valueOf(3)); - assertEquals("4", row.get("one")); - assertEquals("5", row.get("TWO")); - assertEquals("6", row.get("Three")); - } - - public void testEmptyResultSetHandle() throws SQLException { - ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<String>(); - Map<String,Map<String,Object>> results = h.handle(this.emptyResultSet); - assertNotNull(results); - assertTrue(results.isEmpty()); - } -}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java deleted file mode 100644 index 741e035..0000000 --- a/src/test/java/org/apache/commons/dbutils/handlers/MapHandlerTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.handlers; - -import java.sql.SQLException; -import java.util.Map; - -import org.apache.commons.dbutils.BaseTestCase; -import org.apache.commons.dbutils.ResultSetHandler; - -/** - * MapHandlerTest - */ -public class MapHandlerTest extends BaseTestCase { - - public void testHandle() throws SQLException { - ResultSetHandler<Map<String,Object>> h = new MapHandler(); - Map<String,Object> results = h.handle(this.rs); - - assertNotNull(results); - assertEquals(COLS, results.keySet().size()); - assertEquals("1", results.get("ONE")); - assertEquals("2", results.get("two")); - assertEquals("3", results.get("Three")); - } - - public void testEmptyResultSetHandle() throws SQLException { - ResultSetHandler<Map<String,Object>> h = new MapHandler(); - Map<String,Object> results = h.handle(this.emptyResultSet); - - assertNull(results); - } - -} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java deleted file mode 100644 index 275a591..0000000 --- a/src/test/java/org/apache/commons/dbutils/handlers/MapListHandlerTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.handlers; - -import java.sql.SQLException; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.apache.commons.dbutils.BaseTestCase; -import org.apache.commons.dbutils.ResultSetHandler; - -/** - * MapListHandlerTest - */ -public class MapListHandlerTest extends BaseTestCase { - - public void testHandle() throws SQLException { - ResultSetHandler<List<Map<String,Object>>> h = new MapListHandler(); - List<Map<String,Object>> results = h.handle(this.rs); - - assertNotNull(results); - assertEquals(ROWS, results.size()); - - Iterator<Map<String,Object>> iter = results.iterator(); - Map<String,Object> row = null; - assertTrue(iter.hasNext()); - row = iter.next(); - assertEquals(COLS, row.keySet().size()); - assertEquals("1", row.get("one")); - assertEquals("2", row.get("TWO")); - assertEquals("3", row.get("Three")); - - assertTrue(iter.hasNext()); - row = iter.next(); - assertEquals(COLS, row.keySet().size()); - - assertEquals("4", row.get("one")); - assertEquals("5", row.get("TWO")); - assertEquals("6", row.get("Three")); - - assertFalse(iter.hasNext()); - } - - public void testEmptyResultSetHandle() throws SQLException { - ResultSetHandler<List<Map<String,Object>>> h = new MapListHandler(); - List<Map<String,Object>> results = h.handle(this.emptyResultSet); - - assertNotNull(results); - assertTrue(results.isEmpty()); - } - -} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java b/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java deleted file mode 100644 index afebdbd..0000000 --- a/src/test/java/org/apache/commons/dbutils/handlers/ScalarHandlerTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.handlers; - -import java.sql.SQLException; - -import org.apache.commons.dbutils.BaseTestCase; -import org.apache.commons.dbutils.ResultSetHandler; - -public class ScalarHandlerTest extends BaseTestCase { - - public void testHandle() throws SQLException { - ResultSetHandler<String> h = new ScalarHandler<String>(); - Object results = h.handle(this.rs); - assertNotNull(results); - assertEquals("1", results); - } - - public void testColumnIndexHandle() throws SQLException { - ResultSetHandler<String> h = new ScalarHandler<String>(2); - Object results = h.handle(this.rs); - assertNotNull(results); - assertEquals("2", results); - } - - public void testColumnNameHandle() throws SQLException { - ResultSetHandler<Integer> h = new ScalarHandler<Integer>("intTest"); - Object results = h.handle(this.rs); - assertNotNull(results); - assertEquals(Integer.valueOf(1), results); - } - - public void testEmptyResultSetHandle() throws SQLException { - ResultSetHandler<String> h = new ScalarHandler<String>(); - Object results = h.handle(this.emptyResultSet); - assertNull(results); - } - -} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/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 deleted file mode 100644 index e40f8b7..0000000 --- a/src/test/java/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java +++ /dev/null @@ -1,1020 +0,0 @@ -/* - * 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; - } - -} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java b/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java deleted file mode 100644 index 3df4070..0000000 --- a/src/test/java/org/apache/commons/dbutils/wrappers/StringTrimmedResultSetTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.sql.ResultSet; -import java.sql.SQLException; - -import org.apache.commons.dbutils.BaseTestCase; -import org.apache.commons.dbutils.MockResultSet; -import org.apache.commons.dbutils.ProxyFactory; - -/** - * StringTrimmedResultSetTest - */ -public class StringTrimmedResultSetTest extends BaseTestCase { - - @Override - public void setUp() throws Exception { - super.setUp(); - this.rs = StringTrimmedResultSet.wrap(this.rs); - } - - public void testGetString() throws SQLException { - this.rs.next(); - assertEquals("notInBean", rs.getString(4)); - } - - public void testGetObject() throws SQLException { - this.rs.next(); - assertEquals("notInBean", rs.getObject(4)); - } - - /** - * Make sure 2 wrappers work together. - * @throws SQLException if a database access error occurs - */ - public void testMultipleWrappers() throws Exception { - // Create a ResultSet with data - Object[][] rows = new Object[][] { { null } - }; - ResultSet rs = MockResultSet.create(metaData, rows); - - // Wrap the ResultSet with a null checked version - SqlNullCheckedResultSet ncrs = new SqlNullCheckedResultSet(rs); - ncrs.setNullString(" trim this "); - rs = ProxyFactory.instance().createResultSet(ncrs); - - // Wrap the wrapper with a string trimmed version - rs = StringTrimmedResultSet.wrap(rs); - - rs.next(); - assertEquals("trim this", rs.getString(1)); - } - -} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java new file mode 100644 index 0000000..61fcc91 --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbutils2; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Types; + +import org.apache.commons.dbutils2.AbstractExecutor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class AbstractExecutorTest { + + @SuppressWarnings("rawtypes") // don't care about this in the unit test + private AbstractExecutor executor; + + @Mock private Connection conn; + @Mock private PreparedStatement stmt; + + @Before + public void setup() throws SQLException { + MockitoAnnotations.initMocks(this); + + when(conn.prepareStatement(any(String.class))).thenReturn(stmt); + } + + @SuppressWarnings("rawtypes") + public void createExecutor(String sql) throws SQLException { + executor = new AbstractExecutor(conn, sql) { }; + } + + @Test + public void testGoodSql() throws SQLException { + createExecutor("select * from blah :first = first and :last=last and phone=:phone"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last and phone=?"); + + executor.bind("first", "first_name") + .bind(":last", "last_name") + .bind("phone", Integer.valueOf(12345)); + + verify(stmt, times(1)).setObject(1, "first_name"); + verify(stmt, times(1)).setObject(2, "last_name"); + verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345))); + + executor.throwIfUnmappedParams(); + } + + @Test + public void testNoParamsSql() throws SQLException { + createExecutor("select * from blah"); + + verify(conn, times(1)).prepareStatement("select * from blah"); + verify(stmt, times(0)).setObject(any(Integer.class), any(Object.class)); + + executor.throwIfUnmappedParams(); + } + + @Test(expected=SQLException.class) + public void testMissingParamSql() throws SQLException { + createExecutor("select * from blah :first = first and :last=last"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); + + executor.bind("first", "first_name") + .bind(":last", "last_name") + .bind("phone", Integer.valueOf(12345)); // should throw + + verify(stmt, times(1)).setObject(1, "first_name"); + verify(stmt, times(1)).setObject(2, "last_name"); + verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345))); + } + + @Test(expected=SQLException.class) + public void testDoubleBind() throws SQLException { + createExecutor("select * from blah :first = first and :last=last"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); + + executor.bind("first", "first_name") + .bind(":last", "last_name") + .bind(":last", "last_name"); + + verify(stmt, times(1)).setObject(1, "first_name"); + verify(stmt, times(1)).setObject(2, "last_name"); + } + + @Test + public void testNullBind() throws SQLException { + createExecutor("select * from blah :first = first and :last=last"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); + + executor.bindNull("first") + .bindNull(":last", Types.NULL); + + verify(stmt, times(1)).setNull(1, Types.VARCHAR); + verify(stmt, times(1)).setNull(2, Types.NULL); + } +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/AsyncExecutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/AsyncExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/AsyncExecutorTest.java new file mode 100644 index 0000000..fe7f6f7 --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils2/AsyncExecutorTest.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbutils2; + +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import java.sql.SQLException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; + +import org.apache.commons.dbutils2.AsyncExecutor; +import org.apache.commons.dbutils2.InsertExecutor; +import org.apache.commons.dbutils2.QueryExecutor; +import org.apache.commons.dbutils2.QueryRunner; +import org.apache.commons.dbutils2.ResultSetHandler; +import org.apache.commons.dbutils2.UpdateExecutor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@SuppressWarnings("boxing") // test code +public class AsyncExecutorTest { + AsyncExecutor runner; + + @Mock QueryRunner qRunner; + @Mock ResultSetHandler<Object> handler; + @Mock QueryExecutor queryExecutor; + @Mock UpdateExecutor updateExecutor; + @Mock InsertExecutor insertExecutor; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); // init the mocks + + runner = new AsyncExecutor(Executors.newFixedThreadPool(1)); + } + + @Test + public void testQueryExecutor() throws Exception { + runner.execute(queryExecutor, handler).get(); + + verify(queryExecutor, times(1)).execute(handler); + } + + @Test(expected=ExecutionException.class) + public void testQueryExecutorException() throws Exception { + doThrow(SQLException.class).when(queryExecutor).execute(handler); + runner.execute(queryExecutor, handler).get(); + + verify(queryExecutor, times(1)).execute(handler); + } + + @Test + public void testUpdateExecutor() throws Exception { + runner.execute(updateExecutor).get(); + + verify(updateExecutor, times(1)).execute(); + } + + @Test(expected=ExecutionException.class) + public void testUpdateExecutorException() throws Exception { + doThrow(SQLException.class).when(updateExecutor).execute(); + runner.execute(updateExecutor).get(); + + verify(updateExecutor, times(1)).execute(); + } + + @Test + public void testInsertExecutor() throws Exception { + runner.execute(insertExecutor, handler).get(); + + verify(insertExecutor, times(1)).execute(handler); + } + + @Test(expected=ExecutionException.class) + public void testInsertExecutorException() throws Exception { + doThrow(SQLException.class).when(insertExecutor).execute(handler); + runner.execute(insertExecutor, handler).get(); + + verify(insertExecutor, times(1)).execute(handler); + } + + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/BaseResultSetHandlerTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/BaseResultSetHandlerTestCase.java b/src/test/java/org/apache/commons/dbutils2/BaseResultSetHandlerTestCase.java new file mode 100644 index 0000000..f9cd433 --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils2/BaseResultSetHandlerTestCase.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbutils2; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +import org.apache.commons.dbutils2.BaseResultSetHandler; +import org.junit.Test; + +public final class BaseResultSetHandlerTestCase extends BaseTestCase { + + @Test + public void handleWithoutExplicitResultSetInvocation() throws Exception { + Collection<Map<String, Object>> result = new ToMapCollectionHandler().handle(createMockResultSet()); + + assertFalse(result.isEmpty()); + + for (Map<String, Object> current : result) { + assertTrue(current.containsKey("one")); + assertTrue(current.containsKey("two")); + assertTrue(current.containsKey("three")); + assertTrue(current.containsKey("notInBean")); + assertTrue(current.containsKey("intTest")); + assertTrue(current.containsKey("integerTest")); + assertTrue(current.containsKey("nullObjectTest")); + assertTrue(current.containsKey("nullPrimitiveTest")); + assertTrue(current.containsKey("notDate")); + assertTrue(current.containsKey("columnProcessorDoubleTest")); + } + } + + private static final class ToMapCollectionHandler + extends BaseResultSetHandler<Collection<Map<String, Object>>> { + + @Override + protected Collection<Map<String, Object>> handle() throws SQLException { + Collection<Map<String, Object>> result = new LinkedList<Map<String, Object>>(); + + while (next()) { + Map<String, Object> current = new HashMap<String, Object>(); + + for (int i = 1; i <= getMetaData().getColumnCount(); i++) { + current.put(getMetaData().getColumnName(i), getObject(i)); + } + + result.add(current); + } + + return result; + } + + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/BaseTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/BaseTestCase.java b/src/test/java/org/apache/commons/dbutils2/BaseTestCase.java new file mode 100644 index 0000000..0debc1f --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils2/BaseTestCase.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbutils2; + +import java.math.BigInteger; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +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); + + private static final Object[] row1 = + new Object[] { + "1", + "2", + "3", + " notInBean ", + Integer.valueOf(1), + Integer.valueOf(2), + null, + null, + new Date(), + BigInteger.valueOf(13)}; + + private static final Object[] row2 = + new Object[] { + "4", + "5", + "6", + " notInBean ", + Integer.valueOf(3), + Integer.valueOf(4), + null, + null, + new Date(), + 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()); + } +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/BasicRowProcessorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/BasicRowProcessorTest.java b/src/test/java/org/apache/commons/dbutils2/BasicRowProcessorTest.java new file mode 100644 index 0000000..684e9db --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils2/BasicRowProcessorTest.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbutils2; + +import java.sql.SQLException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import org.apache.commons.dbutils2.BasicRowProcessor; +import org.apache.commons.dbutils2.RowProcessor; + +/** + * Test the BasicRowProcessor class. + */ +public class BasicRowProcessorTest extends BaseTestCase { + + private static final RowProcessor processor = new BasicRowProcessor(); + + /** + * Format that matches Date.toString(). + * Sun Mar 14 15:19:15 MST 2004 + */ + private static final DateFormat datef = + new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + + public void testToArray() throws SQLException { + + Object[] a = null; + assertTrue(this.rs.next()); + a = processor.toArray(this.rs); + assertEquals(COLS, a.length); + assertEquals("1", a[0]); + assertEquals("2", a[1]); + assertEquals("3", a[2]); + + assertTrue(this.rs.next()); + a = processor.toArray(this.rs); + assertEquals(COLS, a.length); + + assertEquals("4", a[0]); + assertEquals("5", a[1]); + assertEquals("6", a[2]); + + assertFalse(this.rs.next()); + } + + public void testToBean() throws SQLException, ParseException { + + TestBean row = null; + assertTrue(this.rs.next()); + row = processor.toBean(this.rs, TestBean.class); + assertEquals("1", row.getOne()); + assertEquals("2", row.getTwo()); + assertEquals("3", row.getThree()); + assertEquals("not set", row.getDoNotSet()); + + assertTrue(this.rs.next()); + row = processor.toBean(this.rs, TestBean.class); + + assertEquals("4", row.getOne()); + assertEquals("5", row.getTwo()); + assertEquals("6", row.getThree()); + assertEquals("not set", row.getDoNotSet()); + assertEquals(3, row.getIntTest()); + assertEquals(Integer.valueOf(4), row.getIntegerTest()); + assertEquals(null, row.getNullObjectTest()); + assertEquals(0, row.getNullPrimitiveTest()); + // test date -> string handling + assertNotNull(row.getNotDate()); + assertTrue(!"not a date".equals(row.getNotDate())); + datef.parse(row.getNotDate()); + + assertFalse(this.rs.next()); + + } + + public void testToBeanList() throws SQLException, ParseException { + + List<TestBean> list = processor.toBeanList(this.rs, TestBean.class); + assertNotNull(list); + assertEquals(ROWS, list.size()); + + TestBean b = list.get(0); + assertEquals("1", b.getOne()); + assertEquals("2", b.getTwo()); + assertEquals("3", b.getThree()); + assertEquals("not set", b.getDoNotSet()); + + b = list.get(1); + assertEquals("4", b.getOne()); + assertEquals("5", b.getTwo()); + assertEquals("6", b.getThree()); + assertEquals("not set", b.getDoNotSet()); + assertEquals(3, b.getIntTest()); + assertEquals(Integer.valueOf(4), b.getIntegerTest()); + assertEquals(null, b.getNullObjectTest()); + assertEquals(0, b.getNullPrimitiveTest()); + // test date -> string handling + assertNotNull(b.getNotDate()); + assertTrue(!"not a date".equals(b.getNotDate())); + datef.parse(b.getNotDate()); + } + + public void testToMap() throws SQLException { + + assertTrue(this.rs.next()); + Map<String, Object> m = processor.toMap(this.rs); + assertEquals(COLS, m.keySet().size()); + assertEquals("1", m.get("one")); + assertEquals("2", m.get("TWO")); + assertEquals("3", m.get("Three")); + + assertTrue(this.rs.next()); + m = processor.toMap(this.rs); + assertEquals(COLS, m.keySet().size()); + + assertEquals("4", m.get("One")); // case shouldn't matter + assertEquals("5", m.get("two")); + assertEquals("6", m.get("THREE")); + + assertFalse(this.rs.next()); + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java new file mode 100644 index 0000000..a049bc7 --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbutils2; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import org.apache.commons.dbutils2.BatchExecutor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class BatchExecutorTest { + + private BatchExecutor executor; + + @Mock private Connection conn; + @Mock private PreparedStatement stmt; + + @Before + public void setup() throws SQLException { + MockitoAnnotations.initMocks(this); + + when(conn.prepareStatement(any(String.class))).thenReturn(stmt); + when(stmt.executeBatch()).thenReturn(new int[] { 2, 3, 4 }); + } + + protected void createExecutor(String sql) throws Exception { + executor = new BatchExecutor(conn, sql, true); + } + + @Test + public void testGoodSQL() throws Exception { + createExecutor("insert into blah"); + + executor.addBatch(); + int[] ret = executor.execute(); + + assertEquals(3, ret.length); + assertEquals(2, ret[0]); + assertEquals(3, ret[1]); + assertEquals(4, ret[2]); + verify(conn, times(1)).close(); + verify(stmt, times(1)).close(); + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils2/BeanProcessorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/BeanProcessorTest.java b/src/test/java/org/apache/commons/dbutils2/BeanProcessorTest.java new file mode 100644 index 0000000..73fc26b --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils2/BeanProcessorTest.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.dbutils2; + +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.dbutils2.BeanProcessor; +import org.apache.commons.dbutils2.ProxyFactory; + +public class BeanProcessorTest extends BaseTestCase { + + private static final BeanProcessor beanProc = new BeanProcessor(); + + public void testProcess() throws SQLException { + TestBean b = null; + assertTrue(this.rs.next()); + b = beanProc.toBean(this.rs, TestBean.class); + assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); + + assertTrue(this.rs.next()); + b = beanProc.toBean(this.rs, TestBean.class); + assertEquals(13.0, b.getColumnProcessorDoubleTest(), 0); + + assertFalse(this.rs.next()); + } + + public static class MapColumnToPropertiesBean { + private String one; + + private String two; + + private String three; + + private String four; + + public String getOne() { + return one; + } + + public void setOne(String one) { + this.one = one; + } + + public String getTwo() { + return two; + } + + public void setTwo(String two) { + this.two = two; + } + + public String getThree() { + return three; + } + + public void setThree(String three) { + this.three = three; + } + + public String getFour() { + return four; + } + + public void setFour(String four) { + this.four = four; + } + } + + public void testMapColumnToProperties() throws Exception { + String[] columnNames = { "test", "test", "three" }; + String[] columnLabels = { "one", "two", null }; + ResultSetMetaData rsmd = ProxyFactory.instance().createResultSetMetaData( + new MockResultSetMetaData(columnNames, columnLabels)); + PropertyDescriptor[] props = Introspector.getBeanInfo(MapColumnToPropertiesBean.class).getPropertyDescriptors(); + + int[] columns = beanProc.mapColumnsToProperties(rsmd, props); + for (int i = 1; i < columns.length; i++) { + assertTrue(columns[i] != BeanProcessor.PROPERTY_NOT_FOUND); + } + } + + public void testMapColumnToPropertiesWithOverrides() throws Exception { + Map<String, String> columnToPropertyOverrides = new HashMap<String, String>(); + columnToPropertyOverrides.put("five", "four"); + BeanProcessor beanProc = new BeanProcessor(columnToPropertyOverrides); + String[] columnNames = { "test", "test", "three", "five" }; + String[] columnLabels = { "one", "two", null, null }; + ResultSetMetaData rsmd = ProxyFactory.instance().createResultSetMetaData( + new MockResultSetMetaData(columnNames, columnLabels)); + PropertyDescriptor[] props = Introspector.getBeanInfo(MapColumnToPropertiesBean.class).getPropertyDescriptors(); + + int[] columns = beanProc.mapColumnsToProperties(rsmd, props); + for (int i = 1; i < columns.length; i++) { + assertTrue(columns[i] != BeanProcessor.PROPERTY_NOT_FOUND); + } + } +}
