From 452591e580685bbbb28b546588f025cf2ac3ab5a Mon Sep 17 00:00:00 2001 From: robokaso Date: Mon, 11 Aug 2008 09:43:13 +0000 Subject: [PATCH] RESOLVED - BATCH-766: Insufficient error handling in case of a missing resource for a org.springframework.batch.item.xml.StaxEventItemWriter Added check to open(..) that output resource exists --- .../batch/item/xml/StaxEventItemWriter.java | 2 + .../item/xml/StaxEventItemWriterTests.java | 40 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) 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 39c549db2..ad37a4d45 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 @@ -256,6 +256,8 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen Assert.notNull(resource, "The resource must be set"); + Assert.state(resource.exists(), "Output resource must exist"); + long startAtPosition = 0; // if restart data is provided, restart from provided offset 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 895736d95..53c223c47 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 @@ -8,11 +8,14 @@ import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLStreamException; import javax.xml.transform.Result; -import junit.framework.TestCase; +import static org.junit.Assert.*; 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.xml.oxm.MarshallingEventWriterSerializer; +import org.springframework.core.io.DescriptiveResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.oxm.Marshaller; @@ -24,7 +27,7 @@ import org.springframework.xml.transform.StaxResult; /** * Tests for {@link StaxEventItemWriter}. */ -public class StaxEventItemWriterTests extends TestCase { +public class StaxEventItemWriterTests { // object under test private StaxEventItemWriter writer; @@ -46,7 +49,8 @@ public class StaxEventItemWriterTests extends TestCase { private static final int NOT_FOUND = -1; - protected void setUp() throws Exception { + @Before + public void setUp() throws Exception { resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", "xml")); writer = createItemWriter(); executionContext = new ExecutionContext(); @@ -55,6 +59,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Flush should pass buffered items to Serializer. */ + @Test public void testFlush() throws Exception { writer.open(executionContext); InputCheckMarshaller marshaller = new InputCheckMarshaller(); @@ -70,6 +75,7 @@ public class StaxEventItemWriterTests extends TestCase { } + @Test public void testClear() throws Exception { writer.open(executionContext); writer.write(item); @@ -83,6 +89,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Rolled back records should not be written to output file. */ + @Test public void testRollback() throws Exception { writer.open(executionContext); writer.write(item); @@ -94,6 +101,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Item is written to the output file only after flush. */ + @Test public void testWriteAndFlush() throws Exception { writer.open(executionContext); writer.write(item); @@ -107,6 +115,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Restart scenario - content is appended to the output file after restart. */ + @Test public void testRestart() throws Exception { writer.open(executionContext); // write item @@ -137,6 +146,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Item is written to the output file only after flush. */ + @Test public void testWriteWithHeader() throws Exception { Object header1 = new Object(); Object header2 = new Object(); @@ -153,6 +163,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Item is written to the output file only after flush. */ + @Test public void testWriteWithHeaderAfterRollback() throws Exception { Object header = new Object(); writer.setHeaderItems(new Object[] {header}); @@ -170,6 +181,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Item is written to the output file only after flush. */ + @Test public void testWriteWithHeaderAfterFlushAndRollback() throws Exception { Object header = new Object(); writer.setHeaderItems(new Object[] {header}); @@ -190,6 +202,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Count of 'records written so far' is returned as statistics. */ + @Test public void testStreamContext() throws Exception { writer.open(executionContext); final int NUMBER_OF_RECORDS = 10; @@ -206,6 +219,7 @@ public class StaxEventItemWriterTests extends TestCase { /** * Open method writes the root tag, close method adds corresponding end tag. */ + @Test public void testOpenAndClose() throws Exception { writer.setRootTagName("testroot"); writer.setRootElementAttributes(new HashMap() { @@ -222,6 +236,26 @@ public class StaxEventItemWriterTests extends TestCase { assertTrue(outputFileContent().indexOf("") != NOT_FOUND); assertTrue(outputFileContent().endsWith("")); } + + @Test + public void testNonExistantResource() throws Exception { + Resource doesntExist = new DescriptiveResource("") { + + public boolean exists() { + return false; + } + + }; + writer.setResource(doesntExist); + + try { + writer.open(executionContext); + fail(); + } + catch (IllegalStateException e) { + assertEquals("Output resource must exist", e.getMessage()); + } + } /** * Checks the received parameters.