BATCH-1150: Added test for using ListPreparedStatementSetter in xml configuration
This commit is contained in:
@@ -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<Long> parameters = new ArrayList<Long>();
|
||||
parameters.add(1L);
|
||||
parameters.add(4L);
|
||||
pss.setParameters(parameters);
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testSetValues(){
|
||||
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testSetValues() {
|
||||
|
||||
final List<String> results = new ArrayList<String>();
|
||||
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<Foo> 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<Foo> {
|
||||
private List<Foo> foos = new ArrayList<Foo>();
|
||||
|
||||
public void write(List<? extends Foo> items) throws Exception {
|
||||
foos.addAll(items);
|
||||
}
|
||||
|
||||
public List<Foo> getFoos() {
|
||||
return foos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user