BATCH-851: Removed DrivingQueryItemReader and related classes.

This commit is contained in:
lucasward
2009-02-10 23:21:00 +00:00
parent 947351e52d
commit ecbc2ba5f9
15 changed files with 0 additions and 1521 deletions

View File

@@ -1,206 +0,0 @@
package org.springframework.batch.item.database;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.InitializingBean;
@SuppressWarnings("deprecation")
public class DrivingQueryItemReaderTests extends TestCase {
DrivingQueryItemReader<Foo> itemReader;
protected void setUp() throws Exception {
super.setUp();
itemReader = createItemReader();
}
private DrivingQueryItemReader<Foo> createItemReader() throws Exception {
DrivingQueryItemReader<Foo> inputSource = new DrivingQueryItemReader<Foo>();
inputSource.setKeyCollector(new MockKeyGenerator());
inputSource.setSaveState(true);
return inputSource;
}
/**
* Regular scenario - read all rows and eventually return null.
*/
public void testNormalProcessing() throws Exception {
getAsInitializingBean(itemReader).afterPropertiesSet();
getAsItemStream(itemReader).open(new ExecutionContext());
Foo foo1 = (Foo) itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
assertEquals(2, foo2.getValue());
Foo foo3 = (Foo) itemReader.read();
assertEquals(3, foo3.getValue());
Foo foo4 = (Foo) itemReader.read();
assertEquals(4, foo4.getValue());
Foo foo5 = (Foo) itemReader.read();
assertEquals(5, foo5.getValue());
assertNull(itemReader.read());
}
/**
* Restart scenario.
*
* @throws Exception
*/
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());
getAsItemStream(itemReader).update(executionContext);
// create new input source
itemReader = createItemReader();
getAsItemStream(itemReader).open(executionContext);
Foo fooAfterRestart = (Foo) itemReader.read();
assertEquals(3, fooAfterRestart.getValue());
}
/**
* Reading from an input source and then trying to restore causes an error.
*/
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());
getAsItemStream(itemReader).update(executionContext);
// create new input source
itemReader = createItemReader();
getAsItemStream(itemReader).open(new ExecutionContext());
Foo foo = (Foo) itemReader.read();
assertEquals(1, foo.getValue());
try {
getAsItemStream(itemReader).open(executionContext);
fail();
} catch (IllegalStateException ex) {
// expected
}
}
/**
* Empty restart data should be handled gracefully.
*
* @throws Exception
*/
public void testRestoreFromEmptyData() throws Exception {
ExecutionContext streamContext = new ExecutionContext(new ExecutionContext());
getAsItemStream(itemReader).open(streamContext);
Foo foo = (Foo) itemReader.read();
assertEquals(1, foo.getValue());
}
public void testRetriveZeroKeys() {
itemReader.setKeyCollector(new KeyCollector<Foo>() {
public List<Foo> retrieveKeys(ExecutionContext executionContext) {
return new ArrayList<Foo>();
}
public void updateContext(Foo key,
ExecutionContext executionContext) {
}
});
itemReader.open(new ExecutionContext());
assertNull(itemReader.read());
}
private InitializingBean getAsInitializingBean(ItemReader<Foo> source) {
return (InitializingBean) source;
}
private ItemStream getAsItemStream(ItemReader<Foo> source) {
return (ItemStream) source;
}
private static class MockKeyGenerator implements KeyCollector<Foo> {
static ExecutionContext streamContext;
List<Foo> keys;
List<Foo> restartKeys;
static final String RESTART_KEY = "restart.keys";
static {
// restart data properties cannot be empty.
streamContext = new ExecutionContext();
streamContext.put("", "");
}
public MockKeyGenerator() {
keys = new ArrayList<Foo>();
keys.add(new Foo(1, "1", 1));
keys.add(new Foo(2, "2", 2));
keys.add(new Foo(3, "3", 3));
keys.add(new Foo(4, "4", 4));
keys.add(new Foo(5, "5", 5));
restartKeys = new ArrayList<Foo>();
restartKeys.add(new Foo(3, "3", 3));
restartKeys.add(new Foo(4, "4", 4));
restartKeys.add(new Foo(5, "5", 5));
}
public ExecutionContext saveState(Object key) {
return streamContext;
}
public List<Foo> retrieveKeys(ExecutionContext executionContext) {
if (executionContext.containsKey(RESTART_KEY)) {
return restartKeys;
} else {
return keys;
}
}
public void updateContext(Foo key, ExecutionContext executionContext) {
executionContext.put(RESTART_KEY, restartKeys);
}
}
}

View File

@@ -1,61 +0,0 @@
package org.springframework.batch.item.database;
import javax.sql.DataSource;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
@SuppressWarnings("deprecation")
class FooItemReader implements ItemStream, ItemReader<Foo>, DisposableBean, InitializingBean {
DrivingQueryItemReader<?> itemReader;
public void setItemReader(DrivingQueryItemReader<?> itemReader) {
this.itemReader = itemReader;
}
FooDao fooDao = new SingleKeyFooDao();
public FooItemReader(DrivingQueryItemReader<?> inputSource, DataSource dataSource) {
this.itemReader = inputSource;
fooDao.setDataSource(dataSource);
}
public Foo read() {
Object key = itemReader.read();
if (key != null) {
return fooDao.getFoo(key);
}
else {
return null;
}
}
public void update(ExecutionContext executionContext) {
itemReader.update(executionContext);
}
public void destroy() throws Exception {
itemReader.close();
}
public void setFooDao(FooDao fooDao) {
this.fooDao = fooDao;
}
public void afterPropertiesSet() throws Exception {
}
public void open(ExecutionContext executionContext) {
itemReader.open(executionContext);
}
public void close() {
itemReader.close();
}
}

View File

@@ -1,60 +0,0 @@
package org.springframework.batch.item.database;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.IbatisKeyCollector;
import org.springframework.batch.item.sample.Foo;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.junit.runner.RunWith;
import org.junit.internal.runners.JUnit4ClassRunner;
import com.ibatis.sqlmap.client.SqlMapClient;
@SuppressWarnings({"unchecked", "deprecation"})
@RunWith(JUnit4ClassRunner.class)
public class IbatisItemReaderCommonTests extends AbstractDatabaseItemStreamItemReaderTests {
protected ItemReader<Foo> getItemReader() throws Exception {
SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(new ClassPathResource("ibatis-config.xml", getClass()));
factory.setDataSource(getDataSource());
factory.afterPropertiesSet();
SqlMapClient sqlMapClient = createSqlMapClient();
IbatisDrivingQueryItemReader reader = new IbatisDrivingQueryItemReader();
IbatisKeyCollector<Long> keyGenerator = new IbatisKeyCollector<Long>();
keyGenerator.setDrivingQueryId("getAllFooIds");
reader.setDetailsQueryId("getFooById");
keyGenerator.setRestartQueryId("getAllFooIdsRestart");
keyGenerator.setSqlMapClient(sqlMapClient);
reader.setSqlMapClient(sqlMapClient);
reader.setKeyCollector(keyGenerator);
reader.setSaveState(true);
return reader;
}
private SqlMapClient createSqlMapClient() throws Exception {
SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(new ClassPathResource("ibatis-config.xml", getClass()));
factory.setDataSource(getDataSource());
factory.afterPropertiesSet();
return (SqlMapClient) factory.getObject();
}
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
IbatisDrivingQueryItemReader reader = (IbatisDrivingQueryItemReader) tested;
reader.close();
IbatisKeyCollector<Long> keyCollector = new IbatisKeyCollector<Long>();
keyCollector.setDrivingQueryId("getNoFoos");
keyCollector.setSqlMapClient(createSqlMapClient());
reader.setKeyCollector(keyCollector);
reader.afterPropertiesSet();
reader.open(new ExecutionContext());
}
}

View File

@@ -1,48 +0,0 @@
package org.springframework.batch.item.database;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.IbatisKeyCollector;
import org.springframework.batch.item.sample.Foo;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ibatis.sqlmap.client.SqlMapClient;
/**
* Tests for {@link IbatisDrivingQueryItemReader}
*
* @author Robert Kasanicky
*
* @deprecated
*/
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "data-source-context.xml")
public class IbatisItemReaderIntegrationTests extends AbstractDataSourceItemReaderIntegrationTests {
protected ItemReader<Foo> createItemReader() throws Exception {
SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(new ClassPathResource("ibatis-config.xml", getClass()));
factory.setDataSource(dataSource);
factory.afterPropertiesSet();
SqlMapClient sqlMapClient = (SqlMapClient) factory.getObject();
IbatisDrivingQueryItemReader inputSource = new IbatisDrivingQueryItemReader();
IbatisKeyCollector<Long> keyGenerator = new IbatisKeyCollector<Long>();
keyGenerator.setDrivingQueryId("getAllFooIds");
inputSource.setDetailsQueryId("getFooById");
keyGenerator.setRestartQueryId("getAllFooIdsRestart");
keyGenerator.setSqlMapClient(sqlMapClient);
inputSource.setSqlMapClient(sqlMapClient);
inputSource.setKeyCollector(keyGenerator);
inputSource.setSaveState(true);
return inputSource;
}
}

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.database;
import java.util.Map;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.MultipleColumnJdbcKeyCollector;
import org.springframework.batch.item.sample.Foo;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Lucas Ward
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "data-source-context.xml")
@SuppressWarnings("deprecation")
public class MultipleColumnJdbcDrivingQueryItemReaderIntegrationTests extends
AbstractJdbcItemReaderIntegrationTests {
protected ItemReader<Foo> createItemReader() throws Exception {
MultipleColumnJdbcKeyCollector<Map<?,?>> keyGenerator =
new MultipleColumnJdbcKeyCollector<Map<?,?>>(simpleJdbcTemplate.getJdbcOperations(),
"SELECT ID, VALUE from T_FOOS order by ID, VALUE");
keyGenerator.setRestartSql("SELECT ID, VALUE from T_FOOS where ID > ? and VALUE > ? order by ID");
DrivingQueryItemReader<Map<?,?>> inputSource = new DrivingQueryItemReader<Map<?,?>>();
inputSource.setSaveState(true);
inputSource.setKeyCollector(keyGenerator);
FooItemReader fooItemReader = new FooItemReader(inputSource, dataSource);
fooItemReader.setFooDao(new CompositeKeyFooDao(dataSource));
return fooItemReader;
}
}

View File

@@ -1,45 +0,0 @@
package org.springframework.batch.item.database;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.SingleColumnJdbcKeyCollector;
import org.springframework.batch.item.sample.Foo;
import org.springframework.jdbc.core.JdbcTemplate;
import org.junit.runner.RunWith;
import org.junit.internal.runners.JUnit4ClassRunner;
@RunWith(JUnit4ClassRunner.class)
@SuppressWarnings("deprecation")
public class SingleColumnJdbcDrivingQueryItemReaderCommonTests extends AbstractDatabaseItemStreamItemReaderTests {
protected ItemReader<Foo> getItemReader() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
SingleColumnJdbcKeyCollector<Long> keyCollector = new SingleColumnJdbcKeyCollector<Long>(jdbcTemplate,
"SELECT ID from T_FOOS order by ID");
keyCollector.setRestartSql("SELECT ID from T_FOOS where ID > ? order by ID");
DrivingQueryItemReader<Long> reader = new DrivingQueryItemReader<Long>();
reader.setKeyCollector(keyCollector);
reader.setSaveState(true);
return new FooItemReader(reader, getDataSource());
}
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
FooItemReader fooReader = (FooItemReader) tested;
fooReader.close();
DrivingQueryItemReader<Long> reader = new DrivingQueryItemReader<Long>();
reader.close();
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
SingleColumnJdbcKeyCollector<Long> keyCollector = new SingleColumnJdbcKeyCollector<Long>(jdbcTemplate,
"SELECT ID from T_FOOS where ID < 0");
reader.setKeyCollector(keyCollector);
reader.afterPropertiesSet();
fooReader.setItemReader(reader);
fooReader.open(new ExecutionContext());
}
}

View File

@@ -1,32 +0,0 @@
package org.springframework.batch.item.database;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.SingleColumnJdbcKeyCollector;
import org.springframework.batch.item.sample.Foo;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.junit.runner.RunWith;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "data-source-context.xml")
@SuppressWarnings("deprecation")
public class SingleColumnJdbcDrivingQueryItemReaderIntegrationTests extends AbstractJdbcItemReaderIntegrationTests {
protected ItemReader<Long> source;
/**
* @return input source with all necessary dependencies set
*/
protected ItemReader<Foo> createItemReader() throws Exception {
SingleColumnJdbcKeyCollector<Long> keyStrategy =
new SingleColumnJdbcKeyCollector<Long>(simpleJdbcTemplate.getJdbcOperations(),
"SELECT ID from T_FOOS order by ID");
keyStrategy.setRestartSql("SELECT ID from T_FOOS where ID > ? order by ID");
DrivingQueryItemReader<Long> inputSource = new DrivingQueryItemReader<Long>();
inputSource.setKeyCollector(keyStrategy);
inputSource.setSaveState(true);
return new FooItemReader(inputSource, dataSource);
}
}

View File

@@ -1,98 +0,0 @@
/**
*
*/
package org.springframework.batch.item.database.support;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.util.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import javax.sql.DataSource;
/**
* @author Lucas Ward
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/batch/item/database/data-source-context.xml")
@SuppressWarnings("deprecation")
public class MultipleColumnJdbcKeyGeneratorIntegrationTests {
MultipleColumnJdbcKeyCollector<Map<?,?>> keyStrategy;
ExecutionContext executionContext;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Before
public void onSetUpBeforeTransaction() throws Exception {
keyStrategy = new MultipleColumnJdbcKeyCollector<Map<?,?>>(simpleJdbcTemplate.getJdbcOperations(),
"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();
}
@Transactional @Test
public void testRetrieveKeys(){
List<Map<?,?>> keys = keyStrategy.retrieveKeys(executionContext);
for (int i = 0; i < keys.size(); i++) {
Map<?,?> id = keys.get(i);
assertEquals(i + 1L, id.get("ID"));
assertEquals(i + 1, id.get("VALUE"));
}
}
@Transactional @Test
public void testRestoreKeys(){
Map<String, String> keyMap = new LinkedHashMap<String, String>();
keyMap.put("ID", "3");
keyMap.put("VALUE", "3");
executionContext.put(ClassUtils.getShortName(MultipleColumnJdbcKeyCollector.class)+ ".current.key", keyMap);
List<Map<?, ?>> keys = keyStrategy.retrieveKeys(executionContext);
assertEquals(2, keys.size());
Map<?,?> key = keys.get(0);
assertEquals(4L, key.get("ID"));
assertEquals(4, key.get("VALUE"));
key = keys.get(1);
assertEquals(5L, key.get("ID"));
assertEquals(5, key.get("VALUE"));
}
@Transactional @Test
public void testGetNullKeyAsStreamContext(){
try{
keyStrategy.updateContext(null, null);
fail();
}catch(IllegalArgumentException ex){
//expected
}
}
}

View File

@@ -1,112 +0,0 @@
package org.springframework.batch.item.database.support;
import static org.junit.Assert.*;
import java.util.List;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.util.ClassUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.sql.DataSource;
/**
*
* @author Lucas Ward
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/batch/item/database/data-source-context.xml")
@SuppressWarnings("deprecation")
public class SingleColumnJdbcKeyGeneratorIntegrationTests {
SingleColumnJdbcKeyCollector<Long> keyStrategy;
ExecutionContext executionContext;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Before
public void onSetUpBeforeTransaction() throws Exception {
keyStrategy = new SingleColumnJdbcKeyCollector<Long>(simpleJdbcTemplate.getJdbcOperations(),
"SELECT ID from T_FOOS order by ID");
keyStrategy.setRestartSql("SELECT ID from T_FOOS where ID > ? order by ID");
executionContext = new ExecutionContext();
}
@Transactional @Test
public void testRetrieveKeys(){
List<Long> keys = keyStrategy.retrieveKeys(new ExecutionContext());
for (int i = 0; i < keys.size(); i++) {
Long id = keys.get(i);
assertEquals(Long.valueOf(i + 1), id);
}
for (Long key : keys) {
System.out.println(key);
}
}
@Transactional @Test
public void testRestoreKeys(){
keyStrategy.updateContext(3L, executionContext);
List<Long> keys = keyStrategy.retrieveKeys(executionContext);
assertEquals(2, keys.size());
assertEquals(Long.valueOf(4), keys.get(0));
assertEquals(Long.valueOf(5), keys.get(1));
for (Long key : keys) {
System.out.println(key);
}
}
@Transactional @Test
public void testGetKeyAsStreamContext(){
keyStrategy.updateContext(3L, executionContext);
assertEquals(1, executionContext.size());
assertEquals(3L, executionContext.get(ClassUtils.getShortName(SingleColumnJdbcKeyCollector.class) + ".key"));
}
@Transactional @Test
public void testGetNullKeyAsStreamContext(){
try{
keyStrategy.updateContext(null, null);
fail();
}catch(IllegalArgumentException ex){
//expected
}
}
@Transactional @Test
public void testRestoreKeysFromNull(){
try{
keyStrategy.updateContext(null, null);
}catch(IllegalArgumentException ex){
//expected
}
}
}