forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractStepExecutionDaoTests.java
340 lines (287 loc) · 11 KB
/
AbstractStepExecutionDaoTests.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright 2006-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.dao;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.transaction.annotation.Transactional;
/**
* Tests for {@link StepExecutionDao} implementations.
*
* @see #getStepExecutionDao()
*/
public abstract class AbstractStepExecutionDaoTests extends AbstractTransactionalJUnit4SpringContextTests {
protected StepExecutionDao dao;
protected JobInstance jobInstance;
protected JobExecution jobExecution;
protected Step step;
protected StepExecution stepExecution;
protected JobRepository repository;
/**
* @return {@link StepExecutionDao} implementation ready for use.
*/
protected abstract StepExecutionDao getStepExecutionDao();
/**
* @return {@link JobRepository} that uses the stepExecution DAO.
*/
protected abstract JobRepository getJobRepository();
@Before
public void onSetUp() throws Exception {
repository = getJobRepository();
jobExecution = repository.createJobExecution("job", new JobParameters());
jobInstance = jobExecution.getJobInstance();
step = new StepSupport("foo");
stepExecution = new StepExecution(step.getName(), jobExecution);
dao = getStepExecutionDao();
}
@Transactional
@Test
public void testSaveExecutionAssignsIdAndVersion() throws Exception {
assertNull(stepExecution.getId());
assertNull(stepExecution.getVersion());
dao.saveStepExecution(stepExecution);
assertNotNull(stepExecution.getId());
assertNotNull(stepExecution.getVersion());
}
@Transactional
@Test
public void testSaveAndGetExecution() {
stepExecution.setStatus(BatchStatus.STARTED);
stepExecution.setReadSkipCount(7);
stepExecution.setProcessSkipCount(2);
stepExecution.setWriteSkipCount(5);
stepExecution.setProcessSkipCount(11);
stepExecution.setRollbackCount(3);
stepExecution.setLastUpdated(new Date(System.currentTimeMillis()));
stepExecution.setReadCount(17);
stepExecution.setFilterCount(15);
stepExecution.setWriteCount(13);
dao.saveStepExecution(stepExecution);
StepExecution retrieved = dao.getStepExecution(jobExecution, stepExecution.getId());
assertStepExecutionsAreEqual(stepExecution, retrieved);
assertNotNull(retrieved.getVersion());
assertNotNull(retrieved.getJobExecution());
assertNotNull(retrieved.getJobExecution().getId());
assertNotNull(retrieved.getJobExecution().getJobId());
assertNotNull(retrieved.getJobExecution().getJobInstance());
}
@Transactional
@Test
public void testSaveAndGetExecutions() {
List<StepExecution> stepExecutions = new ArrayList<>();
for (int i = 0; i < 3; i++) {
StepExecution se = new StepExecution("step" + i, jobExecution);
se.setStatus(BatchStatus.STARTED);
se.setReadSkipCount(i);
se.setProcessSkipCount(i);
se.setWriteSkipCount(i);
se.setProcessSkipCount(i);
se.setRollbackCount(i);
se.setLastUpdated(new Date(System.currentTimeMillis()));
se.setReadCount(i);
se.setFilterCount(i);
se.setWriteCount(i);
stepExecutions.add(se);
}
dao.saveStepExecutions(stepExecutions);
for (int i = 0; i < 3; i++) {
StepExecution retrieved = dao.getStepExecution(jobExecution, stepExecutions.get(i).getId());
assertStepExecutionsAreEqual(stepExecutions.get(i), retrieved);
assertNotNull(retrieved.getVersion());
assertNotNull(retrieved.getJobExecution());
assertNotNull(retrieved.getJobExecution().getId());
assertNotNull(retrieved.getJobExecution().getJobId());
assertNotNull(retrieved.getJobExecution().getJobInstance());
}
}
@Transactional
@Test
public void testSaveAndGetLastExecution() {
Instant now = Instant.now();
StepExecution stepExecution1 = new StepExecution("step1", jobExecution);
stepExecution1.setStartTime(Date.from(now));
StepExecution stepExecution2 = new StepExecution("step1", jobExecution);
stepExecution2.setStartTime(Date.from(now.plusMillis(500)));
dao.saveStepExecutions(Arrays.asList(stepExecution1, stepExecution2));
StepExecution lastStepExecution = dao.getLastStepExecution(jobInstance, "step1");
assertNotNull(lastStepExecution);
assertEquals(stepExecution2.getId(), lastStepExecution.getId());
}
@Transactional
@Test
public void testSaveAndGetLastExecutionWhenSameStartTime() {
Instant now = Instant.now();
StepExecution stepExecution1 = new StepExecution("step1", jobExecution);
stepExecution1.setStartTime(Date.from(now));
StepExecution stepExecution2 = new StepExecution("step1", jobExecution);
stepExecution2.setStartTime(Date.from(now));
dao.saveStepExecutions(Arrays.asList(stepExecution1, stepExecution2));
StepExecution lastStepExecution = stepExecution1.getId() > stepExecution2.getId() ? stepExecution1 : stepExecution2;
StepExecution retrieved = dao.getLastStepExecution(jobInstance, "step1");
assertNotNull(retrieved);
assertEquals(lastStepExecution.getId(), retrieved.getId());
}
@Transactional
@Test(expected = IllegalArgumentException.class)
public void testSaveNullCollectionThrowsException() {
dao.saveStepExecutions(null);
}
@Transactional
@Test
public void testSaveEmptyCollection() {
dao.saveStepExecutions(new ArrayList<>());
}
@Transactional
@Test
public void testSaveAndGetNonExistentExecution() {
assertNull(dao.getStepExecution(jobExecution, 45677L));
}
@Transactional
@Test
public void testSaveAndFindExecution() {
stepExecution.setStatus(BatchStatus.STARTED);
stepExecution.setReadSkipCount(7);
stepExecution.setWriteSkipCount(5);
stepExecution.setRollbackCount(3);
dao.saveStepExecution(stepExecution);
dao.addStepExecutions(jobExecution);
Collection<StepExecution> retrieved = jobExecution.getStepExecutions();
assertStepExecutionsAreEqual(stepExecution, retrieved.iterator().next());
}
@Transactional
@Test
public void testGetForNotExistingJobExecution() {
assertNull(dao.getStepExecution(new JobExecution(jobInstance, (long) 777, new JobParameters()), 11L));
}
/**
* To-be-saved execution must not already have an id.
*/
@Transactional
@Test
public void testSaveExecutionWithIdAlreadySet() {
stepExecution.setId((long) 7);
try {
dao.saveStepExecution(stepExecution);
fail();
}
catch (IllegalArgumentException e) {
// expected
}
}
/**
* To-be-saved execution must not already have a version.
*/
@Transactional
@Test
public void testSaveExecutionWithVersionAlreadySet() {
stepExecution.incrementVersion();
try {
dao.saveStepExecution(stepExecution);
fail();
}
catch (IllegalArgumentException e) {
// expected
}
}
/**
* Update and retrieve updated StepExecution - make sure the update is
* reflected as expected and version number has been incremented
*/
@Transactional
@Test
public void testUpdateExecution() {
stepExecution.setStatus(BatchStatus.STARTED);
dao.saveStepExecution(stepExecution);
Integer versionAfterSave = stepExecution.getVersion();
stepExecution.setStatus(BatchStatus.ABANDONED);
stepExecution.setLastUpdated(new Date(System.currentTimeMillis()));
dao.updateStepExecution(stepExecution);
assertEquals(versionAfterSave + 1, stepExecution.getVersion().intValue());
StepExecution retrieved = dao.getStepExecution(jobExecution, stepExecution.getId());
assertEquals(stepExecution, retrieved);
assertEquals(stepExecution.getLastUpdated(), retrieved.getLastUpdated());
assertEquals(BatchStatus.ABANDONED, retrieved.getStatus());
}
/**
* Exception should be raised when the version of update argument doesn't
* match the version of persisted entity.
*/
@Transactional
@Test
public void testConcurrentModificationException() {
step = new StepSupport("foo");
StepExecution exec1 = new StepExecution(step.getName(), jobExecution);
dao.saveStepExecution(exec1);
StepExecution exec2 = new StepExecution(step.getName(), jobExecution);
exec2.setId(exec1.getId());
exec2.incrementVersion();
assertEquals(Integer.valueOf(0), exec1.getVersion());
assertEquals(exec1.getVersion(), exec2.getVersion());
dao.updateStepExecution(exec1);
assertEquals(Integer.valueOf(1), exec1.getVersion());
try {
dao.updateStepExecution(exec2);
fail();
}
catch (OptimisticLockingFailureException e) {
// expected
}
}
@Test
public void testGetStepExecutionsWhenNoneExist() throws Exception {
int count = jobExecution.getStepExecutions().size();
dao.addStepExecutions(jobExecution);
assertEquals("Incorrect size of collection", count, jobExecution.getStepExecutions().size());
}
private void assertStepExecutionsAreEqual(StepExecution expected, StepExecution actual) {
assertEquals(expected.getId(), actual.getId());
assertEquals(expected.getStartTime(), actual.getStartTime());
assertEquals(expected.getEndTime(), actual.getEndTime());
assertEquals(expected.getSkipCount(), actual.getSkipCount());
assertEquals(expected.getCommitCount(), actual.getCommitCount());
assertEquals(expected.getReadCount(), actual.getReadCount());
assertEquals(expected.getWriteCount(), actual.getWriteCount());
assertEquals(expected.getFilterCount(), actual.getFilterCount());
assertEquals(expected.getWriteSkipCount(), actual.getWriteSkipCount());
assertEquals(expected.getReadSkipCount(), actual.getReadSkipCount());
assertEquals(expected.getProcessSkipCount(), actual.getProcessSkipCount());
assertEquals(expected.getRollbackCount(), actual.getRollbackCount());
assertEquals(expected.getExitStatus(), actual.getExitStatus());
assertEquals(expected.getLastUpdated(), actual.getLastUpdated());
assertEquals(expected.getExitStatus(), actual.getExitStatus());
assertEquals(expected.getJobExecutionId(), actual.getJobExecutionId());
assertEquals(expected.getCreateTime(), actual.getCreateTime());
}
}