Fix machine autostart with annotation config

- This seem to relate with machine build from an annotation
  config which is then used as a session scoped bean.
- Fix autostart usage where appropriate and also fix some
  test which were using wrong logic to determine autostart
  usage(tests were wrong).
- Fixes #141
This commit is contained in:
Janne Valkealahti
2015-12-12 20:05:43 +00:00
parent 05a4155d9d
commit 8aaf66afea
4 changed files with 53 additions and 14 deletions

View File

@@ -149,6 +149,7 @@ public class StateMachineConfiguration<S, E> extends
stateMachineFactory.setBeanFactory(getBeanFactory());
stateMachineFactory.setContextEventsEnabled(contextEvents);
stateMachineFactory.setBeanName(beanName);
stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());
StateMachine<S, E> stateMachine = stateMachineFactory.getStateMachine();
this.lifecycle = (SmartLifecycle) stateMachine;
this.disposableBean = (DisposableBean) stateMachine;

View File

@@ -70,6 +70,17 @@ public class SessionScopedAnnotationTests {
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void testAutoStart() throws Exception {
MockHttpSession session1 = new MockHttpSession();
mvc.
perform(get("/ping").session(session1)).
andExpect(status().isOk());
Object machine = session1.getAttribute("scopedTarget.stateMachine");
assertThat(machine, notNullValue());
assertThat(TestUtils.callMethod("isRunning", machine), is(true));
}
@Test
public void testScopedMachines() throws Exception {
MockHttpSession session1 = new MockHttpSession();
@@ -171,6 +182,13 @@ public class SessionScopedAnnotationTests {
@Autowired
StateMachine<String, String> stateMachine;
@RequestMapping(path="/ping", method=RequestMethod.GET)
public HttpEntity<Void> dummyPing() {
// dummy ping to instantiate session and then create machine
stateMachine.getState();
return new ResponseEntity<Void>(HttpStatus.OK);
}
@RequestMapping(path="/state", method=RequestMethod.POST)
public HttpEntity<Void> setState(@RequestParam("event") String event) {
stateMachine.sendEvent(event);
@@ -180,7 +198,6 @@ public class SessionScopedAnnotationTests {
@RequestMapping(path="/state", method=RequestMethod.GET)
@ResponseBody
public String getState() {
stateMachine.start();
return stateMachine.getState().getId();
}

View File

@@ -114,6 +114,17 @@ public class SessionScopedManualTests {
assertThat(TestUtils.readField("running", machine), is(false));
}
@Test
public void testAutoStart() throws Exception {
MockHttpSession session1 = new MockHttpSession();
mvc.
perform(get("/ping").session(session1)).
andExpect(status().isOk());
Object machine = session1.getAttribute("scopedTarget.stateMachine");
assertThat(machine, notNullValue());
assertThat(TestUtils.callMethod("isRunning", machine), is(true));
}
@Configuration
static class Config {
@@ -150,6 +161,13 @@ public class SessionScopedManualTests {
@Autowired
StateMachine<String, String> stateMachine;
@RequestMapping(path="/ping", method=RequestMethod.GET)
public HttpEntity<Void> dummyPing() {
// dummy ping to instantiate session and then create machine
stateMachine.getState();
return new ResponseEntity<Void>(HttpStatus.OK);
}
@RequestMapping(path="/state", method=RequestMethod.POST)
public HttpEntity<Void> setState(@RequestParam("event") String event) {
stateMachine.sendEvent(event);