From 6549ecc5309606a1f0dcce171d06622fdd62bc61 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Mon, 19 Feb 2024 15:23:50 +0100 Subject: [PATCH] Refine contribution #4471 - Update tests - Move test class to the core module (cherry picked from commit 4b8e504972a59a99e33e634a12c6ceebbad5fca8) --- .../AbstractTaskletStepBuilderTests.java | 88 ++++++++++++++ .../test/AbstractTaskletStepBuilderTests.java | 114 ------------------ 2 files changed, 88 insertions(+), 114 deletions(-) create mode 100644 spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/AbstractTaskletStepBuilderTests.java delete mode 100644 spring-batch-test/src/test/java/org/springframework/batch/test/AbstractTaskletStepBuilderTests.java diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/AbstractTaskletStepBuilderTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/AbstractTaskletStepBuilderTests.java new file mode 100644 index 0000000000..6cd6f2374e --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/builder/AbstractTaskletStepBuilderTests.java @@ -0,0 +1,88 @@ +/* + * Copyright 2024 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.step.builder; + +import org.junit.jupiter.api.Test; + +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.tasklet.TaskletStep; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.transaction.PlatformTransactionManager; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.mockito.Mockito.mock; + +/** + * Test cases for verifying the {@link AbstractTaskletStepBuilder} and faultTolerant() + * functionality. + * + * Issue: https://github.com/spring-projects/spring-batch/issues/4438 + * + * @author Ilpyo Yang + * @author Mahmoud Ben Hassine + */ +public class AbstractTaskletStepBuilderTests { + + private final JobRepository jobRepository = mock(JobRepository.class); + + private final PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class); + + private final int chunkSize = 10; + + private final ItemReader itemReader = mock(ItemReader.class); + + private final ItemProcessor itemProcessor = mock(ItemProcessor.class); + + private final ItemWriter itemWriter = mock(ItemWriter.class); + + private final SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); + + @Test + void testSetTaskExecutorBeforeFaultTolerant() { + TaskletStep step = new StepBuilder("step-name", jobRepository) + .chunk(chunkSize, transactionManager) + .taskExecutor(taskExecutor) + .reader(itemReader) + .processor(itemProcessor) + .writer(itemWriter) + .faultTolerant() + .build(); + + Object stepOperations = ReflectionTestUtils.getField(step, "stepOperations"); + assertInstanceOf(TaskExecutorRepeatTemplate.class, stepOperations); + } + + @Test + void testSetTaskExecutorAfterFaultTolerant() { + TaskletStep step = new StepBuilder("step-name", jobRepository) + .chunk(chunkSize, transactionManager) + .reader(itemReader) + .processor(itemProcessor) + .writer(itemWriter) + .faultTolerant() + .taskExecutor(taskExecutor) + .build(); + + Object stepOperations = ReflectionTestUtils.getField(step, "stepOperations"); + assertInstanceOf(TaskExecutorRepeatTemplate.class, stepOperations); + } + +} diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractTaskletStepBuilderTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractTaskletStepBuilderTests.java deleted file mode 100644 index 465d6f5f5b..0000000000 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractTaskletStepBuilderTests.java +++ /dev/null @@ -1,114 +0,0 @@ -package org.springframework.batch.test; -/* - * Copyright 2020-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. - */ - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.batch.core.repository.JobRepository; -import org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder; -import org.springframework.batch.core.step.builder.SimpleStepBuilder; -import org.springframework.batch.core.step.builder.StepBuilderHelper; -import org.springframework.batch.item.ItemProcessor; -import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.ItemWriter; -import org.springframework.batch.test.context.SpringBatchTest; -import org.springframework.core.task.SimpleAsyncTaskExecutor; -import org.springframework.core.task.TaskExecutor; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; - -/** - * Test cases for verifying the {@link AbstractTaskletStepBuilder} and faultTolerant() functionality. - * - * @author Ilpyo Yang - */ -@SpringBatchTest -@SpringJUnitConfig -public class AbstractTaskletStepBuilderTests { - private final JobRepository jobRepository = mock(JobRepository.class); - private final int chunkSize = 10; - private final ItemReader itemReader = mock(ItemReader.class); - private final ItemProcessor itemProcessor = mock(ItemProcessor.class); - private final ItemWriter itemWriter = mock(ItemWriter.class); - private final SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); - SimpleStepBuilder simpleStepBuilder; - - private T accessPrivateField(Object o, String fieldName) throws ReflectiveOperationException { - Field field = o.getClass().getDeclaredField(fieldName); - field.setAccessible(true); - return (T) field.get(o); - } - - private T accessSuperClassPrivateField(Object o, String fieldName) throws ReflectiveOperationException { - Field field = o.getClass().getSuperclass().getDeclaredField(fieldName); - field.setAccessible(true); - return (T) field.get(o); - } - - @BeforeEach - void set(){ - StepBuilderHelper stepBuilderHelper = new StepBuilderHelper("test", jobRepository) { - @Override - protected StepBuilderHelper self() { - return null; - } - }; - simpleStepBuilder = new SimpleStepBuilder(stepBuilderHelper); - simpleStepBuilder.chunk(chunkSize); - simpleStepBuilder.reader(itemReader); - simpleStepBuilder.processor(itemProcessor); - simpleStepBuilder.writer(itemWriter); - } - - @Test - void copyConstractorTest() throws ReflectiveOperationException { - Constructor constructor = SimpleStepBuilder.class.getDeclaredConstructor(SimpleStepBuilder.class); - constructor.setAccessible(true); - SimpleStepBuilder copySimpleStepBuilder = constructor.newInstance(simpleStepBuilder); - - int copyChunkSize = accessPrivateField(copySimpleStepBuilder, "chunkSize"); - ItemReader copyItemReader = accessPrivateField(copySimpleStepBuilder, "reader"); - ItemProcessor copyItemProcessor = accessPrivateField(copySimpleStepBuilder, "processor"); - ItemWriter copyItemWriter = accessPrivateField(copySimpleStepBuilder, "writer"); - - assertEquals(chunkSize, copyChunkSize); - assertEquals(itemReader, copyItemReader); - assertEquals(itemProcessor, copyItemProcessor); - assertEquals(itemWriter, copyItemWriter); - } - - @Test - void faultTolerantMethodTest() throws ReflectiveOperationException { - simpleStepBuilder.taskExecutor(taskExecutor); // The task executor is set before faultTolerant() - simpleStepBuilder.faultTolerant(); - - int afterChunkSize = accessPrivateField(simpleStepBuilder, "chunkSize"); - ItemReader afterItemReader = accessPrivateField(simpleStepBuilder, "reader"); - ItemProcessor afterItemProcessor = accessPrivateField(simpleStepBuilder, "processor"); - ItemWriter afterItemWriter = accessPrivateField(simpleStepBuilder, "writer"); - TaskExecutor afterTaskExecutor = accessSuperClassPrivateField(simpleStepBuilder, "taskExecutor"); - - assertEquals(chunkSize, afterChunkSize); - assertEquals(itemReader, afterItemReader); - assertEquals(itemProcessor, afterItemProcessor); - assertEquals(itemWriter, afterItemWriter); - assertEquals(taskExecutor, afterTaskExecutor); - } -}