You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ws...@apache.org on 2013/02/26 04:52:38 UTC
svn commit: r1450009 [2/2] - in /commons/proper/dbutils/branches/2_0/src:
main/java/org/apache/commons/dbutils/ test/java/org/apache/commons/dbutils/
Added: commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/InsertExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/InsertExecutorTest.java?rev=1450009&view=auto
==============================================================================
--- commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/InsertExecutorTest.java (added)
+++ commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/InsertExecutorTest.java Tue Feb 26 03:52:37 2013
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class InsertExecutorTest {
+
+ private InsertExecutor executor;
+
+ @Mock private ResultSetHandler<Object> handler;
+ @Mock private Connection conn;
+ @Mock private PreparedStatement stmt;
+ @Mock private ResultSet resultSet;
+
+ @Before
+ public void setup() throws SQLException {
+ MockitoAnnotations.initMocks(this);
+
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.getGeneratedKeys()).thenReturn(resultSet);
+ when(handler.handle(any(ResultSet.class))).thenReturn(new Object());
+ }
+
+ protected void createExecutor(String sql) throws Exception {
+ executor = new InsertExecutor(conn, sql, true);
+ }
+
+ @Test
+ public void testGoodSQL() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.insert(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUnmappedParams() throws Exception {
+ createExecutor("insert into blah (:something)");
+
+ Object ret = executor.insert(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullHandler() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.insert(null);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+}
Added: commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryExecutorTest.java?rev=1450009&view=auto
==============================================================================
--- commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryExecutorTest.java (added)
+++ commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryExecutorTest.java Tue Feb 26 03:52:37 2013
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class QueryExecutorTest {
+
+ private QueryExecutor executor;
+
+ @Mock private ResultSetHandler<Object> handler;
+ @Mock private Connection conn;
+ @Mock private PreparedStatement stmt;
+ @Mock private ResultSet resultSet;
+
+ @Before
+ public void setup() throws SQLException {
+ MockitoAnnotations.initMocks(this);
+
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.executeQuery()).thenReturn(resultSet);
+ when(handler.handle(any(ResultSet.class))).thenReturn(new Object());
+ }
+
+ protected void createExecutor(String sql) throws Exception {
+ executor = new QueryExecutor(conn, sql, true);
+ }
+
+ @Test
+ public void testGoodSQL() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.query(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUnmappedParams() throws Exception {
+ createExecutor("insert into blah (:something)");
+
+ Object ret = executor.query(handler);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testNullHandler() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.query(null);
+
+ assertNotNull(ret);
+ verify(handler, times(1)).handle(resultSet);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+}
Modified: commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java?rev=1450009&r1=1450008&r2=1450009&view=diff
==============================================================================
--- commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java (original)
+++ commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/QueryRunnerTest.java Tue Feb 26 03:52:37 2013
@@ -16,28 +16,13 @@
*/
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.junit.Assert.assertNotNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-
import java.sql.Connection;
-import java.sql.ParameterMetaData;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
-
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;
@@ -46,466 +31,70 @@ import org.mockito.MockitoAnnotations;
@SuppressWarnings("boxing") // test code
public class QueryRunnerTest {
QueryRunner 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); // 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(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 closed the connection
- }
-
- 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 closed the connection
-
- // 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 closed the connection
- }
-
- 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);
+ runner = new QueryRunner(dataSource);
}
-
-
- // 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 closed the connection
-
- // 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 closed the connection
-
- // 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 closed the connection
- }
-
- 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
- }
-
+
+ // query tests
+
@Test
- public void testGoodUpdate() throws Exception {
- callGoodUpdate();
+ public void testQuerySQL() throws SQLException {
+ assertNotNull(runner.query("select * from blah where :first=first"));
+ verify(dataSource, times(1)).getConnection();
}
-
+
@Test
- public void testGoodUpdatePmdTrue() throws Exception {
- runner = new QueryRunner(true);
- callGoodUpdate(conn);
+ public void testQueryConnSQL() throws SQLException {
+ assertNotNull(runner.query(conn, "select * from blah where :first=first"));
}
-
+
@Test
- public void testGoodUpdateDefaultConstructor() throws Exception {
- runner = new QueryRunner();
- callGoodUpdate(conn);
+ public void testQueryConnSQLBoolean() throws SQLException {
+ assertNotNull(runner.query(conn, true, "select * from blah where :first=first"));
}
+ // insert tests
+
@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());
+ public void testInsertSQL() throws SQLException {
+ assertNotNull(runner.insert("insert * from blah where :first=first"));
+ verify(dataSource, times(1)).getConnection();
}
-
- // 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();
+ public void testInsertConnSQL() throws SQLException {
+ assertNotNull(runner.insert(conn, "insert * from blah where :first=first"));
}
-
+
@Test
- public void testTooFewParamsUpdate() throws Exception {
- callUpdateWithException("unit");
+ public void testInsertConnSQLBoolean() throws SQLException {
+ assertNotNull(runner.insert(conn, true, "insert * from blah where :first=first"));
}
-
+
+ // update tests
+
@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);
+ public void testUpdateSQL() throws SQLException {
+ assertNotNull(runner.update("select * from blah where :first=first"));
+ verify(dataSource, times(1)).getConnection();
}
-
+
@Test
- public void testExecuteUpdateException() throws Exception {
- doThrow(new SQLException()).when(stmt).executeUpdate();
-
- callUpdateWithException("unit", "test");
+ public void testUpdateConnSQL() throws SQLException {
+ assertNotNull(runner.update(conn, "select * from blah where :first=first"));
}
-
- //
- // 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");
+ public void testUpdateConnSQLBoolean() throws SQLException {
+ assertNotNull(runner.update(conn, true, "select * from blah where :first=first"));
}
+
}
Added: commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/UpdateExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/UpdateExecutorTest.java?rev=1450009&view=auto
==============================================================================
--- commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/UpdateExecutorTest.java (added)
+++ commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/UpdateExecutorTest.java Tue Feb 26 03:52:37 2013
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.dbutils;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class UpdateExecutorTest {
+
+ private UpdateExecutor executor;
+
+ @Mock private Connection conn;
+ @Mock private PreparedStatement stmt;
+ @Mock private ResultSet resultSet;
+
+ @Before
+ public void setup() throws SQLException {
+ MockitoAnnotations.initMocks(this);
+
+ when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+ when(stmt.executeQuery()).thenReturn(resultSet);
+ }
+
+ protected void createExecutor(String sql) throws Exception {
+ executor = new UpdateExecutor(conn, sql, true);
+ }
+
+ @Test
+ public void testGoodSQL() throws Exception {
+ createExecutor("insert into blah");
+
+ Object ret = executor.update();
+
+ assertNotNull(ret);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+ @Test(expected=SQLException.class)
+ public void testUnmappedParams() throws Exception {
+ createExecutor("insert into blah (:something)");
+
+ Object ret = executor.update();
+
+ assertNotNull(ret);
+ verify(conn, times(1)).close();
+ verify(stmt, times(1)).close();
+ }
+
+}