Author: wspeirs Date: Mon Feb 25 20:55:29 2013 New Revision: 1449890 URL: http://svn.apache.org/r1449890 Log: - Removed all deprecated methods & fields - Updated unit tests
Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AsyncQueryRunner.java commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BaseResultSetHandler.java commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/DbUtils.java commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/QueryRunner.java commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java?rev=1449890&r1=1449889&r2=1449890&view=diff ============================================================================== --- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java (original) +++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AbstractQueryRunner.java Mon Feb 25 20:55:29 2013 @@ -46,10 +46,8 @@ public abstract class AbstractQueryRunne /** * The DataSource to retrieve connections from. - * @deprecated Access to this field should be through {@link #getDataSource()}. */ - @Deprecated - protected final DataSource ds; + private final DataSource ds; /** * Default constructor, sets pmdKnownBroken to false and ds to null. Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AsyncQueryRunner.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AsyncQueryRunner.java?rev=1449890&r1=1449889&r2=1449890&view=diff ============================================================================== --- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AsyncQueryRunner.java (original) +++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/AsyncQueryRunner.java Mon Feb 25 20:55:29 2013 @@ -17,15 +17,11 @@ package org.apache.commons.dbutils; import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; -import javax.sql.DataSource; - /** * Executes SQL queries with pluggable strategies for handling * <code>ResultSet</code>s. This class is thread safe. @@ -51,119 +47,6 @@ public class AsyncQueryRunner extends Ab } /** - * Constructor for AsyncQueryRunner. - * - * @param executorService the {@code ExecutorService} instance used to run JDBC invocations concurrently. - */ - public AsyncQueryRunner(ExecutorService executorService) { - this(null, false, executorService); - } - - /** - * @deprecated Use {@link #AsyncQueryRunner(ExecutorService, QueryRunner)} instead. - * Constructor for AsyncQueryRunner that controls the use of <code>ParameterMetaData</code>. - * - * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) }; - * if <code>pmdKnownBroken</code> is set to true, we won't even try it; if false, we'll try it, - * and if it breaks, we'll remember not to use it again. - * @param executorService the {@code ExecutorService} instance used to run JDBC invocations concurrently. - */ - @Deprecated - public AsyncQueryRunner(boolean pmdKnownBroken, ExecutorService executorService) { - this(null, pmdKnownBroken, executorService); - } - - /** - * @deprecated Use {@link #AsyncQueryRunner(ExecutorService, QueryRunner)} instead. - * Constructor for AsyncQueryRunner that takes a <code>DataSource</code>. - * - * Methods that do not take a <code>Connection</code> parameter will retrieve connections from this - * <code>DataSource</code>. - * - * @param ds The <code>DataSource</code> to retrieve connections from. - * @param executorService the {@code ExecutorService} instance used to run JDBC invocations concurrently. - */ - @Deprecated - public AsyncQueryRunner(DataSource ds, ExecutorService executorService) { - this(ds, false, executorService); - } - - /** - * @deprecated Use {@link #AsyncQueryRunner(ExecutorService, QueryRunner)} instead. - * Constructor for AsyncQueryRunner that take a <code>DataSource</code> and controls the use of <code>ParameterMetaData</code>. - * Methods that do not take a <code>Connection</code> parameter will retrieve connections from this - * <code>DataSource</code>. - * - * @param ds The <code>DataSource</code> to retrieve connections from. - * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) }; - * if <code>pmdKnownBroken</code> is set to true, we won't even try it; if false, we'll try it, - * and if it breaks, we'll remember not to use it again. - * @param executorService the {@code ExecutorService} instance used to run JDBC invocations concurrently. - */ - @Deprecated - public AsyncQueryRunner(DataSource ds, boolean pmdKnownBroken, ExecutorService executorService) { - super(ds, pmdKnownBroken); - this.executorService = executorService; - this.queryRunner = new QueryRunner(ds, pmdKnownBroken); - } - - /** - * @deprecated No longer used by this class. Will be removed in a future version. - * Class that encapsulates the continuation for batch calls. - */ - @Deprecated - protected class BatchCallableStatement implements Callable<int[]> { - private final String sql; - private final Object[][] params; - private final Connection conn; - private final boolean closeConn; - private final PreparedStatement ps; - - /** - * Creates a new BatchCallableStatement instance. - * - * @param sql The SQL statement to execute. - * @param params An array of query replacement parameters. Each row in - * this array is one set of batch replacement values. - * @param conn The connection to use for the batch call. - * @param closeConn True if the connection should be closed, false otherwise. - * @param ps The {@link PreparedStatement} to be executed. - */ - public BatchCallableStatement(String sql, Object[][] params, Connection conn, boolean closeConn, PreparedStatement ps) { - this.sql = sql; - this.params = params.clone(); - this.conn = conn; - this.closeConn = closeConn; - this.ps = ps; - } - - /** - * The actual call to executeBatch. - * - * @return an array of update counts containing one element for each command in the batch. - * @throws SQLException if a database access error occurs or one of the commands sent to the database fails. - * @see PreparedStatement#executeBatch() - */ - @Override - public int[] call() throws SQLException { - int[] ret = null; - - try { - ret = ps.executeBatch(); - } catch (SQLException e) { - rethrow(e, sql, (Object[])params); - } finally { - close(ps); - if (closeConn) { - close(conn); - } - } - - return ret; - } - } - - /** * Execute a batch of SQL INSERT, UPDATE, or DELETE queries. * * @param conn The <code>Connection</code> to use to run the query. The caller is @@ -209,72 +92,6 @@ public class AsyncQueryRunner extends Ab } /** - * Class that encapsulates the continuation for query calls. - * @param <T> The type of the result from the call to handle. - */ - protected class QueryCallableStatement<T> implements Callable<T> { - private final String sql; - private final Object[] params; - private final Connection conn; - private final boolean closeConn; - private final PreparedStatement ps; - private final ResultSetHandler<T> rsh; - - /** - * Creates a new {@code QueryCallableStatement} instance. - * - * @param conn The connection to use for the batch call. - * @param closeConn True if the connection should be closed, false otherwise. - * @param ps The {@link PreparedStatement} to be executed. - * @param rsh The handler that converts the results into an object. - * @param sql The SQL statement to execute. - * @param params An array of query replacement parameters. Each row in - * this array is one set of batch replacement values. - */ - public QueryCallableStatement(Connection conn, boolean closeConn, PreparedStatement ps, - ResultSetHandler<T> rsh, String sql, Object... params) { - this.sql = sql; - this.params = params; - this.conn = conn; - this.closeConn = closeConn; - this.ps = ps; - this.rsh = rsh; - } - - /** - * The actual call to {@code handle()} method. - * - * @return an array of update counts containing one element for each command in the batch. - * @throws SQLException if a database access error occurs. - * @see ResultSetHandler#handle(ResultSet) - */ - @Override - public T call() throws SQLException { - ResultSet rs = null; - T ret = null; - - try { - rs = wrap(ps.executeQuery()); - ret = rsh.handle(rs); - } catch (SQLException e) { - rethrow(e, sql, params); - } finally { - try { - close(rs); - } finally { - close(ps); - if (closeConn) { - close(conn); - } - } - } - - return ret; - } - - } - - /** * Execute an SQL SELECT query with replacement parameters. The * caller is responsible for closing the connection. * @param <T> The type of object that the handler returns @@ -366,64 +183,6 @@ public class AsyncQueryRunner extends Ab } /** - * @deprecated No longer used by this class. Will be removed in a future version. - * Class that encapsulates the continuation for update calls. - */ - @Deprecated - protected class UpdateCallableStatement implements Callable<Integer> { - private final String sql; - private final Object[] params; - private final Connection conn; - private final boolean closeConn; - private final PreparedStatement ps; - - /** - * - * - * @param conn The connection to use for the batch call. - * @param closeConn True if the connection should be closed, false otherwise. - * @param ps The {@link PreparedStatement} to be executed. - * @param sql The SQL statement to execute. - * @param params An array of query replacement parameters. Each row in - * this array is one set of batch replacement values. - */ - public UpdateCallableStatement(Connection conn, boolean closeConn, PreparedStatement ps, String sql, Object... params) { - this.sql = sql; - this.params = params; - this.conn = conn; - this.closeConn = closeConn; - this.ps = ps; - } - - /** - * The actual call to {@code executeUpdate()} method. - * - * @return either (1) the row count for SQL Data Manipulation Language (DML) statements or - * (2) 0 for SQL statements that return nothing - * @throws SQLException if a database access error occurs. - * @see PreparedStatement#executeUpdate() - */ - @Override - public Integer call() throws SQLException { - int rows = 0; - - try { - rows = ps.executeUpdate(); - } catch (SQLException e) { - rethrow(e, sql, params); - } finally { - close(ps); - if (closeConn) { - close(conn); - } - } - - return Integer.valueOf(rows); - } - - } - - /** * Execute an SQL INSERT, UPDATE, or DELETE query without replacement * parameters. * Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BaseResultSetHandler.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BaseResultSetHandler.java?rev=1449890&r1=1449889&r2=1449890&view=diff ============================================================================== --- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BaseResultSetHandler.java (original) +++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BaseResultSetHandler.java Mon Feb 25 20:55:29 2013 @@ -204,18 +204,6 @@ public abstract class BaseResultSetHandl /** * @param columnIndex - * @param scale - * @return - * @throws SQLException - * @deprecated - * @see java.sql.ResultSet#getBigDecimal(int, int) - */ - protected final BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { - return rs.getBigDecimal(columnIndex, scale); - } - - /** - * @param columnIndex * @return * @throws SQLException * @see java.sql.ResultSet#getBigDecimal(int) @@ -226,18 +214,6 @@ public abstract class BaseResultSetHandl /** * @param columnLabel - * @param scale - * @return - * @throws SQLException - * @deprecated - * @see java.sql.ResultSet#getBigDecimal(java.lang.String, int) - */ - protected final BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { - return rs.getBigDecimal(columnLabel, scale); - } - - /** - * @param columnLabel * @return * @throws SQLException * @see java.sql.ResultSet#getBigDecimal(java.lang.String) @@ -896,28 +872,6 @@ public abstract class BaseResultSetHandl } /** - * @param columnIndex - * @return - * @throws SQLException - * @deprecated - * @see java.sql.ResultSet#getUnicodeStream(int) - */ - protected final InputStream getUnicodeStream(int columnIndex) throws SQLException { - return rs.getUnicodeStream(columnIndex); - } - - /** - * @param columnLabel - * @return - * @throws SQLException - * @deprecated - * @see java.sql.ResultSet#getUnicodeStream(java.lang.String) - */ - protected final InputStream getUnicodeStream(String columnLabel) throws SQLException { - return rs.getUnicodeStream(columnLabel); - } - - /** * @return * @throws SQLException * @see java.sql.ResultSet#getWarnings() Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java?rev=1449890&r1=1449889&r2=1449890&view=diff ============================================================================== --- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java (original) +++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/BasicRowProcessor.java Mon Feb 25 20:55:29 2013 @@ -42,23 +42,6 @@ public class BasicRowProcessor implement private static final BeanProcessor defaultConvert = new BeanProcessor(); /** - * The Singleton instance of this class. - */ - private static final BasicRowProcessor instance = new BasicRowProcessor(); - - /** - * Returns the Singleton instance of this class. - * - * @return The single instance of this class. - * @deprecated Create instances with the constructors instead. This will - * be removed after DbUtils 1.1. - */ - @Deprecated - public static BasicRowProcessor instance() { - return instance; - } - - /** * Use this to process beans. */ private final BeanProcessor convert; Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/DbUtils.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/DbUtils.java?rev=1449890&r1=1449889&r2=1449890&view=diff ============================================================================== --- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/DbUtils.java (original) +++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/DbUtils.java Mon Feb 25 20:55:29 2013 @@ -17,7 +17,6 @@ package org.apache.commons.dbutils; import static java.sql.DriverManager.registerDriver; - import java.io.PrintWriter; import java.lang.reflect.Constructor; import java.sql.Connection; @@ -27,8 +26,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.sql.Statement; -import java.util.logging.Logger; import java.util.Properties; +import java.util.logging.Logger; /** * A collection of JDBC helper methods. This class is thread safe. @@ -36,14 +35,11 @@ import java.util.Properties; public final class DbUtils { /** - * Default constructor. - * - * Utility classes should not have a public or default constructor, - * but this one preserves retro-compatibility. + * Utility classes do not have a public default constructor. * * @since 1.4 */ - public DbUtils() { + private DbUtils() { // do nothing } @@ -400,6 +396,7 @@ public final class DbUtils { /** * Java 1.7 method. */ + @SuppressWarnings("unused") public Logger getParentLogger() throws SQLFeatureNotSupportedException { throw new SQLFeatureNotSupportedException(); } Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/QueryRunner.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/QueryRunner.java?rev=1449890&r1=1449889&r2=1449890&view=diff ============================================================================== --- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/QueryRunner.java (original) +++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils/QueryRunner.java Mon Feb 25 20:55:29 2013 @@ -164,40 +164,6 @@ public class QueryRunner extends Abstrac } /** - * Execute an SQL SELECT query with a single replacement parameter. The - * caller is responsible for closing the connection. - * @param <T> The type of object that the handler returns - * @param conn The connection to execute the query in. - * @param sql The query to execute. - * @param param The replacement parameter. - * @param rsh The handler that converts the results into an object. - * @return The object returned by the handler. - * @throws SQLException if a database access error occurs - * @deprecated Use {@link #query(Connection, String, ResultSetHandler, Object...)} - */ - @Deprecated - public <T> T query(Connection conn, String sql, Object param, ResultSetHandler<T> rsh) throws SQLException { - return this.<T>query(conn, false, sql, rsh, new Object[]{param}); - } - - /** - * Execute an SQL SELECT query with replacement parameters. The - * caller is responsible for closing the connection. - * @param <T> The type of object that the handler returns - * @param conn The connection to execute the query in. - * @param sql The query to execute. - * @param params The replacement parameters. - * @param rsh The handler that converts the results into an object. - * @return The object returned by the handler. - * @throws SQLException if a database access error occurs - * @deprecated Use {@link #query(Connection,String,ResultSetHandler,Object...)} instead - */ - @Deprecated - public <T> T query(Connection conn, String sql, Object[] params, ResultSetHandler<T> rsh) throws SQLException { - return this.<T>query(conn, false, sql, rsh, params); - } - - /** * Execute an SQL SELECT query with replacement parameters. The * caller is responsible for closing the connection. * @param <T> The type of object that the handler returns @@ -227,50 +193,6 @@ public class QueryRunner extends Abstrac } /** - * Executes the given SELECT SQL with a single replacement parameter. - * The <code>Connection</code> is retrieved from the - * <code>DataSource</code> set in the constructor. - * @param <T> The type of object that the handler returns - * @param sql The SQL statement to execute. - * @param param The replacement parameter. - * @param rsh The handler used to create the result object from - * the <code>ResultSet</code>. - * - * @return An object generated by the handler. - * @throws SQLException if a database access error occurs - * @deprecated Use {@link #query(String, ResultSetHandler, Object...)} - */ - @Deprecated - public <T> T query(String sql, Object param, ResultSetHandler<T> rsh) throws SQLException { - Connection conn = this.prepareConnection(); - - return this.<T>query(conn, true, sql, rsh, new Object[]{param}); - } - - /** - * Executes the given SELECT SQL query and returns a result object. - * The <code>Connection</code> is retrieved from the - * <code>DataSource</code> set in the constructor. - * @param <T> The type of object that the handler returns - * @param sql The SQL statement to execute. - * @param params Initialize the PreparedStatement's IN parameters with - * this array. - * - * @param rsh The handler used to create the result object from - * the <code>ResultSet</code>. - * - * @return An object generated by the handler. - * @throws SQLException if a database access error occurs - * @deprecated Use {@link #query(String, ResultSetHandler, Object...)} - */ - @Deprecated - public <T> T query(String sql, Object[] params, ResultSetHandler<T> rsh) throws SQLException { - Connection conn = this.prepareConnection(); - - return this.<T>query(conn, true, sql, rsh, params); - } - - /** * Executes the given SELECT SQL query and returns a result object. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. Modified: commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java?rev=1449890&r1=1449889&r2=1449890&view=diff ============================================================================== --- commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java (original) +++ commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils/AsyncQueryRunnerTest.java Mon Feb 25 20:55:29 2013 @@ -18,23 +18,15 @@ 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.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import java.sql.Connection; -import java.sql.ParameterMetaData; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.concurrent.Future; - -import javax.sql.DataSource; - import org.apache.commons.dbutils.handlers.ArrayHandler; import org.junit.Before; import org.junit.Test; @@ -46,52 +38,34 @@ public class AsyncQueryRunnerTest { AsyncQueryRunner runner; ArrayHandler handler; - @Mock DataSource dataSource; + @Mock QueryRunner qRunner; @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 AsyncQueryRunner(dataSource, Executors.newFixedThreadPool(1)); + runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1), qRunner); } // // Batch test cases // private void callGoodBatch(Connection conn, Object[][] params) throws Exception { - when(meta.getParameterCount()).thenReturn(2); Future<int[]> future = runner.batch(conn, "select * from blah where ? = ?", params); future.get(); - verify(stmt, times(2)).addBatch(); - verify(stmt, times(1)).executeBatch(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(0)).close(); // make sure we closed the connection + verify(qRunner, times(1)).batch(eq(conn), any(String.class), eq(params)); } private void callGoodBatch(Object[][] params) throws Exception { - when(meta.getParameterCount()).thenReturn(2); Future<int[]> future = runner.batch("select * from blah where ? = ?", params); future.get(); - verify(stmt, times(2)).addBatch(); - verify(stmt, times(1)).executeBatch(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(1)).close(); // make sure we closed the connection + verify(qRunner, times(1)).batch(any(String.class), eq(params)); } @Test @@ -103,7 +77,6 @@ public class AsyncQueryRunnerTest { @Test public void testGoodBatchPmdTrue() throws Exception { - runner = new AsyncQueryRunner(dataSource, true, Executors.newFixedThreadPool(1)); String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } }; callGoodBatch(params); @@ -111,7 +84,6 @@ public class AsyncQueryRunnerTest { @Test public void testGoodBatchDefaultConstructor() throws Exception { - runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1)); String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } }; callGoodBatch(conn, params); @@ -135,11 +107,8 @@ public class AsyncQueryRunnerTest { future = runner.batch(sql, params); future.get(); - - verify(stmt, times(2)).addBatch(); - verify(stmt, times(1)).executeBatch(); - verify(stmt, times(1)).close(); // make sure the statement is closed - verify(conn, times(1)).close(); // make sure the connection is closed + + verify(qRunner, times(1)).batch(any(String.class), params); } catch(Exception e) { caught = true; } @@ -163,45 +132,16 @@ public class AsyncQueryRunnerTest { } @Test(expected=ExecutionException.class) - public void testNullConnectionBatch() throws Exception { - String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } }; - - when(meta.getParameterCount()).thenReturn(2); - when(dataSource.getConnection()).thenReturn(null); - - runner.batch("select * from blah where ? = ?", params).get(); - } - - @Test(expected=ExecutionException.class) - public void testNullSqlBatch() throws Exception { - String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } }; - - when(meta.getParameterCount()).thenReturn(2); - - runner.batch(null, params).get(); - } - - @Test(expected=ExecutionException.class) - public void testNullParamsArgBatch() throws Exception { - when(meta.getParameterCount()).thenReturn(2); - + public void testExceptionBatch() throws Exception { + doThrow(ExecutionException.class).when(qRunner).batch(any(String.class), any(Object[][].class)); runner.batch("select * from blah where ? = ?", null).get(); } @Test - public void testAddBatchException() throws Exception { - String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } }; - - doThrow(new SQLException()).when(stmt).addBatch(); - - callBatchWithException("select * from blah where ? = ?", params); - } - - @Test - public void testExecuteBatchException() throws Exception { + public void testBatchException() throws Exception { String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } }; - doThrow(new SQLException()).when(stmt).executeBatch(); + doThrow(new SQLException()).when(qRunner).batch(any(String.class), any(Object[][].class)); callBatchWithException("select * from blah where ? = ?", params); } @@ -211,41 +151,25 @@ public class AsyncQueryRunnerTest { // Query test cases // private void callGoodQuery(Connection conn) throws Exception { - when(meta.getParameterCount()).thenReturn(2); runner.query(conn, "select * from blah where ? = ?", handler, "unit", "test").get(); - - verify(stmt, times(1)).executeQuery(); - verify(results, times(1)).close(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(0)).close(); // make sure we closed the connection + + verify(qRunner, times(1)).query(eq(conn), any(String.class), eq(handler), any(String.class), any(String.class)); // call the other variation of query - when(meta.getParameterCount()).thenReturn(0); runner.query(conn, "select * from blah", handler).get(); - verify(stmt, times(2)).executeQuery(); - verify(results, times(2)).close(); - verify(stmt, times(2)).close(); // make sure we closed the statement - verify(conn, times(0)).close(); // make sure we closed the connection + verify(qRunner, times(1)).query(eq(conn), any(String.class), eq(handler)); } private void callGoodQuery() throws Exception { - when(meta.getParameterCount()).thenReturn(2); runner.query("select * from blah where ? = ?", handler, "unit", "test").get(); - verify(stmt, times(1)).executeQuery(); - verify(results, times(1)).close(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(1)).close(); // make sure we closed the connection + verify(qRunner, times(1)).query(any(String.class), eq(handler), any(String.class), any(String.class)); // call the other variation of query - when(meta.getParameterCount()).thenReturn(0); runner.query("select * from blah", handler).get(); - verify(stmt, times(2)).executeQuery(); - verify(results, times(2)).close(); - verify(stmt, times(2)).close(); // make sure we closed the statement - verify(conn, times(2)).close(); // make sure we closed the connection + verify(qRunner, times(1)).query(any(String.class), eq(handler)); } @Test @@ -254,80 +178,15 @@ public class AsyncQueryRunnerTest { } @Test - public void testGoodQueryPmdTrue() throws Exception { - runner = new AsyncQueryRunner(true, Executors.newFixedThreadPool(1)); + public void testGoodQueryWithConn() throws Exception { callGoodQuery(conn); } - @Test - public void testGoodQueryDefaultConstructor() throws Exception { - runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1)); - callGoodQuery(conn); - } - - - // helper method for calling batch when an exception is expected - private void callQueryWithException(Object... params) throws Exception { - boolean caught = false; - - try { - when(meta.getParameterCount()).thenReturn(2); - runner.query("select * from blah where ? = ?", handler, params).get(); - - verify(stmt, times(1)).executeQuery(); - verify(results, times(1)).close(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(1)).close(); // make sure we closed the connection - } catch(Exception e) { - caught = true; - } - - if(!caught) - fail("Exception never thrown, but expected"); - } - - @Test - public void testNoParamsQuery() throws Exception { - callQueryWithException(); - } - - @Test - public void testTooFewParamsQuery() throws Exception { - callQueryWithException("unit"); - } - - @Test - public void testTooManyParamsQuery() throws Exception { - callQueryWithException("unit", "test", "fail"); - } - @Test(expected=ExecutionException.class) - public void testNullConnectionQuery() throws Exception { - when(meta.getParameterCount()).thenReturn(2); - when(dataSource.getConnection()).thenReturn(null); - - runner.query("select * from blah where ? = ?", handler, "unit", "test").get(); - } - - @Test(expected=ExecutionException.class) - public void testNullSqlQuery() throws Exception { - when(meta.getParameterCount()).thenReturn(2); - - runner.query(null, handler).get(); - } - - @Test(expected=ExecutionException.class) - public void testNullHandlerQuery() throws Exception { - when(meta.getParameterCount()).thenReturn(2); - - runner.query("select * from blah where ? = ?", null).get(); - } - - @Test public void testExecuteQueryException() throws Exception { - doThrow(new SQLException()).when(stmt).executeQuery(); + doThrow(new SQLException()).when(qRunner).query(any(String.class), eq(handler), any(String.class), any(String.class)); - callQueryWithException(handler, "unit", "test"); + runner.query("select * from blah where ? = ?", handler, "unit", "test").get(); } @@ -335,53 +194,35 @@ public class AsyncQueryRunnerTest { // Update test cases // private void callGoodUpdate(Connection conn) throws Exception { - when(meta.getParameterCount()).thenReturn(2); runner.update(conn, "update blah set ? = ?", "unit", "test").get(); - verify(stmt, times(1)).executeUpdate(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(0)).close(); // make sure we closed the connection + verify(qRunner, times(1)).update(eq(conn), any(String.class), any(String.class), any(String.class)); // call the other variation - when(meta.getParameterCount()).thenReturn(0); runner.update(conn, "update blah set unit = test").get(); - verify(stmt, times(2)).executeUpdate(); - verify(stmt, times(2)).close(); // make sure we closed the statement - verify(conn, times(0)).close(); // make sure we closed the connection + verify(qRunner, times(1)).update(eq(conn), any(String.class)); // call the other variation - when(meta.getParameterCount()).thenReturn(1); runner.update(conn, "update blah set unit = ?", "test").get(); - verify(stmt, times(3)).executeUpdate(); - verify(stmt, times(3)).close(); // make sure we closed the statement - verify(conn, times(0)).close(); // make sure we closed the connection + verify(qRunner, times(1)).update(eq(conn), any(String.class), any(String.class)); } private void callGoodUpdate() throws Exception { - when(meta.getParameterCount()).thenReturn(2); runner.update("update blah set ? = ?", "unit", "test").get(); - verify(stmt, times(1)).executeUpdate(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(1)).close(); // make sure we closed the connection + verify(qRunner, times(1)).update(any(String.class), any(String.class), any(String.class)); // call the other variation - when(meta.getParameterCount()).thenReturn(0); runner.update("update blah set unit = test").get(); - verify(stmt, times(2)).executeUpdate(); - verify(stmt, times(2)).close(); // make sure we closed the statement - verify(conn, times(2)).close(); // make sure we closed the connection + verify(qRunner, times(1)).update(any(String.class)); // call the other variation - when(meta.getParameterCount()).thenReturn(1); runner.update("update blah set unit = ?", "test").get(); - verify(stmt, times(3)).executeUpdate(); - verify(stmt, times(3)).close(); // make sure we closed the statement - verify(conn, times(3)).close(); // make sure we closed the connection + verify(qRunner, times(1)).update(any(String.class), any(String.class)); } @Test @@ -390,95 +231,31 @@ public class AsyncQueryRunnerTest { } @Test - public void testGoodUpdatePmdTrue() throws Exception { - runner = new AsyncQueryRunner(true, Executors.newFixedThreadPool(1)); - callGoodUpdate(conn); - } - - @Test - public void testGoodUpdateDefaultConstructor() throws Exception { - runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1)); + public void testGoodUpdateConnection() throws Exception { callGoodUpdate(conn); } - // helper method for calling batch when an exception is expected - private void callUpdateWithException(Object... params) throws Exception { - boolean caught = false; - - try { - when(meta.getParameterCount()).thenReturn(2); - runner.update("select * from blah where ? = ?", params).get(); - - verify(stmt, times(1)).executeUpdate(); - verify(stmt, times(1)).close(); // make sure we closed the statement - verify(conn, times(1)).close(); // make sure we closed the connection - } catch(Exception e) { - caught = true; - } - - if(!caught) - fail("Exception never thrown, but expected"); - } - - @Test - public void testNoParamsUpdate() throws Exception { - callUpdateWithException(); - } - - @Test - public void testTooFewParamsUpdate() throws Exception { - callUpdateWithException("unit"); - } - - @Test - public void testTooManyParamsUpdate() throws Exception { - callUpdateWithException("unit", "test", "fail"); - } @Test public void testInsertUsesGivenQueryRunner() throws Exception { - QueryRunner mockQueryRunner = mock(QueryRunner.class); - runner = new AsyncQueryRunner(Executors.newSingleThreadExecutor(), mockQueryRunner); - runner.insert("1", handler); runner.insert("2", handler, "param1"); runner.insert(conn, "3", handler); runner.insert(conn, "4", handler, "param1"); - verify(mockQueryRunner).insert("1", handler); - verify(mockQueryRunner).insert("2", handler, "param1"); - verify(mockQueryRunner).insert(conn, "3", handler); - verify(mockQueryRunner).insert(conn, "4", handler, "param1"); + verify(qRunner).insert("1", handler); + verify(qRunner).insert("2", handler, "param1"); + verify(qRunner).insert(conn, "3", handler); + verify(qRunner).insert(conn, "4", handler, "param1"); } @Test(expected=ExecutionException.class) - public void testNullConnectionUpdate() throws Exception { - when(meta.getParameterCount()).thenReturn(2); - when(dataSource.getConnection()).thenReturn(null); - + public void testExceptionUpdate() throws Exception { + doThrow(new SQLException()).when(qRunner).update(any(String.class), any(String.class), any(String.class)); + runner.update("select * from blah where ? = ?", "unit", "test").get(); + + verify(qRunner, times(1)).update(any(String.class), any(String.class), any(String.class)); } - @Test(expected=ExecutionException.class) - public void testNullSqlUpdate() throws Exception { - when(meta.getParameterCount()).thenReturn(2); - - runner.update(null).get(); - } - - @Test - public void testExecuteUpdateException() throws Exception { - doThrow(new SQLException()).when(stmt).executeUpdate(); - - callUpdateWithException("unit", "test"); - } - - // - // Random tests - // - @Test(expected=ExecutionException.class) - public void testBadPrepareConnection() throws Exception { - runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1)); - runner.update("update blah set unit = test").get(); - } }