RESOLVED - BATCH-734: ItemReaders with Resource input should check for file during ItemStream#open

This commit is contained in:
robokaso
2008-07-21 13:13:44 +00:00
parent 05f94e5a15
commit 705bc284bf
9 changed files with 52 additions and 55 deletions

View File

@@ -214,7 +214,6 @@ public class FlatFileItemReader<T> 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<T> 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);

View File

@@ -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);
}

View File

@@ -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<T> extends ExecutionContextUserSupport implements ItemReader<T>, ItemStream,
InitializingBean {
public class MultiResourceItemReader<T> extends ExecutionContextUserSupport implements ItemReader<T>, ItemStream {
/**
* Unique object instance that marks resource boundaries in the item buffer
@@ -198,6 +196,8 @@ public class MultiResourceItemReader<T> 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<T> 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

View File

@@ -79,7 +79,6 @@ public class StaxEventItemReader<T> 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<T> 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();

View File

@@ -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

View File

@@ -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());
}
}

View File

@@ -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();
}
/**

View File

@@ -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("<foos />"
.getBytes()) });
multiReader.afterPropertiesSet();
multiReader.open(new ExecutionContext());
}
}

View File

@@ -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<XMLEvent> 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<List<XMLEvent>> {
/**
* 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());