diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java index 6ff30fdac..328dda516 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java @@ -327,6 +327,7 @@ public class JobExecution extends Entity { */ public void addStepExecutions(List stepExecutions) { if (stepExecutions!=null) { + this.stepExecutions.removeAll(stepExecutions); this.stepExecutions.addAll(stepExecutions); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/MapStepExecutionDaoTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/MapStepExecutionDaoTests.java index db27a4efc..c5546e4b9 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/MapStepExecutionDaoTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/MapStepExecutionDaoTests.java @@ -1,5 +1,7 @@ package org.springframework.batch.core.repository.dao; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import java.util.Date; @@ -7,6 +9,7 @@ import java.util.Date; import org.junit.Test; import org.junit.internal.runners.JUnit4ClassRunner; import org.junit.runner.RunWith; +import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.repository.JobRepository; @@ -48,4 +51,53 @@ public class MapStepExecutionDaoTests extends AbstractStepExecutionDaoTests { assertNull(stored.getEndTime()); } + @Test + public void testAddStepExecutions() { + StepExecutionDao tested = new MapStepExecutionDao(); + + JobExecution jobExecution = new JobExecution(88L); + + // Create step execution with status STARTED + StepExecution stepExecution = new StepExecution("Step one", jobExecution); + stepExecution.setStatus(BatchStatus.STARTED); + + // Save and check id + tested.saveStepExecution(stepExecution); + assertNotNull(stepExecution.getId()); + + // Job execution instance doesn't contain step execution instances + assertEquals(0, jobExecution.getStepExecutions().size()); + + // Load all execution steps and check + tested.addStepExecutions(jobExecution); + assertEquals(1, jobExecution.getStepExecutions().size()); + + // Check the first (and only) step execution instance of the job instance + StepExecution jobStepExecution = jobExecution.getStepExecutions().iterator().next(); + assertEquals(BatchStatus.STARTED, jobStepExecution.getStatus()); + assertEquals(stepExecution.getId(), jobStepExecution.getId()); + + // Load the step execution instance from the repository and check is it the same + StepExecution repoStepExecution = tested.getStepExecution(jobExecution, stepExecution.getId()); + assertEquals(stepExecution.getId(), repoStepExecution.getId()); + assertEquals(BatchStatus.STARTED, repoStepExecution.getStatus()); + + // Update the step execution instance + repoStepExecution.setStatus(BatchStatus.COMPLETED); + + // Update the step execution in the repository and check + tested.updateStepExecution(repoStepExecution); + StepExecution updatedStepExecution = tested.getStepExecution(jobExecution, stepExecution.getId()); + assertEquals(stepExecution.getId(), updatedStepExecution.getId()); + assertEquals(BatchStatus.COMPLETED, updatedStepExecution.getStatus()); + + // Now, add step executions from the repository and check + tested.addStepExecutions(jobExecution); + + jobStepExecution = jobExecution.getStepExecutions().iterator().next(); + assertEquals(1, jobExecution.getStepExecutions().size()); + assertEquals(stepExecution.getId(), jobStepExecution.getId()); + assertEquals(BatchStatus.COMPLETED, jobStepExecution.getStatus()); + } + }