Skip to content
This repository was archived by the owner on Mar 29, 2019. It is now read-only.

Commit e87968c

Browse files
committed
Updated JobExecutionTests to validate all sample jobs
1 parent bbafbfb commit e87968c

File tree

5 files changed

+55
-23
lines changed

5 files changed

+55
-23
lines changed

spring-batch-admin-manager/src/main/resources/META-INF/spring/batch/bootstrap/integration/file-context.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans:beans xmlns="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
4-
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
5-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
2+
<beans:beans xmlns="http://www.springframework.org/schema/integration"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:beans="http://www.springframework.org/schema/beans"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
66
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
77

88
<publish-subscribe-channel id="input-files" />

spring-batch-admin-manager/src/main/resources/META-INF/spring/batch/bootstrap/manager/execution-context.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
<context:annotation-config />
1313

14-
<bean class="org.springframework.batch.core.scope.StepScope" />
14+
<bean class="org.springframework.batch.core.scope.StepScope"/>
15+
16+
<bean class="org.springframework.batch.core.scope.JobScope"/>
1517

1618
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
1719
<property name="jobRepository" ref="jobRepository" />

spring-batch-admin-sample/src/main/java/org/springframework/batch/admin/sample/job/JobConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.batch.core.Job;
2121
import org.springframework.batch.core.Step;
2222
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
23+
import org.springframework.batch.core.configuration.annotation.JobScope;
2324
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
2425
import org.springframework.batch.core.configuration.annotation.StepScope;
2526
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,7 +41,7 @@ public class JobConfiguration {
4041
public StepBuilderFactory stepBuilderFactory;
4142

4243
@Bean
43-
@StepScope
44+
@JobScope
4445
public ExampleItemReader itemReader() {
4546
return new ExampleItemReader();
4647
}

spring-batch-admin-sample/src/test/java/org/springframework/batch/admin/sample/JobExecutionTests.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525

26+
import org.springframework.batch.admin.service.JobService;
2627
import org.springframework.batch.core.BatchStatus;
27-
import org.springframework.batch.core.Job;
2828
import org.springframework.batch.core.JobExecution;
2929
import org.springframework.batch.core.JobParameters;
3030
import org.springframework.batch.core.JobParametersBuilder;
31-
import org.springframework.batch.core.launch.JobLauncher;
3231
import org.springframework.beans.factory.annotation.Autowired;
33-
import org.springframework.beans.factory.annotation.Qualifier;
3432
import org.springframework.jdbc.core.JdbcTemplate;
3533
import org.springframework.test.annotation.DirtiesContext;
3634
import org.springframework.test.annotation.DirtiesContext.ClassMode;
@@ -43,14 +41,10 @@
4341
@DirtiesContext(classMode=ClassMode.AFTER_CLASS)
4442
public class JobExecutionTests {
4543

46-
@Autowired
47-
private JobLauncher jobLauncher;
48-
4944
private JobParameters jobParameters = new JobParametersBuilder().addString("fail", "false").toJobParameters();
5045

5146
@Autowired
52-
@Qualifier("job1")
53-
private Job job1;
47+
private JobService jobService;
5448

5549
private JdbcTemplate jdbcTemplate;
5650

@@ -61,13 +55,38 @@ public void setDataSource(DataSource dataSource) {
6155

6256
@Test
6357
public void testSimpleProperties() throws Exception {
64-
assertNotNull(jobLauncher);
58+
assertNotNull(jobService);
59+
}
60+
61+
@Test
62+
public void testLaunchJsrBasedJob() throws Exception {
63+
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
64+
JobExecution jobExecution = jobService.launch("jsr352-job", jobParameters);
65+
66+
while(jobExecution.isRunning()) {
67+
jobExecution = jobService.getJobExecution(jobExecution.getId());
68+
}
69+
70+
assertNotNull(jobExecution);
71+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
72+
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
73+
assertEquals(before + 1, after);
74+
}
75+
76+
@Test
77+
public void testLaunchJavaConfiguredJob() throws Exception {
78+
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
79+
JobExecution jobExecution = jobService.launch("javaJob", jobParameters);
80+
assertNotNull(jobExecution);
81+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
82+
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
83+
assertEquals(before + 1, after);
6584
}
6685

6786
@Test
6887
public void testLaunchJob() throws Exception {
6988
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
70-
JobExecution jobExecution = jobLauncher.run(job1, jobParameters);
89+
JobExecution jobExecution = jobService.launch("job1", jobParameters);
7190
assertNotNull(jobExecution);
7291
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
7392
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
@@ -78,7 +97,7 @@ public void testLaunchJob() throws Exception {
7897
public void testFailedJob() throws Exception {
7998
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
8099
jobParameters = new JobParametersBuilder().addString("fail", "true").toJobParameters();
81-
JobExecution jobExecution = jobLauncher.run(job1, jobParameters);
100+
JobExecution jobExecution = jobService.launch("job1", jobParameters);
82101
assertNotNull(jobExecution);
83102
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
84103
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
@@ -89,9 +108,9 @@ public void testFailedJob() throws Exception {
89108
public void testLaunchTwoJobs() throws Exception {
90109
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION");
91110
long count = 0;
92-
JobExecution jobExecution1 = jobLauncher.run(job1, new JobParametersBuilder(jobParameters).addLong("run.id", count++)
111+
JobExecution jobExecution1 = jobService.launch("job1", new JobParametersBuilder(jobParameters).addLong("run.id", count++)
93112
.toJobParameters());
94-
JobExecution jobExecution2 = jobLauncher.run(job1, new JobParametersBuilder(jobParameters).addLong("run.id", count++)
113+
JobExecution jobExecution2 = jobService.launch("job1", new JobParametersBuilder(jobParameters).addLong("run.id", count++)
95114
.toJobParameters());
96115
assertEquals(BatchStatus.COMPLETED, jobExecution1.getStatus());
97116
assertEquals(BatchStatus.COMPLETED, jobExecution2.getStatus());

spring-batch-admin-sample/src/test/resources/org/springframework/batch/admin/sample/JobExecutionTests-context.xml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xmlns:integration="http://www.springframework.org/schema/integration"
33
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
4-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
4+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
55

66
<import resource="classpath*:/META-INF/spring/batch/bootstrap/resources/*.xml" />
77
<import resource="classpath*:/META-INF/spring/batch/bootstrap/manager/*.xml" />
8-
<import resource="classpath:/META-INF/spring/batch/jobs/jobs-*.xml" />
9-
8+
109
<bean id="jobLauncherTaskExecutor" class="org.springframework.core.task.SyncTaskExecutor"/>
1110
<!-- prevent loading of other jobs by overriding the loader in the main bootstrap context -->
12-
<bean id="jobLoader" class="java.lang.String"/>
11+
<bean id="jobLoader" class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
12+
<property name="applicationContextFactories">
13+
<bean class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean">
14+
<property name="resources" value="classpath:/META-INF/spring/batch/jobs/*.xml" />
15+
</bean>
16+
</property>
17+
<property name="jobLoader">
18+
<bean class="org.springframework.batch.core.configuration.support.DefaultJobLoader">
19+
<property name="jobRegistry" ref="jobRegistry" />
20+
</bean>
21+
</property>
22+
</bean>
1323

1424
<integration:channel id="job-requests"/>
1525

0 commit comments

Comments
 (0)