|
| 1 | +/* |
| 2 | + * Copyright 2010-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
1 | 16 | package org.springframework.batch.core.test.step;
|
2 | 17 |
|
3 | 18 | import java.util.ArrayList;
|
|
17 | 32 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
18 | 33 | import org.springframework.batch.core.repository.JobRepository;
|
19 | 34 | import org.springframework.batch.core.step.builder.FaultTolerantStepBuilder;
|
| 35 | +import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy; |
20 | 36 | import org.springframework.batch.core.step.skip.SkipLimitExceededException;
|
21 | 37 | import org.springframework.batch.core.step.skip.SkipPolicy;
|
22 | 38 | import org.springframework.batch.item.ItemProcessor;
|
@@ -195,6 +211,53 @@ public void write(List<? extends Integer> items) throws Exception {
|
195 | 211 | assertEquals(1, stepExecution.getProcessSkipCount());
|
196 | 212 | }
|
197 | 213 |
|
| 214 | + @Test(timeout = 3000) |
| 215 | + public void testExceptionInProcessAndWriteDuringChunkScan() throws Exception { |
| 216 | + // Given |
| 217 | + ListItemReader<Integer> itemReader = new ListItemReader<>(Arrays.asList(1, 2, 3)); |
| 218 | + |
| 219 | + ItemProcessor<Integer, Integer> itemProcessor = new ItemProcessor<Integer, Integer>() { |
| 220 | + @Override |
| 221 | + public Integer process(Integer item) throws Exception { |
| 222 | + if (item.equals(2)) { |
| 223 | + throw new Exception("Error during process item " + item); |
| 224 | + } |
| 225 | + return item; |
| 226 | + } |
| 227 | + }; |
| 228 | + |
| 229 | + ItemWriter<Integer> itemWriter = new ItemWriter<Integer>() { |
| 230 | + @Override |
| 231 | + public void write(List<? extends Integer> items) throws Exception { |
| 232 | + if (items.contains(3)) { |
| 233 | + throw new Exception("Error during write"); |
| 234 | + } |
| 235 | + } |
| 236 | + }; |
| 237 | + |
| 238 | + Step step = new StepBuilderFactory(jobRepository, transactionManager).get("step") |
| 239 | + .<Integer, Integer>chunk(5) |
| 240 | + .reader(itemReader) |
| 241 | + .processor(itemProcessor) |
| 242 | + .writer(itemWriter) |
| 243 | + .faultTolerant() |
| 244 | + .skipPolicy(new AlwaysSkipItemSkipPolicy()) |
| 245 | + .build(); |
| 246 | + |
| 247 | + // When |
| 248 | + StepExecution stepExecution = execute(step); |
| 249 | + |
| 250 | + // Then |
| 251 | + assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); |
| 252 | + assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus()); |
| 253 | + assertEquals(3, stepExecution.getReadCount()); |
| 254 | + assertEquals(1, stepExecution.getWriteCount()); |
| 255 | + assertEquals(1, stepExecution.getWriteSkipCount()); |
| 256 | + assertEquals(1, stepExecution.getProcessSkipCount()); |
| 257 | + assertEquals(3, stepExecution.getRollbackCount()); |
| 258 | + assertEquals(2, stepExecution.getCommitCount()); |
| 259 | + } |
| 260 | + |
198 | 261 | private List<Integer> createItems() {
|
199 | 262 | List<Integer> items = new ArrayList<>(TOTAL_ITEMS);
|
200 | 263 | for (int i = 1; i <= TOTAL_ITEMS; i++) {
|
|
0 commit comments