Skip to content

Commit 81b5df1

Browse files
quafffmbenhassine
authored andcommitted
Introduce AbstractJobRepositoryFactoryBean for both JDBC and MongoDB repository
Signed-off-by: Yanming Zhou <[email protected]>
1 parent ac7536d commit 81b5df1

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java renamed to spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryIntegrationTests.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222
import org.springframework.batch.core.job.JobInstance;
2323
import org.springframework.batch.core.job.parameters.JobParameters;
2424
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
25+
import org.springframework.batch.core.repository.JobRepository;
2526
import org.springframework.batch.core.step.Step;
2627
import org.springframework.batch.core.step.StepExecution;
2728
import org.springframework.batch.core.job.JobSupport;
2829
import org.springframework.batch.core.launch.JobExecutionAlreadyRunningException;
2930
import org.springframework.batch.core.step.StepSupport;
3031
import org.springframework.batch.infrastructure.item.ExecutionContext;
3132
import org.springframework.beans.factory.annotation.Autowired;
32-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
33-
import org.springframework.transaction.annotation.Transactional;
3433

3534
import java.time.LocalDateTime;
3635
import java.time.temporal.ChronoUnit;
@@ -43,20 +42,17 @@
4342
import static org.junit.jupiter.api.Assertions.assertThrows;
4443

4544
/**
46-
* Repository tests using JDBC DAOs (rather than mocks).
45+
* Abstract repository tests using DAOs (rather than mocks).
4746
*
4847
* @author Robert Kasanicky
4948
* @author Dimitrios Liapis
5049
* @author Mahmoud Ben Hassine
5150
* @author Yanming Zhou
5251
*/
53-
// TODO rename to JdbcJobRepositoryIntegrationTests and update to new domain model
54-
// TODO should add a mongodb similar test suite
55-
@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/jdbc/sql-dao-test.xml")
56-
class SimpleJobRepositoryIntegrationTests {
52+
abstract class AbstractJobRepositoryIntegrationTests {
5753

5854
@Autowired
59-
private SimpleJobRepository jobRepository;
55+
private JobRepository jobRepository;
6056

6157
private final JobSupport job = new JobSupport("SimpleJobRepositoryIntegrationTestsJob");
6258

@@ -66,9 +62,8 @@ class SimpleJobRepositoryIntegrationTests {
6662
* Create two job executions for same job+parameters tuple. Check both executions
6763
* belong to the same job instance and job.
6864
*/
69-
@Transactional
7065
@Test
71-
void testCreateAndFind() throws Exception {
66+
void testCreateAndFind() {
7267

7368
job.setRestartable(true);
7469

@@ -98,9 +93,8 @@ void testCreateAndFind() throws Exception {
9893
* Create two job executions for same job+parameters tuple. Check both executions
9994
* belong to the same job instance and job.
10095
*/
101-
@Transactional
10296
@Test
103-
void testCreateAndFindWithNoStartDate() throws Exception {
97+
void testCreateAndFindWithNoStartDate() {
10498
job.setRestartable(true);
10599

106100
JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters);
@@ -122,7 +116,6 @@ void testCreateAndFindWithNoStartDate() throws Exception {
122116
* Save multiple StepExecutions for the same step and check the returned count and
123117
* last execution are correct.
124118
*/
125-
@Transactional
126119
@Test
127120
void testGetStepExecutionCountAndLastStepExecution() throws Exception {
128121
job.setRestartable(true);
@@ -160,9 +153,8 @@ void testGetStepExecutionCountAndLastStepExecution() throws Exception {
160153
/*
161154
* Save execution context and retrieve it.
162155
*/
163-
@Transactional
164156
@Test
165-
void testSaveExecutionContext() throws Exception {
157+
void testSaveExecutionContext() {
166158
ExecutionContext ctx = new ExecutionContext(Map.of("crashedPosition", 7));
167159
JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters);
168160
JobExecution jobExec = jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext());
@@ -187,10 +179,9 @@ void testSaveExecutionContext() throws Exception {
187179
* If JobExecution is already running, exception will be thrown in attempt to create
188180
* new execution.
189181
*/
190-
@Transactional
191182
@Test
192183
@Disabled("JobExecutionAlreadyRunningException is not thrown at repository level")
193-
void testOnlyOneJobExecutionAllowedRunning() throws Exception {
184+
void testOnlyOneJobExecutionAllowedRunning() {
194185
job.setRestartable(true);
195186
JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters);
196187
JobExecution jobExecution = jobRepository.createJobExecution(jobInstance, jobParameters,
@@ -204,7 +195,6 @@ void testOnlyOneJobExecutionAllowedRunning() throws Exception {
204195
() -> jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext()));
205196
}
206197

207-
@Transactional
208198
@Test
209199
void testGetLastJobExecution() throws Exception {
210200
JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters);
@@ -225,9 +215,8 @@ void testGetLastJobExecution() throws Exception {
225215
* Create two job executions for the same job+parameters tuple. Should ignore
226216
* non-identifying job parameters when identifying the job instance.
227217
*/
228-
@Transactional
229218
@Test
230-
void testReExecuteWithSameJobParameters() throws Exception {
219+
void testReExecuteWithSameJobParameters() {
231220
JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo", false).toJobParameters();
232221
JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters);
233222
JobExecution jobExecution1 = jobRepository.createJobExecution(jobInstance, jobParameters,
@@ -245,10 +234,9 @@ void testReExecuteWithSameJobParameters() throws Exception {
245234
* When a job execution is running, JobExecutionAlreadyRunningException should be
246235
* thrown if trying to create any other ones with same job parameters.
247236
*/
248-
@Transactional
249237
@Test
250238
@Disabled("JobExecutionAlreadyRunningException is not thrown at repository level")
251-
void testReExecuteWithSameJobParametersWhenRunning() throws Exception {
239+
void testReExecuteWithSameJobParametersWhenRunning() {
252240
JobParameters jobParameters = new JobParametersBuilder().addString("stringKey", "stringValue")
253241
.toJobParameters();
254242

@@ -273,9 +261,8 @@ void testReExecuteWithSameJobParametersWhenRunning() throws Exception {
273261
() -> jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext()));
274262
}
275263

276-
@Transactional
277264
@Test
278-
void testDeleteJobInstance() throws Exception {
265+
void testDeleteJobInstance() {
279266
var jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
280267
JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters);
281268
var jobExecution = jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext());
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2008-2025 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.support;
17+
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.core.io.FileSystemResource;
21+
import org.springframework.jdbc.datasource.init.ScriptUtils;
22+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
23+
24+
import javax.sql.DataSource;
25+
import java.sql.Connection;
26+
27+
/**
28+
* Repository tests using JDBC DAOs (rather than mocks).
29+
*
30+
* @author Yanming Zhou
31+
**/
32+
@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/jdbc/sql-dao-test.xml")
33+
public class JdbcJobRepositoryIntegrationTests extends AbstractJobRepositoryIntegrationTests {
34+
35+
@Autowired
36+
private DataSource dataSource;
37+
38+
@BeforeEach
39+
public void setUp() throws Exception {
40+
try (Connection connection = dataSource.getConnection()) {
41+
ScriptUtils.executeSqlScript(connection,
42+
new FileSystemResource("src/main/resources/org/springframework/batch/core/schema-drop-hsqldb.sql"));
43+
ScriptUtils.executeSqlScript(connection,
44+
new FileSystemResource("src/main/resources/org/springframework/batch/core/schema-hsqldb.sql"));
45+
}
46+
}
47+
48+
}

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/MongoDBJobRepositoryIntegrationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4141

4242
/**
43+
* Repository tests using MongoDB DAOs (rather than mocks).
44+
*
4345
* @author Mahmoud Ben Hassine
4446
* @author Yanming Zhou
4547
*/
4648
@DirtiesContext
4749
@Testcontainers(disabledWithoutDocker = true)
4850
@SpringJUnitConfig(MongoDBIntegrationTestConfiguration.class)
49-
public class MongoDBJobRepositoryIntegrationTests {
51+
public class MongoDBJobRepositoryIntegrationTests extends AbstractJobRepositoryIntegrationTests {
5052

5153
@Autowired
5254
private MongoTemplate mongoTemplate;

0 commit comments

Comments
 (0)