/** * Locate start state and pre-populate data structures needed for execution. * * @see InitializingBean#afterPropertiesSet() */ @Override public void afterPropertiesSet() throws Exception { if (startState == null) { initializeTransitions(); } }
/** * Create a new end state {@link StateTransition} specification. This * transition explicitly goes unconditionally to an end state (i.e. no more * executions). * * @param state the {@link State} used to generate the outcome for this * transition * @return {@link StateTransition} that was created. */ public static StateTransition createEndStateTransition(State state) { return createStateTransition(state, null, null); }
/** * @see Flow#start(FlowExecutor) */ @Override public FlowExecution start(FlowExecutor executor) throws FlowExecutionException { if (startState == null) { initializeTransitions(); } State state = startState; String stateName = state.getName(); return resume(stateName, executor); }
@Test public void testUnconnectedSteps() throws Exception { flow.setStateTransitions(collect(StateTransition.createEndStateTransition(new StubState("step1")), StateTransition.createEndStateTransition(new StubState("step2")))); flow.afterPropertiesSet(); FlowExecution execution = flow.start(executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step1", execution.getName()); }
@Test public void testBranching() throws Exception { flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "step2"), StateTransition.createStateTransition(new StubState("step1"), ExitStatus.COMPLETED.getExitCode(), "step3"), StateTransition.createEndStateTransition(new StubState("step2")), StateTransition .createEndStateTransition(new StubState("step3")))); flow.setStateTransitionComparator(new DefaultStateTransitionComparator()); flow.afterPropertiesSet(); FlowExecution execution = flow.start(executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step3", execution.getName()); }
@Test public void testResume() throws Exception { flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "step2"), StateTransition.createEndStateTransition(new StubState("step2")))); flow.afterPropertiesSet(); FlowExecution execution = flow.resume("step2", executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step2", execution.getName()); }
@Test public void testGetStateDoesNotExist() throws Exception { flow.setStateTransitions(Collections.singletonList(StateTransition.createEndStateTransition(new StubState( "step1")))); flow.afterPropertiesSet(); State state = flow.getState("bar"); assertNull(state); }
@Test(expected = IllegalArgumentException.class) public void testNoNextStepSpecified() throws Exception { flow.setStateTransitions(Collections.singletonList(StateTransition.createStateTransition(new StateSupport( "step"), "foo"))); flow.afterPropertiesSet(); }
@Override protected boolean isFlowContinued(State state, FlowExecutionStatus status, StepExecution stepExecution) { if(state instanceof DelegateState) { state = ((DelegateState) state).getState(); } if(state instanceof JsrStepState) { currentStep = (JsrStepState) state; } return super.isFlowContinued(state, status, stepExecution); }
@Before public void setUp() throws Exception { comparator = new DefaultStateTransitionComparator(); }
@Test public void testTwoSteps() throws Exception { flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "step2"), StateTransition.createEndStateTransition(new StubState("step2")))); flow.afterPropertiesSet(); FlowExecution execution = flow.start(executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step2", execution.getName()); }
@Test public void testGetStateExists() throws Exception { flow.setStateTransitions(Collections.singletonList(StateTransition.createEndStateTransition(new StubState( "step1")))); flow.afterPropertiesSet(); State state = flow.getState("step1"); assertNotNull(state); assertEquals("step1", state.getName()); }
/** * Create a new state {@link StateTransition} specification with a wildcard * pattern that matches all outcomes. * * @param state the {@link State} used to generate the outcome for this * transition * @param next the name of the next {@link State} to execute * @return {@link StateTransition} that was created. */ public static StateTransition createStateTransition(State state, String next) { return createStateTransition(state, null, next); }
@Test public void testFailedStep() throws Exception { flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1") { @Override public FlowExecutionStatus handle(FlowExecutor executor) { return FlowExecutionStatus.FAILED; } }, "step2"), StateTransition.createEndStateTransition(new StubState("step2")))); flow.afterPropertiesSet(); FlowExecution execution = flow.start(executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step2", execution.getName()); }
/** * Convenience method to switch the origin and destination of a transition, * creating a new instance. * * @param stateTransition an existing state transition * @param state the new state for the origin * @param next the new name for the destination * * @return {@link StateTransition} that was created. */ public static StateTransition switchOriginAndDestination(StateTransition stateTransition, State state, String next) { return createStateTransition(state, stateTransition.pattern, next); }
@Test public void testNoMatchForNextStep() throws Exception { flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step1"), "FOO", "step2"), StateTransition.createEndStateTransition(new StubState("step2")))); flow.afterPropertiesSet(); try { flow.start(executor); fail("Expected JobExecutionException"); } catch (FlowExecutionException e) { // expected String message = e.getMessage(); assertTrue("Wrong message: " + message, message.toLowerCase().contains("next state not found")); } }
/** * Create a new end state {@link StateTransition} specification. This * transition explicitly goes to an end state (i.e. no more processing) if * the outcome matches the pattern. * * @param state the {@link State} used to generate the outcome for this * transition * @param pattern the pattern to match in the exit status of the * {@link State} * @return {@link StateTransition} that was created. */ public static StateTransition createEndStateTransition(State state, String pattern) { return createStateTransition(state, pattern, null); }
@Test public void testExplicitStartStep() throws Exception { flow.setStateTransitions(collect(StateTransition.createStateTransition(new StubState("step"), ExitStatus.FAILED.getExitCode(), "step"), StateTransition.createEndStateTransition(new StubState("step")))); flow.afterPropertiesSet(); FlowExecution execution = flow.start(executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step", execution.getName()); }
@Test public void testOneStep() throws Exception { flow.setStateTransitions(Collections.singletonList(StateTransition.createEndStateTransition(new StubState( "step1")))); flow.afterPropertiesSet(); FlowExecution execution = flow.start(executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step1", execution.getName()); }
@Test public void testStepLoop() throws Exception { flow.setStateTransitions(collect(StateTransition.createStateTransition(new StateSupport("step"), ExitStatus.FAILED.getExitCode(), "step"), StateTransition.createEndStateTransition(new StateSupport("step")))); flow.afterPropertiesSet(); FlowExecution execution = flow.start(executor); assertEquals(FlowExecutionStatus.COMPLETED, execution.getStatus()); assertEquals("step", execution.getName()); }