diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryIntegrationTests.java similarity index 90% rename from spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java rename to spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryIntegrationTests.java index 7320363334..899d751183 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryIntegrationTests.java @@ -22,6 +22,7 @@ import org.springframework.batch.core.job.JobInstance; import org.springframework.batch.core.job.parameters.JobParameters; import org.springframework.batch.core.job.parameters.JobParametersBuilder; +import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.Step; import org.springframework.batch.core.step.StepExecution; import org.springframework.batch.core.job.JobSupport; @@ -29,8 +30,6 @@ import org.springframework.batch.core.step.StepSupport; import org.springframework.batch.infrastructure.item.ExecutionContext; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; @@ -43,21 +42,17 @@ import static org.junit.jupiter.api.Assertions.assertThrows; /** - * Repository tests using JDBC DAOs (rather than mocks). + * Abstract repository tests using DAOs (rather than mocks). * * @author Robert Kasanicky * @author Dimitrios Liapis * @author Mahmoud Ben Hassine * @author Yanming Zhou */ -// TODO rename to JdbcJobRepositoryIntegrationTests and update to new domain model -// TODO should add a mongodb similar test suite -@Disabled -@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/jdbc/sql-dao-test.xml") -class SimpleJobRepositoryIntegrationTests { +abstract class AbstractJobRepositoryIntegrationTests { @Autowired - private SimpleJobRepository jobRepository; + private JobRepository jobRepository; private final JobSupport job = new JobSupport("SimpleJobRepositoryIntegrationTestsJob"); @@ -67,9 +62,8 @@ class SimpleJobRepositoryIntegrationTests { * Create two job executions for same job+parameters tuple. Check both executions * belong to the same job instance and job. */ - @Transactional @Test - void testCreateAndFind() throws Exception { + void testCreateAndFind() { job.setRestartable(true); @@ -81,7 +75,7 @@ void testCreateAndFind() throws Exception { JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters); JobExecution firstExecution = jobRepository.createJobExecution(jobInstance, jobParameters, executionContext); firstExecution.setStartTime(LocalDateTime.now()); - assertNotNull(firstExecution.getLastUpdated()); + assertNull(firstExecution.getLastUpdated()); assertEquals(job.getName(), firstExecution.getJobInstance().getJobName()); @@ -99,9 +93,8 @@ void testCreateAndFind() throws Exception { * Create two job executions for same job+parameters tuple. Check both executions * belong to the same job instance and job. */ - @Transactional @Test - void testCreateAndFindWithNoStartDate() throws Exception { + void testCreateAndFindWithNoStartDate() { job.setRestartable(true); JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters); @@ -123,7 +116,6 @@ void testCreateAndFindWithNoStartDate() throws Exception { * Save multiple StepExecutions for the same step and check the returned count and * last execution are correct. */ - @Transactional @Test void testGetStepExecutionCountAndLastStepExecution() throws Exception { job.setRestartable(true); @@ -161,9 +153,8 @@ void testGetStepExecutionCountAndLastStepExecution() throws Exception { /* * Save execution context and retrieve it. */ - @Transactional @Test - void testSaveExecutionContext() throws Exception { + void testSaveExecutionContext() { ExecutionContext ctx = new ExecutionContext(Map.of("crashedPosition", 7)); JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters); JobExecution jobExec = jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext()); @@ -188,9 +179,9 @@ void testSaveExecutionContext() throws Exception { * If JobExecution is already running, exception will be thrown in attempt to create * new execution. */ - @Transactional @Test - void testOnlyOneJobExecutionAllowedRunning() throws Exception { + @Disabled("JobExecutionAlreadyRunningException is not thrown at repository level") + void testOnlyOneJobExecutionAllowedRunning() { job.setRestartable(true); JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters); JobExecution jobExecution = jobRepository.createJobExecution(jobInstance, jobParameters, @@ -204,7 +195,6 @@ void testOnlyOneJobExecutionAllowedRunning() throws Exception { () -> jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext())); } - @Transactional @Test void testGetLastJobExecution() throws Exception { JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters); @@ -225,9 +215,8 @@ void testGetLastJobExecution() throws Exception { * Create two job executions for the same job+parameters tuple. Should ignore * non-identifying job parameters when identifying the job instance. */ - @Transactional @Test - void testReExecuteWithSameJobParameters() throws Exception { + void testReExecuteWithSameJobParameters() { JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo", false).toJobParameters(); JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters); JobExecution jobExecution1 = jobRepository.createJobExecution(jobInstance, jobParameters, @@ -245,9 +234,9 @@ void testReExecuteWithSameJobParameters() throws Exception { * When a job execution is running, JobExecutionAlreadyRunningException should be * thrown if trying to create any other ones with same job parameters. */ - @Transactional @Test - void testReExecuteWithSameJobParametersWhenRunning() throws Exception { + @Disabled("JobExecutionAlreadyRunningException is not thrown at repository level") + void testReExecuteWithSameJobParametersWhenRunning() { JobParameters jobParameters = new JobParametersBuilder().addString("stringKey", "stringValue") .toJobParameters(); @@ -272,9 +261,8 @@ void testReExecuteWithSameJobParametersWhenRunning() throws Exception { () -> jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext())); } - @Transactional @Test - void testDeleteJobInstance() throws Exception { + void testDeleteJobInstance() { var jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters(); JobInstance jobInstance = jobRepository.createJobInstance(job.getName(), jobParameters); var jobExecution = jobRepository.createJobExecution(jobInstance, jobParameters, new ExecutionContext()); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JdbcJobRepositoryIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JdbcJobRepositoryIntegrationTests.java new file mode 100644 index 0000000000..ac5b2dab88 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JdbcJobRepositoryIntegrationTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2008-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.repository.support; + +import org.junit.jupiter.api.BeforeEach; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; +import org.springframework.jdbc.datasource.init.ScriptUtils; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import javax.sql.DataSource; +import java.sql.Connection; + +/** + * Repository tests using JDBC DAOs (rather than mocks). + * + * @author Yanming Zhou + **/ +@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/jdbc/sql-dao-test.xml") +public class JdbcJobRepositoryIntegrationTests extends AbstractJobRepositoryIntegrationTests { + + @Autowired + private DataSource dataSource; + + @BeforeEach + public void setUp() throws Exception { + try (Connection connection = dataSource.getConnection()) { + ScriptUtils.executeSqlScript(connection, + new FileSystemResource("src/main/resources/org/springframework/batch/core/schema-drop-hsqldb.sql")); + ScriptUtils.executeSqlScript(connection, + new FileSystemResource("src/main/resources/org/springframework/batch/core/schema-hsqldb.sql")); + } + } + +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/MongoDBJobRepositoryIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/MongoDBJobRepositoryIntegrationTests.java index 895cc916c2..ae40bb12a9 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/MongoDBJobRepositoryIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/MongoDBJobRepositoryIntegrationTests.java @@ -40,13 +40,15 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** + * Repository tests using MongoDB DAOs (rather than mocks). + * * @author Mahmoud Ben Hassine * @author Yanming Zhou */ @DirtiesContext @Testcontainers(disabledWithoutDocker = true) @SpringJUnitConfig(MongoDBIntegrationTestConfiguration.class) -public class MongoDBJobRepositoryIntegrationTests { +public class MongoDBJobRepositoryIntegrationTests extends AbstractJobRepositoryIntegrationTests { @Autowired private MongoTemplate mongoTemplate;