Skip to content

Commit 99688e9

Browse files
dkornishevpivovarit
authored andcommitted
Spring State Machine x3 (eugenp#1538)
* [Fix] Move stateDo onto a separate state * Change tests to spring runner
1 parent ce589cc commit 99688e9

7 files changed

Lines changed: 127 additions & 31 deletions

File tree

‎spring-state-machine/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@
2121
<artifactId>spring-statemachine-core</artifactId>
2222
<version>1.2.3.RELEASE</version>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-test</artifactId>
27+
<version>4.3.7.RELEASE</version>
28+
</dependency>
2429
<dependency>
2530
<groupId>junit</groupId>
2631
<artifactId>junit</artifactId>
27-
<version>4.11</version>
32+
<version>4.12</version>
2833
<scope>test</scope>
2934
</dependency>
3035

‎spring-state-machine/src/main/java/com/baeldung/spring/statemachine/config/SimpleStateMachineConfiguration.java‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public void configure(StateMachineStateConfigurer<String, String> states) throws
3636
.initial("SI")
3737
.end("SF")
3838
.states(new HashSet<>(Arrays.asList("S1", "S2")))
39-
.state("S4", executeAction(), errorAction())
4039
.stateEntry("S3", entryAction())
41-
.stateDo("S3", executeAction())
42-
.stateExit("S3", exitAction());
40+
.stateExit("S3", exitAction())
41+
.state("S4", executeAction(), errorAction())
42+
.stateDo("S5", executeAction());
4343

4444
}
4545

@@ -52,9 +52,11 @@ public void configure(StateMachineTransitionConfigurer<String, String> transitio
5252
.and().withExternal()
5353
.source("SI").target("S3").event("E3")
5454
.and().withExternal()
55-
.source("S3").target("S4").event("E4").and().withExternal().source("S4").target("SF").event("end").guard(simpleGuard())
55+
.source("S3").target("S4").event("E4")
56+
.and().withExternal()
57+
.source("S4").target("S5").event("E5")
5658
.and().withExternal()
57-
.source("S2").target("SF").event("end");
59+
.source("S5").target("SF").event("end").guard(simpleGuard());
5860
}
5961

6062
@Bean
@@ -73,9 +75,16 @@ public Action<String, String> entryAction() {
7375
}
7476

7577
@Bean
76-
public Action<String, String> executeAction() {
78+
public Action<String, String> doAction() {
7779
return (ctx) -> {
7880
LOGGER.info("Do " + ctx.getTarget().getId());
81+
};
82+
}
83+
84+
@Bean
85+
public Action<String, String> executeAction() {
86+
return (ctx) -> {
87+
LOGGER.info("Execute " + ctx.getTarget().getId());
7988
int approvals = (int) ctx.getExtendedState().getVariables().getOrDefault("approvalCount", 0);
8089
approvals++;
8190
ctx.getExtendedState().getVariables().put("approvalCount", approvals);
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
package com.baeldung.spring.statemachine;
22

33
import com.baeldung.spring.statemachine.config.ForkJoinStateMachineConfiguration;
4+
import com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration;
5+
import org.junit.After;
6+
import org.junit.Before;
47
import org.junit.Test;
8+
import org.junit.runner.RunWith;
59
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
610
import org.springframework.statemachine.StateMachine;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
713

14+
import javax.annotation.Resource;
815
import java.util.Arrays;
916

1017
import static org.junit.Assert.assertEquals;
1118
import static org.junit.Assert.assertTrue;
1219

20+
@RunWith(SpringJUnit4ClassRunner.class)
21+
@ContextConfiguration(classes = ForkJoinStateMachineConfiguration.class)
1322
public class ForkJoinStateMachineTest {
1423

15-
@Test
16-
public void whenForkStateEntered_thenMultipleSubStatesEntered() {
17-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
18-
StateMachine stateMachine = ctx.getBean(StateMachine.class);
24+
@Resource
25+
private StateMachine stateMachine;
26+
27+
@Before
28+
public void setUp() {
1929
stateMachine.start();
30+
}
2031

32+
@Test
33+
public void whenForkStateEntered_thenMultipleSubStatesEntered() {
2134
boolean success = stateMachine.sendEvent("E1");
2235

2336
assertTrue(success);
@@ -27,9 +40,6 @@ public void whenForkStateEntered_thenMultipleSubStatesEntered() {
2740

2841
@Test
2942
public void whenAllConfiguredJoinEntryStatesAreEntered_thenTransitionToJoinState() {
30-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ForkJoinStateMachineConfiguration.class);
31-
StateMachine stateMachine = ctx.getBean(StateMachine.class);
32-
stateMachine.start();
3343

3444
boolean success = stateMachine.sendEvent("E1");
3545

@@ -41,4 +51,9 @@ public void whenAllConfiguredJoinEntryStatesAreEntered_thenTransitionToJoinState
4151
assertTrue(stateMachine.sendEvent("sub2"));
4252
assertEquals("SJoin", stateMachine.getState().getId());
4353
}
54+
55+
@After
56+
public void tearDown() {
57+
stateMachine.stop();
58+
}
4459
}

‎spring-state-machine/src/test/java/com/baeldung/spring/statemachine/HierarchicalStateMachineTest.java‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
package com.baeldung.spring.statemachine;
22

33
import com.baeldung.spring.statemachine.config.HierarchicalStateMachineConfiguration;
4+
import com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration;
5+
import org.junit.After;
6+
import org.junit.Before;
47
import org.junit.Test;
8+
import org.junit.runner.RunWith;
59
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
610
import org.springframework.statemachine.StateMachine;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
713

14+
import javax.annotation.Resource;
815
import java.util.Arrays;
916

1017
import static org.junit.Assert.assertEquals;
1118

12-
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
@ContextConfiguration(classes = HierarchicalStateMachineConfiguration.class)
1321
public class HierarchicalStateMachineTest {
1422

15-
@Test
16-
public void whenTransitionToSubMachine_thenSubStateIsEntered() {
17-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(HierarchicalStateMachineConfiguration.class);
18-
StateMachine stateMachine = ctx.getBean(StateMachine.class);
23+
@Resource
24+
private StateMachine stateMachine;
25+
26+
@Before
27+
public void setUp() {
1928
stateMachine.start();
29+
}
2030

31+
@Test
32+
public void whenTransitionToSubMachine_thenSubStateIsEntered() {
2133

2234
assertEquals(Arrays.asList("SI", "SUB1"), stateMachine.getState().getIds());
2335

@@ -34,4 +46,9 @@ public void whenTransitionToSubMachine_thenSubStateIsEntered() {
3446
assertEquals(1, stateMachine.getState().getIds().size());
3547
assertEquals("SF", stateMachine.getState().getId());
3648
}
49+
50+
@After
51+
public void tearDown() {
52+
stateMachine.stop();
53+
}
3754
}
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
package com.baeldung.spring.statemachine;
22

3+
import com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration;
4+
import com.baeldung.spring.statemachine.config.SimpleEnumStateMachineConfiguration;
5+
import org.junit.After;
36
import org.junit.Assert;
7+
import org.junit.Before;
48
import org.junit.Test;
9+
import org.junit.runner.RunWith;
510
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
611
import org.springframework.statemachine.StateMachine;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
714

15+
import javax.annotation.Resource;
816

17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@ContextConfiguration(classes = JunctionStateMachineConfiguration.class)
919
public class JunctionStateMachineTest {
1020

21+
@Resource
22+
private StateMachine stateMachine;
23+
24+
@Before
25+
public void setUp() {
26+
stateMachine.start();
27+
}
28+
1129
@Test
1230
public void whenTransitioningToJunction_thenArriveAtSubJunctionNode() {
13-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(com.baeldung.spring.statemachine.config.JunctionStateMachineConfiguration.class);
14-
StateMachine stateMachine = ctx.getBean(StateMachine.class);
15-
stateMachine.start();
1631

1732
stateMachine.sendEvent("E1");
1833
Assert.assertEquals("low", stateMachine.getState().getId());
1934

2035
stateMachine.sendEvent("end");
2136
Assert.assertEquals("SF", stateMachine.getState().getId());
2237
}
38+
39+
@After
40+
public void tearDown() {
41+
stateMachine.stop();
42+
}
2343
}

‎spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateEnumMachineTest.java‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@
33
import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewEvents;
44
import com.baeldung.spring.statemachine.applicationreview.ApplicationReviewStates;
55
import com.baeldung.spring.statemachine.config.SimpleEnumStateMachineConfiguration;
6+
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
7+
import org.junit.After;
68
import org.junit.Before;
79
import org.junit.Test;
10+
import org.junit.runner.RunWith;
811
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
912
import org.springframework.statemachine.StateMachine;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
16+
import javax.annotation.Resource;
1017

1118
import static org.junit.Assert.assertEquals;
1219
import static org.junit.Assert.assertTrue;
1320

21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@ContextConfiguration(classes = SimpleEnumStateMachineConfiguration.class)
1423
public class StateEnumMachineTest {
1524

25+
@Resource
1626
private StateMachine stateMachine;
1727

1828
@Before
1929
public void setUp() {
20-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SimpleEnumStateMachineConfiguration.class);
21-
stateMachine = ctx.getBean(StateMachine.class);
2230
stateMachine.start();
2331
}
2432

@@ -29,4 +37,9 @@ public void whenStateMachineConfiguredWithEnums_thenStateMachineAcceptsEnumEvent
2937
assertTrue(stateMachine.sendEvent(ApplicationReviewEvents.REJECT));
3038
assertEquals(ApplicationReviewStates.REJECTED, stateMachine.getState().getId());
3139
}
40+
41+
@After
42+
public void tearDown() {
43+
stateMachine.stop();
44+
}
3245
}

‎spring-state-machine/src/test/java/com/baeldung/spring/statemachine/StateMachineIntegrationTest.java‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package com.baeldung.spring.statemachine;
22

3-
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.After;
47
import org.junit.Before;
58
import org.junit.Test;
9+
import org.junit.runner.RunWith;
610
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
711
import org.springframework.statemachine.StateMachine;
812

9-
import static org.junit.Assert.assertEquals;
10-
import static org.junit.Assert.assertTrue;
13+
import com.baeldung.spring.statemachine.config.SimpleStateMachineConfiguration;
14+
import org.springframework.test.context.ContextConfiguration;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
import org.springframework.test.context.junit4.SpringRunner;
1117

18+
import javax.annotation.Resource;
19+
20+
@RunWith(SpringJUnit4ClassRunner.class)
21+
@ContextConfiguration(classes = SimpleStateMachineConfiguration.class)
1222
public class StateMachineIntegrationTest {
1323

14-
private AnnotationConfigApplicationContext ctx;
24+
@Resource
1525
private StateMachine stateMachine;
1626

1727
@Before
1828
public void setUp() {
19-
ctx = new AnnotationConfigApplicationContext(SimpleStateMachineConfiguration.class);
20-
stateMachine = ctx.getBean(StateMachine.class);
2129
stateMachine.start();
2230
}
2331

@@ -42,9 +50,18 @@ public void whenSimpleStringMachineActionState_thenActionExecuted() throws Inter
4250

4351
assertTrue(acceptedE4);
4452
assertEquals("S4", stateMachine.getState().getId());
45-
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
53+
54+
stateMachine.sendEvent("E5");
55+
assertEquals("S5", stateMachine.getState().getId());
4656

4757
stateMachine.sendEvent("end");
4858
assertEquals("SF", stateMachine.getState().getId());
59+
60+
assertEquals(2, stateMachine.getExtendedState().getVariables().get("approvalCount"));
61+
}
62+
63+
@After
64+
public void tearDown() {
65+
stateMachine.stop();
4966
}
5067
}

0 commit comments

Comments
 (0)