|
| 1 | +/* |
| 2 | + * Copyright 2022 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; |
| 17 | + |
| 18 | +import javax.sql.DataSource; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 23 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 24 | +import org.springframework.batch.core.launch.JobLauncher; |
| 25 | +import org.springframework.batch.core.repository.JobRepository; |
| 26 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 27 | +import org.springframework.batch.repeat.RepeatStatus; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 32 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 33 | +import org.springframework.jdbc.support.JdbcTransactionManager; |
| 34 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 35 | +import org.springframework.transaction.PlatformTransactionManager; |
| 36 | + |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 40 | + |
| 41 | +/** |
| 42 | + * Test class for {@link SpringBatchVersion}. |
| 43 | + * |
| 44 | + * @author Mahmoud Ben Hassine |
| 45 | + */ |
| 46 | +@SpringJUnitConfig |
| 47 | +public class SpringBatchVersionTests { |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private JobLauncher jobLauncher; |
| 51 | + |
| 52 | + @Autowired |
| 53 | + private Job job; |
| 54 | + |
| 55 | + @Test |
| 56 | + void testBatchVersionInExecutionContext() throws Exception { |
| 57 | + // given |
| 58 | + JobParameters jobParameters = new JobParametersBuilder().toJobParameters(); |
| 59 | + |
| 60 | + // when |
| 61 | + JobExecution jobExecution = this.jobLauncher.run(this.job, jobParameters); |
| 62 | + |
| 63 | + // then |
| 64 | + assertNotNull(jobExecution); |
| 65 | + assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); |
| 66 | + assertTrue(jobExecution.getExecutionContext().containsKey(SpringBatchVersion.BATCH_VERSION_KEY)); |
| 67 | + assertTrue(jobExecution.getStepExecutions().iterator().next().getExecutionContext() |
| 68 | + .containsKey(SpringBatchVersion.BATCH_VERSION_KEY)); |
| 69 | + } |
| 70 | + |
| 71 | + @Configuration |
| 72 | + @EnableBatchProcessing |
| 73 | + static class TestConfiguration { |
| 74 | + |
| 75 | + @Bean |
| 76 | + public DataSource dataSource() { |
| 77 | + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) |
| 78 | + .addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build(); |
| 79 | + } |
| 80 | + |
| 81 | + @Bean |
| 82 | + public JdbcTransactionManager transactionManager(DataSource dataSource) { |
| 83 | + return new JdbcTransactionManager(dataSource); |
| 84 | + } |
| 85 | + |
| 86 | + @Bean |
| 87 | + public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager) { |
| 88 | + return new JobBuilder("job", jobRepository) |
| 89 | + .start(new StepBuilder("step", jobRepository) |
| 90 | + .tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED, transactionManager).build()) |
| 91 | + .build(); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments