Skip to content

Commit 80e4349

Browse files
committed
Refine contribution #5332
- Use inner class for test configuration - Rename env variable and property - Refactor tests
1 parent 4580c59 commit 80e4349

6 files changed

Lines changed: 114 additions & 142 deletions

File tree

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,19 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
7878
protected static final Log logger = LogFactory.getLog(JobRepositoryFactoryBean.class);
7979

8080
/**
81-
* Classic schema name of the job instance incrementer.
81+
* Legacy (v5) name of the job instance incrementer.
8282
*/
83-
private static final String CLASSIC_JOB_INSTANCE_INCREMENTER_NAME = "JOB_SEQ";
83+
protected static final String LEGACY_JOB_INSTANCE_INCREMENTER_NAME = "JOB_SEQ";
8484

8585
/**
86-
* Environment variable for using the classic schema name of the job instance
87-
* incrementer.
86+
* Environment variable for using the legacy (v5) jdbc schema.
8887
*/
89-
private static final String SPRING_BATCH_JDBC_SCHEMA_CLASSIC = "SPRING_BATCH_JDBC_SCHEMA_CLASSIC";
88+
protected static final String SPRING_BATCH_JDBC_SCHEMA_LEGACY = "SPRING_BATCH_JDBC_SCHEMA_LEGACY";
89+
90+
/**
91+
* System property for using the legacy (v5) jdbc schema.
92+
*/
93+
protected static final String SPRING_BATCH_JDBC_SCHEMA_LEGACY_PROPERTY = "spring.batch.jdbc.schema.legacy";
9094

9195
protected DataSource dataSource;
9296

@@ -324,16 +328,16 @@ public void afterPropertiesSet() throws Exception {
324328
@Override
325329
protected JdbcJobInstanceDao createJobInstanceDao() {
326330
JdbcJobInstanceDao dao = new JdbcJobInstanceDao();
327-
String classicSchema = "false";
328-
if (System.getenv(SPRING_BATCH_JDBC_SCHEMA_CLASSIC) != null) {
329-
classicSchema = System.getenv(SPRING_BATCH_JDBC_SCHEMA_CLASSIC);
331+
String legacySchema = "false";
332+
if (System.getenv(SPRING_BATCH_JDBC_SCHEMA_LEGACY) != null) {
333+
legacySchema = System.getenv(SPRING_BATCH_JDBC_SCHEMA_LEGACY);
330334
}
331-
else if (System.getProperty("spring.batch.jdbc.schema.classic") != null) {
332-
classicSchema = System.getProperty("spring.batch.jdbc.schema.classic");
335+
else if (System.getProperty(SPRING_BATCH_JDBC_SCHEMA_LEGACY_PROPERTY) != null) {
336+
legacySchema = System.getProperty(SPRING_BATCH_JDBC_SCHEMA_LEGACY_PROPERTY);
333337
}
334-
if ("TRUE".equalsIgnoreCase(classicSchema) || "YES".equalsIgnoreCase(classicSchema)) {
335-
jobInstanceIncrementerName = CLASSIC_JOB_INSTANCE_INCREMENTER_NAME;
336-
logger.info("Using classic schema job instance incrementer name of " + jobInstanceIncrementerName);
338+
if ("TRUE".equalsIgnoreCase(legacySchema) || "YES".equalsIgnoreCase(legacySchema)) {
339+
jobInstanceIncrementerName = LEGACY_JOB_INSTANCE_INCREMENTER_NAME;
340+
logger.info("Using legacy name for job instance incrementer: " + jobInstanceIncrementerName);
337341
}
338342
dao.setJdbcTemplate(jdbcOperations);
339343
dao.setJobInstanceIncrementer(

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

Lines changed: 0 additions & 69 deletions
This file was deleted.

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

Lines changed: 0 additions & 60 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2026-present 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 java.sql.Connection;
19+
20+
import javax.sql.DataSource;
21+
22+
import org.apache.commons.dbcp2.BasicDataSource;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
25+
26+
import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.io.FileSystemResource;
31+
import org.springframework.jdbc.datasource.init.ScriptUtils;
32+
import org.springframework.jdbc.support.JdbcTransactionManager;
33+
import org.springframework.test.annotation.DirtiesContext;
34+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
35+
36+
/**
37+
* Repository tests using JDBC DAOs and legacy schema.
38+
*
39+
* @author Thomas Risberg
40+
* @author Mahmoud Ben Hassine
41+
**/
42+
@SpringJUnitConfig
43+
@DirtiesContext
44+
public class JdbcJobRepositoryLegacySchemaIntegrationTests extends AbstractJobRepositoryIntegrationTests {
45+
46+
@Autowired
47+
private DataSource dataSource;
48+
49+
@BeforeEach
50+
public void setUp() throws Exception {
51+
try (Connection connection = dataSource.getConnection()) {
52+
ScriptUtils.executeSqlScript(connection,
53+
new FileSystemResource("src/test/resources/schema-hsqldb-legacy.sql"));
54+
}
55+
}
56+
57+
@AfterEach
58+
public void cleanUp() throws Exception {
59+
try (Connection connection = dataSource.getConnection()) {
60+
ScriptUtils.executeSqlScript(connection,
61+
new FileSystemResource("src/test/resources/schema-drop-hsqldb-legacy.sql"));
62+
}
63+
System.clearProperty("spring.batch.jdbc.schema.legacy");
64+
}
65+
66+
@Configuration
67+
@EnableJdbcJobRepository
68+
static class JdbcJobRepositoryLegacyTestConfiguration {
69+
70+
@Bean
71+
public DataSource dataSource() {
72+
BasicDataSource dataSource = new BasicDataSource();
73+
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
74+
dataSource.setUrl("jdbc:hsqldb:mem:test;sql.enforce_strict_size=true;hsqldb.tx=mvcc");
75+
dataSource.setUsername("sa");
76+
dataSource.setPassword("");
77+
return dataSource;
78+
}
79+
80+
@Bean
81+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
82+
return new JdbcTransactionManager(dataSource);
83+
}
84+
85+
@Bean
86+
public JdbcJobRepositoryFactoryBean jobRepository(DataSource dataSource,
87+
JdbcTransactionManager transactionManager) throws Exception {
88+
System.setProperty("spring.batch.jdbc.schema.legacy", "true");
89+
JdbcJobRepositoryFactoryBean jobRepositoryFactoryBean = new JdbcJobRepositoryFactoryBean();
90+
jobRepositoryFactoryBean.setDataSource(dataSource);
91+
jobRepositoryFactoryBean.setTransactionManager(transactionManager);
92+
return jobRepositoryFactoryBean;
93+
}
94+
95+
}
96+
97+
}

spring-batch-core/src/test/resources/org/springframework/batch/core/schema-drop-classic-hsqldb.sql renamed to spring-batch-core/src/test/resources/schema-drop-hsqldb-legacy.sql

File renamed without changes.

spring-batch-core/src/test/resources/org/springframework/batch/core/schema-classic-hsqldb.sql renamed to spring-batch-core/src/test/resources/schema-hsqldb-legacy.sql

File renamed without changes.

0 commit comments

Comments
 (0)