BATCH-365: There should now be one ExecutionContext per step. All itemStreams will be opened with an execution context, and will be notified before it is saved, to ensure they have all state in the context. Most ItemReader/Writers should now have the logic for whether or not to put their state in the context, but a few have likely been missed.

This commit is contained in:
lucasward
2008-02-26 08:29:37 +00:00
parent 3b48ece7e5
commit 64506040ff
42 changed files with 512 additions and 914 deletions

View File

@@ -3,6 +3,7 @@ package org.springframework.batch.io.cursor;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.springframework.batch.io.support.AbstractDataSourceItemReaderIntegrationTests;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@@ -48,7 +49,7 @@ public class HibernateCursorItemReaderIntegrationTests extends AbstractDataSourc
HibernateCursorItemReader inputSource = ((HibernateCursorItemReader) reader);
// initialize and call setter => error
inputSource.open();
inputSource.open(new ExecutionContext());
try {
inputSource.setUseStatelessSession(false);
fail();

View File

@@ -22,6 +22,7 @@ public class JdbcCursorItemReaderIntegrationTests extends AbstractDataSourceItem
result.setFetchSize(10);
result.setMaxRows(100);
result.setQueryTimeout(1000);
result.setSaveState(true);
return result;
}

View File

@@ -32,6 +32,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
DrivingQueryItemReader inputSource = new DrivingQueryItemReader();
inputSource.setKeyGenerator(new MockKeyGenerator());
inputSource.setSaveState(true);
return inputSource;
}
@@ -67,18 +68,22 @@ public class DrivingQueryItemReaderTests extends TestCase {
*/
public void testRestart() throws Exception {
ExecutionContext executionContext = new ExecutionContext();
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
ExecutionContext streamContext = getAsRestartable(itemReader).getExecutionContext();
getAsItemStream(itemReader).beforeSave();
// create new input source
itemReader = createItemReader();
getAsRestartable(itemReader).restoreFrom(streamContext);
getAsItemStream(itemReader).open(executionContext);
Foo fooAfterRestart = (Foo) itemReader.read();
assertEquals(3, fooAfterRestart.getValue());
@@ -89,13 +94,17 @@ public class DrivingQueryItemReaderTests extends TestCase {
*/
public void testInvalidRestore() throws Exception {
ExecutionContext executionContext = new ExecutionContext();
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
ExecutionContext streamContext = getAsRestartable(itemReader).getExecutionContext();
getAsItemStream(itemReader).beforeSave();
// create new input source
itemReader = createItemReader();
@@ -104,7 +113,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
assertEquals(1, foo.getValue());
try {
getAsRestartable(itemReader).restoreFrom(streamContext);
getAsItemStream(itemReader).open(executionContext);
fail();
}
catch (IllegalStateException ex) {
@@ -119,7 +128,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
public void testRestoreFromEmptyData() throws Exception {
ExecutionContext streamContext = new ExecutionContext(new Properties());
getAsRestartable(itemReader).restoreFrom(streamContext);
getAsItemStream(itemReader).open(streamContext);
Foo foo = (Foo) itemReader.read();
assertEquals(1, foo.getValue());
@@ -158,7 +167,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
return (InitializingBean) source;
}
private ItemStream getAsRestartable(ItemReader source) {
private ItemStream getAsItemStream(ItemReader source) {
return (ItemStream) source;
}
@@ -167,6 +176,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
static ExecutionContext streamContext;
List keys;
List restartKeys;
static final String RESTART_KEY = "restart.keys";
static{
Properties props = new Properties();
@@ -191,18 +201,21 @@ public class DrivingQueryItemReaderTests extends TestCase {
restartKeys.add(new Foo(5, "5", 5));
}
public ExecutionContext getKeyAsExecutionContext(Object key) {
public ExecutionContext saveState(Object key) {
return streamContext;
}
public List restoreKeys(ExecutionContext streamContext) {
assertEquals(MockKeyGenerator.streamContext, streamContext);
return restartKeys;
public List retrieveKeys(ExecutionContext executionContext) {
if(executionContext.containsKey(RESTART_KEY)){
return restartKeys;
}
else{
return keys;
}
}
public List retrieveKeys() {
return keys;
public void saveState(Object key, ExecutionContext executionContext) {
executionContext.put(RESTART_KEY, restartKeys);
}
}

View File

@@ -29,12 +29,8 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader
}
}
public ExecutionContext getExecutionContext() {
return inputSource.getExecutionContext();
}
public void restoreFrom(ExecutionContext data) {
inputSource.restoreFrom(data);
public void beforeSave() {
inputSource.beforeSave();
}
public void destroy() throws Exception {
@@ -48,7 +44,8 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader
public void afterPropertiesSet() throws Exception {
}
public void open() {
public void open(ExecutionContext executionContext) {
inputSource.open(executionContext);
};
public void close() {

View File

@@ -31,6 +31,7 @@ public class IbatisItemReaderIntegrationTests extends AbstractDataSourceItemRead
keyGenerator.setSqlMapClient(sqlMapClient);
inputSource.setSqlMapClient(sqlMapClient);
inputSource.setKeyGenerator(keyGenerator);
inputSource.setSaveState(true);
return inputSource;
}

View File

@@ -34,6 +34,7 @@ public class MultipleColumnJdbcDrivingQueryItemReaderIntegrationTests extends
keyGenerator.setRestartSql("SELECT ID, VALUE from T_FOOS where ID > ? and VALUE > ? order by ID");
DrivingQueryItemReader inputSource = new DrivingQueryItemReader();
inputSource.setSaveState(true);
inputSource.setKeyGenerator(keyGenerator);
FooItemReader fooItemReader = new FooItemReader(inputSource, getJdbcTemplate());
fooItemReader.setFooDao(new CompositeKeyFooDao(getJdbcTemplate()));

View File

@@ -19,7 +19,7 @@ public class SingleColumnJdbcDrivingQueryItemReaderIntegrationTests extends Abst
keyStrategy.setRestartSql("SELECT ID from T_FOOS where ID > ? order by ID");
DrivingQueryItemReader inputSource = new DrivingQueryItemReader();
inputSource.setKeyGenerator(keyStrategy);
inputSource.setSaveState(true);
return new FooItemReader(inputSource, getJdbcTemplate());
}

View File

@@ -20,8 +20,6 @@ import org.springframework.jdbc.core.PreparedStatementSetter;
*/
public class ColumnMapExecutionContextRowMapperTests extends TestCase {
private static final String KEY = ColumnMapExecutionContextRowMapper.KEY_PREFIX;
private ColumnMapExecutionContextRowMapper mapper;
private Map key;
@@ -29,6 +27,8 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
private MockControl psControl = MockControl.createControl(PreparedStatement.class);
private PreparedStatement ps;
private ExecutionContext executionContext;
protected void setUp() throws Exception {
super.setUp();
@@ -37,12 +37,14 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
key = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(2);
key.put("1", new Integer(1));
key.put("2", new Integer(2));
executionContext = new ExecutionContext();
}
public void testCreateExecutionContextWithInvalidType() throws Exception {
try{
mapper.createExecutionContext(new Object());
mapper.mapKeys(new Object(), executionContext);
fail();
}catch(IllegalArgumentException ex){
//expected
@@ -52,7 +54,7 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
public void testCreateExecutionContextWithNull(){
try{
mapper.createExecutionContext(null);
mapper.mapKeys(null, null);
fail();
}catch(IllegalArgumentException ex){
//expected
@@ -60,31 +62,28 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
}
public void testCreateExecutionContext() throws Exception {
ExecutionContext streamContext = mapper.createExecutionContext(key);
Properties props = streamContext.getProperties();
assertEquals("1", props.getProperty(KEY + "0"));
assertEquals("2", props.getProperty(KEY + "1"));
mapper.mapKeys(key, executionContext);
Properties props = executionContext.getProperties();
assertEquals("1", props.getProperty("1"));
assertEquals("2", props.getProperty("2"));
}
public void testCreateExecutionContextFromEmptyKeys() throws Exception {
ExecutionContext streamContext = mapper.createExecutionContext(new HashMap());
assertEquals(0, streamContext.getProperties().size());
mapper.mapKeys(new HashMap(), executionContext);
assertEquals(0, executionContext.size());
}
public void testCreateSetter() throws Exception {
Properties props = new Properties();
props.setProperty(KEY + "0", "1");
props.setProperty(KEY + "1", "2");
ExecutionContext streamContext = new ExecutionContext();
streamContext.putString(KEY + "0", "1");
streamContext.putString(KEY + "1", "2");
streamContext.putString("0", "1");
streamContext.putString("1", "2");
PreparedStatementSetter setter = mapper.createSetter(streamContext);
ps = (PreparedStatement)psControl.getMock();
ps.setString(1, "1");
ps.setString(2, "2");
ps.setString(1, "2");
ps.setString(2, "1");
psControl.replay();
setter.setValues(ps);

View File

@@ -19,6 +19,8 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
MultipleColumnJdbcKeyGenerator keyStrategy;
ExecutionContext executionContext;
protected String[] getConfigLocations(){
return new String[] { "org/springframework/batch/io/sql/data-source-context.xml"};
}
@@ -30,11 +32,13 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
"SELECT ID, VALUE from T_FOOS order by ID");
keyStrategy.setRestartSql("SELECT ID, VALUE from T_FOOS where ID > ? and VALUE > ? order by ID");
executionContext = new ExecutionContext();
}
public void testRetrieveKeys(){
List keys = keyStrategy.retrieveKeys();
List keys = keyStrategy.retrieveKeys(executionContext);
for (int i = 0; i < keys.size(); i++) {
Map id = (Map)keys.get(i);
@@ -45,11 +49,10 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
public void testRestoreKeys(){
ExecutionContext streamContext = new ExecutionContext();
streamContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "0", "3");
streamContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "1", "3");
executionContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "0", "3");
executionContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "1", "3");
List keys = keyStrategy.restoreKeys(streamContext);
List keys = keyStrategy.retrieveKeys(executionContext);
assertEquals(2, keys.size());
Map key = (Map)keys.get(0);
@@ -66,8 +69,8 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
key.put("ID", new Long(3));
key.put("VALUE", new Integer(3));
ExecutionContext streamContext = keyStrategy.getKeyAsExecutionContext(key);
Properties props = streamContext.getProperties();
keyStrategy.saveState(key, executionContext);
Properties props = executionContext.getProperties();
assertEquals(2, props.size());
assertEquals("3", props.get(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "0"));
@@ -77,19 +80,10 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
public void testGetNullKeyAsStreamContext(){
try{
keyStrategy.getKeyAsExecutionContext(null);
keyStrategy.saveState(null, null);
fail();
}catch(IllegalArgumentException ex){
//expected
}
}
public void testRestoreKeysFromNull(){
try{
keyStrategy.getKeyAsExecutionContext(null);
}catch(IllegalArgumentException ex){
//expected
}
}
}

View File

@@ -1,7 +1,6 @@
package org.springframework.batch.io.driving.support;
import java.util.List;
import java.util.Properties;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
@@ -15,6 +14,8 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
SingleColumnJdbcKeyGenerator keyStrategy;
ExecutionContext executionContext;
protected String[] getConfigLocations(){
return new String[] { "org/springframework/batch/io/sql/data-source-context.xml"};
}
@@ -27,11 +28,13 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
"SELECT ID from T_FOOS order by ID");
keyStrategy.setRestartSql("SELECT ID from T_FOOS where ID > ? order by ID");
executionContext = new ExecutionContext();
}
public void testRetrieveKeys(){
List keys = keyStrategy.retrieveKeys();
List keys = keyStrategy.retrieveKeys(new ExecutionContext());
for (int i = 0; i < keys.size(); i++) {
Long id = (Long)keys.get(i);
@@ -41,11 +44,9 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
public void testRestoreKeys(){
Properties props = new Properties();
props.setProperty(SingleColumnJdbcKeyGenerator.RESTART_KEY, "3");
ExecutionContext streamContext = new ExecutionContext(props);
executionContext.putString(SingleColumnJdbcKeyGenerator.RESTART_KEY, "3");
List keys = keyStrategy.restoreKeys(streamContext);
List keys = keyStrategy.retrieveKeys(executionContext);
assertEquals(2, keys.size());
assertEquals(new Long(4), keys.get(0));
@@ -54,17 +55,16 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
public void testGetKeyAsStreamContext(){
ExecutionContext streamContext = keyStrategy.getKeyAsExecutionContext(new Long(3));
Properties props = streamContext.getProperties();
keyStrategy.saveState(new Long(3), executionContext);
assertEquals(1, props.size());
assertEquals("3", props.get(SingleColumnJdbcKeyGenerator.RESTART_KEY));
assertEquals(1, executionContext.size());
assertEquals("3", executionContext.getString(SingleColumnJdbcKeyGenerator.RESTART_KEY));
}
public void testGetNullKeyAsStreamContext(){
try{
keyStrategy.getKeyAsExecutionContext(null);
keyStrategy.saveState(null, null);
fail();
}catch(IllegalArgumentException ex){
//expected
@@ -74,7 +74,7 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
public void testRestoreKeysFromNull(){
try{
keyStrategy.getKeyAsExecutionContext(null);
keyStrategy.saveState(null, null);
}catch(IllegalArgumentException ex){
//expected
}

View File

@@ -25,7 +25,6 @@ import org.springframework.batch.io.file.mapping.FieldSet;
import org.springframework.batch.io.file.mapping.FieldSetMapper;
import org.springframework.batch.io.file.transform.LineTokenizer;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
@@ -41,6 +40,8 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
// common value used for writing to a file
private String TEST_STRING = "FlatFileInputTemplate-TestData";
private ExecutionContext executionContext;
// simple stub instead of a realistic tokenizer
private LineTokenizer tokenizer = new LineTokenizer() {
@@ -66,7 +67,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
reader.setFieldSetMapper(fieldSetMapper);
// context argument is necessary only for the FileLocator, which
// is mocked
reader.open();
executionContext = new ExecutionContext();
}
/**
@@ -88,7 +89,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
reader.close();
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
reader.open();
reader.open(executionContext);
// read some records
reader.read(); // #1
@@ -123,7 +124,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
reader.close();
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
reader.open();
reader.open(executionContext);
// read some records
reader.read(); // #1
@@ -142,30 +143,11 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
}
public void testRestartFromNullData() throws Exception {
reader.restoreFrom(null);
assertEquals("[FlatFileInputTemplate-TestData]", reader.read().toString());
}
public void testRestartBeforeOpen() throws Exception {
reader = new FlatFileItemReader();
reader.setResource(getInputResource(TEST_STRING));
reader.setFieldSetMapper(fieldSetMapper);
// do not open the template...
try {
reader.restoreFrom(reader.getExecutionContext());
} catch (StreamException e) {
assertTrue("Message does not contain open: "+e.getMessage(), e.getMessage().contains("open"));
}
reader.open();
assertEquals("[FlatFileInputTemplate-TestData]", reader.read().toString());
}
public void testRestart() throws Exception {
reader.close();
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
reader.open();
reader.open(executionContext);
// read some records
reader.read();
@@ -177,24 +159,23 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
reader.read();
// get restart data
ExecutionContext streamContext = reader.getExecutionContext();
assertEquals("4", (String) streamContext.getProperties().getProperty(
FlatFileItemReader.READ_STATISTICS_NAME));
reader.beforeSave();
assertEquals(4, executionContext.getLong(
FlatFileItemReader.class.getName() + "." + FlatFileItemReader.READ_STATISTICS_NAME));
// close input
reader.close();
reader.setResource(getInputResource("testLine1\ntestLine2\ntestLine3\ntestLine4\ntestLine5\ntestLine6"));
// init for restart
reader.open();
reader.restoreFrom(streamContext);
reader.open(executionContext);
// read remaining records
assertEquals("[testLine5]", reader.read().toString());
assertEquals("[testLine6]", reader.read().toString());
ExecutionContext statistics = reader.getExecutionContext();
assertEquals(6, statistics.getLong(FlatFileItemReader.READ_STATISTICS_NAME));
reader.beforeSave();
assertEquals(6, executionContext.getLong(FlatFileItemReader.class.getName() + "." + FlatFileItemReader.READ_STATISTICS_NAME));
}
}

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.ExecutionContext;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ByteArrayResource;
@@ -48,6 +49,8 @@ public class FlatFileItemReaderBasicTests extends TestCase {
// common value used for writing to a file
private String TEST_STRING = "FlatFileInputTemplate-TestData";
private String TEST_OUTPUT = "[FlatFileInputTemplate-TestData]";
private ExecutionContext executionContext;
// simple stub instead of a realistic tokenizer
private LineTokenizer tokenizer = new LineTokenizer() {
@@ -72,8 +75,8 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setLineTokenizer(tokenizer);
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.afterPropertiesSet();
itemReader.open();
executionContext = new ExecutionContext();
}
/**
@@ -91,6 +94,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
* Regular usage of <code>read</code> method
*/
public void testRead() throws Exception {
itemReader.open(executionContext);
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
}
@@ -98,6 +102,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
* Regular usage of <code>read</code> method
*/
public void testReadExhausted() throws Exception {
itemReader.open(executionContext);
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
assertEquals(null, itemReader.read());
}
@@ -112,6 +117,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
}
});
try {
itemReader.open(executionContext);
itemReader.read();
fail("Expected ParsingException");
} catch (FlatFileParsingException e) {
@@ -128,6 +134,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
});
try {
itemReader.open(executionContext);
itemReader.read();
fail("Expected ParsingException");
} catch (FlatFileParsingException e) {
@@ -154,7 +161,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.close();
// The open does not happen automatically on a read...
itemReader.open();
itemReader.open(executionContext);
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
}
@@ -170,7 +177,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
}
public void testOpenTwiceHasNoEffect() throws Exception {
itemReader.open();
itemReader.open(executionContext);
testRead();
}
@@ -179,7 +186,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setEncoding("UTF-8");
itemReader.setResource(getInputResource(TEST_STRING));
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.open();
itemReader.open(executionContext);
testRead();
}
@@ -188,7 +195,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setEncoding(null);
itemReader.setResource(getInputResource(TEST_STRING));
try {
itemReader.open();
itemReader.open(executionContext);
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
@@ -201,7 +208,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setEncoding("foo");
itemReader.setResource(getInputResource(TEST_STRING));
try {
itemReader.open();
itemReader.open(executionContext);
fail("Expected BatchEnvironmentException");
}
catch (BatchEnvironmentException e) {
@@ -238,7 +245,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.setFirstLineIsHeader(true);
itemReader.afterPropertiesSet();
itemReader.open();
itemReader.open(executionContext);
FieldSet fs = (FieldSet) itemReader.read();
assertEquals("value1", fs.readString("name1"));
@@ -261,7 +268,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
itemReader.setFieldSetMapper(fieldSetMapper);
itemReader.setLinesToSkip(1);
itemReader.afterPropertiesSet();
itemReader.open();
itemReader.open(executionContext);
FieldSet fs = (FieldSet) itemReader.read();
assertEquals("one", fs.readString(0));
@@ -286,7 +293,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
testReader.afterPropertiesSet();
try{
testReader.open();
testReader.open(executionContext);
fail();
}catch(IllegalStateException ex){
//expected
@@ -309,7 +316,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
//replace the resource to simulate runtime resource creation
testReader.setResource(getInputResource(TEST_STRING));
testReader.open();
testReader.open(executionContext);
assertEquals(TEST_OUTPUT, testReader.read().toString());
}

View File

@@ -28,6 +28,8 @@ import org.springframework.batch.io.file.mapping.FieldSet;
import org.springframework.batch.io.file.mapping.FieldSetCreator;
import org.springframework.batch.io.file.mapping.PassThroughFieldSetMapper;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -53,6 +55,8 @@ public class FlatFileItemWriterTests extends TestCase {
// reads the output file to check the result
private BufferedReader reader;
private ExecutionContext executionContext;
/**
* Create temporary output file, define mock behaviour, set dependencies and
@@ -70,9 +74,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.setResource(new FileSystemResource(outputFile));
inputSource.setFieldSetUnmapper(new PassThroughFieldSetMapper());
inputSource.afterPropertiesSet();
inputSource.open();
executionContext = new ExecutionContext();
}
/**
@@ -105,6 +107,7 @@ public class FlatFileItemWriterTests extends TestCase {
* @throws Exception
*/
public void testWriteString() throws Exception {
inputSource.open(executionContext);
inputSource.write(TEST_STRING);
inputSource.close();
String lineFromFile = readLine();
@@ -198,6 +201,7 @@ public class FlatFileItemWriterTests extends TestCase {
public void testRestart() throws Exception {
inputSource.open(executionContext);
// write some lines
inputSource.write("testLine1");
inputSource.write("testLine2");
@@ -220,25 +224,12 @@ public class FlatFileItemWriterTests extends TestCase {
commit();
// get restart data
ExecutionContext streamContext = inputSource.getExecutionContext();
inputSource.beforeSave();
// close template
inputSource.close();
// init for restart
inputSource.setBufferSize(0);
inputSource.open();
// try empty restart data...
try {
inputSource.restoreFrom(null);
assertTrue(true);
}
catch (IllegalArgumentException iae) {
fail("null restart data should be handled gracefully");
}
// init with correct data
inputSource.restoreFrom(streamContext);
inputSource.open(executionContext);
// write more lines
inputSource.write("testLine6");
@@ -246,7 +237,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.write("testLine8");
// get statistics
ExecutionContext statistics = inputSource.getExecutionContext();
inputSource.beforeSave();
// close template
inputSource.close();
@@ -256,7 +247,7 @@ public class FlatFileItemWriterTests extends TestCase {
}
// 3 lines were written to the file after restart
assertEquals(3, statistics.getLong(FlatFileItemWriter.WRITTEN_STATISTICS_NAME));
assertEquals(3, executionContext.getLong(FlatFileItemWriter.class.getName() + "." + FlatFileItemWriter.WRITTEN_STATISTICS_NAME));
}
@@ -276,11 +267,11 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.setResource(new FileSystemResource(outputFile));
inputSource.setFieldSetUnmapper(new PassThroughFieldSetMapper());
inputSource.afterPropertiesSet();
inputSource.open();
ExecutionContext streamContext = inputSource.getExecutionContext();
assertNotNull(streamContext);
assertEquals(3, streamContext.getProperties().size());
assertEquals(0, streamContext.getLong(FlatFileItemWriter.RESTART_DATA_NAME));
inputSource.open(executionContext);
inputSource.beforeSave();
assertNotNull(executionContext);
assertEquals(3, executionContext.entrySet().size());
assertEquals(0, executionContext.getLong(FlatFileItemWriter.class.getName() + "." + FlatFileItemWriter.RESTART_DATA_NAME));
}
private void commit() throws Exception {
@@ -291,4 +282,7 @@ public class FlatFileItemWriterTests extends TestCase {
inputSource.clear();
}
private ItemStream getAsItemStream(ItemWriter itemWriter){
return (ItemStream)itemWriter;
}
}

View File

@@ -1,9 +1,9 @@
package org.springframework.batch.io.sql;
import org.springframework.batch.io.sample.domain.Foo;
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.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
@@ -19,7 +19,8 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
protected ItemReader itemReader;
protected ExecutionContext executionContext;
/**
* @return input source with all necessary dependencies set
*/
@@ -33,6 +34,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
super.onSetUp();
itemReader = createItemReader();
getAsInitializingBean(itemReader).afterPropertiesSet();
executionContext = new ExecutionContext();
}
protected void onTearDown()throws Exception {
@@ -45,6 +47,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
*/
public void testNormalProcessing() throws Exception {
getAsInitializingBean(itemReader).afterPropertiesSet();
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
assertEquals(1, foo1.getValue());
@@ -69,19 +72,18 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
* @throws Exception
*/
public void testRestart() throws Exception {
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
ExecutionContext streamContext = getAsRestartable(itemReader).getExecutionContext();
getAsItemStream(itemReader).beforeSave();
// create new input source
itemReader = createItemReader();
getAsRestartable(itemReader).restoreFrom(streamContext);
getAsItemStream(itemReader).open(executionContext);
Foo fooAfterRestart = (Foo) itemReader.read();
assertEquals(3, fooAfterRestart.getValue());
@@ -92,13 +94,14 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
*/
public void testInvalidRestore() throws Exception {
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
ExecutionContext streamContext = getAsRestartable(itemReader).getExecutionContext();
getAsItemStream(itemReader).beforeSave();
// create new input source
itemReader = createItemReader();
@@ -107,7 +110,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
assertEquals(1, foo.getValue());
try {
getAsRestartable(itemReader).restoreFrom(streamContext);
getAsItemStream(itemReader).open(executionContext);
fail();
}
catch (IllegalStateException ex) {
@@ -122,7 +125,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
public void testRestoreFromEmptyData() throws Exception {
ExecutionContext streamContext = new ExecutionContext();
getAsRestartable(itemReader).restoreFrom(streamContext);
getAsItemStream(itemReader).open(executionContext);
Foo foo = (Foo) itemReader.read();
assertEquals(1, foo.getValue());
@@ -157,7 +160,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
itemReader.reset();
}
private ItemStream getAsRestartable(ItemReader source) {
private ItemStream getAsItemStream(ItemReader source) {
return (ItemStream) source;
}

View File

@@ -20,6 +20,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
AbstractTransactionalDataSourceSpringContextTests {
protected ItemReader reader;
protected ExecutionContext executionContext;
/**
* @return configured input source ready for use
@@ -37,6 +38,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
protected void onSetUpInTransaction() throws Exception {
super.onSetUpInTransaction();
reader = createItemReader();
executionContext = new ExecutionContext();
}
/*
@@ -79,18 +81,20 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
*/
public void testRestart() throws Exception {
getAsItemStream(reader).open(executionContext);
Foo foo1 = (Foo) reader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) reader.read();
assertEquals(2, foo2.getValue());
ExecutionContext streamContext = getAsItemStream(reader).getExecutionContext();
getAsItemStream(reader).beforeSave();
// create new input source
reader = createItemReader();
getAsItemStream(reader).restoreFrom(streamContext);
getAsItemStream(reader).open(executionContext);
Foo fooAfterRestart = (Foo) reader.read();
assertEquals(3, fooAfterRestart.getValue());
@@ -101,13 +105,15 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
*/
public void testInvalidRestore() throws Exception {
getAsItemStream(reader).open(executionContext);
Foo foo1 = (Foo) reader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) reader.read();
assertEquals(2, foo2.getValue());
ExecutionContext streamContext = getAsItemStream(reader).getExecutionContext();
getAsItemStream(reader).beforeSave();
// create new input source
reader = createItemReader();
@@ -116,7 +122,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
assertEquals(1, foo.getValue());
try {
getAsItemStream(reader).restoreFrom(streamContext);
getAsItemStream(reader).open(executionContext);
fail();
}
catch (IllegalStateException ex) {
@@ -129,9 +135,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
* @throws Exception
*/
public void testRestoreFromEmptyData() throws Exception {
ExecutionContext streamContext = new ExecutionContext();
getAsItemStream(reader).restoreFrom(streamContext);
getAsItemStream(reader).beforeSave();
Foo foo = (Foo) reader.read();
assertEquals(1, foo.getValue());
@@ -198,6 +202,8 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
return;
}
getAsItemStream(reader).open(executionContext);
Foo foo1 = (Foo) reader.read();
commit();
@@ -212,12 +218,12 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
rollback();
ExecutionContext streamContext = getAsItemStream(reader).getExecutionContext();
getAsItemStream(reader).beforeSave();
// create new input source
reader = createItemReader();
getAsItemStream(reader).restoreFrom(streamContext);
getAsItemStream(reader).open(executionContext);
assertEquals(foo2, reader.read());
Foo foo4 = (Foo) reader.read();

View File

@@ -37,8 +37,11 @@ public class StaxEventItemReaderTests extends TestCase {
private EventReaderDeserializer deserializer = new MockFragmentDeserializer();
private static final String FRAGMENT_ROOT_ELEMENT = "fragment";
private ExecutionContext executionContext;
protected void setUp() throws Exception {
this.executionContext = new ExecutionContext();
source = createNewInputSouce();
}
@@ -83,7 +86,7 @@ public class StaxEventItemReaderTests extends TestCase {
*/
public void testFragmentWrapping() throws Exception {
source.afterPropertiesSet();
source.open(executionContext);
// see asserts in the mock deserializer
assertNotNull(source.read());
assertNotNull(source.read());
@@ -112,13 +115,14 @@ public class StaxEventItemReaderTests extends TestCase {
* Save restart data and restore from it.
*/
public void testRestart() {
source.open(executionContext);
source.read();
ExecutionContext streamContext = source.getExecutionContext();
assertEquals(1, streamContext.getLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME));
source.beforeSave();
assertEquals(1, executionContext.getLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME));
List expectedAfterRestart = (List) source.read();
source = createNewInputSouce();
source.restoreFrom(streamContext);
source.open(executionContext);
List afterRestart = (List) source.read();
assertEquals(expectedAfterRestart.size(), afterRestart.size());
}
@@ -131,7 +135,7 @@ public class StaxEventItemReaderTests extends TestCase {
ExecutionContext context = new ExecutionContext();
context.putLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME, 100000);
try {
source.restoreFrom(context);
source.open(context);
fail("Expected StreamException");
}
catch (StreamException e) {
@@ -143,12 +147,13 @@ public class StaxEventItemReaderTests extends TestCase {
public void testRestoreWorksFromClosedStream() throws Exception {
source.close();
source.restoreFrom(new ExecutionContext());
source.beforeSave();
}
/**
* Skipping marked records after rollback.
*/
public void testSkip() {
source.open(executionContext);
List first = (List) source.read();
source.skip();
List second = (List) source.read();
@@ -162,7 +167,7 @@ public class StaxEventItemReaderTests extends TestCase {
* Rollback to last commited record.
*/
public void testRollback() {
source.open(executionContext);
// rollback between deserializing records
List first = (List) source.read();
source.mark();
@@ -190,13 +195,16 @@ public class StaxEventItemReaderTests extends TestCase {
* Statistics return the current record count. Calling read after end of
* input does not increase the counter.
*/
public void testStreamContext() {
public void testExecutionContext() {
final int NUMBER_OF_RECORDS = 2;
source.open(executionContext);
source.beforeSave();
for (int i = 0; i < NUMBER_OF_RECORDS; i++) {
long recordCount = extractRecordCount();
assertEquals(i, recordCount);
source.read();
source.beforeSave();
}
assertEquals(NUMBER_OF_RECORDS, extractRecordCount());
@@ -205,7 +213,7 @@ public class StaxEventItemReaderTests extends TestCase {
}
private long extractRecordCount() {
return source.getExecutionContext().getLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME);
return executionContext.getLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME);
}
public void testCloseWithoutOpen() throws Exception {
@@ -221,7 +229,7 @@ public class StaxEventItemReaderTests extends TestCase {
newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
newSource.setFragmentDeserializer(deserializer);
newSource.open();
newSource.open(executionContext);
Object item = newSource.read();
assertNotNull(item);
@@ -256,7 +264,7 @@ public class StaxEventItemReaderTests extends TestCase {
});
try {
source.open();
source.open(executionContext);
}
catch (DataAccessResourceFailureException ex) {
assertTrue(ex.getCause() instanceof IOException);
@@ -270,7 +278,7 @@ public class StaxEventItemReaderTests extends TestCase {
source.afterPropertiesSet();
try {
source.open();
source.open(executionContext);
fail();
}
catch (IllegalStateException ex) {
@@ -284,6 +292,7 @@ public class StaxEventItemReaderTests extends TestCase {
source.afterPropertiesSet();
source.setResource(new ByteArrayResource(xml.getBytes()));
source.open(executionContext);
source.read();
}
@@ -296,8 +305,6 @@ public class StaxEventItemReaderTests extends TestCase {
newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
newSource.setFragmentDeserializer(deserializer);
newSource.open();
return newSource;
}
@@ -381,8 +388,8 @@ public class StaxEventItemReaderTests extends TestCase {
private boolean openCalled = false;
public void open() {
super.open();
public void open(ExecutionContext executionContext) {
super.open(executionContext);
openCalled = true;
}

View File

@@ -30,6 +30,8 @@ public class StaxEventItemWriterTests extends TestCase {
// output file
private Resource resource;
private ExecutionContext executionContext;
// test record for writing to output
private Object record = new Object() {
@@ -45,13 +47,14 @@ public class StaxEventItemWriterTests extends TestCase {
protected void setUp() throws Exception {
resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", "xml"));
writer = createItemWriter();
writer.open();
executionContext = new ExecutionContext();
}
/**
* Write should pass its argument and StaxResult object to Serializer
*/
public void testWrite() throws Exception {
writer.open(executionContext);
Marshaller marshaller = new InputCheckMarshaller();
MarshallingEventWriterSerializer serializer = new MarshallingEventWriterSerializer(marshaller);
writer.setSerializer(serializer);
@@ -65,6 +68,7 @@ public class StaxEventItemWriterTests extends TestCase {
* Rolled back records should not be written to output file.
*/
public void testRollback() throws Exception {
writer.open(executionContext);
writer.write(record);
// rollback
writer.clear();
@@ -75,6 +79,7 @@ public class StaxEventItemWriterTests extends TestCase {
* Commited output is written to the output file.
*/
public void testCommit() throws Exception {
writer.open(executionContext);
writer.write(record);
// commit
writer.flush();
@@ -85,15 +90,16 @@ public class StaxEventItemWriterTests extends TestCase {
* Restart scenario - content is appended to the output file after restart.
*/
public void testRestart() throws Exception {
writer.open(executionContext);
// write record
writer.write(record);
// writer.mark();
ExecutionContext streamContext = writer.getExecutionContext();
writer.beforeSave();
writer.close();
// create new writer from saved restart data and continue writing
writer = createItemWriter();
writer.restoreFrom(streamContext);
writer.open(executionContext);
writer.write(record);
writer.close();
@@ -114,10 +120,12 @@ public class StaxEventItemWriterTests extends TestCase {
* Count of 'records written so far' is returned as statistics.
*/
public void testStreamContext() throws Exception {
writer.open(executionContext);
final int NUMBER_OF_RECORDS = 10;
for (int i = 1; i <= NUMBER_OF_RECORDS; i++) {
writer.write(record);
long writeStatistics = writer.getExecutionContext().getLong(StaxEventItemWriter.WRITE_STATISTICS_NAME);
writer.beforeSave();
long writeStatistics = executionContext.getLong(StaxEventItemWriter.WRITE_STATISTICS_NAME);
assertEquals(i, writeStatistics);
}
@@ -133,7 +141,7 @@ public class StaxEventItemWriterTests extends TestCase {
put("attribute", "value");
}
});
writer.open();
writer.open(executionContext);
writer.flush();
assertTrue(outputFileContent().indexOf("<testroot attribute=\"value\"") != NOT_FOUND);

View File

@@ -37,10 +37,13 @@ public class DelegatingItemReaderTests extends TestCase {
private DelegatingItemReader itemProvider = new DelegatingItemReader();
private ItemReader source;
private ExecutionContext executionContext;
// create input template and inject it to data provider
protected void setUp() throws Exception {
source = new MockItemReader(this);
executionContext = new ExecutionContext();
source = new MockItemReader(this, executionContext);
itemProvider.setItemReader(source);
}
@@ -73,17 +76,8 @@ public class DelegatingItemReaderTests extends TestCase {
* Gets restart data from the input template
*/
public void testGetStreamContext() {
Properties props = itemProvider.getExecutionContext().getProperties();
assertEquals("foo", props.getProperty("value"));
}
/**
* Forwared restart data to input template
* @throws Exception
*/
public void testRestoreFrom() throws Exception {
itemProvider.restoreFrom(new ExecutionContext(PropertiesConverter.stringToProperties("value=bar")));
assertEquals("bar", itemProvider.read());
itemProvider.beforeSave();
assertEquals("foo", executionContext.getString("value"));
}
public void testSkip() throws Exception {
@@ -94,21 +88,19 @@ 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 ExecutionContext getExecutionContext() {
return new ExecutionContext(PropertiesConverter.stringToProperties("value=foo"));
public void beforeSave() {
executionContext.putString("value", "foo");
}
public void restoreFrom(ExecutionContext data) {
value = data.getProperties().getProperty("value");
}
public MockItemReader(Object value) {
public MockItemReader(Object value, ExecutionContext executionContext) {
this.value = value;
this.executionContext = executionContext;
}
public Object read() {
@@ -118,7 +110,7 @@ public class DelegatingItemReaderTests extends TestCase {
public void close() {
}
public void open() {
public void open(ExecutionContext executionContext) {
}
public void skip() {

View File

@@ -16,19 +16,15 @@
package org.springframework.batch.item.stream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import junit.framework.TestCase;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.util.ClassUtils;
/**
* @author Dave Syer
@@ -49,7 +45,7 @@ public class SimpleStreamManagerTests extends TestCase {
public void testSimpleStreamManagerPlatformTransactionManager() {
manager = new SimpleStreamManager();
try {
manager.getTransaction("foo");
manager.getTransaction();
fail("Expected NullPointerException");
}
catch (NullPointerException e) {
@@ -68,117 +64,21 @@ public class SimpleStreamManagerTests extends TestCase {
return super.doGetTransaction();
}
});
manager.getTransaction("foo");
manager.getTransaction();
assertEquals("bar", list.get(0));
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#getExecutionContext(java.lang.Object)}.
*/
public void testGetStreamContextEmpty() {
ExecutionContext streamContext = manager.getExecutionContext("foo");
assertEquals(0, streamContext.entrySet().size());
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#getExecutionContext(java.lang.Object)}.
*/
public void testGetStreamContextNotEmpty() {
manager.register("foo", stream);
ExecutionContext streamContext = manager.getExecutionContext("foo");
assertEquals(1, streamContext.entrySet().size());
assertEquals("bar", streamContext.getString(ClassUtils.getQualifiedName(stream.getClass()) + ".foo"));
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#getExecutionContext(java.lang.Object)}.
*/
public void testGetStreamContextNotEmptyAndRestore() {
testGetStreamContextNotEmpty();
ExecutionContext context = manager.getExecutionContext("foo");
// Register again, now with the context that was created from the same
// stream...
manager.restoreFrom("foo", context);
assertEquals(1, list.size());
// The list should have the foo= map value from the sub-context
assertEquals("bar", list.get(0));
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#getExecutionContext(java.lang.Object)}.
*/
public void testGetStreamContextNotEmptyAndRestoreWithNoPrefix() {
ExecutionContext context = new ExecutionContext(PropertiesConverter.stringToProperties("foo=bar"));
manager.setUseClassNameAsPrefix(false);
manager.register("foo", stream);
manager.restoreFrom("foo", context);
assertEquals(1, list.size());
// The list should have the foo= map value from the sub-context
assertEquals("bar", list.get(0));
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#getExecutionContext(java.lang.Object)}.
*/
public void testGetStreamContextWithNoPrefix() {
manager.setUseClassNameAsPrefix(false);
manager.register("foo", stream);
ExecutionContext context = manager.getExecutionContext("foo");
assertEquals(1, context.entrySet().size());
// The list should have the foo= map value from the sub-context
assertEquals("bar", context.getString("foo"));
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#getExecutionContext(java.lang.Object)}.
*/
public void testGetStreamContextTwoRegistrations() {
manager.register("foo", new ItemStreamSupport() {
public ExecutionContext getExecutionContext() {
return new ExecutionContext(PropertiesConverter.stringToProperties("foo=bar"));
}
});
manager.register("foo", new ItemStreamSupport() {
public ExecutionContext getExecutionContext() {
return new ExecutionContext(PropertiesConverter.stringToProperties("foo=spam"));
}
});
ExecutionContext streamContext = manager.getExecutionContext("foo");
assertEquals(2, streamContext.entrySet().size());
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#close(java.lang.Object)}.
*/
public void testClose() {
manager.register("foo", new ItemStreamSupport() {
public void close() throws StreamException {
list.add("bar");
super.close();
}
});
manager.close("foo");
assertEquals(1, list.size());
}
/**
* Test method for
* {@link org.springframework.batch.item.stream.SimpleStreamManager#commit(org.springframework.transaction.TransactionStatus)}.
*/
public void testCommitWithoutMark() {
manager.register("foo", new ItemStreamSupport() {
manager.register(new ItemStreamSupport() {
public void mark() {
list.add("bar");
}
});
TransactionStatus status = manager.getTransaction("foo");
TransactionStatus status = manager.getTransaction();
manager.commit(status);
assertEquals(0, list.size());
}
@@ -188,47 +88,30 @@ public class SimpleStreamManagerTests extends TestCase {
* {@link org.springframework.batch.item.stream.SimpleStreamManager#rollback(org.springframework.transaction.TransactionStatus)}.
*/
public void testRollbackWithoutMark() {
manager.register("foo", new ItemStreamSupport() {
manager.register( new ItemStreamSupport() {
public void reset() {
list.add("bar");
}
});
TransactionStatus status = manager.getTransaction("foo");
TransactionStatus status = manager.getTransaction();
manager.rollback(status);
assertEquals(0, list.size());
}
/**
* Make sure the values from registered stream are present in
* manager's execution context.
*/
public void testGetExecutionContextPreservesValues() {
stream = new ItemStreamSupport() {
public ExecutionContext getExecutionContext() {
ExecutionContext ctx = new ExecutionContext();
ctx.putString("string", "testString");
ctx.putDouble("double", 5.5);
ctx.putLong("long", 7);
return ctx;
}
};
manager.register("foo", stream);
ExecutionContext streamContext = stream.getExecutionContext();
ExecutionContext managerContext = manager.getExecutionContext("foo");
for (Iterator it = streamContext.entrySet().iterator(); it.hasNext();) {
Entry entry = (Entry) it.next();
assertTrue(managerContext.containsValue(entry.getValue()));
private final class StubItemStream extends ItemStreamSupport {
private ExecutionContext executionContext;
public void open(ExecutionContext executionContext)
throws StreamException {
this.executionContext = executionContext;
}
}
private final class StubItemStream extends ItemStreamSupport {
public ExecutionContext getExecutionContext() {
return new ExecutionContext(PropertiesConverter.stringToProperties("foo=bar"));
public void beforeSave() {
executionContext.putString("foo", "bar");
}
public void restoreFrom(ExecutionContext context) {
list.add(context.getString("foo"));
}
}
}

View File

@@ -21,6 +21,7 @@ import junit.framework.TestCase;
import org.springframework.batch.io.file.FlatFileItemReader;
import org.springframework.batch.io.file.mapping.FieldSet;
import org.springframework.batch.io.file.mapping.FieldSetMapper;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.reader.DelegatingItemReader;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.core.io.ClassPathResource;
@@ -45,7 +46,7 @@ public abstract class AbstractTradeBatchTests extends TestCase {
protected void setUp() throws Exception {
super.setUp();
provider = new TradeItemReader(resource);
provider.open();
provider.open(new ExecutionContext());
}
protected static class TradeItemReader extends DelegatingItemReader {