OPEN - issue BATCH-364: StepScope responsibilities can be assumed by Step (not ApplicationContext)

http://jira.springframework.org/browse/BATCH-364

Instead of lazy open, throw exception is stream is used before open().
This commit is contained in:
dsyer
2008-02-15 09:19:25 +00:00
parent ff0f6266a9
commit 711d0c6e92
15 changed files with 167 additions and 671 deletions

View File

@@ -157,6 +157,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
} catch (StreamException e) {
assertTrue("Message does not contain open: "+e.getMessage(), e.getMessage().contains("open"));
}
reader.open();
assertEquals("[FlatFileInputTemplate-TestData]", reader.read().toString());
}

View File

@@ -29,6 +29,7 @@ import org.springframework.batch.io.file.mapping.FieldSetMapper;
import org.springframework.batch.io.file.separator.DefaultRecordSeparatorPolicy;
import org.springframework.batch.io.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.io.file.transform.LineTokenizer;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
@@ -139,7 +140,12 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader = new FlatFileItemReader();
itemReader.setResource(getInputResource(TEST_STRING));
itemReader.setFieldSetMapper(fieldSetMapper);
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
try {
itemReader.read();
fail("Expected StreamException");
} catch (StreamException e) {
assertTrue(e.getMessage().contains("open"));
}
}
public void testCloseBeforeOpen() throws Exception {
@@ -147,7 +153,8 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setResource(getInputResource(TEST_STRING));
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.close();
// The open still happens automatically on a read...
// The open does not happen automatically on a read...
itemReader.open();
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
}
@@ -172,6 +179,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setEncoding("UTF-8");
itemReader.setResource(getInputResource(TEST_STRING));
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.open();
testRead();
}
@@ -301,6 +309,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
//replace the resource to simulate runtime resource creation
testReader.setResource(getInputResource(TEST_STRING));
testReader.open();
assertEquals(TEST_OUTPUT, testReader.read().toString());
}

View File

@@ -15,6 +15,7 @@ import javax.xml.stream.events.XMLEvent;
import junit.framework.TestCase;
import org.springframework.batch.item.ExecutionAttributes;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
@@ -131,23 +132,19 @@ public class StaxEventItemReaderTests extends TestCase {
context.putLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME, 100000);
try {
source.restoreFrom(context);
fail();
fail("Expected StreamException");
}
catch (IllegalStateException e) {
// expected
}
source = createNewInputSouce();
source.open();
try {
source.restoreFrom(new ExecutionAttributes());
fail();
}
catch (IllegalStateException e) {
catch (StreamException e) {
// expected
String message = e.getMessage();
assertTrue("Wrong message: "+message, message.contains("must be before"));
}
}
public void testRestoreWorksFromClosedStream() throws Exception {
source.close();
source.restoreFrom(new ExecutionAttributes());
}
/**
* Skipping marked records after rollback.
*/
@@ -224,16 +221,22 @@ public class StaxEventItemReaderTests extends TestCase {
newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
newSource.setFragmentDeserializer(deserializer);
newSource.open();
Object item = newSource.read();
assertNotNull(item);
assertTrue(newSource.isOpenCalled());
newSource.close();
newSource.close();
newSource.setOpenCalled(false);
// calling read again should require re-initialization because of close
item = newSource.read();
assertNotNull(item);
assertTrue(newSource.isOpenCalled());
try {
item = newSource.read();
fail("Expected StreamException");
}
catch (StreamException e) {
// expected
}
}
public void testOpenBadIOInput() {
@@ -293,6 +296,8 @@ public class StaxEventItemReaderTests extends TestCase {
newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
newSource.setFragmentDeserializer(deserializer);
newSource.open();
return newSource;
}

View File

@@ -45,6 +45,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
protected void setUp() throws Exception {
resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", "xml"));
writer = createItemWriter();
writer.open();
}
/**
@@ -84,17 +85,18 @@ public class StaxEventWriterItemWriterTests extends TestCase {
* Restart scenario - content is appended to the output file after restart.
*/
public void testRestart() throws Exception {
// write records
// write record
writer.write(record);
writer.mark();
// writer.mark();
ExecutionAttributes streamContext = writer.getExecutionAttributes();
writer.close();
// create new writer from saved restart data and continue writing
writer = createItemWriter();
writer.restoreFrom(streamContext);
writer.write(record);
writer.close();
// check the output is concatenation of 'before restart' and 'after
// restart' writes.
String outputFile = outputFileContent();
@@ -199,7 +201,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
source.setOverwriteOutput(true);
source.afterPropertiesSet();
return source;
}
}

View File

@@ -45,6 +45,7 @@ public abstract class AbstractTradeBatchTests extends TestCase {
protected void setUp() throws Exception {
super.setUp();
provider = new TradeItemReader(resource);
provider.open();
}
protected static class TradeItemReader extends DelegatingItemReader {