RESOLVED - issue BATCH-687: BatchUpdateItemWriter should fail if any of the statements does not update any rows (at least by default).

This commit is contained in:
dsyer
2008-06-25 11:58:55 +00:00
parent 761120e1ab
commit 786de6f5a8
2 changed files with 70 additions and 6 deletions

View File

@@ -29,6 +29,7 @@ import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.context.RepeatContextSupport;
import org.springframework.batch.repeat.support.RepeatSynchronizationManager;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
@@ -133,9 +134,14 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
/**
* Test method for
* {@link org.springframework.batch.item.database.BatchSqlUpdateItemWriter#flush()}.
* @throws SQLException
*/
public void testFlush() {
public void testFlush() throws SQLException {
assertTrue(TransactionSynchronizationManager.hasResource(writer.getResourceKey()));
ps.addBatch(); // there is one item in the buffer to start
control.setVoidCallable();
control.expectAndReturn(ps.executeBatch(), new int[0]);
control.replay();
writer.flush();
assertFalse(TransactionSynchronizationManager.hasResource(writer.getResourceKey()));
assertEquals(2, list.size());
@@ -149,6 +155,10 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
*/
public void testWriteAndFlush() throws Exception {
assertTrue(TransactionSynchronizationManager.hasResource(writer.getResourceKey()));
ps.addBatch();
control.setVoidCallable(2);
control.expectAndReturn(ps.executeBatch(), new int[] { 123 });
control.replay();
writer.write("bar");
writer.flush();
assertFalse(TransactionSynchronizationManager.hasResource(writer.getResourceKey()));
@@ -156,6 +166,31 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
assertTrue(list.contains("SQL"));
}
/**
* Test method for
* {@link org.springframework.batch.item.database.BatchSqlUpdateItemWriter#flush()}.
* @throws Exception
*/
public void testWriteAndFlushWithEmptyUpdate() throws Exception {
assertTrue(TransactionSynchronizationManager.hasResource(writer.getResourceKey()));
ps.addBatch();
control.setVoidCallable(2);
control.expectAndReturn(ps.executeBatch(), new int[] { 0 });
control.replay();
writer.write("bar");
try {
writer.flush();
fail("Expected EmptyResultDataAccessException");
} catch (EmptyResultDataAccessException e) {
// expected
String message = e.getMessage();
assertTrue("Wrong message: "+message, message.indexOf("did not update")>=0);
}
assertFalse(TransactionSynchronizationManager.hasResource(writer.getResourceKey()));
assertEquals(3, list.size());
assertTrue(list.contains("SQL"));
}
public void testWriteAndFlushWithFailure() throws Exception {
final RuntimeException ex = new RuntimeException("bar");
writer.setItemPreparedStatementSetter(new ItemPreparedStatementSetter() {