diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java index 5090c600c..603db6866 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java @@ -212,7 +212,7 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw catch (Throwable e) { logger.error("Encountered an error executing the step: " + e.getClass() + ": " + e.getMessage(), e); stepExecution.setStatus(determineBatchStatus(e)); - exitStatus = getDefaultExitStatusForFailure(e); + exitStatus = exitStatus.and(getDefaultExitStatusForFailure(e)); stepExecution.addFailureException(e); } finally { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/AbstractStepTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/NonAbstractStepTests.java similarity index 88% rename from spring-batch-core/src/test/java/org/springframework/batch/core/step/AbstractStepTests.java rename to spring-batch-core/src/test/java/org/springframework/batch/core/step/NonAbstractStepTests.java index f5c107460..72c78798f 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/AbstractStepTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/NonAbstractStepTests.java @@ -24,7 +24,7 @@ import org.springframework.util.Assert; /** * Tests for {@link AbstractStep}. */ -public class AbstractStepTests { +public class NonAbstractStepTests { AbstractStep tested = new EventTrackingStep(); @@ -188,9 +188,6 @@ public class AbstractStepTests { .containsKey("afterStep")); } - /** - * Exception during business processing. - */ @Test public void testFailure() throws Exception { tested = new EventTrackingStep() { @@ -225,7 +222,6 @@ public class AbstractStepTests { assertTrue("Execution context modifications made by listener should be persisted", repository.saved .containsKey("afterStep")); } - /** * Exception during business processing. */ @@ -262,6 +258,30 @@ public class AbstractStepTests { .containsKey("afterStep")); } + @Test + public void testStoppedStepWithCustomStatus() throws Exception { + tested = new EventTrackingStep() { + @Override + protected void doExecute(StepExecution context) throws Exception { + super.doExecute(context); + context.setTerminateOnly(); + context.setExitStatus(new ExitStatus("FUNNY")); + } + }; + tested.setJobRepository(repository); + tested.setStepExecutionListeners(new StepExecutionListener[] { listener1, listener2 }); + + tested.execute(execution); + assertEquals(BatchStatus.STOPPED, execution.getStatus()); + Throwable expected = execution.getFailureExceptions().get(0); + assertEquals("JobExecution interrupted.", expected.getMessage()); + + assertEquals("FUNNY", execution.getExitStatus().getExitCode()); + + assertTrue("Execution context modifications made by listener should be persisted", repository.saved + .containsKey("afterStep")); + } + /** * Exception during business processing. */