From 1a485e4d6977f521bf02d4542edf73c22e2b4030 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 12 Jan 2019 09:10:33 +0000 Subject: [PATCH] Add end state action test - Test that entry action for end state is executed. - Relates to #620 --- .../statemachine/state/StateActionTests.java | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/StateActionTests.java b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/StateActionTests.java index d3427a97..1d74142d 100644 --- a/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/StateActionTests.java +++ b/spring-statemachine-core/src/test/java/org/springframework/statemachine/state/StateActionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2019 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. @@ -16,6 +16,7 @@ package org.springframework.statemachine.state; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; @@ -65,6 +66,27 @@ public class StateActionTests extends AbstractStateMachineTests { ctx.close(); } + @Test + @SuppressWarnings("unchecked") + public void testEndStateEntryAction() throws Exception { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config2.class); + ObjectStateMachine machine = + ctx.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class); + TestEntryAction testEntryAction = ctx.getBean("testEntryAction", TestEntryAction.class); + machine.start(); + + assertThat(machine, notNullValue()); + assertThat(machine.isComplete(), is(false)); + assertThat(machine.getState().getIds(), contains(TestStates.SI)); + + machine.sendEvent(TestEvents.E1); + assertThat(machine.isComplete(), is(true)); + assertThat(machine.getState().getIds(), contains(TestStates.SF)); + assertThat(testEntryAction.onExecuteLatch.await(2, TimeUnit.SECONDS), is(true)); + + ctx.close(); + } + @Configuration @EnableStateMachine public static class Config1 extends EnumStateMachineConfigurerAdapter { @@ -106,4 +128,32 @@ public class StateActionTests 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.SF, testEntryAction(), null) + .end(TestStates.SF); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + transitions + .withExternal() + .source(TestStates.SI) + .target(TestStates.SF) + .event(TestEvents.E1); + } + + @Bean + public Action testEntryAction() { + return new TestEntryAction(); + } + } }