@Test(expected=ExecutionException.class) public void testBadPrepareConnection() throws Exception { runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1)); runner.update("update blah set unit = test").get(); } }
/** * 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; }
/** * 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; } }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(dataSource.getConnection()).thenReturn(conn); when(conn.prepareStatement(any(String.class))).thenReturn(stmt); when(stmt.getParameterMetaData()).thenReturn(meta); when(stmt.getResultSet()).thenReturn(results); when(stmt.executeQuery()).thenReturn(results); when(results.next()).thenReturn(false); handler = new ArrayHandler(); runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1), new QueryRunner(dataSource)); }
private void callGoodBatch(Connection conn, Object[][] params) throws Exception { when(meta.getParameterCount()).thenReturn(2); Future<int[]> future = runner.batch(conn, "select * from blah where ? = ?", params); future.get(); verify(stmt, times(2)).addBatch(); verify(stmt, times(1)).executeBatch(); verify(stmt, times(1)).close(); // make sure we closed the statement verify(conn, times(0)).close(); // make sure we closed the connection }
private void callGoodQuery(Connection conn) throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.query(conn, "select * from blah where ? = ?", handler, "unit", "test").get(); verify(stmt, times(1)).executeQuery(); verify(results, times(1)).close(); verify(stmt, times(1)).close(); // make sure we closed the statement verify(conn, times(0)).close(); // make sure we closed the connection // call the other variation of query when(meta.getParameterCount()).thenReturn(0); runner.query(conn, "select * from blah", handler).get(); verify(stmt, times(2)).executeQuery(); verify(results, times(2)).close(); verify(stmt, times(2)).close(); // make sure we closed the statement verify(conn, times(0)).close(); // make sure we closed the connection }
@Test public void testInsertUsesGivenQueryRunner() throws Exception { QueryRunner mockQueryRunner = mock(QueryRunner.class , org.mockito.Mockito.withSettings().verboseLogging() // debug for Continuum ); runner = new AsyncQueryRunner(Executors.newSingleThreadExecutor(), mockQueryRunner); runner.insert("1", handler); runner.insert("2", handler, "param1"); runner.insert(conn, "3", handler); runner.insert(conn, "4", handler, "param1"); // give the Executor time to submit all insert statements. Otherwise the following verify statements will fail from time to time. TimeUnit.MILLISECONDS.sleep(50); verify(mockQueryRunner).insert("1", handler); verify(mockQueryRunner).insert("2", handler, "param1"); verify(mockQueryRunner).insert(conn, "3", handler); verify(mockQueryRunner).insert(conn, "4", handler, "param1"); }
@Test(expected=ExecutionException.class) public void testNullConnectionUpdate() throws Exception { when(meta.getParameterCount()).thenReturn(2); when(dataSource.getConnection()).thenReturn(null); runner.update("select * from blah where ? = ?", "unit", "test").get(); }
/** * 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); }
@SuppressWarnings("deprecation") // deliberate test of deprecated code @Test public void testGoodUpdatePmdTrue() throws Exception { runner = new AsyncQueryRunner(true, Executors.newFixedThreadPool(1)); callGoodUpdate(conn); }
private void callGoodBatch(Object[][] params) throws Exception { when(meta.getParameterCount()).thenReturn(2); Future<int[]> future = runner.batch("select * from blah where ? = ?", params); future.get(); verify(stmt, times(2)).addBatch(); verify(stmt, times(1)).executeBatch(); verify(stmt, times(1)).close(); // make sure we closed the statement verify(conn, times(1)).close(); // make sure we closed the connection }
@Test(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(); }
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"); } }
@SuppressWarnings("deprecation") // deliberate test of deprecated code @Test public void testGoodBatchPmdTrue() throws Exception { runner = new AsyncQueryRunner(dataSource, true, Executors.newFixedThreadPool(1)); String[][] params = new String[][] { { "unit", "unit" }, { "test", "test" } }; callGoodBatch(params); }
@Test(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(); }
private void callGoodQuery() throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.query("select * from blah where ? = ?", handler, "unit", "test").get(); verify(stmt, times(1)).executeQuery(); verify(results, times(1)).close(); verify(stmt, times(1)).close(); // make sure we closed the statement verify(conn, times(1)).close(); // make sure we closed the connection // call the other variation of query when(meta.getParameterCount()).thenReturn(0); runner.query("select * from blah", handler).get(); verify(stmt, times(2)).executeQuery(); verify(results, times(2)).close(); verify(stmt, times(2)).close(); // make sure we closed the statement verify(conn, times(2)).close(); // make sure we closed the connection }
@Test(expected=ExecutionException.class) public void testNullSqlUpdate() throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.update(null).get(); }
@Test public void testGoodQueryDefaultConstructor() throws Exception { runner = new AsyncQueryRunner(Executors.newFixedThreadPool(1)); callGoodQuery(conn); }
@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 testNullSqlQuery() throws Exception { when(meta.getParameterCount()).thenReturn(2); runner.query(null, handler).get(); }