From 5d879ab27b6521a10ca0c48578b08a7dc51e695f Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Thu, 8 Nov 2012 14:15:10 -0600 Subject: [PATCH] BATCH-1799: Updated to allow exceptions to be bubbled during flush/close --- .../TransactionAwareBufferedWriter.java | 15 ++-- .../TransactionAwareBufferedWriterTests.java | 80 ++++++++++++++++--- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java index d5de7014f..1efc62214 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2007 the original author or authors. + * Copyright 2006-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager * underlying writer only when the transaction is committed. * * @author Dave Syer + * @author Michael Minella * */ public class TransactionAwareBufferedWriter extends Writer { @@ -72,17 +73,17 @@ public class TransactionAwareBufferedWriter extends Writer { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCompletion(int status) { + clear(); + } + + @Override + public void afterCommit() { try { - if (status == STATUS_COMMITTED) { - complete(); - } + complete(); } catch (IOException e) { throw new FlushFailedException("Could not write to output buffer", e); } - finally { - clear(); - } } private void complete() throws IOException { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java index de834facc..9d3711a38 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/transaction/TransactionAwareBufferedWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2007 the original author or authors. + * Copyright 2006-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,13 @@ package org.springframework.batch.support.transaction; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; +import org.junit.Before; import org.junit.Test; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; @@ -31,22 +33,28 @@ import org.springframework.transaction.support.TransactionTemplate; /** * @author Dave Syer + * @author Michael Minella * */ public class TransactionAwareBufferedWriterTests { private Writer stringWriter = new StringWriter(); - private TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(stringWriter, new Runnable() { - public void run() { - try { - stringWriter.append("c"); + private TransactionAwareBufferedWriter writer; + + @Before + public void init() { + writer = new TransactionAwareBufferedWriter(stringWriter, new Runnable() { + public void run() { + try { + stringWriter.append("c"); + } + catch (IOException e) { + throw new IllegalStateException(e); + } } - catch (IOException e) { - throw new IllegalStateException(e); - } - } - }); + }); + } private PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); @@ -80,6 +88,7 @@ public class TransactionAwareBufferedWriterTests { } @Test + @SuppressWarnings({"unchecked", "rawtypes"}) public void testFlushInTransaction() throws Exception { Writer mock = new Writer() { @Override @@ -117,6 +126,7 @@ public class TransactionAwareBufferedWriterTests { } @Test + @SuppressWarnings({"unchecked", "rawtypes"}) public void testWriteWithCommit() throws Exception { new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { @@ -135,6 +145,7 @@ public class TransactionAwareBufferedWriterTests { } @Test + @SuppressWarnings({"unchecked", "rawtypes"}) public void tesBufferSizeInTransaction() throws Exception { new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { @@ -151,6 +162,7 @@ public class TransactionAwareBufferedWriterTests { } @Test + @SuppressWarnings({"unchecked", "rawtypes"}) public void testWriteWithRollback() throws Exception { try { new TransactionTemplate(transactionManager).execute(new TransactionCallback() { @@ -179,5 +191,53 @@ public class TransactionAwareBufferedWriterTests { testWriteWithRollback(); testWriteWithCommit(); } + + @Test + @SuppressWarnings({"unchecked", "rawtypes"}) + public void testExceptionOnFlush() throws Exception { + final Writer badWriter = new Writer() { + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + } + + @Override + public void flush() throws IOException { + throw new IOException("This should be bubbled"); + } + + @Override + public void close() throws IOException { + } + }; + writer = new TransactionAwareBufferedWriter(badWriter, new Runnable() { + public void run() { + try { + badWriter.append("c"); + } + catch (IOException e) { + throw new IllegalStateException(e); + } + } + }); + try { + new TransactionTemplate(transactionManager).execute(new TransactionCallback() { + public Object doInTransaction(TransactionStatus status) { + try { + writer.write("foo"); + } + catch (IOException e) { + throw new IllegalStateException("Unexpected IOException", e); + } + assertEquals("", stringWriter.toString()); + return null; + } + }); + + fail("Exception was not thrown"); + } catch (FlushFailedException ffe) { + assertEquals("Could not write to output buffer", ffe.getMessage()); + } + } }