RESOLVED - BATCH-1032: Modify AbstractJobTests in test project to be able to launch FlowJob steps individually
applied patch with some tweaks
This commit is contained in:
@@ -1,64 +1,66 @@
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/sampleJob.xml" })
|
||||
public class SampleJobTests extends AbstractSimpleJobTests {
|
||||
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.jdbcTemplate.update("create table TESTS (ID integer, NAME varchar(40))");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.jdbcTemplate.update("drop table TESTS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJob() throws Exception {
|
||||
assertEquals(BatchStatus.COMPLETED,this.launchJob().getStatus());
|
||||
this.verifyTasklet(1);
|
||||
this.verifyTasklet(2);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalStateException.class)
|
||||
public void voidTestNonExistentStep(){
|
||||
launchStep("nonExistent");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStep1Execution() {
|
||||
assertEquals(BatchStatus.COMPLETED, this.launchStep("step1").getStatus());
|
||||
this.verifyTasklet(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStep2Execution() {
|
||||
assertEquals(BatchStatus.COMPLETED, this.launchStep("step2").getStatus());
|
||||
this.verifyTasklet(2);
|
||||
}
|
||||
|
||||
private void verifyTasklet(int id) {
|
||||
assertEquals(id, jdbcTemplate.queryForInt("SELECT ID from TESTS where NAME = 'SampleTasklet" + id + "'"));
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
|
||||
/**
|
||||
* This is an abstract test class to be used by test classes to test the
|
||||
* {@link AbstractJobTests} class.
|
||||
*
|
||||
* @author Dan Garrette
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class AbstractSampleJobTests extends AbstractJobTests {
|
||||
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setJdbcTemplate(SimpleJdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.jdbcTemplate.update("create table TESTS (ID integer, NAME varchar(40))");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.jdbcTemplate.update("drop table TESTS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJob() throws Exception {
|
||||
assertEquals(BatchStatus.COMPLETED, this.launchJob().getStatus());
|
||||
this.verifyTasklet(1);
|
||||
this.verifyTasklet(2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testNonExistentStep() {
|
||||
launchStep("nonExistent");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStep1Execution() {
|
||||
assertEquals(BatchStatus.COMPLETED, this.launchStep("step1").getStatus());
|
||||
this.verifyTasklet(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStep2Execution() {
|
||||
assertEquals(BatchStatus.COMPLETED, this.launchStep("step2").getStatus());
|
||||
this.verifyTasklet(2);
|
||||
}
|
||||
|
||||
private void verifyTasklet(int id) {
|
||||
assertEquals(id, jdbcTemplate.queryForInt("SELECT ID from TESTS where NAME = 'SampleTasklet" + id + "'"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.job.flow.FlowJob;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* This class will specifically test the capabilities of
|
||||
* {@link AbstractSampleJobTests} to test {@link FlowJob}s.
|
||||
*
|
||||
* @author Dan Garrette
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/sampleFlowJob.xml" })
|
||||
public class SampleFlowJobTests extends AbstractSampleJobTests {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.job.SimpleJob;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* This class will specifically test the capabilities of
|
||||
* {@link AbstractSampleJobTests} to test {@link SimpleJob}s.
|
||||
*
|
||||
* @author Dan Garrette
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/sampleSimpleJob.xml" })
|
||||
public class SampleSimpleJobTests extends AbstractSampleJobTests {
|
||||
|
||||
}
|
||||
@@ -1,60 +1,60 @@
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/sampleJob.xml" })
|
||||
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))");
|
||||
stepRunner = new StepRunner(jobLauncher, jobRepository);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.jdbcTemplate.update("drop table TESTS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTasklet() {
|
||||
Step step = (Step)context.getBean("step2");
|
||||
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 {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
}
|
||||
package org.springframework.batch.test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
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{
|
||||
|
||||
@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))");
|
||||
stepRunner = new StepRunner(jobLauncher, jobRepository);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
this.jdbcTemplate.update("drop table TESTS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTasklet() {
|
||||
Step step = (Step)context.getBean("step2");
|
||||
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 {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
57
spring-batch-test/src/test/resources/jobs/sampleJob.xml → spring-batch-test/src/test/resources/jobs/sample-steps.xml
Executable file → Normal file
57
spring-batch-test/src/test/resources/jobs/sampleJob.xml → spring-batch-test/src/test/resources/jobs/sample-steps.xml
Executable file → Normal file
@@ -1,33 +1,26 @@
|
||||
<?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"
|
||||
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">
|
||||
|
||||
<bean id="sampleJob" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<list>
|
||||
<bean id="step1" parent="taskletStep">
|
||||
<property name="tasklet">
|
||||
<bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
<ref bean="step2" />
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="step2" parent="taskletStep">
|
||||
<property name="tasklet">
|
||||
<bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="2" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<?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"
|
||||
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">
|
||||
|
||||
<bean id="step1" parent="taskletStep">
|
||||
<property name="tasklet">
|
||||
<bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="step2" parent="taskletStep">
|
||||
<property name="tasklet">
|
||||
<bean class="org.springframework.batch.test.sample.SampleTasklet">
|
||||
<constructor-arg value="2" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
22
spring-batch-test/src/test/resources/jobs/sampleFlowJob.xml
Normal file
22
spring-batch-test/src/test/resources/jobs/sampleFlowJob.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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: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/batch
|
||||
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
|
||||
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 name="step1" next="step2"/>
|
||||
<step name="step2"/>
|
||||
</job>
|
||||
|
||||
</beans:beans>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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"
|
||||
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">
|
||||
|
||||
<import resource="sample-steps.xml" />
|
||||
|
||||
<bean id="sampleJob" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<list>
|
||||
<ref bean="step1" />
|
||||
<ref bean="step2" />
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user