|
| 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.job; |
| 17 | + |
| 18 | +import javax.sql.DataSource; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | + |
| 24 | +import org.springframework.batch.core.ExitStatus; |
| 25 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 26 | +import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository; |
| 27 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 28 | +import org.springframework.batch.core.job.parameters.InvalidJobParametersException; |
| 29 | +import org.springframework.batch.core.job.parameters.JobParameters; |
| 30 | +import org.springframework.batch.core.job.parameters.JobParametersBuilder; |
| 31 | +import org.springframework.batch.core.launch.JobExecutionAlreadyRunningException; |
| 32 | +import org.springframework.batch.core.launch.JobInstanceAlreadyCompleteException; |
| 33 | +import org.springframework.batch.core.launch.JobOperator; |
| 34 | +import org.springframework.batch.core.launch.JobRestartException; |
| 35 | +import org.springframework.batch.core.repository.JobRepository; |
| 36 | +import org.springframework.batch.core.step.Step; |
| 37 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 38 | +import org.springframework.batch.infrastructure.repeat.RepeatStatus; |
| 39 | +import org.springframework.beans.factory.annotation.Autowired; |
| 40 | +import org.springframework.context.annotation.Bean; |
| 41 | +import org.springframework.context.annotation.Configuration; |
| 42 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 43 | +import org.springframework.jdbc.support.JdbcTransactionManager; |
| 44 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 45 | + |
| 46 | +@ExtendWith(SpringExtension.class) |
| 47 | +public class JobRestartIntegrationTests { |
| 48 | + |
| 49 | + @Test |
| 50 | + void testJobRestartWithNoIdentifyingJobParameters(@Autowired JobOperator jobOperator, @Autowired Job job) |
| 51 | + throws JobInstanceAlreadyCompleteException, InvalidJobParametersException, |
| 52 | + JobExecutionAlreadyRunningException, JobRestartException { |
| 53 | + // given |
| 54 | + JobParameters jobParameters1 = new JobParametersBuilder().addString("name", "foo", false).toJobParameters(); |
| 55 | + JobParameters jobParameters2 = new JobParametersBuilder().addString("name", "bar", false).toJobParameters(); |
| 56 | + |
| 57 | + // when |
| 58 | + JobExecution jobExecution = jobOperator.start(job, jobParameters1); |
| 59 | + |
| 60 | + // then |
| 61 | + Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); |
| 62 | + Assertions.assertThrows(JobInstanceAlreadyCompleteException.class, |
| 63 | + () -> jobOperator.start(job, jobParameters2)); |
| 64 | + } |
| 65 | + |
| 66 | + @Configuration |
| 67 | + @EnableBatchProcessing |
| 68 | + @EnableJdbcJobRepository |
| 69 | + static class JobConfiguration { |
| 70 | + |
| 71 | + @Bean |
| 72 | + public Job job(JobRepository jobRepository) { |
| 73 | + Step step = new StepBuilder("step", jobRepository) |
| 74 | + .tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED) |
| 75 | + .build(); |
| 76 | + return new JobBuilder("job", jobRepository).start(step).build(); |
| 77 | + } |
| 78 | + |
| 79 | + @Bean |
| 80 | + public DataSource dataSource() { |
| 81 | + return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") |
| 82 | + .addScript("/org/springframework/batch/core/schema-hsqldb.sql") |
| 83 | + .generateUniqueName(true) |
| 84 | + .build(); |
| 85 | + } |
| 86 | + |
| 87 | + @Bean |
| 88 | + public JdbcTransactionManager transactionManager(DataSource dataSource) { |
| 89 | + return new JdbcTransactionManager(dataSource); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments