From 930bb0243c1eccf6f8ffc823d8008c2da72f94c4 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Wed, 16 Nov 2022 15:34:08 +0100 Subject: [PATCH] Implement default methods in SimpleJobRepository Resolves #4229 --- .../support/SimpleJobRepository.java | 15 +++++++++ .../support/SimpleJobRepositoryTests.java | 32 +++++++++++++++++++ .../test/JobRepositoryTestUtilsTests.java | 20 ++++++++++-- .../test/SpringBatchTestJUnit4Tests.java | 8 ++--- .../test/SpringBatchTestJUnit5Tests.java | 7 +--- 5 files changed, 67 insertions(+), 15 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java index a8f81a240..8107106f0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java @@ -87,6 +87,21 @@ public class SimpleJobRepository implements JobRepository { this.ecDao = ecDao; } + @Override + public List getJobNames() { + return this.jobInstanceDao.getJobNames(); + } + + @Override + public List findJobInstancesByName(String jobName, int start, int count) { + return this.jobInstanceDao.findJobInstancesByName(jobName, start, count); + } + + @Override + public List findJobExecutions(JobInstance jobInstance) { + return this.jobExecutionDao.findJobExecutions(jobInstance); + } + @Override public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) { return jobInstanceDao.getJobInstance(jobName, jobParameters) != null; diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java index bb785f757..5fdb67e9a 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java @@ -130,6 +130,38 @@ class SimpleJobRepositoryTests { jobExecution = new JobExecution(new JobInstance(1L, job.getName()), 1L, jobParameters); } + @Test + void testGetJobNames() { + // when + this.jobRepository.getJobNames(); + + // then + verify(this.jobInstanceDao).getJobNames(); + } + + @Test + void testFindJobInstancesByName() { + // given + String jobName = "job"; + int start = 1; + int count = 10; + + // when + this.jobRepository.findJobInstancesByName(jobName, start, count); + + // then + verify(this.jobInstanceDao).findJobInstancesByName(jobName, start, count); + } + + @Test + void testFindJobExecutions() { + // when + this.jobRepository.findJobExecutions(this.jobInstance); + + // then + verify(this.jobExecutionDao).findJobExecutions(this.jobInstance); + } + @Test void testSaveOrUpdateInvalidJobExecution() { diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java index 0420c0397..261077305 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java @@ -15,9 +15,6 @@ */ package org.springframework.batch.test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -26,6 +23,7 @@ import javax.sql.DataSource; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; @@ -37,6 +35,8 @@ import org.springframework.lang.Nullable; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.jdbc.JdbcTestUtils; +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * @author Dave Syer * @author Mahmoud Ben Hassine @@ -133,4 +133,18 @@ class JobRepositoryTestUtilsTests { assertEquals(beforeJobs, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION")); } + @Test + void testRemoveJobExecutions() throws Exception { + // given + utils = new JobRepositoryTestUtils(jobRepository); + utils.createJobExecutions("foo", new String[] {}, 2); + assertEquals(2, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION")); + + // when + utils.removeJobExecutions(); + + // then + assertEquals(0, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION")); + } + } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java index f0562064b..99eb2cdba 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java @@ -16,10 +16,10 @@ package org.springframework.batch.test; import java.util.Arrays; + import javax.sql.DataSource; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -69,11 +69,6 @@ public class SpringBatchTestJUnit4Tests { @Autowired private ItemReader jobScopedItemReader; - @Before - public void setUp() { - this.jobRepositoryTestUtils.removeJobExecutions(); - } - public StepExecution getStepExecution() { StepExecution execution = MetaDataInstanceFactory.createStepExecution(); execution.getExecutionContext().putString("input.data", "foo,bar"); @@ -103,6 +98,7 @@ public class SpringBatchTestJUnit4Tests { @Test public void testJob() throws Exception { // when + this.jobRepositoryTestUtils.removeJobExecutions(); JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(); // then diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java index 1944d6173..ec24285f4 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java @@ -19,7 +19,6 @@ import java.util.Arrays; import javax.sql.DataSource; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.batch.core.ExitStatus; @@ -70,11 +69,6 @@ public class SpringBatchTestJUnit5Tests { @Autowired private ItemReader jobScopedItemReader; - @BeforeEach - void setup() { - this.jobRepositoryTestUtils.removeJobExecutions(); - } - @Test void testStepScopedItemReader() throws Exception { assertEquals("foo", this.stepScopedItemReader.read()); @@ -92,6 +86,7 @@ public class SpringBatchTestJUnit5Tests { @Test void testJob() throws Exception { // given + this.jobRepositoryTestUtils.removeJobExecutions(); JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters(); // when