diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java index ad007ff7c..89804454d 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java @@ -214,7 +214,6 @@ public class FlatFileItemReader extends AbstractBufferedItemReaderItemStream< } public void afterPropertiesSet() throws Exception { - Assert.notNull(resource, "Input resource must not be null"); Assert.notNull(fieldSetMapper, "FieldSetMapper must not be null."); } @@ -231,6 +230,7 @@ public class FlatFileItemReader extends AbstractBufferedItemReaderItemStream< } protected void doOpen() throws Exception { + Assert.notNull(resource, "Input resource must not be null"); Assert.state(resource.exists(), "Resource must exist: [" + resource + "]"); log.debug("Opening flat file for reading: " + resource); 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 3577f53e9..6652ce0a0 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 @@ -108,7 +108,6 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() throws Exception { - Assert.notNull(resource, "The resource must be set"); Assert.notNull(fieldSetCreator, "A FieldSetCreator must be provided."); } @@ -237,6 +236,8 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I */ public void open(ExecutionContext executionContext) throws ItemStreamException { + Assert.notNull(resource, "The resource must be set"); + if (!getOutputState().isInitialized()) { doOpen(executionContext); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java index d6f48ca75..b77e2beaa 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java @@ -16,7 +16,6 @@ import org.springframework.batch.item.ParseException; import org.springframework.batch.item.ResetFailedException; import org.springframework.batch.item.UnexpectedInputException; import org.springframework.batch.item.util.ExecutionContextUserSupport; -import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -34,8 +33,7 @@ import org.springframework.util.ClassUtils; * * @author Robert Kasanicky */ -public class MultiResourceItemReader extends ExecutionContextUserSupport implements ItemReader, ItemStream, - InitializingBean { +public class MultiResourceItemReader extends ExecutionContextUserSupport implements ItemReader, ItemStream { /** * Unique object instance that marks resource boundaries in the item buffer @@ -198,6 +196,8 @@ public class MultiResourceItemReader extends ExecutionContextUserSupport impl */ public void open(ExecutionContext executionContext) throws ItemStreamException { + Assert.notEmpty(resources, "There must be at least one input resource"); + Arrays.sort(resources, comparator); index.open(executionContext); @@ -233,10 +233,6 @@ public class MultiResourceItemReader extends ExecutionContextUserSupport impl this.delegate = delegate; } - public void afterPropertiesSet() throws Exception { - Assert.notEmpty(resources, "There must be at least one input resource"); - } - /** * Set the boolean indicating whether or not state should be saved in the * provided {@link ExecutionContext} during the {@link ItemStream} call to diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java index d366e45ee..de6846ca6 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java @@ -79,7 +79,6 @@ public class StaxEventItemReader extends AbstractBufferedItemReaderItemStream * @throws IllegalStateException if the Resource does not exist. */ public void afterPropertiesSet() throws Exception { - Assert.notNull(resource, "The Resource must not be null."); Assert.notNull(eventReaderDeserializer, "The FragmentDeserializer must not be null."); Assert.hasLength(fragmentRootElementName, "The FragmentRootElementName must not be null"); } @@ -135,6 +134,7 @@ public class StaxEventItemReader extends AbstractBufferedItemReaderItemStream } protected void doOpen() throws Exception { + Assert.notNull(resource, "The Resource must not be null."); Assert.state(resource.exists(), "Input resource does not exist: [" + resource + "]"); inputStream = resource.getInputStream(); 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 4935a4d20..876ebbbce 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 @@ -240,7 +240,6 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implements * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ public void afterPropertiesSet() throws Exception { - Assert.notNull(resource); Assert.notNull(serializer); } @@ -250,6 +249,9 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implements * @see org.springframework.batch.item.ItemStream#open(ExecutionContext) */ public void open(ExecutionContext executionContext) { + + Assert.notNull(resource, "The resource must be set"); + 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/file/MultiResourceItemReaderFlatFileTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderFlatFileTests.java index 02112e889..118c098c9 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderFlatFileTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderFlatFileTests.java @@ -43,7 +43,6 @@ public class MultiResourceItemReaderFlatFileTests extends } }); - multiReader.afterPropertiesSet(); return multiReader; } @@ -53,9 +52,7 @@ public class MultiResourceItemReaderFlatFileTests extends multiReader.close(new ExecutionContext()); multiReader.setResources(new Resource[] { new ByteArrayResource("" .getBytes()) }); - multiReader.afterPropertiesSet(); multiReader.open(new ExecutionContext()); - } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java index a75339994..2115aefdf 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java @@ -45,7 +45,6 @@ public class MultiResourceItemReaderIntegrationTests extends TestCase { return 0; // do not change ordering }}); tested.setResources(new Resource[] { r1, r2, r3, r4, r5 }); - tested.afterPropertiesSet(); } /** diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderXmlTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderXmlTests.java index 61f725c4b..163265547 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderXmlTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderXmlTests.java @@ -56,7 +56,6 @@ public class MultiResourceItemReaderXmlTests extends CommonItemStreamItemReaderT return 0; // preserve original ordering } }); - multiReader.afterPropertiesSet(); return multiReader; } @@ -66,9 +65,7 @@ public class MultiResourceItemReaderXmlTests extends CommonItemStreamItemReaderT multiReader.close(new ExecutionContext()); multiReader.setResources(new Resource[] { new ByteArrayResource("" .getBytes()) }); - multiReader.afterPropertiesSet(); multiReader.open(new ExecutionContext()); - } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java index d7379c5c9..a56abe6d8 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java @@ -50,20 +50,14 @@ public class StaxEventItemReaderTests extends TestCase { } public void testAfterPropertesSetException() throws Exception { - source.setResource(null); - try { - source.afterPropertiesSet(); - fail(); - } catch (IllegalArgumentException e) { - // expected; - } source = createNewInputSouce(); source.setFragmentRootElementName(""); try { source.afterPropertiesSet(); fail(); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { // expected } @@ -72,14 +66,15 @@ public class StaxEventItemReaderTests extends TestCase { try { source.afterPropertiesSet(); fail(); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { // expected } } /** - * Regular usage scenario. ItemReader should pass XML fragments to deserializer wrapped with StartDocument and - * EndDocument events. + * Regular usage scenario. ItemReader should pass XML fragments to + * deserializer wrapped with StartDocument and EndDocument events. */ public void testFragmentWrapping() throws Exception { source.afterPropertiesSet(); @@ -125,21 +120,24 @@ public class StaxEventItemReaderTests extends TestCase { assertEquals(expectedAfterRestart.size(), afterRestart.size()); } -// /** -// * Restore point must not exceed end of file, input source must not be already initialised when restoring. -// */ -// public void testInvalidRestore() { -// ExecutionContext context = new ExecutionContext(); -// context.putLong(ClassUtils.getShortName(StaxEventItemReader.class) + ".item.count", 100000); -// try { -// source.open(context); -// fail("Expected StreamException"); -// } catch (Exception e) { -// // expected -// String message = e.getMessage(); -// assertTrue("Wrong message: " + message, contains(message, "must be before")); -// } -// } + // /** + // * Restore point must not exceed end of file, input source must not be + // already initialised when restoring. + // */ + // public void testInvalidRestore() { + // ExecutionContext context = new ExecutionContext(); + // context.putLong(ClassUtils.getShortName(StaxEventItemReader.class) + + // ".item.count", 100000); + // try { + // source.open(context); + // fail("Expected StreamException"); + // } catch (Exception e) { + // // expected + // String message = e.getMessage(); + // assertTrue("Wrong message: " + message, contains(message, + // "must be before")); + // } + // } public void testRestoreWorksFromClosedStream() throws Exception { source.close(executionContext); @@ -149,7 +147,7 @@ public class StaxEventItemReaderTests extends TestCase { /** * Rollback to last commited record. */ - public void testRollback() throws Exception{ + public void testRollback() throws Exception { source.open(executionContext); // rollback between deserializing records List first = source.read(); @@ -163,9 +161,10 @@ public class StaxEventItemReaderTests extends TestCase { } /** - * Statistics return the current record count. Calling read after end of input does not increase the counter. + * Statistics return the current record count. Calling read after end of + * input does not increase the counter. */ - public void testExecutionContext() throws Exception{ + public void testExecutionContext() throws Exception { final int NUMBER_OF_RECORDS = 2; source.open(executionContext); source.update(executionContext); @@ -211,7 +210,8 @@ public class StaxEventItemReaderTests extends TestCase { try { newSource.read(); fail("Expected ReaderNotOpenException"); - } catch (Exception e) { + } + catch (Exception e) { // expected } } @@ -234,7 +234,8 @@ public class StaxEventItemReaderTests extends TestCase { try { source.open(executionContext); - } catch (ItemStreamException ex) { + } + catch (ItemStreamException ex) { // expected } @@ -248,7 +249,8 @@ public class StaxEventItemReaderTests extends TestCase { try { source.open(executionContext); fail(); - } catch (ItemStreamException ex) { + } + catch (ItemStreamException ex) { // expected } } @@ -277,13 +279,15 @@ public class StaxEventItemReaderTests extends TestCase { } /** - * A simple XMLEvent deserializer mock - check for the start and end document events for the fragment root & end - * tags + skips the fragment contents. + * A simple XMLEvent deserializer mock - check for the start and end + * document events for the fragment root & end tags + skips the fragment + * contents. */ private static class MockFragmentDeserializer implements EventReaderDeserializer> { /** - * A simple mapFragment implementation checking the StaxEventReaderItemReader basic read functionality. + * A simple mapFragment implementation checking the + * StaxEventReaderItemReader basic read functionality. * * @param eventReader * @return list of the events from fragment body @@ -312,7 +316,8 @@ public class StaxEventItemReaderTests extends TestCase { XMLEvent event4 = eventReader.nextEvent(); assertTrue(event4.isEndDocument()); - } catch (XMLStreamException e) { + } + catch (XMLStreamException e) { throw new RuntimeException("Error occured in FragmentDeserializer", e); } return fragmentContent; @@ -327,7 +332,7 @@ public class StaxEventItemReaderTests extends TestCase { do { eventInsideFragment = eventReader.peek(); if (eventInsideFragment instanceof EndElement - && ((EndElement) eventInsideFragment).getName().getLocalPart().equals(FRAGMENT_ROOT_ELEMENT)) { + && ((EndElement) eventInsideFragment).getName().getLocalPart().equals(FRAGMENT_ROOT_ELEMENT)) { break; } events.add(eventReader.nextEvent());