diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java index 08757c7f..d88384a3 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/state/RegionState.java @@ -89,16 +89,24 @@ public class RegionState extends AbstractState { @Override public void exit(E event, StateContext context) { - // TODO Auto-generated method stub - + Collection> actions = getExitActions(); + if (actions != null) { + for (Action action : actions) { + action.execute(context); + } + } } + @Override public void entry(E event, StateContext context) { - // TODO Auto-generated method stub - + Collection> actions = getEntryActions(); + if (actions != null) { + for (Action action : actions) { + action.execute(context); + } + } } - @Override public Collection getIds() { ArrayList ids = new ArrayList(); diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/RegionMachineTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/RegionMachineTests.java new file mode 100644 index 00000000..78a35bdb --- /dev/null +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/RegionMachineTests.java @@ -0,0 +1,109 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.statemachine; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.springframework.core.task.SyncTaskExecutor; +import org.springframework.statemachine.AbstractStateMachineTests.TestEntryAction; +import org.springframework.statemachine.AbstractStateMachineTests.TestEvents; +import org.springframework.statemachine.AbstractStateMachineTests.TestExitAction; +import org.springframework.statemachine.AbstractStateMachineTests.TestStates; +import org.springframework.statemachine.action.Action; +import org.springframework.statemachine.region.Region; +import org.springframework.statemachine.state.EnumState; +import org.springframework.statemachine.state.RegionState; +import org.springframework.statemachine.state.State; +import org.springframework.statemachine.transition.DefaultExternalTransition; +import org.springframework.statemachine.transition.Transition; + +/** + * Statemachine tests using regions. + * + * @author Janne Valkealahti + * + */ +public class RegionMachineTests { + + @Test + public void testSimpleRegion() throws Exception { + TestEntryAction entryActionS1 = new TestEntryAction("S1"); + TestExitAction exitActionS1 = new TestExitAction("S1"); + Collection> entryActionsS1 = new ArrayList>(); + entryActionsS1.add(entryActionS1); + Collection> exitActionsS1 = new ArrayList>(); + exitActionsS1.add(exitActionS1); + + + State stateSI = new EnumState(TestStates.SI); + State stateS1 = new EnumState(TestStates.S1, null, entryActionsS1, exitActionsS1); + State stateS2 = new EnumState(TestStates.S2); + State stateS3 = new EnumState(TestStates.S3); + + Collection> states = new ArrayList>(); + states.add(stateSI); + states.add(stateS1); + states.add(stateS2); + states.add(stateS3); + + Collection> transitions = new ArrayList>(); + + DefaultExternalTransition transitionFromSIToS1 = + new DefaultExternalTransition(stateSI, stateS1, null, TestEvents.E1, null); + + DefaultExternalTransition transitionFromS1ToS2 = + new DefaultExternalTransition(stateS1, stateS2, null, TestEvents.E2, null); + + DefaultExternalTransition transitionFromS2ToS3 = + new DefaultExternalTransition(stateS2, stateS3, null, TestEvents.E3, null); + + transitions.add(transitionFromSIToS1); + transitions.add(transitionFromS1ToS2); + transitions.add(transitionFromS2ToS3); + + SyncTaskExecutor taskExecutor = new SyncTaskExecutor(); + EnumStateMachine machine = new EnumStateMachine(states, transitions, stateSI, null); + machine.setTaskExecutor(taskExecutor); + machine.start(); + + Collection> regions = new ArrayList>(); + regions.add(machine); + RegionState state = new RegionState(regions); + + assertThat(state.isSimple(), is(false)); + assertThat(state.isComposite(), is(true)); + assertThat(state.isOrthogonal(), is(false)); + assertThat(state.isSubmachineState(), is(false)); + + assertThat(state.getIds(), contains(TestStates.SI)); + + machine.sendEvent(TestEvents.E1); + machine.sendEvent(TestEvents.E2); + + assertThat(entryActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(true)); + assertThat(exitActionS1.onExecuteLatch.await(1, TimeUnit.SECONDS), is(true)); + assertThat(entryActionS1.stateContexts.size(), is(1)); + assertThat(exitActionS1.stateContexts.size(), is(1)); + } + +}