From ad57bc7085d2fb31c9bc8848dc8815669c8918c7 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 29 Aug 2015 17:11:15 +0100 Subject: [PATCH] Add transition header tests for choice --- .../TransitionEventHeaderTests.java | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/transition/TransitionEventHeaderTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/transition/TransitionEventHeaderTests.java index 0ce68163..7e9cf222 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/transition/TransitionEventHeaderTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/transition/TransitionEventHeaderTests.java @@ -37,8 +37,16 @@ import org.springframework.statemachine.config.EnableStateMachine; import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; +import org.springframework.statemachine.guard.Guard; import org.springframework.statemachine.transition.TransitionTests.TestListener; +/** + * Tests for making sure that events are passed through various + * transition stages. + * + * @author Janne Valkealahti + * + */ public class TransitionEventHeaderTests extends AbstractStateMachineTests { @Override @@ -79,6 +87,68 @@ public class TransitionEventHeaderTests extends AbstractStateMachineTests { assertThat(eventCheckAction4.context.getEvent(), is(TestEvents.E1)); } + @SuppressWarnings("unchecked") + @Test + public void testEventPassedThroughChoice1() throws Exception { + context.register(Config2.class); + context.refresh(); + + assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE)); + ObjectStateMachine machine = + context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class); + + EventCheckAction eventCheckAction1 = context.getBean("eventCheckAction1", EventCheckAction.class); + EventCheckAction eventCheckAction3 = context.getBean("eventCheckAction3", EventCheckAction.class); + EventCheckAction eventCheckAction4 = context.getBean("eventCheckAction4", EventCheckAction.class); + + TestListener listener = new TestListener(); + machine.addStateListener(listener); + + machine.start(); + assertThat(machine.getState().getIds(), contains(TestStates.S1)); + + listener.reset(1); + machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build()); + assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true)); + assertThat(listener.stateChangedCount, is(1)); + assertThat(machine.getState().getIds(), contains(TestStates.S4)); + + assertThat(eventCheckAction1.context.getEvent(), nullValue()); + assertThat(eventCheckAction3.context, nullValue()); + assertThat(eventCheckAction4.context.getEvent(), is(TestEvents.E1)); + } + + @SuppressWarnings("unchecked") + @Test + public void testEventPassedThroughChoice2() throws Exception { + context.register(Config3.class); + context.refresh(); + + assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE)); + ObjectStateMachine machine = + context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class); + + EventCheckAction eventCheckAction1 = context.getBean("eventCheckAction1", EventCheckAction.class); + EventCheckAction eventCheckAction3 = context.getBean("eventCheckAction3", EventCheckAction.class); + EventCheckAction eventCheckAction4 = context.getBean("eventCheckAction4", EventCheckAction.class); + + TestListener listener = new TestListener(); + machine.addStateListener(listener); + + machine.start(); + assertThat(machine.getState().getIds(), contains(TestStates.S1)); + + listener.reset(1); + machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build()); + assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true)); + assertThat(listener.stateChangedCount, is(1)); + assertThat(machine.getState().getIds(), contains(TestStates.S3)); + + assertThat(eventCheckAction1.context.getEvent(), nullValue()); + assertThat(eventCheckAction3.context.getEvent(), is(TestEvents.E1)); + assertThat(eventCheckAction4.context, nullValue()); + } + @Configuration @EnableStateMachine public static class Config1 extends EnumStateMachineConfigurerAdapter { @@ -132,6 +202,118 @@ public class TransitionEventHeaderTests extends AbstractStateMachineTests { } } + @Configuration + @EnableStateMachine + public static class Config2 extends EnumStateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial(TestStates.S1) + .state(TestStates.S1, eventCheckAction1(), null) + .choice(TestStates.S2) + .state(TestStates.S3, eventCheckAction3(), null) + .state(TestStates.S4, eventCheckAction4(), null); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source(TestStates.S1) + .target(TestStates.S2) + .event(TestEvents.E1) + .and() + .withChoice() + .source(TestStates.S2) + .first(TestStates.S3, guard()) + .last(TestStates.S4); + } + + @Bean + public EventCheckAction eventCheckAction1() { + return new EventCheckAction(); + } + + @Bean + public EventCheckAction eventCheckAction3() { + return new EventCheckAction(); + } + + @Bean + public EventCheckAction eventCheckAction4() { + return new EventCheckAction(); + } + + @Bean + public Guard guard() { + return new Guard() { + + @Override + public boolean evaluate(StateContext context) { + return false; + } + }; + } + } + + @Configuration + @EnableStateMachine + public static class Config3 extends EnumStateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial(TestStates.S1) + .state(TestStates.S1, eventCheckAction1(), null) + .choice(TestStates.S2) + .state(TestStates.S3, eventCheckAction3(), null) + .state(TestStates.S4, eventCheckAction4(), null); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source(TestStates.S1) + .target(TestStates.S2) + .event(TestEvents.E1) + .and() + .withChoice() + .source(TestStates.S2) + .first(TestStates.S3, guard()) + .last(TestStates.S4); + } + + @Bean + public EventCheckAction eventCheckAction1() { + return new EventCheckAction(); + } + + @Bean + public EventCheckAction eventCheckAction3() { + return new EventCheckAction(); + } + + @Bean + public EventCheckAction eventCheckAction4() { + return new EventCheckAction(); + } + + @Bean + public Guard guard() { + return new Guard() { + + @Override + public boolean evaluate(StateContext context) { + return true; + } + }; + } + } + private static class EventCheckAction implements Action { StateContext context;