Spring State Machine x3 (#1538)
* [Fix] Move stateDo onto a separate state * Change tests to spring runner
This commit is contained in:
parent
ce589cc7b5
commit
99688e9b19
|
@ -21,10 +21,15 @@
|
|||
<artifactId>spring-statemachine-core</artifactId>
|
||||
<version>1.2.3.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>4.3.7.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
|
@ -36,10 +36,10 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt
|
|||
.initial("SI")
|
||||
.end("SF")
|
||||
.states(new HashSet<>(Arrays.asList("S1", "S2")))
|
||||
.state("S4", executeAction(), errorAction())
|
||||
.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()
|
||||
.source("SI").target("S3").event("E3")
|
||||
.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()
|
||||
.source("S2").target("SF").event("end");
|
||||
.source("S4").target("S5").event("E5")
|
||||
.and().withExternal()
|
||||
.source("S5").target("SF").event("end").guard(simpleGuard());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -73,9 +75,16 @@ public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapt
|
|||
}
|
||||
|
||||
@Bean
|
||||
public Action<String, String> executeAction() {
|
||||
public Action<String, String> doAction() {
|
||||
return (ctx) -> {
|
||||
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);
|
||||
approvals++;
|
||||
ctx.getExtendedState().getVariables().put("approvalCount", approvals);
|
||||
|
|
|
@ -1,23 +1,36 @@
|
|||
package com.baeldung.spring.statemachine;
|
||||
|
||||
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.runner.RunWith;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = ForkJoinStateMachineConfiguration.class)
|
||||
public class ForkJoinStateMachineTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
stateMachine.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenForkStateEntered_thenMultipleSubStatesEntered() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
|
||||
StateMachine stateMachine = ctx.getBean(StateMachine.class);
|
||||
stateMachine.start();
|
||||
|
||||
boolean success = stateMachine.sendEvent("E1");
|
||||
|
||||
assertTrue(success);
|
||||
|
@ -27,9 +40,6 @@ public class ForkJoinStateMachineTest {
|
|||
|
||||
@Test
|
||||
public void whenAllConfiguredJoinEntryStatesAreEntered_thenTransitionToJoinState() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
|
||||
StateMachine stateMachine = ctx.getBean(StateMachine.class);
|
||||
stateMachine.start();
|
||||
|
||||
boolean success = stateMachine.sendEvent("E1");
|
||||
|
||||
|
@ -41,4 +51,9 @@ public class ForkJoinStateMachineTest {
|
|||
assertTrue(stateMachine.sendEvent("sub2"));
|
||||
assertEquals("SJoin", stateMachine.getState().getId());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
stateMachine.stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,23 +1,35 @@
|
|||
package com.baeldung.spring.statemachine;
|
||||
|
||||
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.runner.RunWith;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = HierarchicalStateMachineConfiguration.class)
|
||||
public class HierarchicalStateMachineTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
stateMachine.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
|
||||
|
@ -34,4 +46,9 @@ public class HierarchicalStateMachineTest {
|
|||
assertEquals(1, stateMachine.getState().getIds().size());
|
||||
assertEquals("SF", stateMachine.getState().getId());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
stateMachine.stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,33 @@
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
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 {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
stateMachine.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
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");
|
||||
Assert.assertEquals("low", stateMachine.getState().getId());
|
||||
|
@ -20,4 +35,9 @@ public class JunctionStateMachineTest {
|
|||
stateMachine.sendEvent("end");
|
||||
Assert.assertEquals("SF", stateMachine.getState().getId());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
stateMachine.stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,22 +3,30 @@ package com.baeldung.spring.statemachine;
|
|||
import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewEvents;
|
||||
import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewStates;
|
||||
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.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
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.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = SimpleEnumStateMachineConfiguration.class)
|
||||
public class StateEnumMachineTest {
|
||||
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SimpleEnumStateMachineConfiguration.class);
|
||||
stateMachine = ctx.getBean(StateMachine.class);
|
||||
stateMachine.start();
|
||||
}
|
||||
|
||||
|
@ -29,4 +37,9 @@ public class StateEnumMachineTest {
|
|||
assertTrue(stateMachine.sendEvent(ApplicationReviewEvents.REJECT));
|
||||
assertEquals(ApplicationReviewStates.REJECTED, stateMachine.getState().getId());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
stateMachine.stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,23 +1,31 @@
|
|||
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.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 {
|
||||
|
||||
private AnnotationConfigApplicationContext ctx;
|
||||
@Resource
|
||||
private StateMachine stateMachine;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ctx = new AnnotationConfigApplicationContext(SimpleStateMachineConfiguration.class);
|
||||
stateMachine = ctx.getBean(StateMachine.class);
|
||||
stateMachine.start();
|
||||
}
|
||||
|
||||
|
@ -42,9 +50,18 @@ public class StateMachineIntegrationTest {
|
|||
|
||||
assertTrue(acceptedE4);
|
||||
assertEquals("S4", stateMachine.getState().getId());
|
||||
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
|
||||
|
||||
stateMachine.sendEvent("E5");
|
||||
assertEquals("S5", stateMachine.getState().getId());
|
||||
|
||||
stateMachine.sendEvent("end");
|
||||
assertEquals("SF", stateMachine.getState().getId());
|
||||
|
||||
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
stateMachine.stop();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue