Skip to content

Commit 7f304a1

Browse files
committed
Refine contribution #4364
Add integration test Related to #4246
1 parent e329b61 commit 7f304a1

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/explore/support/SimpleJobExplorerIntegrationTests.java

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import javax.sql.DataSource;
2222

2323
import org.apache.commons.dbcp2.BasicDataSource;
24+
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.Test;
2526
import test.jdbc.datasource.DataSourceInitializer;
2627

@@ -30,6 +31,7 @@
3031
import org.springframework.batch.core.JobInstance;
3132
import org.springframework.batch.core.JobInterruptedException;
3233
import org.springframework.batch.core.JobParameters;
34+
import org.springframework.batch.core.JobParametersBuilder;
3335
import org.springframework.batch.core.Step;
3436
import org.springframework.batch.core.StepExecution;
3537
import org.springframework.batch.core.UnexpectedJobExecutionException;
@@ -50,29 +52,36 @@
5052
import org.springframework.batch.core.repository.JobRestartException;
5153
import org.springframework.batch.core.step.builder.StepBuilder;
5254
import org.springframework.beans.factory.annotation.Autowired;
55+
import org.springframework.context.ApplicationContext;
56+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5357
import org.springframework.context.annotation.Bean;
5458
import org.springframework.context.annotation.Configuration;
5559
import org.springframework.core.io.ClassPathResource;
5660
import org.springframework.core.io.Resource;
61+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
62+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
5763
import org.springframework.jdbc.support.JdbcTransactionManager;
5864
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
5965

6066
import static org.junit.jupiter.api.Assertions.assertEquals;
6167
import static org.junit.jupiter.api.Assertions.assertNotNull;
6268

6369
/**
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.
6971
*
7072
* @author Sergey Shcherbakov
7173
* @author Mahmoud Ben Hassine
7274
*/
7375
@SpringJUnitConfig(classes = { SimpleJobExplorerIntegrationTests.Config.class })
7476
class SimpleJobExplorerIntegrationTests {
7577

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+
*/
7685
@Configuration
7786
@EnableBatchProcessing
7887
static class Config {
@@ -185,4 +194,65 @@ void getLastJobExecutionShouldFetchStepExecutions() throws Exception {
185194
assertNotNull(stepExecution.getExecutionContext());
186195
}
187196

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+
188258
}

0 commit comments

Comments
 (0)