diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java index 768bf0ddf..f1d5a7f93 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java @@ -27,7 +27,6 @@ import java.nio.channels.FileChannel; import java.util.Collections; import java.util.List; import java.util.Map; - import javax.xml.namespace.QName; import javax.xml.stream.FactoryConfigurationError; import javax.xml.stream.XMLEventFactory; @@ -38,10 +37,12 @@ import javax.xml.transform.Result; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.WriteFailedException; +import org.springframework.batch.item.WriterNotOpenException; import org.springframework.batch.item.file.ResourceAwareItemWriterItemStream; import org.springframework.batch.item.support.AbstractItemStreamItemWriter; import org.springframework.batch.item.util.FileUtils; @@ -152,6 +153,8 @@ ResourceAwareItemWriterItemStream, InitializingBean { private boolean shouldDeleteIfEmpty = false; private boolean restarted = false; + + private boolean initialized = false; // List holding the QName of elements that were opened in the header callback, but not closed private List unclosedHeaderCallbackElements = Collections.emptyList(); @@ -412,6 +415,8 @@ ResourceAwareItemWriterItemStream, InitializingBean { } } + this.initialized = true; + } /** @@ -710,6 +715,8 @@ ResourceAwareItemWriterItemStream, InitializingBean { } } } + + this.initialized = false; } private void closeStream() { @@ -731,6 +738,10 @@ ResourceAwareItemWriterItemStream, InitializingBean { @Override public void write(List items) throws XmlMappingException, Exception { + if(!this.initialized) { + throw new WriterNotOpenException("Writer must be open before it can be written to"); + } + currentRecordCount += items.size(); for (Object object : items) { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java index 66bf40b4a..cc36f7f87 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java @@ -15,11 +15,23 @@ */ package org.springframework.batch.item.xml; +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.stream.XMLEventFactory; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.Result; + import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Test; + import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.UnexpectedInputException; +import org.springframework.batch.item.WriterNotOpenException; import org.springframework.batch.support.transaction.ResourcelessTransactionManager; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; @@ -34,16 +46,6 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.stream.XMLEventFactory; -import javax.xml.stream.XMLEventWriter; -import javax.xml.stream.XMLStreamException; -import javax.xml.transform.Result; -import java.io.File; -import java.io.IOException; -import java.util.Collections; -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -128,6 +130,13 @@ public class StaxEventItemWriterTests { assertTrue("execution context keys should be prefixed with writer name", executionContext.containsKey("test.position")); } + @Test(expected = WriterNotOpenException.class) + public void testAssertWriterIsInitialized() throws Exception { + StaxEventItemWriter writer = new StaxEventItemWriter(); + + writer.write(Collections.singletonList("foo")); + } + /** * Item is written to the output file only after flush. */