|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | + */ |
| 16 | +package org.springframework.batch.core.step.builder; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import org.springframework.batch.core.repository.JobRepository; |
| 21 | +import org.springframework.batch.core.step.tasklet.TaskletStep; |
| 22 | +import org.springframework.batch.item.ItemProcessor; |
| 23 | +import org.springframework.batch.item.ItemReader; |
| 24 | +import org.springframework.batch.item.ItemWriter; |
| 25 | +import org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate; |
| 26 | +import org.springframework.core.task.SimpleAsyncTaskExecutor; |
| 27 | +import org.springframework.test.util.ReflectionTestUtils; |
| 28 | +import org.springframework.transaction.PlatformTransactionManager; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | + |
| 33 | +/** |
| 34 | + * Test cases for verifying the {@link AbstractTaskletStepBuilder} and faultTolerant() |
| 35 | + * functionality. |
| 36 | + * |
| 37 | + * Issue: https://github.com/spring-projects/spring-batch/issues/4438 |
| 38 | + * |
| 39 | + * @author Ilpyo Yang |
| 40 | + * @author Mahmoud Ben Hassine |
| 41 | + */ |
| 42 | +public class AbstractTaskletStepBuilderTests { |
| 43 | + |
| 44 | + private final JobRepository jobRepository = mock(JobRepository.class); |
| 45 | + |
| 46 | + private final PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class); |
| 47 | + |
| 48 | + private final int chunkSize = 10; |
| 49 | + |
| 50 | + private final ItemReader<String> itemReader = mock(ItemReader.class); |
| 51 | + |
| 52 | + private final ItemProcessor<String, String> itemProcessor = mock(ItemProcessor.class); |
| 53 | + |
| 54 | + private final ItemWriter<String> itemWriter = mock(ItemWriter.class); |
| 55 | + |
| 56 | + private final SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); |
| 57 | + |
| 58 | + @Test |
| 59 | + void testSetTaskExecutorBeforeFaultTolerant() { |
| 60 | + TaskletStep step = new StepBuilder("step-name", jobRepository) |
| 61 | + .<String, String>chunk(chunkSize, transactionManager) |
| 62 | + .taskExecutor(taskExecutor) |
| 63 | + .reader(itemReader) |
| 64 | + .processor(itemProcessor) |
| 65 | + .writer(itemWriter) |
| 66 | + .faultTolerant() |
| 67 | + .build(); |
| 68 | + |
| 69 | + Object stepOperations = ReflectionTestUtils.getField(step, "stepOperations"); |
| 70 | + assertInstanceOf(TaskExecutorRepeatTemplate.class, stepOperations); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testSetTaskExecutorAfterFaultTolerant() { |
| 75 | + TaskletStep step = new StepBuilder("step-name", jobRepository) |
| 76 | + .<String, String>chunk(chunkSize, transactionManager) |
| 77 | + .reader(itemReader) |
| 78 | + .processor(itemProcessor) |
| 79 | + .writer(itemWriter) |
| 80 | + .faultTolerant() |
| 81 | + .taskExecutor(taskExecutor) |
| 82 | + .build(); |
| 83 | + |
| 84 | + Object stepOperations = ReflectionTestUtils.getField(step, "stepOperations"); |
| 85 | + assertInstanceOf(TaskExecutorRepeatTemplate.class, stepOperations); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments