23
23
import org .junit .Test ;
24
24
import org .junit .runner .RunWith ;
25
25
26
+ import org .springframework .batch .admin .service .JobService ;
26
27
import org .springframework .batch .core .BatchStatus ;
27
- import org .springframework .batch .core .Job ;
28
28
import org .springframework .batch .core .JobExecution ;
29
29
import org .springframework .batch .core .JobParameters ;
30
30
import org .springframework .batch .core .JobParametersBuilder ;
31
- import org .springframework .batch .core .launch .JobLauncher ;
32
31
import org .springframework .beans .factory .annotation .Autowired ;
33
- import org .springframework .beans .factory .annotation .Qualifier ;
34
32
import org .springframework .jdbc .core .JdbcTemplate ;
35
33
import org .springframework .test .annotation .DirtiesContext ;
36
34
import org .springframework .test .annotation .DirtiesContext .ClassMode ;
43
41
@ DirtiesContext (classMode =ClassMode .AFTER_CLASS )
44
42
public class JobExecutionTests {
45
43
46
- @ Autowired
47
- private JobLauncher jobLauncher ;
48
-
49
44
private JobParameters jobParameters = new JobParametersBuilder ().addString ("fail" , "false" ).toJobParameters ();
50
45
51
46
@ Autowired
52
- @ Qualifier ("job1" )
53
- private Job job1 ;
47
+ private JobService jobService ;
54
48
55
49
private JdbcTemplate jdbcTemplate ;
56
50
@@ -61,13 +55,38 @@ public void setDataSource(DataSource dataSource) {
61
55
62
56
@ Test
63
57
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 );
65
84
}
66
85
67
86
@ Test
68
87
public void testLaunchJob () throws Exception {
69
88
int before = JdbcTestUtils .countRowsInTable (jdbcTemplate , "BATCH_STEP_EXECUTION" );
70
- JobExecution jobExecution = jobLauncher . run ( job1 , jobParameters );
89
+ JobExecution jobExecution = jobService . launch ( " job1" , jobParameters );
71
90
assertNotNull (jobExecution );
72
91
assertEquals (BatchStatus .COMPLETED , jobExecution .getStatus ());
73
92
int after = JdbcTestUtils .countRowsInTable (jdbcTemplate , "BATCH_STEP_EXECUTION" );
@@ -78,7 +97,7 @@ public void testLaunchJob() throws Exception {
78
97
public void testFailedJob () throws Exception {
79
98
int before = JdbcTestUtils .countRowsInTable (jdbcTemplate , "BATCH_STEP_EXECUTION" );
80
99
jobParameters = new JobParametersBuilder ().addString ("fail" , "true" ).toJobParameters ();
81
- JobExecution jobExecution = jobLauncher . run ( job1 , jobParameters );
100
+ JobExecution jobExecution = jobService . launch ( " job1" , jobParameters );
82
101
assertNotNull (jobExecution );
83
102
assertEquals (BatchStatus .FAILED , jobExecution .getStatus ());
84
103
int after = JdbcTestUtils .countRowsInTable (jdbcTemplate , "BATCH_STEP_EXECUTION" );
@@ -89,9 +108,9 @@ public void testFailedJob() throws Exception {
89
108
public void testLaunchTwoJobs () throws Exception {
90
109
int before = JdbcTestUtils .countRowsInTable (jdbcTemplate , "BATCH_STEP_EXECUTION" );
91
110
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 ++)
93
112
.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 ++)
95
114
.toJobParameters ());
96
115
assertEquals (BatchStatus .COMPLETED , jobExecution1 .getStatus ());
97
116
assertEquals (BatchStatus .COMPLETED , jobExecution2 .getStatus ());
0 commit comments