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+ }
0 commit comments