From 7d26f505a1f64e6d8ddbdc03f1ed71014e62509d Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 30 May 2015 09:29:10 +0100 Subject: [PATCH] Add tests for turnstile sample --- .../demo/AbstractStateMachineCommands.java | 8 +- .../java/demo/turnstile/TurnstileTests.java | 155 ++++++++++++++++++ 2 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 spring-statemachine-samples/turnstile/src/test/java/demo/turnstile/TurnstileTests.java diff --git a/spring-statemachine-samples/src/main/java/demo/AbstractStateMachineCommands.java b/spring-statemachine-samples/src/main/java/demo/AbstractStateMachineCommands.java index b9e93f5e..7364022b 100644 --- a/spring-statemachine-samples/src/main/java/demo/AbstractStateMachineCommands.java +++ b/spring-statemachine-samples/src/main/java/demo/AbstractStateMachineCommands.java @@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.shell.core.CommandMarker; import org.springframework.shell.core.annotation.CliCommand; import org.springframework.statemachine.StateMachine; +import org.springframework.statemachine.state.State; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; @@ -43,7 +44,12 @@ public class AbstractStateMachineCommands implements CommandMarker { @CliCommand(value = "sm state", help = "Prints current state") public String state() { - return StringUtils.collectionToCommaDelimitedString(stateMachine.getState().getIds()); + State state = stateMachine.getState(); + if (state != null) { + return StringUtils.collectionToCommaDelimitedString(state.getIds()); + } else { + return "No state"; + } } @CliCommand(value = "sm start", help = "Start a state machine") diff --git a/spring-statemachine-samples/turnstile/src/test/java/demo/turnstile/TurnstileTests.java b/spring-statemachine-samples/turnstile/src/test/java/demo/turnstile/TurnstileTests.java new file mode 100644 index 00000000..e4f7e37e --- /dev/null +++ b/spring-statemachine-samples/turnstile/src/test/java/demo/turnstile/TurnstileTests.java @@ -0,0 +1,155 @@ +/* + * 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 demo.turnstile; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.statemachine.EnumStateMachine; +import org.springframework.statemachine.StateMachine; +import org.springframework.statemachine.StateMachineSystemConstants; +import org.springframework.statemachine.listener.StateMachineListener; +import org.springframework.statemachine.listener.StateMachineListenerAdapter; +import org.springframework.statemachine.state.State; +import org.springframework.statemachine.transition.Transition; + +import demo.CommonConfiguration; +import demo.turnstile.Application.Events; +import demo.turnstile.Application.States; + +public class TurnstileTests { + + private AnnotationConfigApplicationContext context; + + private StateMachine machine; + + private TestListener listener; + + private StateMachineCommands commands; + + @Test + public void testNotStarted() throws Exception { + assertThat(commands.state(), is("No state")); + } + + @Test + public void testInitialState() throws Exception { + machine.start(); + listener.stateChangedLatch.await(1, TimeUnit.SECONDS); + listener.stateEnteredLatch.await(1, TimeUnit.SECONDS); + assertThat(machine.getState().getIds(), contains(States.LOCKED)); + assertThat(listener.statesEntered.size(), is(1)); + assertThat(listener.statesEntered.get(0).getId(), is(States.LOCKED)); + assertThat(listener.statesExited.size(), is(0)); + } + + static class Config { + + @Autowired + private StateMachine machine; + + @Bean + public StateMachineListener stateMachineListener() { + TestListener listener = new TestListener(); + machine.addStateListener(listener); + return listener; + } + } + + static class TestListener extends StateMachineListenerAdapter { + + volatile CountDownLatch stateChangedLatch = new CountDownLatch(1); + volatile CountDownLatch stateEnteredLatch = new CountDownLatch(2); + volatile CountDownLatch stateExitedLatch = new CountDownLatch(0); + volatile CountDownLatch transitionLatch = new CountDownLatch(0); + volatile List> transitions = new ArrayList>(); + List> statesEntered = new ArrayList>(); + List> statesExited = new ArrayList>(); + volatile int transitionCount = 0; + + @Override + public void stateChanged(State from, State to) { + stateChangedLatch.countDown(); + } + + @Override + public void stateEntered(State state) { + statesEntered.add(state); + stateEnteredLatch.countDown(); + } + + @Override + public void stateExited(State state) { + statesExited.add(state); + stateExitedLatch.countDown(); + } + + @Override + public void transition(Transition transition) { + transitions.add(transition); + transitionLatch.countDown(); + transitionCount++; + } + + public void reset(int c1, int c2, int c3) { + reset(c1, c2, c3, 0); + } + + public void reset(int c1, int c2, int c3, int c4) { + stateChangedLatch = new CountDownLatch(c1); + stateEnteredLatch = new CountDownLatch(c2); + stateExitedLatch = new CountDownLatch(c3); + transitionLatch = new CountDownLatch(c4); + statesEntered.clear(); + statesExited.clear(); + transitionCount = 0; + transitions.clear(); + } + + } + + @SuppressWarnings("unchecked") + @Before + public void setup() { + context = new AnnotationConfigApplicationContext(); + context.register(CommonConfiguration.class, Application.class, Config.class, StateMachineCommands.class); + context.refresh(); + machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class); + listener = context.getBean(TestListener.class); + commands = context.getBean(StateMachineCommands.class); + } + + @After + public void clean() { + machine.stop(); + context.close(); + context = null; + machine = null; + } + +}