|
1 | 1 | /* |
2 | | - * Copyright 2013-2022 the original author or authors. |
| 2 | + * Copyright 2013-2023 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
21 | 21 | import javax.sql.DataSource; |
22 | 22 |
|
23 | 23 | import org.apache.commons.dbcp2.BasicDataSource; |
| 24 | +import org.junit.jupiter.api.Assertions; |
24 | 25 | import org.junit.jupiter.api.Test; |
25 | 26 | import test.jdbc.datasource.DataSourceInitializer; |
26 | 27 |
|
|
30 | 31 | import org.springframework.batch.core.JobInstance; |
31 | 32 | import org.springframework.batch.core.JobInterruptedException; |
32 | 33 | import org.springframework.batch.core.JobParameters; |
| 34 | +import org.springframework.batch.core.JobParametersBuilder; |
33 | 35 | import org.springframework.batch.core.Step; |
34 | 36 | import org.springframework.batch.core.StepExecution; |
35 | 37 | import org.springframework.batch.core.UnexpectedJobExecutionException; |
|
50 | 52 | import org.springframework.batch.core.repository.JobRestartException; |
51 | 53 | import org.springframework.batch.core.step.builder.StepBuilder; |
52 | 54 | import org.springframework.beans.factory.annotation.Autowired; |
| 55 | +import org.springframework.context.ApplicationContext; |
| 56 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
53 | 57 | import org.springframework.context.annotation.Bean; |
54 | 58 | import org.springframework.context.annotation.Configuration; |
55 | 59 | import org.springframework.core.io.ClassPathResource; |
56 | 60 | import org.springframework.core.io.Resource; |
| 61 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 62 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
57 | 63 | import org.springframework.jdbc.support.JdbcTransactionManager; |
58 | 64 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
59 | 65 |
|
60 | 66 | import static org.junit.jupiter.api.Assertions.assertEquals; |
61 | 67 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
62 | 68 |
|
63 | 69 | /** |
64 | | - * Integration test for the BATCH-2034 issue. The {@link FlowStep} execution should not |
65 | | - * fail in the remote partitioning use case because the {@link SimpleJobExplorer} doesn't |
66 | | - * retrieve the {@link JobInstance} from the {@link JobRepository}. To illustrate the |
67 | | - * issue the test simulates the behavior of the {@code StepExecutionRequestHandler} from |
68 | | - * the spring-batch-integration project. |
| 70 | + * Integration tests for the <code>SimpleJobExplorer</code> implementation. |
69 | 71 | * |
70 | 72 | * @author Sergey Shcherbakov |
71 | 73 | * @author Mahmoud Ben Hassine |
72 | 74 | */ |
73 | 75 | @SpringJUnitConfig(classes = { SimpleJobExplorerIntegrationTests.Config.class }) |
74 | 76 | class SimpleJobExplorerIntegrationTests { |
75 | 77 |
|
| 78 | + /* |
| 79 | + * Integration test for the BATCH-2034 issue. The {@link FlowStep} execution should |
| 80 | + * not fail in the remote partitioning use case because the {@link SimpleJobExplorer} |
| 81 | + * doesn't retrieve the {@link JobInstance} from the {@link JobRepository}. To |
| 82 | + * illustrate the issue the test simulates the behavior of the {@code |
| 83 | + * StepExecutionRequestHandler} from the spring-batch-integration project. |
| 84 | + */ |
76 | 85 | @Configuration |
77 | 86 | @EnableBatchProcessing |
78 | 87 | static class Config { |
@@ -185,4 +194,65 @@ void getLastJobExecutionShouldFetchStepExecutions() throws Exception { |
185 | 194 | assertNotNull(stepExecution.getExecutionContext()); |
186 | 195 | } |
187 | 196 |
|
| 197 | + /* |
| 198 | + * Test case for https://github.com/spring-projects/spring-batch/issues/4246: |
| 199 | + * SimpleJobExplorer#getJobExecutions(JobInstance) should return a list of job |
| 200 | + * executions, where each execution has its own job parameters. |
| 201 | + */ |
| 202 | + |
| 203 | + @Configuration |
| 204 | + @EnableBatchProcessing |
| 205 | + static class JobConfiguration { |
| 206 | + |
| 207 | + @Bean |
| 208 | + public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) { |
| 209 | + return new StepBuilder("step", jobRepository).tasklet((contribution, chunkContext) -> { |
| 210 | + throw new RuntimeException("Expected failure!"); |
| 211 | + }, transactionManager).build(); |
| 212 | + } |
| 213 | + |
| 214 | + @Bean |
| 215 | + public Job job(JobRepository jobRepository, Step step) { |
| 216 | + return new JobBuilder("job", jobRepository).start(step).build(); |
| 217 | + } |
| 218 | + |
| 219 | + @Bean |
| 220 | + public DataSource dataSource() { |
| 221 | + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2) |
| 222 | + .addScript("/org/springframework/batch/core/schema-h2.sql").generateUniqueName(true).build(); |
| 223 | + } |
| 224 | + |
| 225 | + @Bean |
| 226 | + public JdbcTransactionManager transactionManager(DataSource dataSource) { |
| 227 | + return new JdbcTransactionManager(dataSource); |
| 228 | + } |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + void retrievedJobExecutionsShouldHaveTheirOwnParameters() throws Exception { |
| 234 | + // given |
| 235 | + ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class); |
| 236 | + JobLauncher jobLauncher = context.getBean(JobLauncher.class); |
| 237 | + JobExplorer jobExplorer = context.getBean(JobExplorer.class); |
| 238 | + Job job = context.getBean(Job.class); |
| 239 | + long id = 1L; |
| 240 | + JobParameters jobParameters1 = new JobParametersBuilder().addLong("id", id).addString("name", "foo", false) |
| 241 | + .toJobParameters(); |
| 242 | + JobParameters jobParameters2 = new JobParametersBuilder().addLong("id", id).addString("name", "bar", false) |
| 243 | + .toJobParameters(); |
| 244 | + |
| 245 | + // when |
| 246 | + JobExecution jobExecution1 = jobLauncher.run(job, jobParameters1); |
| 247 | + JobExecution jobExecution2 = jobLauncher.run(job, jobParameters2); |
| 248 | + |
| 249 | + // then |
| 250 | + Assertions.assertEquals(jobExecution1.getJobInstance(), jobExecution2.getJobInstance()); |
| 251 | + List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobExecution1.getJobInstance()); |
| 252 | + Assertions.assertEquals(2, jobExecutions.size()); |
| 253 | + JobParameters actualJobParameters1 = jobExecutions.get(0).getJobParameters(); |
| 254 | + JobParameters actualJobParameters2 = jobExecutions.get(1).getJobParameters(); |
| 255 | + Assertions.assertNotEquals(actualJobParameters1, actualJobParameters2); |
| 256 | + } |
| 257 | + |
188 | 258 | } |
0 commit comments