Add tests for fork/join

This commit is contained in:
Janne Valkealahti
2015-05-14 17:12:49 +01:00
parent 80b62bc555
commit d847dbacc4
2 changed files with 262 additions and 0 deletions

View File

@@ -79,6 +79,71 @@ public class ForkStateTests extends AbstractStateMachineTests {
assertThat((String)s31EntryAction.stateContexts.get(0).getMessageHeader("foo"), is("bar"));
}
@Test
@SuppressWarnings("unchecked")
public void testForkToSuperEventNotPassed() throws Exception {
context.register(BaseConfig.class, Config2.class);
context.refresh();
EnumStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
TestListener listener = new TestListener();
machine.addStateListener(listener);
TestEntryAction s20EntryAction = context.getBean("s20EntryAction", TestEntryAction.class);
TestEntryAction s21EntryAction = context.getBean("s21EntryAction", TestEntryAction.class);
TestEntryAction s30EntryAction = context.getBean("s30EntryAction", TestEntryAction.class);
TestEntryAction s31EntryAction = context.getBean("s31EntryAction", TestEntryAction.class);
assertThat(machine, notNullValue());
machine.start();
listener.reset(3);
machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).setHeader("foo", "bar").build());
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(listener.stateChangedCount, is(3));
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S2, TestStates.S20, TestStates.S30));
assertThat(s20EntryAction.stateContexts.size(), is(1));
assertThat(s21EntryAction.stateContexts.size(), is(0));
assertThat(s30EntryAction.stateContexts.size(), is(1));
assertThat(s31EntryAction.stateContexts.size(), is(0));
assertThat((String)s20EntryAction.stateContexts.get(0).getMessageHeader("foo"), nullValue());
assertThat((String)s30EntryAction.stateContexts.get(0).getMessageHeader("foo"), nullValue());
}
@Test
@SuppressWarnings("unchecked")
public void testForkToSuperAndSubEventPassed() throws Exception {
context.register(BaseConfig.class, Config3.class);
context.refresh();
EnumStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
TestListener listener = new TestListener();
machine.addStateListener(listener);
TestEntryAction s20EntryAction = context.getBean("s20EntryAction", TestEntryAction.class);
TestEntryAction s21EntryAction = context.getBean("s21EntryAction", TestEntryAction.class);
TestEntryAction s30EntryAction = context.getBean("s30EntryAction", TestEntryAction.class);
TestEntryAction s31EntryAction = context.getBean("s31EntryAction", TestEntryAction.class);
assertThat(machine, notNullValue());
machine.start();
listener.reset(3);
machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).setHeader("foo", "bar").build());
assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(listener.stateChangedCount, is(3));
assertThat(machine.getState().getIds(), containsInAnyOrder(TestStates.S2, TestStates.S20, TestStates.S31));
assertThat(s20EntryAction.stateContexts.size(), is(1));
assertThat(s21EntryAction.stateContexts.size(), is(0));
assertThat(s30EntryAction.stateContexts.size(), is(1));
assertThat(s31EntryAction.stateContexts.size(), is(1));
assertThat((String)s20EntryAction.stateContexts.get(0).getMessageHeader("foo"), nullValue());
assertThat((String)s30EntryAction.stateContexts.get(0).getMessageHeader("foo"), nullValue());
assertThat((String)s31EntryAction.stateContexts.get(0).getMessageHeader("foo"), is("bar"));
}
@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@@ -142,6 +207,130 @@ public class ForkStateTests extends AbstractStateMachineTests {
}
@Configuration
@EnableStateMachine
static class Config2 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.SI)
.fork(TestStates.S1)
.state(TestStates.SI)
.state(TestStates.S2)
.end(TestStates.SF)
.and()
.withStates()
.parent(TestStates.S2)
.initial(TestStates.S20)
.state(TestStates.S20, s20EntryAction(), null)
.state(TestStates.S21, s21EntryAction(), null)
.and()
.withStates()
.parent(TestStates.S2)
.initial(TestStates.S30)
.state(TestStates.S30, s30EntryAction(), null)
.state(TestStates.S31, s31EntryAction(), null);
}
@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
transitions
.withExternal()
.source(TestStates.SI)
.target(TestStates.S1)
.event(TestEvents.E1)
.and()
.withFork()
.source(TestStates.S1)
.target(TestStates.S2);
}
@Bean
public TestEntryAction s20EntryAction() {
return new TestEntryAction();
}
@Bean
public TestEntryAction s21EntryAction() {
return new TestEntryAction();
}
@Bean
public TestEntryAction s30EntryAction() {
return new TestEntryAction();
}
@Bean
public TestEntryAction s31EntryAction() {
return new TestEntryAction();
}
}
@Configuration
@EnableStateMachine
static class Config3 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.SI)
.fork(TestStates.S1)
.state(TestStates.SI)
.state(TestStates.S2)
.end(TestStates.SF)
.and()
.withStates()
.parent(TestStates.S2)
.initial(TestStates.S20)
.state(TestStates.S20, s20EntryAction(), null)
.state(TestStates.S21, s21EntryAction(), null)
.and()
.withStates()
.parent(TestStates.S2)
.initial(TestStates.S30)
.state(TestStates.S30, s30EntryAction(), null)
.state(TestStates.S31, s31EntryAction(), null);
}
@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
transitions
.withExternal()
.source(TestStates.SI)
.target(TestStates.S1)
.event(TestEvents.E1)
.and()
.withFork()
.source(TestStates.S1)
.target(TestStates.S31);
}
@Bean
public TestEntryAction s20EntryAction() {
return new TestEntryAction();
}
@Bean
public TestEntryAction s21EntryAction() {
return new TestEntryAction();
}
@Bean
public TestEntryAction s30EntryAction() {
return new TestEntryAction();
}
@Bean
public TestEntryAction s31EntryAction() {
return new TestEntryAction();
}
}
static class TestListener extends StateMachineListenerAdapter<TestStates, TestEvents> {
volatile CountDownLatch stateChangedLatch = new CountDownLatch(1);

View File

@@ -53,6 +53,22 @@ public class JoinStateTests extends AbstractStateMachineTests {
assertThat(machine.getState().getIds(), contains(TestStates.S4));
}
@Test
@SuppressWarnings("unchecked")
public void testJoinSuper() {
context.register(BaseConfig.class, Config2.class);
context.refresh();
EnumStateMachine<TestStates,TestEvents> machine =
context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
assertThat(machine, notNullValue());
machine.start();
machine.sendEvent(TestEvents.E1);
machine.sendEvent(TestEvents.E2);
machine.sendEvent(TestEvents.E3);
assertThat(machine.getState().getIds(), contains(TestStates.S4));
}
@Configuration
@EnableStateMachine
static class Config1 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@@ -111,4 +127,61 @@ public class JoinStateTests extends AbstractStateMachineTests {
}
@Configuration
@EnableStateMachine
static class Config2 extends EnumStateMachineConfigurerAdapter<TestStates, TestEvents> {
@Override
public void configure(StateMachineStateConfigurer<TestStates, TestEvents> states) throws Exception {
states
.withStates()
.initial(TestStates.SI)
.state(TestStates.SI)
.state(TestStates.S2)
.end(TestStates.SF)
.join(TestStates.S3)
.state(TestStates.S4)
.and()
.withStates()
.parent(TestStates.S2)
.initial(TestStates.S20)
.state(TestStates.S20)
.state(TestStates.S21)
.and()
.withStates()
.parent(TestStates.S2)
.initial(TestStates.S30)
.state(TestStates.S30)
.state(TestStates.S31);
}
@Override
public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> transitions) throws Exception {
transitions
.withExternal()
.source(TestStates.SI)
.target(TestStates.S2)
.event(TestEvents.E1)
.and()
.withExternal()
.source(TestStates.S20)
.target(TestStates.S21)
.event(TestEvents.E2)
.and()
.withExternal()
.source(TestStates.S30)
.target(TestStates.S31)
.event(TestEvents.E3)
.and()
.withJoin()
.source(TestStates.S2)
.target(TestStates.S3)
.and()
.withExternal()
.source(TestStates.S3)
.target(TestStates.S4);
}
}
}