Skip to content

Commit 8954563

Browse files
committed
GH 4755: Fix RecordFieldSetMapper for empty record
commit 3b4322b Author: Seungyong Hong <[email protected]> Date: Sat Aug 16 15:37:35 2025 +0900 Fix RecordFieldSetMapper for empty record - Update test casese. Signed-off-by: Seungyong Hong <[email protected]> commit dde6cbe Author: Seungyong Hong <[email protected]> Date: Sat Aug 16 14:44:25 2025 +0900 Fix RecordFieldSetMapper for empty record - formatting. Signed-off-by: Seungyong Hong <[email protected]> commit 0ce9de6 Author: Seungyong Hong <[email protected]> Date: Fri Aug 15 23:04:26 2025 +0900 Fix wrong behavior of JobRepository with empty identifying job parameters - simplify brackets. Signed-off-by: Seungyong Hong <[email protected]> commit 45efc57 Author: Seungyong Hong <[email protected]> Date: Fri Aug 15 20:43:39 2025 +0900 Fix wrong behavior of JobRepository with empty identifying job parameters - Job has to be not completed whether empty identifying job parameters or not. Signed-off-by: Seungyong Hong <[email protected]>
1 parent 3bcc525 commit 8954563

File tree

6 files changed

+44
-34
lines changed

6 files changed

+44
-34
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Baris Cubukcuoglu
5555
* @author Parikshit Dutta
5656
* @author Mark John Moreno
57+
* @author Seungyong Hong
5758
* @see JobRepository
5859
* @see JobInstanceDao
5960
* @see JobExecutionDao
@@ -109,10 +110,9 @@ public JobExecution createJobExecution(String jobName, JobParameters jobParamete
109110
+ "The last execution ended with a failure that could not be rolled back, "
110111
+ "so it may be dangerous to proceed. Manual intervention is probably necessary.");
111112
}
112-
JobParameters allJobParameters = execution.getJobParameters();
113-
JobParameters identifyingJobParameters = new JobParameters(allJobParameters.getIdentifyingParameters());
114-
if (!identifyingJobParameters.isEmpty()
115-
&& (status == BatchStatus.COMPLETED || status == BatchStatus.ABANDONED)) {
113+
if (status == BatchStatus.COMPLETED || status == BatchStatus.ABANDONED) {
114+
JobParameters identifyingJobParameters = new JobParameters(
115+
execution.getJobParameters().getIdentifyingParameters());
116116
throw new JobInstanceAlreadyCompleteException(
117117
"A job instance already exists and is complete for identifying parameters="
118118
+ identifyingJobParameters + ". If you want to run this job again, "

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/JobStepParserTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
import org.springframework.batch.core.BatchStatus;
2626
import org.springframework.batch.core.job.Job;
2727
import org.springframework.batch.core.job.JobExecution;
28-
import org.springframework.batch.core.job.parameters.JobParameters;
28+
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
2929
import org.springframework.batch.core.step.StepExecution;
3030
import org.springframework.batch.core.repository.JobRepository;
3131
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,7 +35,7 @@
3535
/**
3636
* @author Dave Syer
3737
* @author Mahmoud Ben Hassine
38-
*
38+
* @author Seungyong Hong
3939
*/
4040
@SpringJUnitConfig
4141
class JobStepParserTests {
@@ -54,7 +54,8 @@ class JobStepParserTests {
5454
@Test
5555
void testFlowStep() throws Exception {
5656
assertNotNull(job1);
57-
JobExecution jobExecution = jobRepository.createJobExecution(job1.getName(), new JobParameters());
57+
JobExecution jobExecution = jobRepository.createJobExecution(job1.getName(),
58+
new JobParametersBuilder().addString("name", "foo").toJobParameters());
5859
job1.execute(jobExecution);
5960
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
6061
List<String> stepNames = getStepNames(jobExecution);
@@ -65,7 +66,8 @@ void testFlowStep() throws Exception {
6566
@Test
6667
void testFlowExternalStep() throws Exception {
6768
assertNotNull(job2);
68-
JobExecution jobExecution = jobRepository.createJobExecution(job2.getName(), new JobParameters());
69+
JobExecution jobExecution = jobRepository.createJobExecution(job2.getName(),
70+
new JobParametersBuilder().addString("name", "bar").toJobParameters());
6971
job2.execute(jobExecution);
7072
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
7173
List<String> stepNames = getStepNames(jobExecution);

spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.batch.core.job.parameters.JobParameters;
3939
import org.springframework.batch.core.job.parameters.JobParametersBuilder;
4040
import org.springframework.batch.core.listener.JobExecutionListener;
41+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
4142
import org.springframework.batch.core.step.Step;
4243
import org.springframework.batch.core.step.StepExecution;
4344
import org.springframework.batch.core.observability.BatchJobObservation;
@@ -49,12 +50,7 @@
4950
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
5051
import org.springframework.jdbc.support.JdbcTransactionManager;
5152

52-
import static org.junit.jupiter.api.Assertions.assertEquals;
53-
import static org.junit.jupiter.api.Assertions.assertFalse;
54-
import static org.junit.jupiter.api.Assertions.assertNotNull;
55-
import static org.junit.jupiter.api.Assertions.assertNull;
56-
import static org.junit.jupiter.api.Assertions.assertSame;
57-
import static org.junit.jupiter.api.Assertions.assertTrue;
53+
import static org.junit.jupiter.api.Assertions.*;
5854
import static org.mockito.Mockito.mock;
5955

6056
/**
@@ -64,6 +60,7 @@
6460
* @author Will Schipp
6561
* @author Mahmoud Ben Hassine
6662
* @author Jinwoo Bae
63+
* @author Seungyong Hong
6764
*/
6865
class SimpleJobTests {
6966

@@ -343,11 +340,9 @@ void testStepAlreadyComplete() throws Exception {
343340
jobExecution.setEndTime(LocalDateTime.now());
344341
jobExecution.setStatus(BatchStatus.COMPLETED);
345342
jobRepository.update(jobExecution);
346-
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
347-
job.execute(jobExecution);
348-
assertEquals(0, jobExecution.getFailureExceptions().size());
349-
assertEquals(1, jobExecution.getStepExecutions().size());
350-
assertEquals(stepExecution2.getStepName(), jobExecution.getStepExecutions().iterator().next().getStepName());
343+
assertThrows(JobInstanceAlreadyCompleteException.class, () -> {
344+
jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
345+
});
351346
}
352347

353348
@Test

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Robert Kasanicky
4747
* @author Dimitrios Liapis
4848
* @author Mahmoud Ben Hassine
49+
* @author Seungyong Hong
4950
*/
5051
@SpringJUnitConfig(locations = "/org/springframework/batch/core/repository/dao/jdbc/sql-dao-test.xml")
5152
class SimpleJobRepositoryIntegrationTests {
@@ -100,7 +101,7 @@ void testCreateAndFindWithNoStartDate() throws Exception {
100101
LocalDateTime now = LocalDateTime.now();
101102
firstExecution.setStartTime(now);
102103
firstExecution.setEndTime(now.plus(1, ChronoUnit.SECONDS));
103-
firstExecution.setStatus(BatchStatus.COMPLETED);
104+
firstExecution.setStatus(BatchStatus.STOPPED);
104105
jobRepository.update(firstExecution);
105106
JobExecution secondExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
106107

@@ -203,18 +204,23 @@ void testGetLastJobExecution() throws Exception {
203204
}
204205

205206
/*
206-
* Create two job executions for the same job+parameters tuple. Should ignore
207-
* non-identifying job parameters when identifying the job instance.
207+
* Create two job executions for the same identifying job+parameters tuple. Should
208+
* ignore non-identifying job parameters when identifying the job instance.
208209
*/
209210
@Transactional
210211
@Test
211212
void testReExecuteWithSameJobParameters() throws Exception {
212-
JobParameters jobParameters = new JobParametersBuilder().addString("name", "foo", false).toJobParameters();
213-
JobExecution jobExecution1 = jobRepository.createJobExecution(job.getName(), jobParameters);
213+
JobParameters jobParameters1 = new JobParametersBuilder().addString("name", "foo", true)
214+
.addString("age", "13", false)
215+
.toJobParameters();
216+
JobExecution jobExecution1 = jobRepository.createJobExecution(job.getName(), jobParameters1);
214217
jobExecution1.setStatus(BatchStatus.COMPLETED);
215218
jobExecution1.setEndTime(LocalDateTime.now());
216219
jobRepository.update(jobExecution1);
217-
JobExecution jobExecution2 = jobRepository.createJobExecution(job.getName(), jobParameters);
220+
JobParameters jobParameters2 = new JobParametersBuilder().addString("name", "bar", true)
221+
.addString("age", "13", false)
222+
.toJobParameters();
223+
JobExecution jobExecution2 = jobRepository.createJobExecution(job.getName(), jobParameters2);
218224
assertNotNull(jobExecution1);
219225
assertNotNull(jobExecution2);
220226
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,8 @@
3333

3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.EnumSource;
3638
import org.springframework.batch.core.BatchStatus;
3739
import org.springframework.batch.core.job.JobExecution;
3840
import org.springframework.batch.core.job.JobInstance;
@@ -61,6 +63,7 @@
6163
* @author Baris Cubukcuoglu
6264
* @author Mahmoud Ben Hassine
6365
* @author Parikshit Dutta
66+
* @author Seungyong Hong
6467
*
6568
*/
6669
class SimpleJobRepositoryTests {
@@ -103,7 +106,7 @@ void setUp() {
103106

104107
jobRepository = new SimpleJobRepository(jobInstanceDao, jobExecutionDao, stepExecutionDao, ecDao);
105108

106-
jobParameters = new JobParametersBuilder().addString("bar", "test").toJobParameters();
109+
jobParameters = new JobParametersBuilder().addString("bar", "test", false).toJobParameters();
107110

108111
job = new JobSupport();
109112
job.setBeanName("RepositoryTest");
@@ -289,9 +292,10 @@ void testCreateJobExecutionStatusUnknown() {
289292
assertThrows(JobRestartException.class, () -> jobRepository.createJobExecution("foo", new JobParameters()));
290293
}
291294

292-
@Test
293-
void testCreateJobExecutionAlreadyComplete() {
294-
jobExecution.setStatus(BatchStatus.COMPLETED);
295+
@ParameterizedTest
296+
@EnumSource(mode = EnumSource.Mode.INCLUDE, names = { "COMPLETED", "ABANDONED" })
297+
void testCreateJobExecutionAlreadyComplete(BatchStatus batchStatus) {
298+
jobExecution.setStatus(batchStatus);
295299
jobExecution.setEndTime(LocalDateTime.now());
296300

297301
when(jobInstanceDao.getJobInstance("foo", new JobParameters())).thenReturn(jobInstance);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
/**
4040
* @author Dave Syer
4141
* @author Mahmoud Ben Hassine
42+
* @author Seungyong Hong
4243
*
4344
*/
4445
@SpringJUnitConfig(locations = "/simple-job-launcher-context.xml")
@@ -81,12 +82,14 @@ void testCreateJobExecutions() throws Exception {
8182
void testRemoveJobExecutionsWithSameJobInstance() throws Exception {
8283
utils = new JobRepositoryTestUtils(jobRepository);
8384
List<JobExecution> list = new ArrayList<>();
84-
JobExecution jobExecution = jobRepository.createJobExecution("job", new JobParameters());
85+
JobExecution jobExecution = jobRepository.createJobExecution("job",
86+
new JobParametersBuilder().addString("name", "foo").toJobParameters());
8587
jobExecution.setEndTime(LocalDateTime.now());
8688
jobExecution.setStatus(BatchStatus.COMPLETED);
8789
list.add(jobExecution);
8890
jobRepository.update(jobExecution);
89-
jobExecution = jobRepository.createJobExecution("job", new JobParameters());
91+
jobExecution = jobRepository.createJobExecution("job",
92+
new JobParametersBuilder().addString("name", "bar").toJobParameters());
9093
list.add(jobExecution);
9194
assertEquals(beforeJobs + 2, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
9295
utils.removeJobExecutions(list);

0 commit comments

Comments
 (0)