Spring State Machine x3 (#1538)

* [Fix] Move stateDo onto a separate state

* Change tests to spring runner
This commit is contained in:
Danil Kornishev 2017-03-30 16:27:22 -04:00 committed by Grzegorz Piwowarek
parent ce589cc7b5
commit 99688e9b19
7 changed files with 130 additions and 34 deletions

View File

@ -21,10 +21,15 @@
<artifactId>spring-statemachine-core</artifactId> <artifactId>spring-statemachine-core</artifactId>
<version>1.2.3.RELEASE</version> <version>1.2.3.RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>

View File

@ -36,10 +36,10 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt
.initial("SI") .initial("SI")
.end("SF") .end("SF")
.states(new HashSet<>(Arrays.asList("S1", "S2"))) .states(new HashSet<>(Arrays.asList("S1", "S2")))
.state("S4", executeAction(), errorAction())
.stateEntry("S3", entryAction()) .stateEntry("S3", entryAction())
.stateDo("S3", executeAction()) .stateExit("S3", exitAction())
.stateExit("S3", exitAction()); .state("S4", executeAction(), errorAction())
.stateDo("S5", executeAction());
} }
@ -52,9 +52,11 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt
.and().withExternal() .and().withExternal()
.source("SI").target("S3").event("E3") .source("SI").target("S3").event("E3")
.and().withExternal() .and().withExternal()
.source("S3").target("S4").event("E4").and().withExternal().source("S4").target("SF").event("end").guard(simpleGuard()) .source("S3").target("S4").event("E4")
.and().withExternal() .and().withExternal()
.source("S2").target("SF").event("end"); .source("S4").target("S5").event("E5")
.and().withExternal()
.source("S5").target("SF").event("end").guard(simpleGuard());
} }
@Bean @Bean
@ -73,9 +75,16 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt
} }
@Bean @Bean
public Action<String, String> executeAction() { public Action<String, String> doAction() {
return (ctx) -> { return (ctx) -> {
LOGGER.info("Do " + ctx.getTarget().getId()); LOGGER.info("Do " + ctx.getTarget().getId());
};
}
@Bean
public Action<String, String> executeAction() {
return (ctx) -> {
LOGGER.info("Execute " + ctx.getTarget().getId());
int approvals = (int) ctx.getExtendedState().getVariables().getOrDefault("approvalCount", 0); int approvals = (int) ctx.getExtendedState().getVariables().getOrDefault("approvalCount", 0);
approvals++; approvals++;
ctx.getExtendedState().getVariables().put("approvalCount", approvals); ctx.getExtendedState().getVariables().put("approvalCount", approvals);

View File

@ -1,23 +1,36 @@
package com.baeldung.spring.statemachine; package com.baeldung.spring.statemachine;
import com.baeldung.spring.statemachine.config.ForkJoinStateMachineConfiguration; import com.baeldung.spring.statemachine.config.ForkJoinStateMachineConfiguration;
import com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ForkJoinStateMachineConfiguration.class)
public class ForkJoinStateMachineTest { public class ForkJoinStateMachineTest {
@Resource
private StateMachine stateMachine;
@Before
public void setUp() {
stateMachine.start();
}
@Test @Test
public void whenForkStateEntered_thenMultipleSubStatesEntered() { public void whenForkStateEntered_thenMultipleSubStatesEntered() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
boolean success = stateMachine.sendEvent("E1"); boolean success = stateMachine.sendEvent("E1");
assertTrue(success); assertTrue(success);
@ -27,9 +40,6 @@ public class ForkJoinStateMachineTest {
@Test @Test
public void whenAllConfiguredJoinEntryStatesAreEntered_thenTransitionToJoinState() { public void whenAllConfiguredJoinEntryStatesAreEntered_thenTransitionToJoinState() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
boolean success = stateMachine.sendEvent("E1"); boolean success = stateMachine.sendEvent("E1");
@ -41,4 +51,9 @@ public class ForkJoinStateMachineTest {
assertTrue(stateMachine.sendEvent("sub2")); assertTrue(stateMachine.sendEvent("sub2"));
assertEquals("SJoin", stateMachine.getState().getId()); assertEquals("SJoin", stateMachine.getState().getId());
} }
@After
public void tearDown() {
stateMachine.stop();
}
} }

View File

@ -1,23 +1,35 @@
package com.baeldung.spring.statemachine; package com.baeldung.spring.statemachine;
import com.baeldung.spring.statemachine.config.HierarchicalStateMachineConfiguration; import com.baeldung.spring.statemachine.config.HierarchicalStateMachineConfiguration;
import com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.Arrays; import java.util.Arrays;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HierarchicalStateMachineConfiguration.class)
public class HierarchicalStateMachineTest { public class HierarchicalStateMachineTest {
@Resource
private StateMachine stateMachine;
@Before
public void setUp() {
stateMachine.start();
}
@Test @Test
public void whenTransitionToSubMachine_thenSubStateIsEntered() { public void whenTransitionToSubMachine_thenSubStateIsEntered() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(HierarchicalStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
assertEquals(Arrays.asList("SI", "SUB1"), stateMachine.getState().getIds()); assertEquals(Arrays.asList("SI", "SUB1"), stateMachine.getState().getIds());
@ -34,4 +46,9 @@ public class HierarchicalStateMachineTest {
assertEquals(1, stateMachine.getState().getIds().size()); assertEquals(1, stateMachine.getState().getIds().size());
assertEquals("SF", stateMachine.getState().getId()); assertEquals("SF", stateMachine.getState().getId());
} }
@After
public void tearDown() {
stateMachine.stop();
}
} }

View File

@ -1,18 +1,33 @@
package com.baeldung.spring.statemachine; package com.baeldung.spring.statemachine;
import com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration;
import com.baeldung.spring.statemachine.config.SimpleEnumStateMachineConfiguration;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JunctionStateMachineConfiguration.class)
public class JunctionStateMachineTest { public class JunctionStateMachineTest {
@Resource
private StateMachine stateMachine;
@Before
public void setUp() {
stateMachine.start();
}
@Test @Test
public void whenTransitioningToJunction_thenArriveAtSubJunctionNode() { public void whenTransitioningToJunction_thenArriveAtSubJunctionNode() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration.class);
StateMachine stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start();
stateMachine.sendEvent("E1"); stateMachine.sendEvent("E1");
Assert.assertEquals("low", stateMachine.getState().getId()); Assert.assertEquals("low", stateMachine.getState().getId());
@ -20,4 +35,9 @@ public class JunctionStateMachineTest {
stateMachine.sendEvent("end"); stateMachine.sendEvent("end");
Assert.assertEquals("SF", stateMachine.getState().getId()); Assert.assertEquals("SF", stateMachine.getState().getId());
} }
@After
public void tearDown() {
stateMachine.stop();
}
} }

View File

@ -3,22 +3,30 @@ package com.baeldung.spring.statemachine;
import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewEvents; import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewEvents;
import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewStates; import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewStates;
import com.baeldung.spring.statemachine.config.SimpleEnumStateMachineConfiguration; import com.baeldung.spring.statemachine.config.SimpleEnumStateMachineConfiguration;
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachine;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SimpleEnumStateMachineConfiguration.class)
public class StateEnumMachineTest { public class StateEnumMachineTest {
@Resource
private StateMachine stateMachine; private StateMachine stateMachine;
@Before @Before
public void setUp() { public void setUp() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SimpleEnumStateMachineConfiguration.class);
stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start(); stateMachine.start();
} }
@ -29,4 +37,9 @@ public class StateEnumMachineTest {
assertTrue(stateMachine.sendEvent(ApplicationReviewEvents.REJECT)); assertTrue(stateMachine.sendEvent(ApplicationReviewEvents.REJECT));
assertEquals(ApplicationReviewStates.REJECTED, stateMachine.getState().getId()); assertEquals(ApplicationReviewStates.REJECTED, stateMachine.getState().getId());
} }
@After
public void tearDown() {
stateMachine.stop();
}
} }

View File

@ -1,23 +1,31 @@
package com.baeldung.spring.statemachine; package com.baeldung.spring.statemachine;
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.statemachine.StateMachine;
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SimpleStateMachineConfiguration.class)
public class StateMachineIntegrationTest { public class StateMachineIntegrationTest {
private AnnotationConfigApplicationContext ctx; @Resource
private StateMachine stateMachine; private StateMachine stateMachine;
@Before @Before
public void setUp() { public void setUp() {
ctx = new AnnotationConfigApplicationContext(SimpleStateMachineConfiguration.class);
stateMachine = ctx.getBean(StateMachine.class);
stateMachine.start(); stateMachine.start();
} }
@ -42,9 +50,18 @@ public class StateMachineIntegrationTest {
assertTrue(acceptedE4); assertTrue(acceptedE4);
assertEquals("S4", stateMachine.getState().getId()); assertEquals("S4", stateMachine.getState().getId());
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
stateMachine.sendEvent("E5");
assertEquals("S5", stateMachine.getState().getId());
stateMachine.sendEvent("end"); stateMachine.sendEvent("end");
assertEquals("SF", stateMachine.getState().getId()); assertEquals("SF", stateMachine.getState().getId());
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
}
@After
public void tearDown() {
stateMachine.stop();
} }
} }