Updated StaxEventItemWriter to assert that it has been opened.

This commit adds a proper assertion that the writer has been properly
opened prior to executing a write.  This now returns a
WriterNotOpenException instead of the previous NullPointerException.

Resolves BATCH-2343
This commit is contained in:
Michael Minella
2017-01-26 12:08:22 -06:00
parent 4917ac3017
commit 934fcad6c2
2 changed files with 31 additions and 11 deletions

View File

@@ -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<T>, 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<QName> unclosedHeaderCallbackElements = Collections.emptyList();
@@ -412,6 +415,8 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
}
}
this.initialized = true;
}
/**
@@ -710,6 +715,8 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
}
}
}
this.initialized = false;
}
private void closeStream() {
@@ -731,6 +738,10 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
@Override
public void write(List<? extends T> 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) {

View File

@@ -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.
*/