diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java index 7b5df7f23..719281fc3 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java @@ -238,13 +238,9 @@ public class SimpleJobRepository implements JobRepository { Assert.notNull(stepExecution, "StepExecution cannot be null."); Assert.notNull(stepExecution.getStepName(), "StepExecution's step name cannot be null."); - + Assert.notNull(stepExecution.getJobExecutionId(), "StepExecution must belong to persisted JobExecution"); + if (stepExecution.getId() == null) { - // new execution, obtain id and insert - JobExecution jobExecution = stepExecution.getJobExecution(); - if (jobExecution.getId() == null) { - jobExecutionDao.saveJobExecution(jobExecution); - } stepExecutionDao.saveStepExecution(stepExecution); } else { diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java index 065b86981..53854c7da 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java @@ -318,50 +318,6 @@ public class SimpleJobRepositoryTests extends TestCase { jobRepository.saveOrUpdate(jobExecution); } - public void testUpdateStepExecution() { - StepExecution stepExecution = new StepExecution(new StepSupport("stepName"), null, new Long(1)); - stepExecution.setId(new Long(11)); - ExecutionContext executionContext = new ExecutionContext(); - stepExecution.setExecutionContext(executionContext); - stepExecutionDao.updateStepExecution(stepExecution); - stepExecutionDaoControl.replay(); - jobRepository.saveOrUpdate(stepExecution); - stepExecutionDaoControl.verify(); - } - - public void testUpdateExecutionContext() { - StepExecution stepExecution = new StepExecution(new StepSupport("stepName"), null, new Long(1)); - stepExecution.setId(new Long(11)); - ExecutionContext executionContext = new ExecutionContext(); - stepExecution.setExecutionContext(executionContext); - stepExecutionDao.updateStepExecution(stepExecution); - stepExecutionDao.saveOrUpdateExecutionContext(stepExecution); - stepExecutionDaoControl.replay(); - jobRepository.saveOrUpdateExecutionContext(stepExecution); - stepExecutionDaoControl.verify(); - } - - public void testSaveExistingStepExecution() { - StepExecution stepExecution = new StepExecution(new StepSupport("stepName"), new JobExecution(null), null); - ExecutionContext executionContext = new ExecutionContext(); - stepExecution.setExecutionContext(executionContext); - stepExecutionDao.saveStepExecution(stepExecution); - stepExecutionDaoControl.replay(); - jobRepository.saveOrUpdate(stepExecution); - stepExecutionDaoControl.verify(); - } - - public void testSaveExistingExecutionContext() { - StepExecution stepExecution = new StepExecution(new StepSupport("stepName"), new JobExecution(null), null); - ExecutionContext executionContext = new ExecutionContext(); - stepExecution.setExecutionContext(executionContext); - stepExecutionDao.saveStepExecution(stepExecution); - stepExecutionDao.saveOrUpdateExecutionContext(stepExecution); - stepExecutionDaoControl.replay(); - jobRepository.saveOrUpdateExecutionContext(stepExecution); - stepExecutionDaoControl.verify(); - } - public void testSaveOrUpdateStepExecutionException() { StepExecution stepExecution = new StepExecution(new StepSupport("stepName"), null, null); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java index 1dbc8d5f1..97febd172 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java @@ -144,8 +144,8 @@ public class ItemOrientedStepTests extends TestCase { SimpleJobRepository repository = new SimpleJobRepository(new MapJobInstanceDao(), new MapJobExecutionDao(), new MapStepExecutionDao()); itemOrientedStep.setJobRepository(repository); - JobExecution jobExecutionContext = new JobExecution(jobInstance); - StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecutionContext); + JobExecution jobExecution = repository.createJobExecution(jobInstance.getJob(), jobInstance.getJobParameters()); + StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecution); itemOrientedStep.execute(stepExecution); assertEquals(1, processed.size()); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StepExecutorInterruptionTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StepExecutorInterruptionTests.java index 72f1451ba..c43928799 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StepExecutorInterruptionTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/StepExecutorInterruptionTests.java @@ -20,7 +20,6 @@ import junit.framework.TestCase; import org.springframework.batch.core.domain.BatchStatus; import org.springframework.batch.core.domain.JobExecution; -import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobInterruptedException; import org.springframework.batch.core.domain.JobParameters; import org.springframework.batch.core.domain.StepExecution; @@ -52,6 +51,8 @@ public class StepExecutorInterruptionTests extends TestCase { private StepExecutionDao stepExecutionDao = new MapStepExecutionDao(); private ItemOrientedStep step; + + private JobExecution jobExecution; public void setUp() throws Exception { MapJobInstanceDao.clear(); @@ -64,7 +65,7 @@ public class StepExecutorInterruptionTests extends TestCase { step = new ItemOrientedStep("interruptedStep"); jobConfiguration.addStep(step); jobConfiguration.setBeanName("testJob"); - jobRepository.createJobExecution(jobConfiguration, new JobParameters()); + jobExecution = jobRepository.createJobExecution(jobConfiguration, new JobParameters()); step.setJobRepository(jobRepository); step.setTransactionManager(new ResourcelessTransactionManager()); step.setItemReader(new ItemReaderAdapter()); @@ -76,8 +77,7 @@ public class StepExecutorInterruptionTests extends TestCase { public void testInterruptChunk() throws Exception { - JobExecution jobExecutionContext = new JobExecution(new JobInstance(new Long(0L), new JobParameters(), new JobSupport("testJob"))); - final StepExecution stepExecution = new StepExecution(step, jobExecutionContext); + final StepExecution stepExecution = new StepExecution(step, jobExecution); step.setItemReader(new AbstractItemReader() { public Object read() throws Exception { // do something non-trivial (and not Thread.sleep())