Skip to content

Commit 930bb02

Browse files
committed
Implement default methods in SimpleJobRepository
Resolves #4229
1 parent 05bbd3f commit 930bb02

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ public SimpleJobRepository(JobInstanceDao jobInstanceDao, JobExecutionDao jobExe
8787
this.ecDao = ecDao;
8888
}
8989

90+
@Override
91+
public List<String> getJobNames() {
92+
return this.jobInstanceDao.getJobNames();
93+
}
94+
95+
@Override
96+
public List<JobInstance> findJobInstancesByName(String jobName, int start, int count) {
97+
return this.jobInstanceDao.findJobInstancesByName(jobName, start, count);
98+
}
99+
100+
@Override
101+
public List<JobExecution> findJobExecutions(JobInstance jobInstance) {
102+
return this.jobExecutionDao.findJobExecutions(jobInstance);
103+
}
104+
90105
@Override
91106
public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
92107
return jobInstanceDao.getJobInstance(jobName, jobParameters) != null;

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,38 @@ void setUp() {
130130
jobExecution = new JobExecution(new JobInstance(1L, job.getName()), 1L, jobParameters);
131131
}
132132

133+
@Test
134+
void testGetJobNames() {
135+
// when
136+
this.jobRepository.getJobNames();
137+
138+
// then
139+
verify(this.jobInstanceDao).getJobNames();
140+
}
141+
142+
@Test
143+
void testFindJobInstancesByName() {
144+
// given
145+
String jobName = "job";
146+
int start = 1;
147+
int count = 10;
148+
149+
// when
150+
this.jobRepository.findJobInstancesByName(jobName, start, count);
151+
152+
// then
153+
verify(this.jobInstanceDao).findJobInstancesByName(jobName, start, count);
154+
}
155+
156+
@Test
157+
void testFindJobExecutions() {
158+
// when
159+
this.jobRepository.findJobExecutions(this.jobInstance);
160+
161+
// then
162+
verify(this.jobExecutionDao).findJobExecutions(this.jobInstance);
163+
}
164+
133165
@Test
134166
void testSaveOrUpdateInvalidJobExecution() {
135167

spring-batch-test/src/test/java/org/springframework/batch/test/JobRepositoryTestUtilsTests.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18-
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
import static org.junit.jupiter.api.Assertions.assertThrows;
20-
2118
import java.time.LocalDateTime;
2219
import java.util.ArrayList;
2320
import java.util.List;
@@ -26,6 +23,7 @@
2623

2724
import org.junit.jupiter.api.BeforeEach;
2825
import org.junit.jupiter.api.Test;
26+
2927
import org.springframework.batch.core.JobExecution;
3028
import org.springframework.batch.core.JobParameters;
3129
import org.springframework.batch.core.JobParametersBuilder;
@@ -37,6 +35,8 @@
3735
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3836
import org.springframework.test.jdbc.JdbcTestUtils;
3937

38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
4040
/**
4141
* @author Dave Syer
4242
* @author Mahmoud Ben Hassine
@@ -133,4 +133,18 @@ public JobParameters getNext(@Nullable JobParameters parameters) {
133133
assertEquals(beforeJobs, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
134134
}
135135

136+
@Test
137+
void testRemoveJobExecutions() throws Exception {
138+
// given
139+
utils = new JobRepositoryTestUtils(jobRepository);
140+
utils.createJobExecutions("foo", new String[] {}, 2);
141+
assertEquals(2, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
142+
143+
// when
144+
utils.removeJobExecutions();
145+
146+
// then
147+
assertEquals(0, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
148+
}
149+
136150
}

spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package org.springframework.batch.test;
1717

1818
import java.util.Arrays;
19+
1920
import javax.sql.DataSource;
2021

2122
import org.junit.Assert;
22-
import org.junit.Before;
2323
import org.junit.Test;
2424
import org.junit.runner.RunWith;
2525

@@ -69,11 +69,6 @@ public class SpringBatchTestJUnit4Tests {
6969
@Autowired
7070
private ItemReader<String> jobScopedItemReader;
7171

72-
@Before
73-
public void setUp() {
74-
this.jobRepositoryTestUtils.removeJobExecutions();
75-
}
76-
7772
public StepExecution getStepExecution() {
7873
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
7974
execution.getExecutionContext().putString("input.data", "foo,bar");
@@ -103,6 +98,7 @@ public void testJobScopedItemReader() throws Exception {
10398
@Test
10499
public void testJob() throws Exception {
105100
// when
101+
this.jobRepositoryTestUtils.removeJobExecutions();
106102
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();
107103

108104
// then

spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import javax.sql.DataSource;
2121

22-
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.batch.core.ExitStatus;
@@ -70,11 +69,6 @@ public class SpringBatchTestJUnit5Tests {
7069
@Autowired
7170
private ItemReader<String> jobScopedItemReader;
7271

73-
@BeforeEach
74-
void setup() {
75-
this.jobRepositoryTestUtils.removeJobExecutions();
76-
}
77-
7872
@Test
7973
void testStepScopedItemReader() throws Exception {
8074
assertEquals("foo", this.stepScopedItemReader.read());
@@ -92,6 +86,7 @@ void testJobScopedItemReader() throws Exception {
9286
@Test
9387
void testJob() throws Exception {
9488
// given
89+
this.jobRepositoryTestUtils.removeJobExecutions();
9590
JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
9691

9792
// when

0 commit comments

Comments
 (0)