From b1f211509a8397d90df4d9a2867172dbd096536f Mon Sep 17 00:00:00 2001 From: robokaso Date: Mon, 22 Sep 2008 09:05:17 +0000 Subject: [PATCH] RESOLVED - BATCH-843: FlatFileItemWriter handling of failure in LineAggregator --- .../batch/item/file/FlatFileItemWriter.java | 19 ++++--- .../item/file/FlatFileItemWriterTests.java | 54 +++++++++++++++---- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java index 3636a1b9f..4d5b6b7b7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java @@ -186,15 +186,19 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implement OutputState state = getOutputState(); + StringBuilder lines = new StringBuilder(); + int lineCount = 0; for (T item : items) { - String line = lineAggregator.aggregate(item) + lineSeparator; - try { - state.write(line); - } - catch (IOException e) { - throw new FlushFailedException("Could not write data. The file may be corrupt.", e); - } + lines.append(lineAggregator.aggregate(item) + lineSeparator); + lineCount++; } + try { + state.write(lines.toString()); + } + catch (IOException e) { + throw new FlushFailedException("Could not write data. The file may be corrupt.", e); + } + state.linesWritten += lineCount; } /** @@ -394,7 +398,6 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implement outputBufferedWriter.write(line); outputBufferedWriter.flush(); - linesWritten++; } /** diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java index 9f2c47fcb..db608ead2 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java @@ -16,10 +16,7 @@ package org.springframework.batch.item.file; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.io.BufferedReader; import java.io.File; @@ -27,8 +24,10 @@ import java.io.FileReader; import java.io.IOException; import java.io.Writer; import java.nio.charset.UnsupportedCharsetException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.List; import org.junit.After; import org.junit.Before; @@ -209,7 +208,7 @@ public class FlatFileItemWriterTests { public void write(Writer writer) throws IOException { writer.write("footer"); } - + }); writer.open(executionContext); @@ -235,7 +234,7 @@ public class FlatFileItemWriterTests { for (int i = 1; i <= 8; i++) { assertEquals("testLine" + i, readLine()); } - + assertEquals("footer", readLine()); // 3 lines were written to the file after restart @@ -321,7 +320,7 @@ public class FlatFileItemWriterTests { public void write(Writer writer) throws IOException { writer.write("a\nb"); } - + }); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); @@ -338,7 +337,7 @@ public class FlatFileItemWriterTests { public void write(Writer writer) throws IOException { writer.write("a\nb"); } - + }); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); @@ -358,7 +357,7 @@ public class FlatFileItemWriterTests { public void write(Writer writer) throws IOException { writer.write("a\nb"); } - + }); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); @@ -383,7 +382,7 @@ public class FlatFileItemWriterTests { public void write(Writer writer) throws IOException { writer.write("a\nb"); } - + }); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); @@ -410,4 +409,39 @@ public class FlatFileItemWriterTests { assertEquals(TEST_STRING, lineFromFile); } + @Test + /** + * Nothing gets written to file if line aggregation fails. + */ + public void testLineAggregatorFailure() throws Exception { + + writer.setLineAggregator(new LineAggregator() { + + public String aggregate(String item) { + if (item.equals("2")) { + throw new RuntimeException("aggregation failed on " + item); + } + return item; + } + }); + List items = new ArrayList() { + { + add("1"); + add("2"); + add("3"); + } + }; + + writer.open(executionContext); + try { + writer.write(items); + fail(); + } + catch (RuntimeException expected) { + assertEquals("aggregation failed on 2", expected.getMessage()); + } + + // nothing was written to output + assertNull(readLine()); + } }