Skip to content

Commit 4629294

Browse files
committed
Fix object graph deletion in SimpleJobRepository#deleteJobExecution
Before this commit, SimpleJobRepository#deleteJobExecution did not delete associated step executions as specified in the contract of the method. This commit fixes the implementation to delete the entire object graph as specified. Resolves #4249
1 parent 4ea5438 commit 4629294

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

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

Lines changed: 4 additions & 1 deletion
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-2023 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.
@@ -320,6 +320,9 @@ public void deleteStepExecution(StepExecution stepExecution) {
320320
public void deleteJobExecution(JobExecution jobExecution) {
321321
this.ecDao.deleteExecutionContext(jobExecution);
322322
this.jobExecutionDao.deleteJobExecutionParameters(jobExecution);
323+
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
324+
deleteStepExecution(stepExecution);
325+
}
323326
this.jobExecutionDao.deleteJobExecution(jobExecution);
324327
}
325328

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

Lines changed: 25 additions & 4 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-2023 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.
@@ -51,9 +51,9 @@
5151
import org.springframework.batch.core.step.StepSupport;
5252

5353
/**
54-
* Test SimpleJobRepository. The majority of test cases are tested using EasyMock,
55-
* however, there were some issues with using it for the stepExecutionDao when testing
56-
* finding or creating steps, so an actual mock class had to be written.
54+
* Test SimpleJobRepository. The majority of test cases are tested using Mockito, however,
55+
* there were some issues with using it for the stepExecutionDao when testing finding or
56+
* creating steps, so an actual mock class had to be written.
5757
*
5858
* @author Lucas Ward
5959
* @author Will Schipp
@@ -345,4 +345,25 @@ public void testGetJobInstanceWithNameAndParameters() {
345345
assertEquals(this.jobInstance, jobInstance);
346346
}
347347

348+
@Test
349+
void testDeleteJobExecution() {
350+
// given
351+
StepExecution stepExecution1 = mock(StepExecution.class);
352+
StepExecution stepExecution2 = mock(StepExecution.class);
353+
JobExecution jobExecution = mock(JobExecution.class);
354+
when(jobExecution.getStepExecutions()).thenReturn(Arrays.asList(stepExecution1, stepExecution2));
355+
356+
// when
357+
this.jobRepository.deleteJobExecution(jobExecution);
358+
359+
// then
360+
verify(this.ecDao).deleteExecutionContext(jobExecution);
361+
verify(this.jobExecutionDao).deleteJobExecutionParameters(jobExecution);
362+
verify(this.ecDao).deleteExecutionContext(stepExecution1);
363+
verify(this.stepExecutionDao).deleteStepExecution(stepExecution1);
364+
verify(this.ecDao).deleteExecutionContext(stepExecution2);
365+
verify(this.stepExecutionDao).deleteStepExecution(stepExecution2);
366+
verify(this.jobExecutionDao).deleteJobExecution(jobExecution);
367+
}
368+
348369
}

spring-batch-test/src/main/java/org/springframework/batch/test/JobRepositoryTestUtils.java

Lines changed: 2 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-2023 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.
@@ -142,13 +142,10 @@ public void removeJobExecutions(Collection<JobExecution> jobExecutions) {
142142

143143
/**
144144
* Remove the {@link JobExecution} and its associated {@link StepExecution} instances
145-
* Ôfrom the standard locations used by Spring Batch.
145+
* from the standard locations used by Spring Batch.
146146
* @param jobExecution the {@link JobExecution} to delete
147147
*/
148148
public void removeJobExecution(JobExecution jobExecution) {
149-
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
150-
this.jobRepository.deleteStepExecution(stepExecution);
151-
}
152149
this.jobRepository.deleteJobExecution(jobExecution);
153150
}
154151

0 commit comments

Comments
 (0)