diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java index 8559fc4c3..f39794595 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ExecutionContextPromotionListener.java @@ -57,7 +57,9 @@ public class ExecutionContextPromotionListener extends StepExecutionListenerSupp Assert.isTrue(stepContext.containsKey(key), "The key [" + key + "] was not found in the Step's ExecutionContext."); } - jobContext.put(key, stepContext.get(key)); + if (stepContext.containsKey(key)) { + jobContext.put(key, stepContext.get(key)); + } } break; } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java index c8abe433b..9b92ba69e 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/ExecutionContextPromotionListenerTests.java @@ -35,7 +35,7 @@ public class ExecutionContextPromotionListenerTests { * EXPECTED: key is promoted. key2 is not. */ @Test - public void promoteEntry_nullStatuses() throws Exception { + public void promoteEntryNullStatuses() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); @@ -64,7 +64,7 @@ public class ExecutionContextPromotionListenerTests { * EXPECTED: key is promoted. key2 is not. */ @Test - public void promoteEntry_statusFound() throws Exception { + public void promoteEntryStatusFound() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); @@ -94,7 +94,7 @@ public class ExecutionContextPromotionListenerTests { * EXPECTED: no promotions. */ @Test - public void promoteEntry_statusNotFound() throws Exception { + public void promoteEntryStatusNotFound() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); @@ -124,7 +124,7 @@ public class ExecutionContextPromotionListenerTests { * EXPECTED: key is promoted. key2 is not. */ @Test - public void promoteEntry_statusWildcardFound() throws Exception { + public void promoteEntryStatusWildcardFound() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); @@ -153,7 +153,7 @@ public class ExecutionContextPromotionListenerTests { * EXPECTED: key is promoted. key2 is not. */ @Test - public void promoteEntries_keyNotFound() throws Exception { + public void promoteEntriesKeyNotFound() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); @@ -174,6 +174,32 @@ public class ExecutionContextPromotionListenerTests { assertFalse(jobExecution.getExecutionContext().containsKey(key2)); } + /** + * CONDITION: keys = {key}. key is already in job but not in step. + * + * EXPECTED: key is not erased. + */ + @Test + public void promoteEntriesKeyNotFoundInStep() throws Exception { + ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); + + JobExecution jobExecution = new JobExecution(1L); + StepExecution stepExecution = jobExecution.createStepExecution("step1"); + stepExecution.setExitStatus(ExitStatus.COMPLETED); + + Assert.state(jobExecution.getExecutionContext().isEmpty()); + Assert.state(stepExecution.getExecutionContext().isEmpty()); + + jobExecution.getExecutionContext().putString(key, value); + + listener.setKeys(new String[] { key }); + listener.afterPropertiesSet(); + + listener.afterStep(stepExecution); + + assertEquals(value, jobExecution.getExecutionContext().getString(key)); + } + /** * CONDITION: strict = true. keys = {key, key2}. Only {key} exists in the * ExecutionContext. @@ -181,7 +207,7 @@ public class ExecutionContextPromotionListenerTests { * EXPECTED: IllegalArgumentException */ @Test(expected = IllegalArgumentException.class) - public void promoteEntries_keyNotFound_strict() throws Exception { + public void promoteEntriesKeyNotFoundStrict() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); listener.setStrict(true);