|
| 1 | +/* |
| 2 | + * Copyright 2025-present 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.repository; |
| 17 | + |
| 18 | +import javax.sql.DataSource; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.batch.core.BatchStatus; |
| 23 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 24 | +import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository; |
| 25 | +import org.springframework.batch.core.job.Job; |
| 26 | +import org.springframework.batch.core.job.JobExecution; |
| 27 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 28 | +import org.springframework.batch.core.job.parameters.JobParameters; |
| 29 | +import org.springframework.batch.core.launch.JobOperator; |
| 30 | +import org.springframework.batch.core.repository.dao.Jackson3ExecutionContextStringSerializer; |
| 31 | +import org.springframework.batch.core.scope.context.ChunkContext; |
| 32 | +import org.springframework.batch.core.step.Step; |
| 33 | +import org.springframework.batch.core.step.StepContribution; |
| 34 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 35 | +import org.springframework.batch.core.step.tasklet.Tasklet; |
| 36 | +import org.springframework.batch.infrastructure.repeat.RepeatStatus; |
| 37 | +import org.springframework.context.ApplicationContext; |
| 38 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 39 | +import org.springframework.context.annotation.Bean; |
| 40 | +import org.springframework.context.annotation.Configuration; |
| 41 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 42 | +import org.springframework.jdbc.support.JdbcTransactionManager; |
| 43 | + |
| 44 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 45 | + |
| 46 | +public class Jackson3ExecutionContextStringSerializerIntegrationTests { |
| 47 | + |
| 48 | + @Test |
| 49 | + void testExecutionContextSerializationDeserializationRoundTrip() throws Exception { |
| 50 | + // given |
| 51 | + ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class); |
| 52 | + JobOperator jobOperator = context.getBean(JobOperator.class); |
| 53 | + Job job = context.getBean(Job.class); |
| 54 | + JobParameters jobParameters = new JobParameters(); |
| 55 | + |
| 56 | + // when |
| 57 | + JobExecution jobExecution = jobOperator.start(job, jobParameters); |
| 58 | + JobExecution restartedExecution = jobOperator.restart(jobExecution); |
| 59 | + |
| 60 | + // then |
| 61 | + assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); |
| 62 | + assertEquals(true, jobExecution.getStepExecutions().iterator().next().getExecutionContext().get("failed")); |
| 63 | + assertEquals(BatchStatus.COMPLETED, restartedExecution.getStatus()); |
| 64 | + assertEquals(false, |
| 65 | + restartedExecution.getStepExecutions().iterator().next().getExecutionContext().get("failed")); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Configuration |
| 70 | + @EnableBatchProcessing |
| 71 | + @EnableJdbcJobRepository(executionContextSerializerRef = "serializer") |
| 72 | + static class JobConfiguration { |
| 73 | + |
| 74 | + @Bean |
| 75 | + public Step step(JobRepository jobRepository, Tasklet tasklet) { |
| 76 | + return new StepBuilder("step", jobRepository).tasklet(tasklet).build(); |
| 77 | + } |
| 78 | + |
| 79 | + @Bean |
| 80 | + public Tasklet tasklet() { |
| 81 | + return new Tasklet() { |
| 82 | + private boolean shouldFail = true; |
| 83 | + |
| 84 | + @Override |
| 85 | + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { |
| 86 | + if (shouldFail) { |
| 87 | + shouldFail = false; |
| 88 | + contribution.getStepExecution().getExecutionContext().put("failed", true); |
| 89 | + throw new Exception("Expected failure"); |
| 90 | + } |
| 91 | + System.out.println("Hello world!"); |
| 92 | + contribution.getStepExecution().getExecutionContext().put("failed", false); |
| 93 | + return RepeatStatus.FINISHED; |
| 94 | + } |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + @Bean |
| 99 | + public Job job(JobRepository jobRepository, Step step) { |
| 100 | + return new JobBuilder("job", jobRepository).start(step).build(); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + public Jackson3ExecutionContextStringSerializer serializer() { |
| 105 | + return new Jackson3ExecutionContextStringSerializer(); |
| 106 | + } |
| 107 | + |
| 108 | + @Bean |
| 109 | + public DataSource dataSource() { |
| 110 | + return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") |
| 111 | + .addScript("/org/springframework/batch/core/schema-hsqldb.sql") |
| 112 | + .generateUniqueName(true) |
| 113 | + .build(); |
| 114 | + } |
| 115 | + |
| 116 | + @Bean |
| 117 | + public JdbcTransactionManager transactionManager(DataSource dataSource) { |
| 118 | + return new JdbcTransactionManager(dataSource); |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments