RESOLVED - issue BATCH-529: StepExecutionListener.afterStep() should only be called on success

RESOLVED - issue BATCH-532: StepExecutionListener can influence exit status but not status of step execution

listener.afterStep() call moved from catch block to the end of the try block
This commit is contained in:
robokaso
2008-04-01 11:07:53 +00:00
parent 25281b236e
commit 7746cfc304
2 changed files with 34 additions and 13 deletions

View File

@@ -278,7 +278,7 @@ public class ItemOrientedStepTests extends TestCase {
}
});
itemOrientedStep.execute(stepExecution);
// context saved before processing starts and updated at the end
assertEquals(2, list.size());
}
@@ -288,9 +288,11 @@ public class ItemOrientedStepTests extends TestCase {
final StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecution);
itemOrientedStep.setJobRepository(new JobRepositorySupport() {
private int counter = 0;
// initial save before item processing succeeds, later calls fail
public void saveOrUpdateExecutionContext(StepExecution stepExecution) {
if (counter > 0) throw new RuntimeException("foo");
if (counter > 0)
throw new RuntimeException("foo");
counter++;
}
});
@@ -744,7 +746,7 @@ public class ItemOrientedStepTests extends TestCase {
/**
* Execution context must not be left empty even if job failed before
* commiting first chunk - otherwise ItemStreams won't recognize it is
* restart scenario on next run.
* restart scenario on next run.
*/
public void testRestartAfterFailureInFirstChunk() throws Exception {
MockRestartableItemReader reader = new MockRestartableItemReader() {
@@ -755,15 +757,14 @@ public class ItemOrientedStepTests extends TestCase {
};
itemOrientedStep.setItemHandler(new SimpleItemHandler(reader, itemWriter));
itemOrientedStep.registerStream(reader);
StepExecution stepExecution = new StepExecution(itemOrientedStep, new JobExecution(jobInstance));
try {
itemOrientedStep.execute(stepExecution);
fail("Expected InfrastructureException");
}
catch (RuntimeException expected) {
// The job actually completed, but the streams couldn't be closed.
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals("CRASH!", expected.getMessage());
assertFalse(stepExecution.getExecutionContext().isEmpty());
@@ -771,6 +772,31 @@ public class ItemOrientedStepTests extends TestCase {
}
}
/**
* Exception in {@link StepExecutionListener#afterStep(StepExecution)}
* causes step to fail.
* @throws JobInterruptedException
*/
public void testStepFailureInAfterStepCallback() throws JobInterruptedException {
StepExecutionListener listener = new StepExecutionListenerSupport() {
public ExitStatus afterStep(StepExecution stepExecution) {
throw new RuntimeException("exception thrown in afterStep to signal failure");
}
};
itemOrientedStep.setStepExecutionListeners(new StepExecutionListener[] { listener });
StepExecution stepExecution = new StepExecution(itemOrientedStep, new JobExecution(jobInstance));
try {
itemOrientedStep.execute(stepExecution);
fail();
}
catch (RuntimeException expected) {
assertEquals("exception thrown in afterStep to signal failure", expected.getMessage());
}
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
}
private boolean contains(String str, String searchStr) {
return str.indexOf(searchStr) != -1;
}