Incomplete - task 84: Fix samples
Fix column mapping in jdbc cursor - order is important.
This commit is contained in:
@@ -78,7 +78,7 @@ public class DrivingQueryItemReaderTests extends TestCase {
|
||||
Foo foo2 = (Foo) itemReader.read();
|
||||
assertEquals(2, foo2.getValue());
|
||||
|
||||
getAsItemStream(itemReader).beforeSave();
|
||||
getAsItemStream(itemReader).update();
|
||||
|
||||
// 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).beforeSave();
|
||||
getAsItemStream(itemReader).update();
|
||||
|
||||
// create new input source
|
||||
itemReader = createItemReader();
|
||||
|
||||
@@ -29,8 +29,8 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader
|
||||
}
|
||||
}
|
||||
|
||||
public void beforeSave() {
|
||||
inputSource.beforeSave();
|
||||
public void update() {
|
||||
inputSource.update();
|
||||
}
|
||||
|
||||
public void destroy() throws Exception {
|
||||
@@ -51,14 +51,6 @@ class FooItemReader extends AbstractItemReader implements ItemStream, ItemReader
|
||||
public void close() {
|
||||
}
|
||||
|
||||
/**
|
||||
* True.
|
||||
* @see org.springframework.batch.item.ItemStream#isMarkSupported()
|
||||
*/
|
||||
public boolean isMarkSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.StreamContext)
|
||||
|
||||
@@ -64,8 +64,8 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
|
||||
public void testCreateExecutionContext() throws Exception {
|
||||
mapper.mapKeys(key, executionContext);
|
||||
Properties props = executionContext.getProperties();
|
||||
assertEquals("1", props.getProperty("1"));
|
||||
assertEquals("2", props.getProperty("2"));
|
||||
assertEquals("1", props.getProperty(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"0"));
|
||||
assertEquals("2", props.getProperty(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"1"));
|
||||
}
|
||||
|
||||
public void testCreateExecutionContextFromEmptyKeys() throws Exception {
|
||||
@@ -77,8 +77,8 @@ public class ColumnMapExecutionContextRowMapperTests extends TestCase {
|
||||
public void testCreateSetter() throws Exception {
|
||||
|
||||
ExecutionContext streamContext = new ExecutionContext();
|
||||
streamContext.putString("0", "1");
|
||||
streamContext.putString("1", "2");
|
||||
streamContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"1", "1");
|
||||
streamContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"0", "2");
|
||||
PreparedStatementSetter setter = mapper.createSetter(streamContext);
|
||||
ps = (PreparedStatement)psControl.getMock();
|
||||
|
||||
|
||||
@@ -3,12 +3,17 @@
|
||||
*/
|
||||
package org.springframework.batch.io.driving.support;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
|
||||
/**
|
||||
@@ -67,14 +72,31 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
|
||||
|
||||
Map key = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(1);
|
||||
key.put("ID", new Long(3));
|
||||
key.put("VALUE", new Integer(3));
|
||||
key.put("VALUE", new Integer(4));
|
||||
|
||||
keyStrategy.setKeyMapper(new ExecutionContextRowMapper() {
|
||||
public PreparedStatementSetter createSetter(ExecutionContext executionContext) {
|
||||
return null;
|
||||
}
|
||||
public void mapKeys(Object key, ExecutionContext executionContext) {
|
||||
// Just slap the key as a map into the context
|
||||
Map keys = (Map) key;
|
||||
for (Iterator it = keys.entrySet().iterator(); it.hasNext();) {
|
||||
Entry entry = (Entry)it.next();
|
||||
executionContext.put(entry.getKey().toString(), entry.getValue());
|
||||
}
|
||||
}
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
keyStrategy.saveState(key, executionContext);
|
||||
Properties props = executionContext.getProperties();
|
||||
|
||||
assertEquals(2, props.size());
|
||||
assertEquals("3", props.get(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "0"));
|
||||
assertEquals("3", props.get(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "1"));
|
||||
System.err.println(props);
|
||||
assertEquals("3", props.get("ID"));
|
||||
assertEquals("4", props.get("VALUE"));
|
||||
}
|
||||
|
||||
public void testGetNullKeyAsStreamContext(){
|
||||
|
||||
@@ -159,7 +159,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
|
||||
reader.read();
|
||||
|
||||
// get restart data
|
||||
reader.beforeSave();
|
||||
reader.update();
|
||||
assertEquals(4, executionContext.getLong(
|
||||
FlatFileItemReader.class.getName() + "." + FlatFileItemReader.READ_STATISTICS_NAME));
|
||||
// close input
|
||||
@@ -174,7 +174,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
|
||||
assertEquals("[testLine5]", reader.read().toString());
|
||||
assertEquals("[testLine6]", reader.read().toString());
|
||||
|
||||
reader.beforeSave();
|
||||
reader.update();
|
||||
assertEquals(6, executionContext.getLong(FlatFileItemReader.class.getName() + "." + FlatFileItemReader.READ_STATISTICS_NAME));
|
||||
}
|
||||
|
||||
|
||||
@@ -224,7 +224,7 @@ public class FlatFileItemWriterTests extends TestCase {
|
||||
commit();
|
||||
|
||||
// get restart data
|
||||
inputSource.beforeSave();
|
||||
inputSource.update();
|
||||
// close template
|
||||
inputSource.close();
|
||||
|
||||
@@ -237,7 +237,7 @@ public class FlatFileItemWriterTests extends TestCase {
|
||||
inputSource.write("testLine8");
|
||||
|
||||
// get statistics
|
||||
inputSource.beforeSave();
|
||||
inputSource.update();
|
||||
// close template
|
||||
inputSource.close();
|
||||
|
||||
@@ -268,7 +268,7 @@ public class FlatFileItemWriterTests extends TestCase {
|
||||
inputSource.setFieldSetUnmapper(new PassThroughFieldSetMapper());
|
||||
inputSource.afterPropertiesSet();
|
||||
inputSource.open(executionContext);
|
||||
inputSource.beforeSave();
|
||||
inputSource.update();
|
||||
assertNotNull(executionContext);
|
||||
assertEquals(3, executionContext.entrySet().size());
|
||||
assertEquals(0, executionContext.getLong(FlatFileItemWriter.class.getName() + "." + FlatFileItemWriter.RESTART_DATA_NAME));
|
||||
|
||||
@@ -79,7 +79,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
|
||||
Foo foo2 = (Foo) itemReader.read();
|
||||
assertEquals(2, foo2.getValue());
|
||||
|
||||
getAsItemStream(itemReader).beforeSave();
|
||||
getAsItemStream(itemReader).update();
|
||||
|
||||
// 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).beforeSave();
|
||||
getAsItemStream(itemReader).update();
|
||||
|
||||
// create new input source
|
||||
itemReader = createItemReader();
|
||||
|
||||
@@ -89,7 +89,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
|
||||
Foo foo2 = (Foo) reader.read();
|
||||
assertEquals(2, foo2.getValue());
|
||||
|
||||
getAsItemStream(reader).beforeSave();
|
||||
getAsItemStream(reader).update();
|
||||
|
||||
// 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).beforeSave();
|
||||
getAsItemStream(reader).update();
|
||||
|
||||
// create new input source
|
||||
reader = createItemReader();
|
||||
@@ -135,7 +135,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testRestoreFromEmptyData() throws Exception {
|
||||
getAsItemStream(reader).beforeSave();
|
||||
getAsItemStream(reader).update();
|
||||
|
||||
Foo foo = (Foo) reader.read();
|
||||
assertEquals(1, foo.getValue());
|
||||
@@ -218,7 +218,7 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
|
||||
|
||||
rollback();
|
||||
|
||||
getAsItemStream(reader).beforeSave();
|
||||
getAsItemStream(reader).update();
|
||||
|
||||
// create new input source
|
||||
reader = createItemReader();
|
||||
|
||||
@@ -117,7 +117,7 @@ public class StaxEventItemReaderTests extends TestCase {
|
||||
public void testRestart() {
|
||||
source.open(executionContext);
|
||||
source.read();
|
||||
source.beforeSave();
|
||||
source.update();
|
||||
assertEquals(1, executionContext.getLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME));
|
||||
List expectedAfterRestart = (List) source.read();
|
||||
|
||||
@@ -147,7 +147,7 @@ public class StaxEventItemReaderTests extends TestCase {
|
||||
|
||||
public void testRestoreWorksFromClosedStream() throws Exception {
|
||||
source.close();
|
||||
source.beforeSave();
|
||||
source.update();
|
||||
}
|
||||
/**
|
||||
* Skipping marked records after rollback.
|
||||
@@ -198,13 +198,13 @@ public class StaxEventItemReaderTests extends TestCase {
|
||||
public void testExecutionContext() {
|
||||
final int NUMBER_OF_RECORDS = 2;
|
||||
source.open(executionContext);
|
||||
source.beforeSave();
|
||||
source.update();
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_RECORDS; i++) {
|
||||
long recordCount = extractRecordCount();
|
||||
assertEquals(i, recordCount);
|
||||
source.read();
|
||||
source.beforeSave();
|
||||
source.update();
|
||||
}
|
||||
|
||||
assertEquals(NUMBER_OF_RECORDS, extractRecordCount());
|
||||
|
||||
@@ -94,7 +94,7 @@ public class StaxEventItemWriterTests extends TestCase {
|
||||
// write record
|
||||
writer.write(record);
|
||||
// writer.mark();
|
||||
writer.beforeSave();
|
||||
writer.update();
|
||||
writer.close();
|
||||
|
||||
// create new writer from saved restart data and continue writing
|
||||
@@ -124,7 +124,7 @@ public class StaxEventItemWriterTests extends TestCase {
|
||||
final int NUMBER_OF_RECORDS = 10;
|
||||
for (int i = 1; i <= NUMBER_OF_RECORDS; i++) {
|
||||
writer.write(record);
|
||||
writer.beforeSave();
|
||||
writer.update();
|
||||
long writeStatistics = executionContext.getLong(StaxEventItemWriter.WRITE_STATISTICS_NAME);
|
||||
|
||||
assertEquals(i, writeStatistics);
|
||||
|
||||
@@ -76,7 +76,7 @@ public class DelegatingItemReaderTests extends TestCase {
|
||||
* Gets restart data from the input template
|
||||
*/
|
||||
public void testGetStreamContext() {
|
||||
itemProvider.beforeSave();
|
||||
itemProvider.update();
|
||||
assertEquals("foo", executionContext.getString("value"));
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ public class DelegatingItemReaderTests extends TestCase {
|
||||
return PropertiesConverter.stringToProperties("a=b");
|
||||
}
|
||||
|
||||
public void beforeSave() {
|
||||
public void update() {
|
||||
executionContext.putString("value", "foo");
|
||||
}
|
||||
|
||||
@@ -117,10 +117,6 @@ public class DelegatingItemReaderTests extends TestCase {
|
||||
value = "after skip";
|
||||
}
|
||||
|
||||
public boolean isMarkSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void mark() {
|
||||
}
|
||||
|
||||
|
||||
@@ -34,10 +34,39 @@ public class SimpleStreamManagerTests extends TestCase {
|
||||
|
||||
private SimpleStreamManager manager = new SimpleStreamManager(new ResourcelessTransactionManager());
|
||||
|
||||
private ItemStreamSupport stream = new StubItemStream();
|
||||
|
||||
private List list = new ArrayList();
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.item.stream.SimpleStreamManager#commit(org.springframework.transaction.TransactionStatus)}.
|
||||
*/
|
||||
public void testRegisterAndOpen() {
|
||||
ItemStreamSupport stream = new ItemStreamSupport() {
|
||||
public void open(ExecutionContext executionContext) throws StreamException {
|
||||
list.add("bar");
|
||||
}
|
||||
};
|
||||
manager.register(stream);
|
||||
manager.open(null);
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.item.stream.SimpleStreamManager#commit(org.springframework.transaction.TransactionStatus)}.
|
||||
*/
|
||||
public void testRegisterTwice() {
|
||||
ItemStreamSupport stream = new ItemStreamSupport() {
|
||||
public void open(ExecutionContext executionContext) throws StreamException {
|
||||
list.add("bar");
|
||||
}
|
||||
};
|
||||
manager.register(stream);
|
||||
manager.register(stream);
|
||||
manager.open(null);
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.item.stream.SimpleStreamManager#SimpleStreamManager(org.springframework.transaction.PlatformTransactionManager)}.
|
||||
@@ -68,13 +97,41 @@ public class SimpleStreamManagerTests extends TestCase {
|
||||
assertEquals("bar", list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.item.stream.SimpleStreamManager#commit(org.springframework.transaction.TransactionStatus)}.
|
||||
*/
|
||||
public void testMark() {
|
||||
manager.register(new ItemStreamSupport() {
|
||||
public void update() {
|
||||
list.add("bar");
|
||||
}
|
||||
});
|
||||
manager.update();
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.item.stream.SimpleStreamManager#commit(org.springframework.transaction.TransactionStatus)}.
|
||||
*/
|
||||
public void testClose() {
|
||||
manager.register(new ItemStreamSupport() {
|
||||
public void close() throws StreamException {
|
||||
list.add("bar");
|
||||
}
|
||||
});
|
||||
manager.close();
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.item.stream.SimpleStreamManager#commit(org.springframework.transaction.TransactionStatus)}.
|
||||
*/
|
||||
public void testCommitWithoutMark() {
|
||||
manager.register(new ItemStreamSupport() {
|
||||
public void mark() {
|
||||
public void update() {
|
||||
list.add("bar");
|
||||
}
|
||||
});
|
||||
@@ -89,7 +146,7 @@ public class SimpleStreamManagerTests extends TestCase {
|
||||
*/
|
||||
public void testRollbackWithoutMark() {
|
||||
manager.register( new ItemStreamSupport() {
|
||||
public void reset() {
|
||||
public void update() {
|
||||
list.add("bar");
|
||||
}
|
||||
});
|
||||
@@ -98,20 +155,4 @@ public class SimpleStreamManagerTests extends TestCase {
|
||||
assertEquals(0, list.size());
|
||||
}
|
||||
|
||||
|
||||
private final class StubItemStream extends ItemStreamSupport {
|
||||
|
||||
private ExecutionContext executionContext;
|
||||
|
||||
public void open(ExecutionContext executionContext)
|
||||
throws StreamException {
|
||||
this.executionContext = executionContext;
|
||||
}
|
||||
|
||||
public void beforeSave() {
|
||||
executionContext.putString("foo", "bar");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user