diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/ListPreparedStatementSetterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/ListPreparedStatementSetterTests.java index 20bfbf299..d86179d12 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/resource/ListPreparedStatementSetterTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/resource/ListPreparedStatementSetterTests.java @@ -16,7 +16,6 @@ package org.springframework.batch.core.resource; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import java.sql.ResultSet; import java.sql.SQLException; @@ -28,7 +27,14 @@ import javax.sql.DataSource; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.job.AbstractJob; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; @@ -38,16 +44,18 @@ import org.springframework.transaction.annotation.Transactional; /** * @author Lucas Ward - * + * */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/data-source-context.xml") +@ContextConfiguration(locations = { + "/org/springframework/batch/core/resource/ListPreparedStatementSetterTests-context.xml", + "/org/springframework/batch/core/repository/dao/data-source-context.xml" }) public class ListPreparedStatementSetterTests { ListPreparedStatementSetter pss; StepExecution stepExecution; - + private SimpleJdbcTemplate simpleJdbcTemplate; @Autowired @@ -55,42 +63,82 @@ public class ListPreparedStatementSetterTests { this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); } + @Autowired + private AbstractJob job; + + @Autowired + private JobLauncher jobLauncher; + + @Autowired + private FooStoringItemWriter fooStoringItemWriter; + @Before public void onSetUpInTransaction() throws Exception { - + pss = new ListPreparedStatementSetter(); List parameters = new ArrayList(); parameters.add(1L); parameters.add(4L); pss.setParameters(parameters); } - - @Transactional @Test - public void testSetValues(){ - + + @Transactional + @Test + public void testSetValues() { + final List results = new ArrayList(); - simpleJdbcTemplate.getJdbcOperations().query( - "SELECT NAME from T_FOOS where ID > ? and ID < ?", - pss, - new RowCallbackHandler(){ + simpleJdbcTemplate.getJdbcOperations().query("SELECT NAME from T_FOOS where ID > ? and ID < ?", pss, + new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { results.add(rs.getString(1)); - }}); - + } + }); + assertEquals(2, results.size()); assertEquals("bar2", results.get(0)); assertEquals("bar3", results.get(1)); } - - @Transactional @Test - public void testAfterPropertiesSet() throws Exception{ - try{ - pss.setParameters(null); - pss.afterPropertiesSet(); - fail(); + + @Transactional + @Test(expected = IllegalArgumentException.class) + public void testAfterPropertiesSet() throws Exception { + pss.setParameters(null); + pss.afterPropertiesSet(); + } + + @Test + public void testXmlConfiguration() throws Exception { + this.simpleJdbcTemplate.update("create table FOO (ID integer, NAME varchar(40), VALUE integer)"); + try { + this.simpleJdbcTemplate.update("insert into FOO values (?,?,?)", 0, "zero", 0); + this.simpleJdbcTemplate.update("insert into FOO values (?,?,?)", 1, "one", 1); + this.simpleJdbcTemplate.update("insert into FOO values (?,?,?)", 2, "two", 2); + + JobParameters params = new JobParametersBuilder().addLong("start.id", 1L).toJobParameters(); + JobExecution jobExecution = this.jobLauncher.run(this.job, params); + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); + + List foos = fooStoringItemWriter.getFoos(); + assertEquals(2, foos.size()); + System.err.println(foos.get(0)); + System.err.println(foos.get(1)); + assertEquals(new Foo(1, "one", 1), foos.get(0)); + assertEquals(new Foo(2, "two", 2), foos.get(1)); } - catch(IllegalArgumentException ex){ - //expected + finally { + this.simpleJdbcTemplate.update("drop table FOO"); + } + } + + public static class FooStoringItemWriter implements ItemWriter { + private List foos = new ArrayList(); + + public void write(List items) throws Exception { + foos.addAll(items); + } + + public List getFoos() { + return foos; } } } diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/resource/ListPreparedStatementSetterTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/resource/ListPreparedStatementSetterTests-context.xml new file mode 100644 index 000000000..473c5c090 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/resource/ListPreparedStatementSetterTests-context.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #{jobParameters[start.id]} + + + + + + \ No newline at end of file