BATCH-365: ItemStream#Update and ItemStream#close now accept an ExecutionContext as an argument. I removed the local ExecutionContexts in the readers and readers, and had them use the passed in ones instead.

This commit is contained in:
lucasward
2008-02-26 21:22:10 +00:00
parent 93445b9164
commit e952a55ab2
28 changed files with 172 additions and 163 deletions

View File

@@ -31,6 +31,7 @@ public class HibernateCursorItemReaderIntegrationTests extends AbstractDataSourc
inputSource.setSessionFactory(sessionFactory);
inputSource.setUseStatelessSession(isUseStatelessSession());
inputSource.afterPropertiesSet();
inputSource.setSaveState(true);
return inputSource;
}

View File

@@ -78,7 +78,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(itemReader).update();
getAsItemStream(itemReader).update(executionContext);
// create new input source
itemReader = createItemReader();
@@ -104,7 +104,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(itemReader).update();
getAsItemStream(itemReader).update(executionContext);
// create new input source
itemReader = createItemReader();

View File

@@ -29,12 +29,12 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader
}
}
public void update() {
inputSource.update();
public void update(ExecutionContext executionContext) {
inputSource.update(executionContext);
}
public void destroy() throws Exception {
inputSource.close();
inputSource.close(null);
}
public void setFooDao(FooDao fooDao) {
@@ -48,7 +48,7 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader
inputSource.open(executionContext);
};
public void close() {
public void close(ExecutionContext executionContext) {
}
/*

View File

@@ -74,7 +74,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
* Release resources and delete the temporary file
*/
protected void tearDown() throws Exception {
reader.close();
reader.close(null);
}
private Resource getInputResource(String input) {
@@ -87,7 +87,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
*/
public void testSkip() throws Exception {
reader.close();
reader.close(null);
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
reader.open(executionContext);
@@ -122,7 +122,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
*/
public void testSkipFirstChunk() throws Exception {
reader.close();
reader.close(null);
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
reader.open(executionContext);
@@ -145,7 +145,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
public void testRestart() throws Exception {
reader.close();
reader.close(null);
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
reader.open(executionContext);
@@ -159,11 +159,11 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
reader.read();
// get restart data
reader.update();
reader.update(executionContext);
assertEquals(4, executionContext.getLong(
FlatFileItemReader.class.getName() + "." + FlatFileItemReader.READ_STATISTICS_NAME));
// close input
reader.close();
reader.close(executionContext);
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
@@ -174,7 +174,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
assertEquals("[testLine5]", reader.read().toString());
assertEquals("[testLine6]", reader.read().toString());
reader.update();
reader.update(executionContext);
assertEquals(6, executionContext.getLong(FlatFileItemReader.class.getName() + "." + FlatFileItemReader.READ_STATISTICS_NAME));
}

View File

@@ -83,7 +83,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
* Release resources.
*/
protected void tearDown() throws Exception {
itemReader.close();
itemReader.close(null);
}
private Resource getInputResource(String input) {
@@ -159,7 +159,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader = new FlatFileItemReader();
itemReader.setResource(getInputResource(TEST_STRING));
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.close();
itemReader.close(null);
// The open does not happen automatically on a read...
itemReader.open(executionContext);
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());

View File

@@ -82,7 +82,7 @@ public class FlatFileItemWriterTests extends TestCase {
if (reader != null) {
reader.close();
}
inputSource.close();
inputSource.close(null);
outputFile.delete();
}
@@ -107,7 +107,7 @@ public class FlatFileItemWriterTests extends TestCase {
public void testWriteString() throws Exception {
inputSource.open(executionContext);
inputSource.write(TEST_STRING);
inputSource.close();
inputSource.close(null);
String lineFromFile = readLine();
assertEquals(TEST_STRING, lineFromFile);
@@ -125,7 +125,7 @@ public class FlatFileItemWriterTests extends TestCase {
});
Object data = new Object();
inputSource.write(data);
inputSource.close();
inputSource.close(null);
String lineFromFile = readLine();
// converter not used if input is String
assertEquals("FOO:" + data.toString(), lineFromFile);
@@ -143,7 +143,7 @@ public class FlatFileItemWriterTests extends TestCase {
});
Object data = new Object();
inputSource.write(data);
inputSource.close();
inputSource.close(null);
String lineFromFile = readLine();
// converter not used if input is String
assertEquals("FOO:" + data.toString(), lineFromFile);
@@ -160,7 +160,7 @@ public class FlatFileItemWriterTests extends TestCase {
}
});
inputSource.write(TEST_STRING);
inputSource.close();
inputSource.close(null);
String lineFromFile = readLine();
assertEquals("FOO:" + TEST_STRING, lineFromFile);
}
@@ -174,7 +174,7 @@ public class FlatFileItemWriterTests extends TestCase {
// AggregatorStub ignores the LineDescriptor, so we pass null
inputSource.write(args);
inputSource.close();
inputSource.close(null);
String lineFromFile = readLine();
assertEquals(args, lineFromFile);
}
@@ -183,7 +183,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.write("testLine1");
// rollback
rollback();
inputSource.close();
inputSource.close(null);
String lineFromFile = readLine();
assertEquals(null, lineFromFile);
}
@@ -192,7 +192,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.write("testLine1");
// rollback
commit();
inputSource.close();
inputSource.close(null);
String lineFromFile = readLine();
assertEquals("testLine1", lineFromFile);
}
@@ -222,9 +222,9 @@ public class FlatFileItemWriterTests extends TestCase {
commit();
// get restart data
inputSource.update();
inputSource.update(executionContext);
// close template
inputSource.close();
inputSource.close(executionContext);
// init with correct data
inputSource.open(executionContext);
@@ -235,9 +235,9 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.write("testLine8");
// get statistics
inputSource.update();
inputSource.update(executionContext);
// close template
inputSource.close();
inputSource.close(executionContext);
// verify what was written to the file
for (int i = 1; i < 9; i++) {
@@ -266,7 +266,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.setFieldSetUnmapper(new PassThroughFieldSetMapper());
inputSource.afterPropertiesSet();
inputSource.open(executionContext);
inputSource.update();
inputSource.update(executionContext);
assertNotNull(executionContext);
assertEquals(3, executionContext.entrySet().size());
assertEquals(0, executionContext.getLong(FlatFileItemWriter.class.getName() + "." + FlatFileItemWriter.RESTART_DATA_NAME));

View File

@@ -66,9 +66,9 @@ public class ResourceLineReaderTests extends TestCase {
Resource resource = new ByteArrayResource("a,b,c\n1,2,3".getBytes());
ResourceLineReader reader = new ResourceLineReader(resource);
reader.open();
reader.close();
reader.close(null);
try {
reader.close(); // just closing a BufferedReader twice should be fine
reader.close(null); // just closing a BufferedReader twice should be fine
} catch (Exception e) {
fail("Unexpected Exception "+e);
}
@@ -181,7 +181,7 @@ public class ResourceLineReaderTests extends TestCase {
Resource resource = new ByteArrayResource("1\n# 2\n3".getBytes());
ResourceLineReader reader = new ResourceLineReader(resource);
reader.read();
reader.close();
reader.close(null);
reader.mark();
}

View File

@@ -79,7 +79,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(itemReader).update();
getAsItemStream(itemReader).update(executionContext);
// create new input source
itemReader = createItemReader();
@@ -101,7 +101,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(itemReader).update();
getAsItemStream(itemReader).update(executionContext);
// create new input source
itemReader = createItemReader();

View File

@@ -46,7 +46,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onTearDownAfterTransaction()
*/
protected void onTearDownAfterTransaction() throws Exception {
getAsItemStream(reader).close();
getAsItemStream(reader).close(null);
super.onTearDownAfterTransaction();
}
@@ -89,7 +89,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
Foo foo2 = (Foo) reader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(reader).update();
getAsItemStream(reader).update(executionContext);
// create new input source
reader = createItemReader();
@@ -113,7 +113,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
Foo foo2 = (Foo) reader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(reader).update();
getAsItemStream(reader).update(executionContext);
// create new input source
reader = createItemReader();
@@ -135,7 +135,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
* @throws Exception
*/
public void testRestoreFromEmptyData() throws Exception {
getAsItemStream(reader).update();
getAsItemStream(reader).update(executionContext);
Foo foo = (Foo) reader.read();
assertEquals(1, foo.getValue());
@@ -218,7 +218,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
rollback();
getAsItemStream(reader).update();
getAsItemStream(reader).update(executionContext);
// create new input source
reader = createItemReader();

View File

@@ -92,7 +92,7 @@ public class StaxEventItemReaderTests extends TestCase {
assertNotNull(source.read());
assertNull(source.read()); // there are only two fragments
source.close();
source.close(executionContext);
}
/**
@@ -117,7 +117,8 @@ public class StaxEventItemReaderTests extends TestCase {
public void testRestart() {
source.open(executionContext);
source.read();
source.update();
source.update(executionContext);
System.out.println(executionContext);
assertEquals(1, executionContext.getLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME));
List expectedAfterRestart = (List) source.read();
@@ -146,8 +147,8 @@ public class StaxEventItemReaderTests extends TestCase {
}
public void testRestoreWorksFromClosedStream() throws Exception {
source.close();
source.update();
source.close(executionContext);
source.update(executionContext);
}
/**
* Skipping marked records after rollback.
@@ -198,13 +199,13 @@ public class StaxEventItemReaderTests extends TestCase {
public void testExecutionContext() {
final int NUMBER_OF_RECORDS = 2;
source.open(executionContext);
source.update();
source.update(executionContext);
for (int i = 0; i < NUMBER_OF_RECORDS; i++) {
long recordCount = extractRecordCount();
assertEquals(i, recordCount);
source.read();
source.update();
source.update(executionContext);
}
assertEquals(NUMBER_OF_RECORDS, extractRecordCount());
@@ -217,7 +218,7 @@ public class StaxEventItemReaderTests extends TestCase {
}
public void testCloseWithoutOpen() throws Exception {
source.close();
source.close(null);
// No error!
}
@@ -235,7 +236,7 @@ public class StaxEventItemReaderTests extends TestCase {
assertNotNull(item);
assertTrue(newSource.isOpenCalled());
newSource.close();
newSource.close(null);
newSource.setOpenCalled(false);
// calling read again should require re-initialization because of close
try {
@@ -304,6 +305,7 @@ public class StaxEventItemReaderTests extends TestCase {
newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
newSource.setFragmentDeserializer(deserializer);
newSource.setSaveState(true);
return newSource;
}

View File

@@ -94,14 +94,14 @@ public class StaxEventItemWriterTests extends TestCase {
// write record
writer.write(record);
// writer.mark();
writer.update();
writer.close();
writer.update(executionContext);
writer.close(executionContext);
// create new writer from saved restart data and continue writing
writer = createItemWriter();
writer.open(executionContext);
writer.write(record);
writer.close();
writer.close(executionContext);
// check the output is concatenation of 'before restart' and 'after
// restart' writes.
@@ -124,8 +124,8 @@ public class StaxEventItemWriterTests extends TestCase {
final int NUMBER_OF_RECORDS = 10;
for (int i = 1; i <= NUMBER_OF_RECORDS; i++) {
writer.write(record);
writer.update();
long writeStatistics = executionContext.getLong(StaxEventItemWriter.WRITE_STATISTICS_NAME);
writer.update(executionContext);
long writeStatistics = executionContext.getLong(StaxEventItemWriter.class.getName() + "." + StaxEventItemWriter.WRITE_STATISTICS_NAME);
assertEquals(i, writeStatistics);
}
@@ -146,7 +146,7 @@ public class StaxEventItemWriterTests extends TestCase {
assertTrue(outputFileContent().indexOf("<testroot attribute=\"value\"") != NOT_FOUND);
writer.close();
writer.close(null);
assertTrue(outputFileContent().endsWith("</testroot>"));
}
@@ -207,6 +207,7 @@ public class StaxEventItemWriterTests extends TestCase {
source.setRootTagName("root");
source.setVersion("1.0");
source.setOverwriteOutput(true);
source.setSaveState(true);
source.afterPropertiesSet();

View File

@@ -21,9 +21,9 @@ import java.util.Properties;
import junit.framework.TestCase;
import org.springframework.batch.io.Skippable;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.support.PropertiesConverter;
/**
@@ -76,7 +76,7 @@ public class DelegatingItemReaderTests extends TestCase {
* Gets restart data from the input template
*/
public void testGetStreamContext() {
itemProvider.update();
itemProvider.update(executionContext);
assertEquals("foo", executionContext.getString("value"));
}
@@ -88,26 +88,24 @@ public class DelegatingItemReaderTests extends TestCase {
private static class MockItemReader extends AbstractItemReader implements ItemReader, ItemStream, Skippable {
private Object value;
private ExecutionContext executionContext;
public Properties getStatistics() {
return PropertiesConverter.stringToProperties("a=b");
}
public void update() {
public void update(ExecutionContext executionContext) {
executionContext.putString("value", "foo");
}
public MockItemReader(Object value, ExecutionContext executionContext) {
this.value = value;
this.executionContext = executionContext;
}
public Object read() {
return value;
}
public void close() {
public void close(ExecutionContext executionContext) {
}
public void open(ExecutionContext executionContext) {

View File

@@ -103,11 +103,11 @@ public class SimpleStreamManagerTests extends TestCase {
*/
public void testMark() {
manager.register(new ItemStreamSupport() {
public void update() {
public void update(ExecutionContext executionContext) {
list.add("bar");
}
});
manager.update();
manager.update(null);
assertEquals(1, list.size());
}
@@ -117,11 +117,11 @@ public class SimpleStreamManagerTests extends TestCase {
*/
public void testClose() {
manager.register(new ItemStreamSupport() {
public void close() throws StreamException {
public void close(ExecutionContext executionContext) throws StreamException {
list.add("bar");
}
});
manager.close();
manager.close(null);
assertEquals(1, list.size());
}
@@ -131,7 +131,7 @@ public class SimpleStreamManagerTests extends TestCase {
*/
public void testCommitWithoutMark() {
manager.register(new ItemStreamSupport() {
public void update() {
public void update(ExecutionContext executionContext) {
list.add("bar");
}
});
@@ -146,7 +146,7 @@ public class SimpleStreamManagerTests extends TestCase {
*/
public void testRollbackWithoutMark() {
manager.register( new ItemStreamSupport() {
public void update() {
public void update(ExecutionContext executionContext) {
list.add("bar");
}
});