BATCH-1295:
*Provide ability to pass job's ExecutionContext into AbstractJobTests.launchStep() and StepRunner.launchStep() *Modified SimpleMethodInvoker.hashCode() so that it is not dependent on commons-lang
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.test.sample.SampleTasklet;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
|
||||
/**
|
||||
@@ -20,6 +24,10 @@ public abstract class AbstractSampleJobTests extends AbstractJobTests {
|
||||
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("tasklet2")
|
||||
private SampleTasklet tasklet2;
|
||||
|
||||
@Autowired
|
||||
public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
@@ -28,6 +36,7 @@ public abstract class AbstractSampleJobTests extends AbstractJobTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.jdbcTemplate.update("create table TESTS (ID integer, NAME varchar(40))");
|
||||
tasklet2.jobContextEntryFound = false;
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -59,6 +68,15 @@ public abstract class AbstractSampleJobTests extends AbstractJobTests {
|
||||
this.verifyTasklet(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStepLaunchJobContextEntry() {
|
||||
ExecutionContext jobContext = new ExecutionContext();
|
||||
jobContext.put("key1", "value1");
|
||||
assertEquals(BatchStatus.COMPLETED, this.launchStep("step2", jobContext).getStatus());
|
||||
this.verifyTasklet(2);
|
||||
assertTrue(tasklet2.jobContextEntryFound);
|
||||
}
|
||||
|
||||
private void verifyTasklet(int id) {
|
||||
assertEquals(id, jdbcTemplate.queryForInt("SELECT ID from TESTS where NAME = 'SampleTasklet" + id + "'"));
|
||||
}
|
||||
|
||||
@@ -20,20 +20,21 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/sample-steps.xml" })
|
||||
public class SampleStepTests implements ApplicationContextAware{
|
||||
public class SampleStepTests implements ApplicationContextAware {
|
||||
|
||||
@Autowired
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
private StepRunner stepRunner;
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
|
||||
@Autowired
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
|
||||
@Autowired
|
||||
private JobRepository jobRepository;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
jdbcTemplate.update("create table TESTS (ID integer, NAME varchar(40))");
|
||||
@@ -47,13 +48,12 @@ public class SampleStepTests implements ApplicationContextAware{
|
||||
|
||||
@Test
|
||||
public void testTasklet() {
|
||||
Step step = (Step)context.getBean("step2");
|
||||
Step step = (Step) context.getBean("s2");
|
||||
assertEquals(BatchStatus.COMPLETED, stepRunner.launchStep(step).getStatus());
|
||||
assertEquals(2, jdbcTemplate.queryForInt("SELECT ID from TESTS where NAME = 'SampleTasklet2'"));
|
||||
}
|
||||
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package org.springframework.batch.test.sample;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.annotation.BeforeStep;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
@@ -12,14 +16,31 @@ public class SampleTasklet implements Tasklet {
|
||||
@Autowired
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
private JobExecution jobExecution;
|
||||
|
||||
private int id = 0;
|
||||
|
||||
public boolean jobContextEntryFound = false;
|
||||
|
||||
public SampleTasklet(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
this.jdbcTemplate.update("insert into TESTS(ID, NAME) values (?, 'SampleTasklet" + id + "')", id);
|
||||
|
||||
if (jobExecution != null) {
|
||||
ExecutionContext jobContext = jobExecution.getExecutionContext();
|
||||
if (jobContext.containsKey("key1")) {
|
||||
jobContextEntryFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
|
||||
@BeforeStep
|
||||
public void storeJobExecution(StepExecution stepExecution) {
|
||||
this.jobExecution = stepExecution.getJobExecution();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:batch="http://www.springframework.org/schema/batch"
|
||||
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"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
|
||||
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd">
|
||||
|
||||
<bean id="step1" parent="taskletStep">
|
||||
<property name="tasklet">
|
||||
<bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
</property>
|
||||
<batch:step id="s1" parent="taskletStep">
|
||||
<batch:tasklet ref="tasklet1">
|
||||
<batch:listeners>
|
||||
<batch:listener ref="tasklet1"/>
|
||||
</batch:listeners>
|
||||
</batch:tasklet>
|
||||
</batch:step>
|
||||
|
||||
<bean id="tasklet1" class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
|
||||
<bean id="step2" parent="taskletStep">
|
||||
<property name="tasklet">
|
||||
<bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="2" />
|
||||
</bean>
|
||||
</property>
|
||||
<batch:step id="s2" parent="taskletStep">
|
||||
<batch:tasklet ref="tasklet2">
|
||||
<batch:listeners>
|
||||
<batch:listener ref="tasklet2"/>
|
||||
</batch:listeners>
|
||||
</batch:tasklet>
|
||||
</batch:step>
|
||||
|
||||
<bean id="tasklet2" class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="2" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -12,25 +12,11 @@
|
||||
http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
|
||||
|
||||
<beans:import resource="sample-steps.xml" />
|
||||
|
||||
<job id="sampleFlowJob">
|
||||
<step id="step1" parent="s1" next="step2"/>
|
||||
<step id="step2" parent="s2"/>
|
||||
</job>
|
||||
|
||||
<beans:bean id="s1" parent="taskletStep">
|
||||
<beans:property name="tasklet">
|
||||
<beans:bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<beans:constructor-arg value="1" />
|
||||
</beans:bean>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="s2" parent="taskletStep">
|
||||
<beans:property name="tasklet">
|
||||
<beans:bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<beans:constructor-arg value="2" />
|
||||
</beans:bean>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
</beans:beans>
|
||||
@@ -12,8 +12,8 @@
|
||||
<bean id="sampleJob" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<list>
|
||||
<ref bean="step1" />
|
||||
<ref bean="step2" />
|
||||
<bean id="step1" parent="s1"/>
|
||||
<bean id="step2" parent="s2"/>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
Reference in New Issue
Block a user