From d847dbacc42e70bb0007b8c8df99bbb46a04f6fd Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Thu, 14 May 2015 17:12:49 +0100 Subject: [PATCH] Add tests for fork/join --- .../statemachine/state/ForkStateTests.java | 189 ++++++++++++++++++ .../statemachine/state/JoinStateTests.java | 73 +++++++ 2 files changed, 262 insertions(+) diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ForkStateTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ForkStateTests.java index b58e3657..8aaf5828 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ForkStateTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/ForkStateTests.java @@ -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 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 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 { @@ -142,6 +207,130 @@ public class ForkStateTests extends AbstractStateMachineTests { } + @Configuration + @EnableStateMachine + static class Config2 extends EnumStateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineStateConfigurer 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 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 { + + @Override + public void configure(StateMachineStateConfigurer 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 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 { volatile CountDownLatch stateChangedLatch = new CountDownLatch(1); diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/JoinStateTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/JoinStateTests.java index 00ffbcf3..ef2c04f5 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/JoinStateTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/JoinStateTests.java @@ -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 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 { @@ -111,4 +127,61 @@ public class JoinStateTests extends AbstractStateMachineTests { } + @Configuration + @EnableStateMachine + static class Config2 extends EnumStateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineStateConfigurer 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 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); + } + + } + }