-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SimpleJobRepositoryIntegrationTests.java
274 lines (237 loc) · 10.2 KB
/
SimpleJobRepositoryIntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright 2008-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.repository.support;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Repository tests using JDBC DAOs (rather than mocks).
*
* @author Robert Kasanicky
* @author Dimitrios Liapis
* @author Mahmoud Ben Hassine
*/
@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/sql-dao-test.xml")
class SimpleJobRepositoryIntegrationTests {
@Autowired
private SimpleJobRepository jobRepository;
private final JobSupport job = new JobSupport("SimpleJobRepositoryIntegrationTestsJob");
private final JobParameters jobParameters = new JobParameters();
/*
* Create two job executions for same job+parameters tuple. Check both executions
* belong to the same job instance and job.
*/
@Transactional
@Test
void testCreateAndFind() throws Exception {
job.setRestartable(true);
JobParametersBuilder builder = new JobParametersBuilder();
builder.addString("stringKey", "stringValue").addLong("longKey", 1L).addDouble("doubleKey", 1.1);
JobParameters jobParams = builder.toJobParameters();
JobExecution firstExecution = jobRepository.createJobExecution(job.getName(), jobParams);
firstExecution.setStartTime(LocalDateTime.now());
assertNotNull(firstExecution.getLastUpdated());
assertEquals(job.getName(), firstExecution.getJobInstance().getJobName());
jobRepository.update(firstExecution);
firstExecution.setStatus(BatchStatus.FAILED);
firstExecution.setEndTime(LocalDateTime.now());
jobRepository.update(firstExecution);
JobExecution secondExecution = jobRepository.createJobExecution(job.getName(), jobParams);
assertEquals(firstExecution.getJobInstance(), secondExecution.getJobInstance());
assertEquals(job.getName(), secondExecution.getJobInstance().getJobName());
}
/*
* Create two job executions for same job+parameters tuple. Check both executions
* belong to the same job instance and job.
*/
@Transactional
@Test
void testCreateAndFindWithNoStartDate() throws Exception {
job.setRestartable(true);
JobExecution firstExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
LocalDateTime now = LocalDateTime.now();
firstExecution.setStartTime(now);
firstExecution.setEndTime(now.plus(1, ChronoUnit.SECONDS));
firstExecution.setStatus(BatchStatus.COMPLETED);
jobRepository.update(firstExecution);
JobExecution secondExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
assertEquals(firstExecution.getJobInstance(), secondExecution.getJobInstance());
assertEquals(job.getName(), secondExecution.getJobInstance().getJobName());
}
/*
* Save multiple StepExecutions for the same step and check the returned count and
* last execution are correct.
*/
@Transactional
@Test
void testGetStepExecutionCountAndLastStepExecution() throws Exception {
job.setRestartable(true);
StepSupport step = new StepSupport("restartedStep");
// first execution
JobExecution firstJobExec = jobRepository.createJobExecution(job.getName(), jobParameters);
StepExecution firstStepExec = new StepExecution(step.getName(), firstJobExec);
jobRepository.add(firstStepExec);
assertEquals(1, jobRepository.getStepExecutionCount(firstJobExec.getJobInstance(), step.getName()));
assertEquals(firstStepExec, jobRepository.getLastStepExecution(firstJobExec.getJobInstance(), step.getName()));
// first execution failed
LocalDateTime now = LocalDateTime.now();
firstJobExec.setStartTime(now);
firstStepExec.setStartTime(now.plus(1, ChronoUnit.SECONDS));
firstStepExec.setStatus(BatchStatus.FAILED);
firstStepExec.setEndTime(now.plus(2, ChronoUnit.SECONDS));
jobRepository.update(firstStepExec);
firstJobExec.setStatus(BatchStatus.FAILED);
firstJobExec.setEndTime(now.plus(3, ChronoUnit.SECONDS));
jobRepository.update(firstJobExec);
// second execution
JobExecution secondJobExec = jobRepository.createJobExecution(job.getName(), jobParameters);
StepExecution secondStepExec = new StepExecution(step.getName(), secondJobExec);
jobRepository.add(secondStepExec);
assertEquals(2, jobRepository.getStepExecutionCount(secondJobExec.getJobInstance(), step.getName()));
assertEquals(secondStepExec,
jobRepository.getLastStepExecution(secondJobExec.getJobInstance(), step.getName()));
}
/*
* Save execution context and retrieve it.
*/
@Transactional
@Test
void testSaveExecutionContext() throws Exception {
ExecutionContext ctx = new ExecutionContext() {
{
putLong("crashedPosition", 7);
}
};
JobExecution jobExec = jobRepository.createJobExecution(job.getName(), jobParameters);
jobExec.setStartTime(LocalDateTime.now());
jobExec.setExecutionContext(ctx);
Step step = new StepSupport("step1");
StepExecution stepExec = new StepExecution(step.getName(), jobExec);
stepExec.setExecutionContext(ctx);
jobRepository.add(stepExec);
StepExecution retrievedStepExec = jobRepository.getLastStepExecution(jobExec.getJobInstance(), step.getName());
assertEquals(stepExec, retrievedStepExec);
assertEquals(ctx, retrievedStepExec.getExecutionContext());
// JobExecution retrievedJobExec =
// jobRepository.getLastJobExecution(jobExec.getJobInstance());
// assertEquals(jobExec, retrievedJobExec);
// assertEquals(ctx, retrievedJobExec.getExecutionContext());
}
/*
* If JobExecution is already running, exception will be thrown in attempt to create
* new execution.
*/
@Transactional
@Test
void testOnlyOneJobExecutionAllowedRunning() throws Exception {
job.setRestartable(true);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
// simulating a running job execution
jobExecution.setStartTime(LocalDateTime.now());
jobRepository.update(jobExecution);
assertThrows(JobExecutionAlreadyRunningException.class,
() -> jobRepository.createJobExecution(job.getName(), jobParameters));
}
@Transactional
@Test
void testGetLastJobExecution() throws Exception {
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
jobExecution.setStatus(BatchStatus.FAILED);
jobExecution.setEndTime(LocalDateTime.now());
jobRepository.update(jobExecution);
Thread.sleep(10);
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
StepExecution stepExecution = new StepExecution("step1", jobExecution);
jobRepository.add(stepExecution);
jobExecution.addStepExecutions(Arrays.asList(stepExecution));
assertEquals(jobExecution, jobRepository.getLastJobExecution(job.getName(), jobParameters));
assertEquals(stepExecution, jobExecution.getStepExecutions().iterator().next());
}
/*
* Create two job executions for the same job+parameters tuple. Should ignore
* non-identifying job parameters when identifying the job instance.
*/
@Transactional
@Test
void testReExecuteWithSameJobParameters() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo", false).toJobParameters();
JobExecution jobExecution1 = jobRepository.createJobExecution(job.getName(), jobParameters);
jobExecution1.setStatus(BatchStatus.COMPLETED);
jobExecution1.setEndTime(LocalDateTime.now());
jobRepository.update(jobExecution1);
JobExecution jobExecution2 = jobRepository.createJobExecution(job.getName(), jobParameters);
assertNotNull(jobExecution1);
assertNotNull(jobExecution2);
}
/*
* When a job execution is running, JobExecutionAlreadyRunningException should be
* thrown if trying to create any other ones with same job parameters.
*/
@Transactional
@Test
public void testReExecuteWithSameJobParametersWhenRunning() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addString("stringKey", "stringValue")
.toJobParameters();
// jobExecution with status STARTING
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
try {
jobRepository.createJobExecution(job.getName(), jobParameters);
fail();
}
catch (JobExecutionAlreadyRunningException e) {
// expected
}
// jobExecution with status STARTED
jobExecution.setStatus(BatchStatus.STARTED);
jobExecution.setStartTime(LocalDateTime.now());
jobRepository.update(jobExecution);
try {
jobRepository.createJobExecution(job.getName(), jobParameters);
fail();
}
catch (JobExecutionAlreadyRunningException e) {
// expected
}
// jobExecution with status STOPPING
jobExecution.setStatus(BatchStatus.STOPPING);
jobRepository.update(jobExecution);
try {
jobRepository.createJobExecution(job.getName(), jobParameters);
fail();
}
catch (JobExecutionAlreadyRunningException e) {
// expected
}
}
}