PreparedStatementExecutor extends AbstractStatementExecutor { @Override public List<QueryResult> executeQuery(final Collection<InputGroup<StatementExecuteUnit>> inputGroups) throws SQLException { boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecutorCallback<QueryResult> sqlExecutorCallback = createDefaultSQLExecutorCallbackWithQueryResult(isExceptionThrown); return getSqlExecutor().execute(inputGroups, sqlExecutorCallback); } PreparedStatementExecutor(final Map<String, DataSource> dataSourceMap, final SchemaContexts schemaContexts, final SQLExecutor sqlExecutor); @Override List<QueryResult> executeQuery(final Collection<InputGroup<StatementExecuteUnit>> inputGroups); @Override int executeUpdate(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext); @Override boolean execute(final Collection<InputGroup<StatementExecuteUnit>> inputGroups, final SQLStatementContext<?> sqlStatementContext);  }
@Test public void assertExecuteQueryForSinglePreparedStatementSuccess() throws SQLException { PreparedStatement preparedStatement = getPreparedStatement(); ResultSet resultSet = mock(ResultSet.class); ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); when(resultSetMetaData.getColumnName(1)).thenReturn("column"); when(resultSetMetaData.getColumnLabel(1)).thenReturn("column"); when(resultSetMetaData.getTableName(1)).thenReturn("table_x"); when(resultSetMetaData.getColumnCount()).thenReturn(1); when(resultSetMetaData.getColumnType(1)).thenReturn(Types.VARCHAR); when(resultSet.getString(1)).thenReturn("value"); when(resultSet.getMetaData()).thenReturn(resultSetMetaData); when(preparedStatement.executeQuery()).thenReturn(resultSet); assertThat(actual.executeQuery(getExecuteGroups(Collections.singletonList(preparedStatement), true)).iterator().next().getValue(1, String.class), is("value")); }
@Test public void assertExecuteQueryForMultiplePreparedStatementsSuccess() throws SQLException { PreparedStatement preparedStatement1 = getPreparedStatement(); PreparedStatement preparedStatement2 = getPreparedStatement(); ResultSet resultSet1 = mock(ResultSet.class); ResultSet resultSet2 = mock(ResultSet.class); ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); when(resultSetMetaData.getColumnName(1)).thenReturn("column"); when(resultSetMetaData.getColumnLabel(1)).thenReturn("column"); when(resultSetMetaData.getTableName(1)).thenReturn("table_x"); when(resultSetMetaData.getColumnCount()).thenReturn(1); when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER); when(resultSet1.getMetaData()).thenReturn(resultSetMetaData); when(resultSet2.getMetaData()).thenReturn(resultSetMetaData); when(resultSet1.getInt(1)).thenReturn(1); when(resultSet2.getInt(1)).thenReturn(2); when(preparedStatement1.executeQuery()).thenReturn(resultSet1); when(preparedStatement2.executeQuery()).thenReturn(resultSet2); List<QueryResult> result = actual.executeQuery(getExecuteGroups(Arrays.asList(preparedStatement1, preparedStatement2), true)); assertThat(String.valueOf(result.get(0).getValue(1, int.class)), is("1")); assertThat(String.valueOf(result.get(1).getValue(1, int.class)), is("2")); verify(preparedStatement1).executeQuery(); verify(preparedStatement2).executeQuery(); }
@Test public void assertExecuteQueryForSinglePreparedStatementFailure() throws SQLException { PreparedStatement preparedStatement = getPreparedStatement(); SQLException ex = new SQLException(""); when(preparedStatement.executeQuery()).thenThrow(ex); assertThat(actual.executeQuery(getExecuteGroups(Collections.singletonList(preparedStatement), true)), is(Collections.singletonList((QueryResult) null))); verify(preparedStatement).executeQuery(); }
@Test public void assertExecuteQueryForMultiplePreparedStatementsFailure() throws SQLException { PreparedStatement preparedStatement1 = getPreparedStatement(); PreparedStatement preparedStatement2 = getPreparedStatement(); SQLException ex = new SQLException(""); when(preparedStatement1.executeQuery()).thenThrow(ex); when(preparedStatement2.executeQuery()).thenThrow(ex); List<QueryResult> actualResultSets = actual.executeQuery(getExecuteGroups(Arrays.asList(preparedStatement1, preparedStatement2), true)); assertThat(actualResultSets, is(Arrays.asList((QueryResult) null, null))); verify(preparedStatement1).executeQuery(); verify(preparedStatement2).executeQuery(); }