From 5dc813ab3fbce27b00b682c381bc50f3479c96aa Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Fri, 4 Sep 2015 08:28:22 +0100 Subject: [PATCH] StateMachineTestPlan starts machine too early - Delaying starting statemachine inside step and after listeners has been reset. - Fixes #104 --- .../test/StateMachineTestPlan.java | 6 +- .../test/StateMachineTestingTests.java | 65 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/spring-statemachine-test/src/main/java/org/springframework/statemachine/test/StateMachineTestPlan.java b/spring-statemachine-test/src/main/java/org/springframework/statemachine/test/StateMachineTestPlan.java index 8d76bcf4..00b1dcd0 100644 --- a/spring-statemachine-test/src/main/java/org/springframework/statemachine/test/StateMachineTestPlan.java +++ b/spring-statemachine-test/src/main/java/org/springframework/statemachine/test/StateMachineTestPlan.java @@ -84,7 +84,6 @@ public class StateMachineTestPlan { LatchStateMachineListener listener = new LatchStateMachineListener(); listeners.put(stateMachine, listener); stateMachine.addStateListener(listener); - stateMachine.start(); } log.info("Running test plan for machines " + StringUtils.collectionToCommaDelimitedString(stateMachines.values())); @@ -106,6 +105,11 @@ public class StateMachineTestPlan { step.expectExtendedStateChanged != null ? step.expectExtendedStateChanged : 0); } + // need to call start here, ok to call from all steps + for (StateMachine stateMachine : stateMachines.values()) { + stateMachine.start(); + } + if (step.expectStateMachineStarted != null) { for (Entry, LatchStateMachineListener> entry : listeners.entrySet()) { assertThat("StateMachineStarted Await not matched for machine " + entry.getKey(), entry.getValue() diff --git a/spring-statemachine-test/src/test/java/org/springframework/statemachine/test/StateMachineTestingTests.java b/spring-statemachine-test/src/test/java/org/springframework/statemachine/test/StateMachineTestingTests.java index 291f0970..43cb99d4 100644 --- a/spring-statemachine-test/src/test/java/org/springframework/statemachine/test/StateMachineTestingTests.java +++ b/spring-statemachine-test/src/test/java/org/springframework/statemachine/test/StateMachineTestingTests.java @@ -17,10 +17,14 @@ package org.springframework.statemachine.test; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.config.EnableStateMachine; import org.springframework.statemachine.config.StateMachineConfigurerAdapter; +import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; @@ -43,6 +47,24 @@ public class StateMachineTestingTests extends AbstractStateMachineTests { plan.test(); } + @SuppressWarnings("unchecked") + @Test + public void testStarted() throws Exception { + registerAndRefresh(Config2.class); + StateMachine machine = context.getBean(StateMachine.class); + + StateMachineTestPlan plan = + StateMachineTestPlanBuilder.builder() + .stateMachine(machine) + .step().expectStateMachineStarted(1).and() + .step().expectState("SI").and() + .step().sendEvent("E1").expectStateChanged(1).expectState("S1").and() + .step().sendEvent("E2").expectStateChanged(1).expectState("S2").and() + .build(); + + plan.test(); + } + @Override protected AnnotationConfigApplicationContext buildContext() { return new AnnotationConfigApplicationContext(); @@ -77,4 +99,47 @@ public class StateMachineTestingTests extends AbstractStateMachineTests { } + @Configuration + @EnableStateMachine + static class Config2 extends StateMachineConfigurerAdapter { + + @Override + public void configure(StateMachineConfigurationConfigurer config) throws Exception { + config + .withConfiguration() + .taskExecutor(taskExecutor()); + } + + @Override + public void configure(StateMachineStateConfigurer states) throws Exception { + states + .withStates() + .initial("SI") + .state("S1") + .state("S2"); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source("SI") + .target("S1") + .event("E1") + .and() + .withExternal() + .source("S1") + .target("S2") + .event("E2"); + } + + @Bean + public TaskExecutor taskExecutor() { + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); + taskExecutor.setCorePoolSize(1); + return taskExecutor; + } + + } + }