From ea7a704299bbfaa13ad171e02845fbfd5881bc2f Mon Sep 17 00:00:00 2001 From: Chris Schaefer Date: Sun, 12 Jan 2014 13:45:15 -0500 Subject: [PATCH] Add failed transition if none exists when next attribute is used. Fixes TCK test testStartLimitVariation1 --- .../jsr/configuration/xml/FlowParser.java | 12 +++++++ .../configuration/xml/FlowParserTests.java | 34 +++++++++++++++++++ ...iledTransitionWhenNextAttributePresent.xml | 18 ++++++++++ ...enNextAndFailedTransitionElementExists.xml | 18 ++++++++++ 4 files changed, 82 insertions(+) create mode 100644 spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepGetsFailedTransitionWhenNextAttributePresent.xml create mode 100644 spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepNoOverrideWhenNextAndFailedTransitionElementExists.xml diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/FlowParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/FlowParser.java index 36264ad13..3b20f5375 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/FlowParser.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/FlowParser.java @@ -148,10 +148,13 @@ public class FlowParser extends AbstractFlowParser { Collection list = new ArrayList(); boolean transitionElementExists = false; + boolean failedTransitionElementExists = false; + List childElements = DomUtils.getChildElements(element); for(Element childElement : childElements) { if(isChildElementTransitionElement(childElement)) { list.addAll(parseTransitionElement(childElement, stepId, stateDef, parserContext)); + failedTransitionElementExists = failedTransitionElementExists || hasFailedTransitionElement(childElement); transitionElementExists = true; } } @@ -171,6 +174,11 @@ public class FlowParser extends AbstractFlowParser { } if (hasNextAttribute) { + if (transitionElementExists && !failedTransitionElementExists) { + list.addAll(createTransition(FlowExecutionStatus.FAILED, FlowExecutionStatus.FAILED.getName(), null, null, + stateDef, parserContext, false)); + } + list.add(getStateTransitionReference(parserContext, stateDef, null, shortNextAttribute)); } @@ -181,6 +189,10 @@ public class FlowParser extends AbstractFlowParser { return TRANSITION_TYPES.contains(childElement.getLocalName()); } + private static boolean hasFailedTransitionElement(Element childName) { + return FAIL_ELE.equals(childName.getLocalName()); + } + protected static Collection parseTransitionElement(Element transitionElement, String stateId, BeanDefinition stateDef, ParserContext parserContext) { FlowExecutionStatus status = getBatchStatusFromEndTransitionName(transitionElement.getNodeName()); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/FlowParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/FlowParserTests.java index c14b81785..2aa6c963c 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/FlowParserTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/FlowParserTests.java @@ -64,6 +64,32 @@ public class FlowParserTests { assertTrue("step1".equals(failedStep.getStepName())); } + @Test + public void testStepGetsFailedTransitionWhenNextAttributePresent() throws Exception { + JobExecution jobExecution = runJob("FlowParserTestsStepGetsFailedTransitionWhenNextAttributePresent", new Properties(), 10000l); + assertEquals(ExitStatus.FAILED.getExitCode(), jobExecution.getExitStatus()); + + JobOperator jobOperator = BatchRuntime.getJobOperator(); + List stepExecutions = jobOperator.getStepExecutions(jobExecution.getExecutionId()); + assertEquals(1, stepExecutions.size()); + StepExecution failedStep = stepExecutions.get(0); + assertTrue("failedExitStatusStep".equals(failedStep.getStepName())); + assertTrue("FAILED".equals(failedStep.getExitStatus())); + } + + @Test + public void testStepNoOverrideWhenNextAndFailedTransitionElementExists() throws Exception { + JobExecution jobExecution = runJob("FlowParserTestsStepNoOverrideWhenNextAndFailedTransitionElementExists", new Properties(), 10000l); + assertEquals(ExitStatus.FAILED.getExitCode(), jobExecution.getExitStatus()); + + JobOperator jobOperator = BatchRuntime.getJobOperator(); + List stepExecutions = jobOperator.getStepExecutions(jobExecution.getExecutionId()); + assertEquals(1, stepExecutions.size()); + StepExecution failedStep = stepExecutions.get(0); + assertTrue("failedExitStatusStepDontOverride".equals(failedStep.getStepName())); + assertTrue("CUSTOM_FAIL".equals(failedStep.getExitStatus())); + } + public static class TestBatchlet extends AbstractBatchlet { private static int CNT; @@ -79,6 +105,14 @@ public class FlowParserTests { CNT++; } + if("failedExitStatusStep".equals(stepContext.getStepName())) { + exitCode = "FAILED"; + } + + if("failedExitStatusStepDontOverride".equals(stepContext.getStepName())) { + exitCode = "CUSTOM_FAIL"; + } + return exitCode; } } diff --git a/spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepGetsFailedTransitionWhenNextAttributePresent.xml b/spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepGetsFailedTransitionWhenNextAttributePresent.xml new file mode 100644 index 000000000..9be73644f --- /dev/null +++ b/spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepGetsFailedTransitionWhenNextAttributePresent.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + diff --git a/spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepNoOverrideWhenNextAndFailedTransitionElementExists.xml b/spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepNoOverrideWhenNextAndFailedTransitionElementExists.xml new file mode 100644 index 000000000..7bceb18df --- /dev/null +++ b/spring-batch-core/src/test/resources/META-INF/batch-jobs/FlowParserTestsStepNoOverrideWhenNextAndFailedTransitionElementExists.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + +