IN PROGRESS - BATCH-710: updated tests to use SpringJUnit4ClassRunner and SimpleJdbcTemplate

This commit is contained in:
trisberg
2008-07-30 17:04:57 +00:00
parent 55874a84fa
commit c809cba5cc
10 changed files with 91 additions and 63 deletions

View File

@@ -1,13 +1,22 @@
package org.springframework.batch.item.database;
import static org.junit.Assert.*;
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;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import javax.sql.DataSource;
/**
* Common scenarios for testing {@link ItemReader} implementations which read data from database.
@@ -15,68 +24,72 @@ import org.springframework.util.Assert;
* @author Lucas Ward
* @author Robert Kasanicky
*/
public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
public abstract class AbstractJdbcItemReaderIntegrationTests {
protected ItemReader<Foo> itemReader;
protected ExecutionContext executionContext;
/**
* @return input source with all necessary dependencies set
*/
protected abstract ItemReader<Foo> createItemReader() throws Exception;
protected String[] getConfigLocations(){
return new String[] { "org/springframework/batch/item/database/data-source-context.xml"};
protected DataSource dataSource;
protected SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
protected void onSetUp()throws Exception{
super.onSetUp();
@Before
public void onSetUp()throws Exception{
itemReader = createItemReader();
getAsInitializingBean(itemReader).afterPropertiesSet();
executionContext = new ExecutionContext();
}
protected void onTearDown()throws Exception {
@After
public void onTearDown()throws Exception {
getAsDisposableBean(itemReader).destroy();
super.onTearDown();
}
/**
/*
* Regular scenario - read all rows and eventually return null.
*/
@Transactional @Test
public void testNormalProcessing() throws Exception {
getAsInitializingBean(itemReader).afterPropertiesSet();
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
Foo foo1 = itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
Foo foo2 = itemReader.read();
assertEquals(2, foo2.getValue());
Foo foo3 = (Foo) itemReader.read();
Foo foo3 = itemReader.read();
assertEquals(3, foo3.getValue());
Foo foo4 = (Foo) itemReader.read();
Foo foo4 = itemReader.read();
assertEquals(4, foo4.getValue());
Foo foo5 = (Foo) itemReader.read();
Foo foo5 = itemReader.read();
assertEquals(5, foo5.getValue());
assertNull(itemReader.read());
}
/**
/*
* Restart scenario.
* @throws Exception
*/
@Transactional @Test
public void testRestart() throws Exception {
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
Foo foo1 = itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
Foo foo2 = itemReader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(itemReader).update(executionContext);
@@ -85,20 +98,21 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
itemReader = createItemReader();
getAsItemStream(itemReader).open(executionContext);
Foo fooAfterRestart = (Foo) itemReader.read();
Foo fooAfterRestart = itemReader.read();
assertEquals(3, fooAfterRestart.getValue());
}
/**
/*
* Reading from an input source and then trying to restore causes an error.
*/
@Transactional @Test
public void testInvalidRestore() throws Exception {
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
Foo foo1 = itemReader.read();
assertEquals(1, foo1.getValue());
Foo foo2 = (Foo) itemReader.read();
Foo foo2 = itemReader.read();
assertEquals(2, foo2.getValue());
getAsItemStream(itemReader).update(executionContext);
@@ -107,7 +121,7 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
itemReader = createItemReader();
getAsItemStream(itemReader).open(new ExecutionContext());
Foo foo = (Foo) itemReader.read();
Foo foo = itemReader.read();
assertEquals(1, foo.getValue());
try {
@@ -119,31 +133,31 @@ public abstract class AbstractJdbcItemReaderIntegrationTests extends AbstractTra
}
}
/**
/*
* Empty restart data should be handled gracefully.
* @throws Exception
*/
@Transactional @Test
public void testRestoreFromEmptyData() throws Exception {
ExecutionContext streamContext = new ExecutionContext();
getAsItemStream(itemReader).open(streamContext);
Foo foo = (Foo) itemReader.read();
Foo foo = itemReader.read();
assertEquals(1, foo.getValue());
}
/**
/*
* Rollback scenario.
* @throws Exception
*/
@Transactional @Test
public void testRollback() throws Exception {
getAsItemStream(itemReader).open(executionContext);
Foo foo1 = (Foo) itemReader.read();
Foo foo1 = itemReader.read();
commit();
Foo foo2 = (Foo) itemReader.read();
Foo foo2 = itemReader.read();
Assert.state(!foo2.equals(foo1));
Foo foo3 = (Foo) itemReader.read();
Foo foo3 = itemReader.read();
Assert.state(!foo2.equals(foo3));
rollback();

View File

@@ -20,19 +20,21 @@ import java.sql.SQLException;
import java.util.Map;
import org.springframework.batch.item.sample.Foo;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import javax.sql.DataSource;
/**
* @author Lucas Ward
*
*/
public class CompositeKeyFooDao extends JdbcDaoSupport implements FooDao {
public CompositeKeyFooDao(JdbcTemplate jdbcTemplate) {
this.setJdbcTemplate(jdbcTemplate);
public CompositeKeyFooDao(DataSource dataSource) {
this.setDataSource(dataSource);
}
/* (non-Javadoc)
* @see org.springframework.batch.io.sql.scratch.FooDao#getFoo(java.lang.Object)
*/

View File

@@ -16,7 +16,8 @@
package org.springframework.batch.item.database;
import org.springframework.batch.item.sample.Foo;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* @author Lucas Ward
@@ -26,5 +27,5 @@ public interface FooDao {
Foo getFoo(Object key);
void setJdbcTemplate(JdbcTemplate jdbcTemplate);
void setDataSource(DataSource dataSource);
}

View File

@@ -6,7 +6,8 @@ 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;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
class FooItemReader implements ItemStream, ItemReader<Foo>, DisposableBean, InitializingBean {
@@ -18,9 +19,9 @@ class FooItemReader implements ItemStream, ItemReader<Foo>, DisposableBean, Init
FooDao fooDao = new SingleKeyFooDao();
public FooItemReader(DrivingQueryItemReader<?> inputSource, JdbcTemplate jdbcTemplate) {
public FooItemReader(DrivingQueryItemReader<?> inputSource, DataSource dataSource) {
this.itemReader = inputSource;
fooDao.setJdbcTemplate(jdbcTemplate);
fooDao.setDataSource(dataSource);
}
public Foo read() {
@@ -50,7 +51,7 @@ class FooItemReader implements ItemStream, ItemReader<Foo>, DisposableBean, Init
public void open(ExecutionContext executionContext) {
itemReader.open(executionContext);
};
}
public void close(ExecutionContext executionContext) {
itemReader.close(executionContext);
@@ -70,5 +71,5 @@ class FooItemReader implements ItemStream, ItemReader<Foo>, DisposableBean, Init
*/
public void reset() {
itemReader.reset();
};
}
}

View File

@@ -20,26 +20,31 @@ import java.util.Map;
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;
import org.junit.runner.RunWith;
/**
* @author Lucas Ward
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "data-source-context.xml")
public class MultipleColumnJdbcDrivingQueryItemReaderIntegrationTests extends
AbstractJdbcItemReaderIntegrationTests {
protected ItemReader<Foo> createItemReader() throws Exception {
MultipleColumnJdbcKeyCollector<Map<?,?>> keyGenerator =
new MultipleColumnJdbcKeyCollector<Map<?,?>>(getJdbcTemplate(),
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, getJdbcTemplate());
fooItemReader.setFooDao(new CompositeKeyFooDao(getJdbcTemplate()));
FooItemReader fooItemReader = new FooItemReader(inputSource, dataSource);
fooItemReader.setFooDao(new CompositeKeyFooDao(dataSource));
return fooItemReader;
}

View File

@@ -16,7 +16,7 @@ public class SingleColumnJdbcDrivingQueryItemReaderCommonTests extends CommonDat
DrivingQueryItemReader<Long> reader = new DrivingQueryItemReader<Long>();
reader.setKeyCollector(keyCollector);
reader.setSaveState(true);
return new FooItemReader(reader, jdbcTemplate);
return new FooItemReader(reader, getDataSource());
}
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {

View File

@@ -3,24 +3,29 @@ 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")
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>(getJdbcTemplate(),
"SELECT ID from T_FOOS order by ID");
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, getJdbcTemplate());
return new FooItemReader(inputSource, dataSource);
}
}

View File

@@ -5,9 +5,9 @@ import java.sql.SQLException;
import org.springframework.batch.item.sample.Foo;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
public class SingleKeyFooDao extends JdbcDaoSupport implements FooDao {
public class SingleKeyFooDao extends SimpleJdbcDaoSupport implements FooDao {
public Foo getFoo(Object key){