diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/MultipleColumnJdbcKeyCollector.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/MultipleColumnJdbcKeyCollector.java index 7da384164..f8432d600 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/MultipleColumnJdbcKeyCollector.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/MultipleColumnJdbcKeyCollector.java @@ -25,9 +25,9 @@ import org.springframework.batch.item.database.ItemPreparedStatementSetter; import org.springframework.batch.item.database.KeyCollector; import org.springframework.batch.item.util.ExecutionContextUserSupport; import org.springframework.jdbc.core.ColumnMapRowMapper; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementSetter; import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.JdbcOperations; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -57,7 +57,7 @@ public class MultipleColumnJdbcKeyCollector extends ExecutionContextUserSuppo private static final String CURRENT_KEY = "current.key"; - private JdbcTemplate jdbcTemplate; + private JdbcOperations jdbcTemplate; private RowMapper keyMapper = new ColumnMapRowMapper(); @@ -78,7 +78,7 @@ public class MultipleColumnJdbcKeyCollector extends ExecutionContextUserSuppo * @param jdbcTemplate * @param sql - SQL statement that returns all keys to process. object. */ - public MultipleColumnJdbcKeyCollector(JdbcTemplate jdbcTemplate, String sql) { + public MultipleColumnJdbcKeyCollector(JdbcOperations jdbcTemplate, String sql) { this(); Assert.notNull(jdbcTemplate, "The JdbcTemplate must not be null."); Assert.hasText(sql, "The sql statement must not be null or empty."); @@ -163,7 +163,7 @@ public class MultipleColumnJdbcKeyCollector extends ExecutionContextUserSuppo this.sql = sql; } - public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + public void setJdbcTemplate(JdbcOperations jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/SingleColumnJdbcKeyCollector.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/SingleColumnJdbcKeyCollector.java index 2ea52dbee..708c88c69 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/SingleColumnJdbcKeyCollector.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/SingleColumnJdbcKeyCollector.java @@ -20,9 +20,9 @@ import java.util.List; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.database.KeyCollector; import org.springframework.batch.item.util.ExecutionContextUserSupport; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SingleColumnRowMapper; +import org.springframework.jdbc.core.JdbcOperations; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -56,7 +56,7 @@ public class SingleColumnJdbcKeyCollector extends ExecutionContextUserSupport private static final String RESTART_KEY = "key"; - private JdbcTemplate jdbcTemplate; + private JdbcOperations jdbcTemplate; private String sql; @@ -77,7 +77,7 @@ public class SingleColumnJdbcKeyCollector extends ExecutionContextUserSupport * @throws IllegalArgumentException if jdbcTemplate is null. * @throws IllegalArgumentException if sql string is empty or null. */ - public SingleColumnJdbcKeyCollector(JdbcTemplate jdbcTemplate, String sql) { + public SingleColumnJdbcKeyCollector(JdbcOperations jdbcTemplate, String sql) { this(); Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null."); Assert.hasText(sql, "The sql statement must not be null or empty."); @@ -158,11 +158,11 @@ public class SingleColumnJdbcKeyCollector extends ExecutionContextUserSupport } /** - * Set the {@link JdbcTemplate} to be used. + * Set the {@link JdbcOperations} to be used. * * @param jdbcTemplate */ - public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + public void setJdbcTemplate(JdbcOperations jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java index b6bbd2aef..38238e3ec 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java @@ -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 itemReader; protected ExecutionContext executionContext; - /** - * @return input source with all necessary dependencies set - */ protected abstract ItemReader 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(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/CompositeKeyFooDao.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/CompositeKeyFooDao.java index d35cff1ee..0c9beccfd 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/CompositeKeyFooDao.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/CompositeKeyFooDao.java @@ -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) */ diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooDao.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooDao.java index 30781fc23..00305952f 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooDao.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooDao.java @@ -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); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java index 5c21a1be4..14ff66959 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java @@ -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, DisposableBean, InitializingBean { @@ -18,9 +19,9 @@ class FooItemReader implements ItemStream, ItemReader, 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, 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, DisposableBean, Init */ public void reset() { itemReader.reset(); - }; + } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/MultipleColumnJdbcDrivingQueryItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/MultipleColumnJdbcDrivingQueryItemReaderIntegrationTests.java index d2549b2c9..beb5fa8e9 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/MultipleColumnJdbcDrivingQueryItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/MultipleColumnJdbcDrivingQueryItemReaderIntegrationTests.java @@ -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 createItemReader() throws Exception { MultipleColumnJdbcKeyCollector> keyGenerator = - new MultipleColumnJdbcKeyCollector>(getJdbcTemplate(), + new MultipleColumnJdbcKeyCollector>(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> inputSource = new DrivingQueryItemReader>(); 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; } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderCommonTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderCommonTests.java index 1366c4ffe..6e9a94f86 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderCommonTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderCommonTests.java @@ -16,7 +16,7 @@ public class SingleColumnJdbcDrivingQueryItemReaderCommonTests extends CommonDat DrivingQueryItemReader reader = new DrivingQueryItemReader(); reader.setKeyCollector(keyCollector); reader.setSaveState(true); - return new FooItemReader(reader, jdbcTemplate); + return new FooItemReader(reader, getDataSource()); } protected void pointToEmptyInput(ItemReader tested) throws Exception { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderIntegrationTests.java index 79135ae9d..5cc5ac7aa 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleColumnJdbcDrivingQueryItemReaderIntegrationTests.java @@ -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 source; - /** * @return input source with all necessary dependencies set */ protected ItemReader createItemReader() throws Exception { - SingleColumnJdbcKeyCollector keyStrategy = new SingleColumnJdbcKeyCollector(getJdbcTemplate(), - "SELECT ID from T_FOOS order by ID"); + SingleColumnJdbcKeyCollector keyStrategy = + new SingleColumnJdbcKeyCollector(simpleJdbcTemplate.getJdbcOperations(), + "SELECT ID from T_FOOS order by ID"); keyStrategy.setRestartSql("SELECT ID from T_FOOS where ID > ? order by ID"); DrivingQueryItemReader inputSource = new DrivingQueryItemReader(); inputSource.setKeyCollector(keyStrategy); inputSource.setSaveState(true); - return new FooItemReader(inputSource, getJdbcTemplate()); + return new FooItemReader(inputSource, dataSource); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleKeyFooDao.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleKeyFooDao.java index 2cc2f2ec9..dd7b375e7 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleKeyFooDao.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/SingleKeyFooDao.java @@ -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){