Skip to content

Commit

Permalink
Fix object graph deletion in SimpleJobRepository#deleteJobExecution
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmbenhassine committed Feb 20, 2023
1 parent 4ea5438 commit 4629294
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
Expand Down Expand Up @@ -320,6 +320,9 @@ public void deleteStepExecution(StepExecution stepExecution) {
public void deleteJobExecution(JobExecution jobExecution) {
this.ecDao.deleteExecutionContext(jobExecution);
this.jobExecutionDao.deleteJobExecutionParameters(jobExecution);
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
deleteStepExecution(stepExecution);
}
this.jobExecutionDao.deleteJobExecution(jobExecution);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
Expand Down Expand Up @@ -51,9 +51,9 @@
import org.springframework.batch.core.step.StepSupport;

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

@Test
void testDeleteJobExecution() {
// given
StepExecution stepExecution1 = mock(StepExecution.class);
StepExecution stepExecution2 = mock(StepExecution.class);
JobExecution jobExecution = mock(JobExecution.class);
when(jobExecution.getStepExecutions()).thenReturn(Arrays.asList(stepExecution1, stepExecution2));

// when
this.jobRepository.deleteJobExecution(jobExecution);

// then
verify(this.ecDao).deleteExecutionContext(jobExecution);
verify(this.jobExecutionDao).deleteJobExecutionParameters(jobExecution);
verify(this.ecDao).deleteExecutionContext(stepExecution1);
verify(this.stepExecutionDao).deleteStepExecution(stepExecution1);
verify(this.ecDao).deleteExecutionContext(stepExecution2);
verify(this.stepExecutionDao).deleteStepExecution(stepExecution2);
verify(this.jobExecutionDao).deleteJobExecution(jobExecution);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
Expand Down Expand Up @@ -142,13 +142,10 @@ public void removeJobExecutions(Collection<JobExecution> jobExecutions) {

/**
* Remove the {@link JobExecution} and its associated {@link StepExecution} instances
* Ôfrom the standard locations used by Spring Batch.
* from the standard locations used by Spring Batch.
* @param jobExecution the {@link JobExecution} to delete
*/
public void removeJobExecution(JobExecution jobExecution) {
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
this.jobRepository.deleteStepExecution(stepExecution);
}
this.jobRepository.deleteJobExecution(jobExecution);
}

Expand Down

0 comments on commit 4629294

Please sign in to comment.