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 89b7424b3..fe795700d 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
@@ -83,6 +83,12 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
private boolean saveState = false;
+ private boolean shouldDeleteIfExists = true;
+
+ private String encoding = OutputState.DEFAULT_CHARSET;
+
+ private int bufferSize = OutputState.DEFAULT_BUFFER_SIZE;
+
public FlatFileItemWriter() {
setName(ClassUtils.getShortName(FlatFileItemWriter.class));
}
@@ -157,21 +163,21 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
* Sets encoding for output template.
*/
public void setEncoding(String newEncoding) {
- getOutputState().setEncoding(newEncoding);
+ this.encoding = newEncoding;
}
/**
* Sets buffer size for output template
*/
public void setBufferSize(int newSize) {
- getOutputState().setBufferSize(newSize);
+ this.bufferSize = newSize;
}
/**
* @param shouldDeleteIfExists the shouldDeleteIfExists to set
*/
public void setShouldDeleteIfExists(boolean shouldDeleteIfExists) {
- getOutputState().setShouldDeleteIfExists(shouldDeleteIfExists);
+ this.shouldDeleteIfExists = shouldDeleteIfExists;
}
/**
@@ -214,6 +220,9 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
private OutputState getOutputState() {
if (state == null) {
state = new OutputState();
+ state.setDeleteIfExists(shouldDeleteIfExists);
+ state.setBufferSize(bufferSize);
+ state.setEncoding(encoding);
}
return (OutputState) state;
}
@@ -277,24 +286,24 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
}
/**
- * @param shouldDeleteIfExists2
+ * @param shouldDeleteIfExists
*/
- public void setShouldDeleteIfExists(boolean shouldDeleteIfExists) {
+ public void setDeleteIfExists(boolean shouldDeleteIfExists) {
this.shouldDeleteIfExists = shouldDeleteIfExists;
}
/**
- * @param newSize
+ * @param bufferSize
*/
- public void setBufferSize(int newSize) {
- bufferSize = newSize;
+ public void setBufferSize(int bufferSize) {
+ this.bufferSize = bufferSize;
}
/**
- * @param newEncoding
+ * @param encoding
*/
- public void setEncoding(String newEncoding) {
- encoding = newEncoding;
+ public void setEncoding(String encoding) {
+ this.encoding = encoding;
}
/**
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 750613f62..9d5edecfa 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
@@ -20,10 +20,12 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.nio.charset.UnsupportedCharsetException;
import junit.framework.TestCase;
import org.springframework.batch.item.ExecutionContext;
+import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.file.mapping.DefaultFieldSet;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.mapping.FieldSetCreator;
@@ -33,8 +35,9 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
import org.springframework.util.ClassUtils;
/**
- * Tests of regular usage for {@link FlatFileItemWriter} Exception cases will be in separate TestCase classes with
- * different setUp and tearDown methods
+ * Tests of regular usage for {@link FlatFileItemWriter} Exception cases will be
+ * in separate TestCase classes with different setUp and
+ * tearDown methods
*
* @author robert.kasanicky
* @author Dave Syer
@@ -57,7 +60,8 @@ public class FlatFileItemWriterTests extends TestCase {
private ExecutionContext executionContext;
/**
- * Create temporary output file, define mock behaviour, set dependencies and initialize the object under test
+ * Create temporary output file, define mock behaviour, set dependencies and
+ * initialize the object under test
*/
protected void setUp() throws Exception {
@@ -87,8 +91,9 @@ public class FlatFileItemWriterTests extends TestCase {
}
/*
- * Read a line from the output file, if the reader has not been created, recreate. This method is only necessary
- * because running the tests in a UNIX environment locks the file if it's open for writing.
+ * Read a line from the output file, if the reader has not been created,
+ * recreate. This method is only necessary because running the tests in a
+ * UNIX environment locks the file if it's open for writing.
*/
private String readLine() throws IOException {
@@ -258,7 +263,8 @@ public class FlatFileItemWriterTests extends TestCase {
try {
inputSource.afterPropertiesSet();
fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
+ }
+ catch (IllegalArgumentException e) {
// expected
}
}
@@ -276,6 +282,40 @@ public class FlatFileItemWriterTests extends TestCase {
assertEquals(0, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".current.count"));
}
+ /**
+ * Regular usage of write(String) method
+ *
+ * @throws Exception
+ */
+ public void testWriteStringWithBogusEncoding() throws Exception {
+ inputSource.setEncoding("BOGUS");
+ inputSource.open(executionContext);
+ try {
+ inputSource.write(TEST_STRING);
+ fail("Expecyted ItemStreamException");
+ }
+ catch (ItemStreamException e) {
+ assertTrue(e.getCause() instanceof UnsupportedCharsetException);
+ }
+ inputSource.close(null);
+ }
+
+ /**
+ * Regular usage of write(String) method
+ *
+ * @throws Exception
+ */
+ public void testWriteStringWithEncodingAfterClose() throws Exception {
+ testWriteStringWithBogusEncoding();
+ inputSource.setEncoding("UTF-8");
+ inputSource.open(executionContext);
+ inputSource.write(TEST_STRING);
+ inputSource.close(null);
+ String lineFromFile = readLine();
+
+ assertEquals(TEST_STRING, lineFromFile);
+ }
+
private void commit() throws Exception {
inputSource.flush();
}