RESOLVED - issue BATCH-1481: Support injection of step-scoped dependencies into unit tests
Added StepScopeTestUtils. Revised a few test cases.
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<bean id="itemReaderParent" class="org.springframework.batch.item.file.FlatFileItemReader" abstract="true">
|
||||
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
|
||||
<property name="lineMapper">
|
||||
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
|
||||
<property name="lineTokenizer">
|
||||
@@ -27,14 +27,11 @@
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
<property name="resource" value="#{jobParameters[inputFile]}" />
|
||||
</bean>
|
||||
|
||||
<bean id="itemReader" parent="itemReaderParent" scope="step">
|
||||
<property name="resource" value="#{jobParameters[fileName]}" />
|
||||
</bean>
|
||||
|
||||
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
|
||||
<property name="resource" value="file:./target/test-outputs/delimitedOutput.csv" />
|
||||
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
|
||||
<property name="resource" value="#{jobParameters[outputFile]}" />
|
||||
<property name="lineAggregator">
|
||||
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
|
||||
<property name="delimiter" value=","/>
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
|
||||
<property name="resource" value="data/iosample/input/fixedLength.txt" />
|
||||
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
|
||||
<property name="resource" value="#{jobParameters[inputFile]}" />
|
||||
<property name="lineMapper">
|
||||
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
|
||||
<property name="lineTokenizer">
|
||||
@@ -31,8 +31,8 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
|
||||
<property name="resource" ref="outputResource" />
|
||||
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
|
||||
<property name="resource" value="#{jobParameters[outputFile]}" />
|
||||
<property name="lineAggregator">
|
||||
<bean
|
||||
class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
|
||||
@@ -46,9 +46,5 @@
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="outputResource" class="org.springframework.core.io.FileSystemResource">
|
||||
<constructor-arg value="target/test-outputs/fixedLengthOutput.txt" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
@@ -9,32 +8,18 @@
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<bean id="itemReaderForTest" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="prototype">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="verifyCursorPosition" value="${batch.verify.cursor.position}"/>
|
||||
<property name="sql" value="select ID, NAME, CREDIT from CUSTOMER"/>
|
||||
<property name="rowMapper">
|
||||
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemReader"
|
||||
class="org.springframework.batch.item.database.JdbcPagingItemReader" autowire-candidate="false"
|
||||
scope="step">
|
||||
<bean id="itemReader" class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="rowMapper">
|
||||
<bean
|
||||
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper" />
|
||||
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper" />
|
||||
</property>
|
||||
<property name="queryProvider">
|
||||
<bean
|
||||
class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
|
||||
<bean class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="sortKey" value="ID" />
|
||||
<property name="selectClause" value="select ID, NAME, CREDIT"/>
|
||||
<property name="selectClause" value="select ID, NAME, CREDIT" />
|
||||
<property name="fromClause" value="FROM CUSTOMER" />
|
||||
<property name="whereClause"
|
||||
value="WHERE CREDIT > :credit" />
|
||||
<property name="whereClause" value="WHERE CREDIT > :credit" />
|
||||
</bean>
|
||||
</property>
|
||||
<property name="pageSize" value="10" />
|
||||
@@ -47,15 +32,12 @@
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemWriter"
|
||||
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
|
||||
<bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
|
||||
<property name="assertUpdates" value="true" />
|
||||
<property name="itemSqlParameterSourceProvider">
|
||||
<bean
|
||||
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
|
||||
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
|
||||
</property>
|
||||
<property name="sql"
|
||||
value="UPDATE CUSTOMER set credit = :credit where id = :id" />
|
||||
<property name="sql" value="UPDATE CUSTOMER set credit = :credit where id = :id" />
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -10,12 +10,14 @@ import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
|
||||
import org.springframework.batch.test.JobLauncherTestUtils;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.batch.test.StepScopeTestExecutionListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -29,8 +31,9 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml", "/jobs/ioSampleJob.xml" })
|
||||
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class})
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml",
|
||||
"/jobs/ioSampleJob.xml" })
|
||||
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
|
||||
public abstract class AbstractIoSampleTests {
|
||||
|
||||
@Autowired
|
||||
@@ -111,4 +114,14 @@ public abstract class AbstractIoSampleTests {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link StepExecution} that can be used to satisfy step scoped
|
||||
* dependencies in the test itself (not in the job it launches).
|
||||
*
|
||||
* @return a {@link StepExecution}
|
||||
*/
|
||||
protected StepExecution getStepExecution() {
|
||||
return MetaDataInstanceFactory.createStepExecution(getUniqueJobParameters());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,37 +16,40 @@
|
||||
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
* @author Dave Syer
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/jobs/iosample/delimited.xml")
|
||||
public class DelimitedFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Map<String, String> jobParameters = Collections.singletonMap("fileName",
|
||||
"file:./target/test-outputs/delimitedOutput.csv");
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
JobParameters jobParameters = new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"file:./target/test-outputs/delimitedOutput.csv").toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
StepSynchronizationManager.close();
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JobParameters getUniqueJobParameters() {
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addString("fileName",
|
||||
"data/iosample/input/delimited.csv").toJobParameters();
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"data/iosample/input/delimited.csv").addString("outputFile",
|
||||
"file:./target/test-outputs/delimitedOutput.csv").toJobParameters();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,11 +17,13 @@
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -29,12 +31,20 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@ContextConfiguration(locations = "/jobs/iosample/fixedLength.xml")
|
||||
public class FixedLengthFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@Autowired
|
||||
private Resource outputResource;
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
FlatFileItemReader<?> fileReader = (FlatFileItemReader<?>) reader;
|
||||
fileReader.setResource(outputResource);
|
||||
JobParameters jobParameters = new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"file:./target/test-outputs/fixedLengthOutput.txt").toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
StepSynchronizationManager.close();
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JobParameters getUniqueJobParameters() {
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"data/iosample/input/fixedLength.txt").addString("outputFile",
|
||||
"file:./target/test-outputs/fixedLengthOutput.txt").toJobParameters();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,13 +19,17 @@ package org.springframework.batch.sample.iosample;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
* @author Dave Syer
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -34,9 +38,13 @@ public class JdbcPagingFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
// no-op
|
||||
JobParameters jobParameters = new JobParametersBuilder(super.getUniqueJobParameters()).addDouble("credit", 0.)
|
||||
.toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
StepSynchronizationManager.close();
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected JobParameters getUniqueJobParameters() {
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addDouble("credit", 10000.).toJobParameters();
|
||||
|
||||
@@ -19,20 +19,25 @@ package org.springframework.batch.sample.iosample;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.job.AbstractJob;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.batch.test.StepScopeTestUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -49,13 +54,14 @@ public class TwoJobInstancesDelimitedFunctionalTests {
|
||||
private JobLauncher launcher;
|
||||
|
||||
@Autowired
|
||||
private AbstractJob job;
|
||||
private Job job;
|
||||
|
||||
@Autowired
|
||||
private FlatFileItemReader<CustomerCredit> reader;
|
||||
private ItemReader<CustomerCredit> reader;
|
||||
|
||||
@Autowired
|
||||
private Resource outputResource;
|
||||
@Qualifier("itemReader")
|
||||
private ItemStream readerStream;
|
||||
|
||||
@Test
|
||||
public void testLaunchJobTwice() throws Exception {
|
||||
@@ -69,18 +75,31 @@ public class TwoJobInstancesDelimitedFunctionalTests {
|
||||
|
||||
private void verifyOutput(int expected) throws Exception {
|
||||
|
||||
reader.setResource(outputResource);
|
||||
reader.open(new ExecutionContext());
|
||||
JobParameters jobParameters = new JobParametersBuilder().addString("fileName",
|
||||
"file:./target/test-outputs/delimitedOutput.csv").toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
|
||||
int count = StepScopeTestUtils.doInStepScope(stepExecution, new Callable<Integer>() {
|
||||
|
||||
public Integer call() throws Exception {
|
||||
|
||||
int count = 0;
|
||||
|
||||
readerStream.open(new ExecutionContext());
|
||||
|
||||
try {
|
||||
while (reader.read() != null) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
readerStream.close();
|
||||
}
|
||||
return count;
|
||||
|
||||
int count = 0;
|
||||
try {
|
||||
while (reader.read() != null) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
assertEquals(expected, count);
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ import javax.sql.DataSource;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.job.AbstractJob;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
@@ -49,7 +49,7 @@ public class TwoJobInstancesPagingFunctionalTests {
|
||||
private JobLauncher launcher;
|
||||
|
||||
@Autowired
|
||||
private AbstractJob job;
|
||||
private Job job;
|
||||
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user