Skip to content

Commit 4fab5f2

Browse files
quafffmbenhassine
authored andcommitted
Introduce AbstractMongoDBDaoIntegrationTests
Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 6fdc225 commit 4fab5f2

File tree

2 files changed

+92
-35
lines changed

2 files changed

+92
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.batch.core.repository.dao.mongodb.MongoExecutionContextDao;
20+
import org.springframework.batch.core.repository.dao.mongodb.MongoJobExecutionDao;
21+
import org.springframework.batch.core.repository.dao.mongodb.MongoJobInstanceDao;
22+
import org.springframework.batch.core.repository.dao.mongodb.MongoStepExecutionDao;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.core.io.FileSystemResource;
27+
import org.springframework.core.io.Resource;
28+
import org.springframework.data.mongodb.core.MongoOperations;
29+
import org.springframework.data.mongodb.core.MongoTemplate;
30+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
31+
import org.testcontainers.junit.jupiter.Testcontainers;
32+
33+
import java.io.IOException;
34+
import java.nio.file.Files;
35+
import java.util.stream.Stream;
36+
37+
/**
38+
* @author Yanming Zhou
39+
*/
40+
@Testcontainers(disabledWithoutDocker = true)
41+
@SpringJUnitConfig({ MongoDBIntegrationTestConfiguration.class,
42+
AbstractMongoDBDaoIntegrationTests.MongoDBDaoConfiguration.class })
43+
abstract class AbstractMongoDBDaoIntegrationTests {
44+
45+
@BeforeEach
46+
void setUp(@Autowired MongoTemplate mongoTemplate) throws IOException {
47+
mongoTemplate.dropCollection("BATCH_JOB_INSTANCE");
48+
mongoTemplate.dropCollection("BATCH_JOB_EXECUTION");
49+
mongoTemplate.dropCollection("BATCH_STEP_EXECUTION");
50+
mongoTemplate.createCollection("BATCH_JOB_INSTANCE");
51+
mongoTemplate.createCollection("BATCH_JOB_EXECUTION");
52+
mongoTemplate.createCollection("BATCH_STEP_EXECUTION");
53+
// sequences
54+
mongoTemplate.dropCollection("BATCH_SEQUENCES");
55+
Resource resource = new FileSystemResource(
56+
"src/main/resources/org/springframework/batch/core/schema-mongodb.jsonl");
57+
try (Stream<String> lines = Files.lines(resource.getFilePath())) {
58+
lines.forEach(mongoTemplate::executeCommand);
59+
}
60+
}
61+
62+
@Configuration
63+
static class MongoDBDaoConfiguration {
64+
65+
@Bean
66+
MongoJobInstanceDao jobInstanceDao(MongoOperations mongoOperations) {
67+
return new MongoJobInstanceDao(mongoOperations);
68+
}
69+
70+
@Bean
71+
MongoJobExecutionDao jobExecutionDao(MongoOperations mongoOperations, MongoJobInstanceDao jobInstanceDao) {
72+
MongoJobExecutionDao jobExecutionDao = new MongoJobExecutionDao(mongoOperations);
73+
jobExecutionDao.setJobInstanceDao(jobInstanceDao);
74+
return jobExecutionDao;
75+
}
76+
77+
@Bean
78+
MongoStepExecutionDao stepExecutionDao(MongoOperations mongoOperations, MongoJobExecutionDao jobExecutionDao) {
79+
MongoStepExecutionDao stepExecutionDao = new MongoStepExecutionDao(mongoOperations);
80+
stepExecutionDao.setJobExecutionDao(jobExecutionDao);
81+
return stepExecutionDao;
82+
}
83+
84+
@Bean
85+
MongoExecutionContextDao executionContextDao(MongoOperations mongoOperations) {
86+
return new MongoExecutionContextDao(mongoOperations);
87+
}
88+
89+
}
90+
91+
}

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

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
*/
1616
package org.springframework.batch.core.repository.support;
1717

18-
import java.io.IOException;
19-
import java.nio.file.Files;
2018
import java.time.LocalDateTime;
2119

22-
import org.junit.jupiter.api.BeforeAll;
2320
import org.junit.jupiter.api.Test;
2421
import org.springframework.batch.core.job.Job;
2522
import org.springframework.batch.core.job.JobExecution;
@@ -29,19 +26,8 @@
2926
import org.springframework.batch.core.step.StepExecution;
3027
import org.springframework.batch.core.launch.JobOperator;
3128
import org.springframework.batch.core.repository.dao.ExecutionContextDao;
32-
import org.springframework.batch.core.repository.dao.mongodb.MongoExecutionContextDao;
33-
import org.springframework.batch.core.repository.support.MongoExecutionContextDaoIntegrationTests.ExecutionContextDaoConfiguration;
3429
import org.springframework.batch.infrastructure.item.ExecutionContext;
3530
import org.springframework.beans.factory.annotation.Autowired;
36-
import org.springframework.context.annotation.Bean;
37-
import org.springframework.context.annotation.Configuration;
38-
import org.springframework.core.io.FileSystemResource;
39-
import org.springframework.core.io.Resource;
40-
import org.springframework.data.mongodb.core.MongoOperations;
41-
import org.springframework.data.mongodb.core.MongoTemplate;
42-
import org.springframework.test.annotation.DirtiesContext;
43-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
44-
import org.testcontainers.junit.jupiter.Testcontainers;
4531

4632
import static org.junit.jupiter.api.Assertions.assertEquals;
4733
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -51,17 +37,7 @@
5137
* @author Henning Pöttker
5238
* @author Yanming Zhou
5339
*/
54-
@DirtiesContext
55-
@Testcontainers(disabledWithoutDocker = true)
56-
@SpringJUnitConfig({ MongoDBIntegrationTestConfiguration.class, ExecutionContextDaoConfiguration.class })
57-
public class MongoExecutionContextDaoIntegrationTests {
58-
59-
@BeforeAll
60-
static void setUp(@Autowired MongoTemplate mongoTemplate) throws IOException {
61-
Resource resource = new FileSystemResource(
62-
"src/main/resources/org/springframework/batch/core/schema-mongodb.jsonl");
63-
Files.lines(resource.getFilePath()).forEach(line -> mongoTemplate.executeCommand(line));
64-
}
40+
public class MongoExecutionContextDaoIntegrationTests extends AbstractMongoDBDaoIntegrationTests {
6541

6642
@Test
6743
void testGetJobExecutionWithEmptyResult(@Autowired ExecutionContextDao executionContextDao) {
@@ -131,14 +107,4 @@ void testSaveStepExecution(@Autowired JobOperator jobOperator, @Autowired Job jo
131107
assertEquals("bar", actual.get("foo"));
132108
}
133109

134-
@Configuration
135-
static class ExecutionContextDaoConfiguration {
136-
137-
@Bean
138-
ExecutionContextDao executionContextDao(MongoOperations mongoOperations) {
139-
return new MongoExecutionContextDao(mongoOperations);
140-
}
141-
142-
}
143-
144110
}

0 commit comments

Comments
 (0)