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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/batch"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/batch
|
||||
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd">
|
||||
|
||||
<job id="pssJob">
|
||||
<step id="step1">
|
||||
<tasklet>
|
||||
<chunk reader="reader" writer="writer" commit-interval="1"/>
|
||||
</tasklet>
|
||||
</step>
|
||||
</job>
|
||||
|
||||
<job-repository id="jobRepository"/>
|
||||
|
||||
<beans:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
|
||||
<beans:property name="jobRepository" ref="jobRepository" />
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="reader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
|
||||
<beans:property name="dataSource" ref="dataSource"/>
|
||||
<beans:property name="sql" value="select ID, NAME, VALUE from FOO where ID >= ?"/>
|
||||
<beans:property name="preparedStatementSetter" ref="pss"/>
|
||||
<beans:property name="rowMapper">
|
||||
<beans:bean class="org.springframework.batch.core.resource.FooRowMapper"/>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="pss" class="org.springframework.batch.core.resource.ListPreparedStatementSetter" scope="step">
|
||||
<beans:property name="parameters">
|
||||
<beans:value>#{jobParameters[start.id]}</beans:value>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="writer" class="org.springframework.batch.core.resource.ListPreparedStatementSetterTests$FooStoringItemWriter"/>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user