|
33 | 33 | import org.springframework.statemachine.access.StateMachineFunction;
|
34 | 34 | import org.springframework.statemachine.action.Action;
|
35 | 35 | import org.springframework.statemachine.config.EnableStateMachine;
|
| 36 | +import org.springframework.statemachine.config.EnableStateMachineFactory; |
36 | 37 | import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
| 38 | +import org.springframework.statemachine.config.StateMachineFactory; |
37 | 39 | import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
|
38 | 40 | import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
39 | 41 | import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
@@ -251,6 +253,31 @@ public void apply(StateMachineAccess<States, Events> function) {
|
251 | 253 | assertThat(machine.getExtendedState().getVariables().size(), is(0));
|
252 | 254 | }
|
253 | 255 |
|
| 256 | + @Test |
| 257 | + public void testRestoreWithTimer() throws Exception { |
| 258 | + context.register(Config4.class); |
| 259 | + context.refresh(); |
| 260 | + @SuppressWarnings("unchecked") |
| 261 | + StateMachineFactory<States, Events> factory = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINEFACTORY, |
| 262 | + StateMachineFactory.class); |
| 263 | + StateMachine<States, Events> machine = factory.getStateMachine(); |
| 264 | + |
| 265 | + DefaultStateMachineContext<States, Events> stateMachineContext = new DefaultStateMachineContext<States, Events>(States.S1, null, |
| 266 | + null, null); |
| 267 | + machine.getStateMachineAccessor().doWithAllRegions(new StateMachineFunction<StateMachineAccess<States,Events>>() { |
| 268 | + |
| 269 | + @Override |
| 270 | + public void apply(StateMachineAccess<States, Events> function) { |
| 271 | + function.resetStateMachine(stateMachineContext); |
| 272 | + } |
| 273 | + }); |
| 274 | + |
| 275 | + machine.start(); |
| 276 | + Thread.sleep(1100); |
| 277 | + assertThat(machine.getState().getIds(), containsInAnyOrder(States.S2)); |
| 278 | + |
| 279 | + } |
| 280 | + |
254 | 281 | @Configuration
|
255 | 282 | @EnableStateMachine
|
256 | 283 | static class Config1 extends EnumStateMachineConfigurerAdapter<States, Events> {
|
@@ -420,6 +447,36 @@ public void execute(StateContext<States, Events> context) {
|
420 | 447 |
|
421 | 448 | }
|
422 | 449 |
|
| 450 | + @Configuration |
| 451 | + @EnableStateMachineFactory |
| 452 | + static class Config4 extends EnumStateMachineConfigurerAdapter<States, Events> { |
| 453 | + |
| 454 | + @Override |
| 455 | + public void configure(StateMachineStateConfigurer<States, Events> states) |
| 456 | + throws Exception { |
| 457 | + states |
| 458 | + .withStates() |
| 459 | + .initial(States.S0) |
| 460 | + .state(States.S1) |
| 461 | + .state(States.S2); |
| 462 | + } |
| 463 | + |
| 464 | + @Override |
| 465 | + public void configure(StateMachineTransitionConfigurer<States, Events> transitions) |
| 466 | + throws Exception { |
| 467 | + transitions |
| 468 | + .withExternal() |
| 469 | + .source(States.S0) |
| 470 | + .target(States.S1) |
| 471 | + .event(Events.A) |
| 472 | + .and() |
| 473 | + .withExternal() |
| 474 | + .source(States.S1) |
| 475 | + .target(States.S2) |
| 476 | + .timerOnce(1000); |
| 477 | + } |
| 478 | + } |
| 479 | + |
423 | 480 | public static enum States {
|
424 | 481 | S0, S1, S11, S12, S2, S21, S211, S212
|
425 | 482 | }
|
|
0 commit comments