Add-some-Unit-Tests Added tests to increase code coverage.
Project: http://git-wip-us.apache.org/repos/asf/commons-dbutils/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-dbutils/commit/eee69c7a Tree: http://git-wip-us.apache.org/repos/asf/commons-dbutils/tree/eee69c7a Diff: http://git-wip-us.apache.org/repos/asf/commons-dbutils/diff/eee69c7a Branch: refs/heads/master Commit: eee69c7acc92db7b77a8dd41703fb27c50913ae4 Parents: 865eca4 Author: Michael Hausegger <[email protected]> Authored: Sun Jun 25 22:19:37 2017 +0200 Committer: Carl Hall <[email protected]> Committed: Sat Jul 8 16:28:50 2017 -0700 ---------------------------------------------------------------------- .../dbutils/BaseResultSetHandlerTest.java | 71 ++++++++++++++++++++ .../dbutils/BaseResultSetHandlerTestCase.java | 71 -------------------- .../org/apache/commons/dbutils/DbUtilsTest.java | 23 +++++-- .../apache/commons/dbutils/QueryLoaderTest.java | 14 ++++ .../commons/dbutils/ResultSetIteratorTest.java | 32 +++++++++ 5 files changed, 135 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/eee69c7a/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTest.java b/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTest.java new file mode 100644 index 0000000..569a591 --- /dev/null +++ b/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTest.java @@ -0,0 +1,71 @@ +/* + * 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.SQLException; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +public final class BaseResultSetHandlerTest 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/eee69c7a/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java b/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java deleted file mode 100644 index a574c73..0000000 --- a/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java +++ /dev/null @@ -1,71 +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; - -import java.sql.SQLException; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; - -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/eee69c7a/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 3d2a16e..33660ef 100644 --- a/src/test/java/org/apache/commons/dbutils/DbUtilsTest.java +++ b/src/test/java/org/apache/commons/dbutils/DbUtilsTest.java @@ -16,17 +16,16 @@ */ package org.apache.commons.dbutils; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; +import org.junit.Test; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import org.junit.Test; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.*; public class DbUtilsTest { @@ -269,4 +268,18 @@ public class DbUtilsTest { 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/eee69c7a/src/test/java/org/apache/commons/dbutils/QueryLoaderTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/QueryLoaderTest.java b/src/test/java/org/apache/commons/dbutils/QueryLoaderTest.java index 957b309..830272c 100644 --- a/src/test/java/org/apache/commons/dbutils/QueryLoaderTest.java +++ b/src/test/java/org/apache/commons/dbutils/QueryLoaderTest.java @@ -39,4 +39,18 @@ public class QueryLoaderTest extends BaseTestCase { assertTrue(q != q3); // pointer comparison should return false } + public void testLoadThrowsIllegalArgumentException() throws IOException { + + QueryLoader queryLoader = QueryLoader.instance(); + + try { + queryLoader.load("e"); + fail("Expecting exception: IllegalArgumentException"); + } catch(IllegalArgumentException e) { + assertEquals("e not found.",e.getMessage()); + assertEquals(QueryLoader.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + } http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/eee69c7a/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java b/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java index 0b9e7ba..e632f77 100644 --- a/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java +++ b/src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java @@ -16,8 +16,14 @@ */ package org.apache.commons.dbutils; +import org.junit.Test; + +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Iterator; +import static org.mockito.Mockito.mock; + /** * ResultSetIteratorTest */ @@ -46,4 +52,30 @@ public class ResultSetIteratorTest extends BaseTestCase { assertFalse(iter.hasNext()); } + @Test + public void testRethrowThrowsRuntimeException() { + + ResultSetIterator resultSetIterator = new ResultSetIterator((ResultSet) null); + Throwable throwable = new Throwable(); + SQLException sQLException = new SQLException(throwable); + + try { + resultSetIterator.rethrow(sQLException); + fail("Expecting exception: RuntimeException"); + } catch(RuntimeException e) { + assertEquals(ResultSetIterator.class.getName(), e.getStackTrace()[0].getClassName()); + } + + } + + @Test + public void testCreatesResultSetIteratorTakingThreeArgumentsAndCallsRemove() { + + ResultSet resultSet = mock(ResultSet.class); + ResultSetIterator resultSetIterator = new ResultSetIterator(resultSet,null); + resultSetIterator.remove(); + + } + + }
