BATCH-480: Quick refactoring to how the MultipleColumnJdbcKeyCollector works. It was nevery fully fixed from when the change to ExecutionContext was made from RestartData. Also some misc. documentation fixes based on feedback.

This commit is contained in:
lucasward
2008-03-25 05:23:26 +00:00
parent 9ec5ecf6f4
commit 1dcc16e583
8 changed files with 146 additions and 197 deletions

View File

@@ -6,88 +6,68 @@ package org.springframework.batch.item.database.support;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.core.CollectionFactory;
import org.springframework.jdbc.core.PreparedStatementSetter;
/**
* @author Lucas Ward
*/
public class ColumnMapExecutionContextRowMapperTests extends TestCase {
private ColumnMapExecutionContextRowMapper mapper;
private ColumnMapKeyMappingPreparedStatementSetter mapper;
private Map key;
private MockControl psControl = MockControl.createControl(PreparedStatement.class);
private PreparedStatement ps;
private ExecutionContext executionContext;
protected void setUp() throws Exception {
super.setUp();
mapper = new ColumnMapExecutionContextRowMapper();
ps = (PreparedStatement)psControl.getMock();
mapper = new ColumnMapKeyMappingPreparedStatementSetter();
key = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(2);
key.put("1", new Integer(1));
key.put("2", new Integer(2));
executionContext = new ExecutionContext();
}
public void testCreateExecutionContextWithInvalidType() throws Exception {
public void testSetValuesWithInvalidType() throws Exception {
try{
mapper.mapKeys(new Object(), executionContext);
mapper.setValues(ps, new Object());
fail();
}catch(IllegalArgumentException ex){
//expected
}
}
public void testCreateExecutionContextWithNull(){
public void testCreateExecutionContextWithNull() throws Exception{
try{
mapper.mapKeys(null, null);
mapper.setValues(ps, null);
fail();
}catch(IllegalArgumentException ex){
//expected
}
}
public void testCreateExecutionContext() throws Exception {
mapper.mapKeys(key, executionContext);
Properties props = executionContext.getProperties();
assertEquals("1", props.getProperty(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"0"));
assertEquals("2", props.getProperty(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"1"));
}
public void testCreateExecutionContextFromEmptyKeys() throws Exception {
mapper.mapKeys(new HashMap(), executionContext);
assertEquals(0, executionContext.size());
psControl.replay();
mapper.setValues(ps, new HashMap());
psControl.verify();
}
public void testCreateSetter() throws Exception {
ExecutionContext streamContext = new ExecutionContext();
streamContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"1", "1");
streamContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX+"0", "2");
PreparedStatementSetter setter = mapper.createSetter(streamContext);
ps = (PreparedStatement)psControl.getMock();
ps.setString(1, "2");
ps.setString(2, "1");
ps.setObject(1, new Integer(1));
ps.setObject(2, new Integer(2));
psControl.replay();
setter.setValues(ps);
mapper.setValues(ps, key);
psControl.verify();
}

View File

@@ -3,18 +3,13 @@
*/
package org.springframework.batch.item.database.support;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashMap;
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;
import org.springframework.util.ClassUtils;
/**
* @author Lucas Ward
@@ -54,8 +49,10 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
public void testRestoreKeys(){
executionContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "0", "3");
executionContext.putString(ColumnMapExecutionContextRowMapper.KEY_PREFIX + "1", "3");
Map keyMap = new LinkedHashMap();
keyMap.put("ID", "3");
keyMap.put("VALUE", "3");
executionContext.put(ClassUtils.getShortName(MultipleColumnJdbcKeyCollector.class)+ ".current.key", keyMap);
List keys = keyStrategy.retrieveKeys(executionContext);
@@ -68,36 +65,36 @@ public class MultipleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTran
assertEquals(new Integer(5), key.get("VALUE"));
}
public void testGetKeyAsExecutionContext(){
Map key = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(1);
key.put("ID", new Long(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.updateContext(key, executionContext);
Properties props = executionContext.getProperties();
assertEquals(2, props.size());
System.err.println(props);
assertEquals("3", props.get("ID"));
assertEquals("4", props.get("VALUE"));
}
// public void testGetKeyAsExecutionContext(){
//
// Map key = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(1);
// key.put("ID", new Long(3));
// key.put("VALUE", new Integer(4));
//
// keyStrategy.setKeyMapper(new KeyMappingPreparedStatementSetter() {
// 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.updateContext(key, executionContext);
// Properties props = executionContext.getProperties();
//
// assertEquals(2, props.size());
// System.err.println(props);
// assertEquals("3", props.get("ID"));
// assertEquals("4", props.get("VALUE"));
// }
public void testGetNullKeyAsStreamContext(){

View File

@@ -4,6 +4,7 @@ import java.util.List;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.util.ClassUtils;
/**
*
@@ -66,7 +67,7 @@ public class SingleColumnJdbcKeyGeneratorIntegrationTests extends AbstractTransa
keyStrategy.updateContext(new Long(3), executionContext);
assertEquals(1, executionContext.size());
assertEquals(new Long(3), executionContext.get("key"));
assertEquals(new Long(3), executionContext.get(ClassUtils.getShortName(SingleColumnJdbcKeyCollector.class) + ".key"));
}
public void testGetNullKeyAsStreamContext(){