Reset machine with null context

- Modify reset feature so that null context
  will take machine back to its initial state
  and empty extended state.
- Fixes #166
This commit is contained in:
Janne Valkealahti
2016-01-26 09:09:30 +00:00
parent f6079c8a94
commit a952714857
2 changed files with 31 additions and 0 deletions

View File

@@ -494,6 +494,9 @@ public abstract class AbstractStateMachine<S, E> extends StateMachineObjectSuppo
@Override
public void resetStateMachine(StateMachineContext<S, E> stateMachineContext) {
if (stateMachineContext == null) {
log.info("Got null context, resetting to initial state and clearing extended state");
currentState = initialState;
extendedState.getVariables().clear();
return;
}
if (log.isDebugEnabled()) {

View File

@@ -223,6 +223,34 @@ public class StateMachineResetTests extends AbstractStateMachineTests {
assertThat((Integer)machine.getExtendedState().getVariables().get("count"), is(2));
}
@Test
public void testResetWithNullContext() throws Exception {
context.register(Config1.class);
context.refresh();
@SuppressWarnings("unchecked")
StateMachine<States, Events> machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
machine.start();
assertThat(machine.getState().getIds(), containsInAnyOrder(States.S0, States.S1, States.S11));
assertThat((Integer)machine.getExtendedState().getVariables().get("foo"), is(0));
machine.sendEvent(Events.I);
assertThat(machine.getState().getIds(), containsInAnyOrder(States.S0, States.S1, States.S12));
assertThat((Integer)machine.getExtendedState().getVariables().get("foo"), is(0));
machine.stop();
machine.getStateMachineAccessor().doWithAllRegions(new StateMachineFunction<StateMachineAccess<States,Events>>() {
@Override
public void apply(StateMachineAccess<States, Events> function) {
function.resetStateMachine(null);
}
});
machine.start();
assertThat(machine.getState().getIds(), containsInAnyOrder(States.S0, States.S1, States.S11));
assertThat(machine.getExtendedState().getVariables().size(), is(0));
}
@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<States, Events> {