Skip to content

Commit

Permalink
Apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Aug 6, 2024
1 parent a0fe365 commit 6869925
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ public class AggregatedBatchUpdateException extends BatchUpdateException {

private final int[][] successfulUpdateCounts;

private final BatchUpdateException originalException;

/**
* Create an aggregated exception with the batches that have completed prior
* to the given {@code cause}.
* @param successfulUpdateCounts the counts of the batches that run successfully
* @param cause the exception this instance aggregates
* @param original the exception this instance aggregates
*/
public AggregatedBatchUpdateException(int[][] successfulUpdateCounts, BatchUpdateException cause) {
super(cause.getMessage(), cause.getSQLState(), cause.getErrorCode(),
cause.getUpdateCounts(), cause.getCause());
public AggregatedBatchUpdateException(int[][] successfulUpdateCounts, BatchUpdateException original) {
super(original.getMessage(), original.getSQLState(), original.getErrorCode(),
original.getUpdateCounts(), original.getCause());
this.successfulUpdateCounts = successfulUpdateCounts;
this.originalException = original;
// Copy state of the original exception
setNextException(original.getNextException());
for (Throwable suppressed : original.getSuppressed()) {
addSuppressed(suppressed);
}
}

/**
Expand All @@ -54,4 +62,12 @@ public int[][] getSuccessfulUpdateCounts() {
return this.successfulUpdateCounts;
}

/**
* Return the original {@link BatchUpdateException} that this exception aggregates.
* @return the original exception
*/
public BatchUpdateException getOriginalException() {
return this.originalException;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import javax.sql.DataSource;

Expand Down Expand Up @@ -802,7 +803,30 @@ void testBatchUpdateWithCollectionOfObjects() throws Exception {
}

@Test
void testBatchUpdateWithBatchFailing() throws Exception {
void testBatchUpdateWithBatchFailingHasUpdateCounts() throws Exception {
test3BatchesOf2ItemsFailing(exception -> assertThat(exception).cause()
.isInstanceOfSatisfying(AggregatedBatchUpdateException.class, ex -> {
assertThat(ex.getSuccessfulUpdateCounts()).hasDimensions(1, 2)
.contains(new int[] { 1, 1 }, Index.atIndex(0));
assertThat(ex.getUpdateCounts()).contains(-3, -3);
}));
}

@Test
void testBatchUpdateWithBatchFailingMatchesOriginalException() throws Exception {
test3BatchesOf2ItemsFailing(exception -> assertThat(exception).cause()
.isInstanceOfSatisfying(AggregatedBatchUpdateException.class, ex -> {
BatchUpdateException originalException = ex.getOriginalException();
assertThat(ex.getMessage()).isEqualTo(originalException.getMessage());
assertThat(ex.getCause()).isEqualTo(originalException.getCause());
assertThat(ex.getSQLState()).isEqualTo(originalException.getSQLState());
assertThat(ex.getErrorCode()).isEqualTo(originalException.getErrorCode());
assertThat((Exception) ex.getNextException()).isSameAs(originalException.getNextException());
assertThat(ex.getSuppressed()).isEqualTo(originalException.getSuppressed());
}));
}

void test3BatchesOf2ItemsFailing(Consumer<Exception> exception) throws Exception {
String sql = "INSERT INTO NOSUCHTABLE values (?)";
List<Integer> ids = Arrays.asList(1, 2, 3, 2, 4, 5);
int[] rowsAffected = new int[] {1, 1};
Expand All @@ -815,12 +839,9 @@ void testBatchUpdateWithBatchFailing() throws Exception {
ParameterizedPreparedStatementSetter<Integer> setter = (ps, argument) -> ps.setInt(1, argument);
JdbcTemplate template = new JdbcTemplate(this.dataSource, false);

assertThatExceptionOfType(DuplicateKeyException.class).isThrownBy(() -> template.batchUpdate(sql, ids, 2, setter))
.havingCause().isInstanceOfSatisfying(AggregatedBatchUpdateException.class, ex -> {
assertThat(ex.getSuccessfulUpdateCounts()).hasDimensions(1, 2)
.contains(rowsAffected, Index.atIndex(0));
assertThat(ex.getUpdateCounts()).contains(-3, -3);
});
assertThatExceptionOfType(DuplicateKeyException.class)
.isThrownBy(() -> template.batchUpdate(sql, ids, 2, setter))
.satisfies(exception);
verify(this.preparedStatement, times(4)).addBatch();
verify(this.preparedStatement).setInt(1, 1);
verify(this.preparedStatement, times(2)).setInt(1, 2);
Expand Down

0 comments on commit 6869925

Please sign in to comment.