33
33
import org .springframework .statemachine .AbstractStateMachineTests .TestGuard ;
34
34
import org .springframework .statemachine .AbstractStateMachineTests .TestStates ;
35
35
import org .springframework .statemachine .ObjectStateMachine ;
36
+ import org .springframework .statemachine .StateContext ;
36
37
import org .springframework .statemachine .StateMachineSystemConstants ;
37
38
import org .springframework .statemachine .config .EnableStateMachine ;
38
39
import org .springframework .statemachine .config .EnumStateMachineConfigurerAdapter ;
41
42
42
43
/**
43
44
* Tests for state machine guards.
44
- *
45
+ *
45
46
* @author Janne Valkealahti
46
47
*
47
48
*/
@@ -57,12 +58,12 @@ public void testGuardEvaluated() throws Exception {
57
58
TestAction testAction = ctx .getBean ("testAction" , TestAction .class );
58
59
assertThat (testGuard , notNullValue ());
59
60
assertThat (testAction , notNullValue ());
60
-
61
+
61
62
machine .start ();
62
- machine .sendEvent (TestEvents .E1 );
63
+ machine .sendEvent (TestEvents .E1 );
63
64
assertThat (testGuard .onEvaluateLatch .await (2 , TimeUnit .SECONDS ), is (true ));
64
65
assertThat (testAction .onExecuteLatch .await (2 , TimeUnit .SECONDS ), is (true ));
65
-
66
+
66
67
ctx .close ();
67
68
}
68
69
@@ -76,18 +77,35 @@ public void testGuardDenyAction() throws Exception {
76
77
TestAction testAction = ctx .getBean ("testAction" , TestAction .class );
77
78
assertThat (testGuard , notNullValue ());
78
79
assertThat (testAction , notNullValue ());
79
-
80
+
80
81
machine .start ();
81
82
assertThat (machine .getState ().getIds (), contains (TestStates .S1 ));
82
-
83
- machine .sendEvent (TestEvents .E1 );
83
+
84
+ machine .sendEvent (TestEvents .E1 );
84
85
assertThat (testGuard .onEvaluateLatch .await (2 , TimeUnit .SECONDS ), is (true ));
85
86
assertThat (testAction .onExecuteLatch .await (2 , TimeUnit .SECONDS ), is (false ));
86
87
assertThat (machine .getState ().getIds (), contains (TestStates .S1 ));
87
-
88
+
88
89
ctx .close ();
89
90
}
90
-
91
+
92
+ @ SuppressWarnings ({ "unchecked" })
93
+ @ Test
94
+ public void testGuardThrows () throws Exception {
95
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (Config3 .class );
96
+ ObjectStateMachine <TestStates ,TestEvents > machine =
97
+ ctx .getBean (StateMachineSystemConstants .DEFAULT_ID_STATEMACHINE , ObjectStateMachine .class );
98
+ machine .start ();
99
+ assertThat (machine .getState ().getIds (), contains (TestStates .S1 ));
100
+ machine .sendEvent (TestEvents .E1 );
101
+ assertThat (machine .getState ().getIds (), contains (TestStates .S1 ));
102
+ machine .sendEvent (TestEvents .E2 );
103
+ assertThat (machine .getState ().getIds (), contains (TestStates .S1 ));
104
+ machine .sendEvent (TestEvents .E3 );
105
+ assertThat (machine .getState ().getIds (), contains (TestStates .S2 ));
106
+ ctx .close ();
107
+ }
108
+
91
109
@ Configuration
92
110
@ EnableStateMachine
93
111
public static class Config1 extends EnumStateMachineConfigurerAdapter <TestStates , TestEvents > {
@@ -116,12 +134,12 @@ public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> t
116
134
public TestAction testAction () {
117
135
return new TestAction ();
118
136
}
119
-
137
+
120
138
@ Bean
121
139
public TestGuard testGuard () {
122
140
return new TestGuard (true );
123
141
}
124
-
142
+
125
143
@ Bean
126
144
public TaskExecutor taskExecutor () {
127
145
return new SyncTaskExecutor ();
@@ -152,12 +170,12 @@ public void configure(StateMachineTransitionConfigurer<TestStates, TestEvents> t
152
170
.action (testAction ())
153
171
.guard (testGuard ());
154
172
}
155
-
173
+
156
174
@ Bean
157
175
public TestGuard testGuard () {
158
176
return new TestGuard (false );
159
177
}
160
-
178
+
161
179
@ Bean
162
180
public TestAction testAction () {
163
181
return new TestAction ();
@@ -169,5 +187,61 @@ public TaskExecutor taskExecutor() {
169
187
}
170
188
171
189
}
172
-
190
+
191
+ @ Configuration
192
+ @ EnableStateMachine
193
+ public static class Config3 extends EnumStateMachineConfigurerAdapter <TestStates , TestEvents > {
194
+
195
+ @ Override
196
+ public void configure (StateMachineStateConfigurer <TestStates , TestEvents > states ) throws Exception {
197
+ states
198
+ .withStates ()
199
+ .initial (TestStates .S1 )
200
+ .state (TestStates .S1 )
201
+ .state (TestStates .S2 );
202
+ }
203
+
204
+ @ Override
205
+ public void configure (StateMachineTransitionConfigurer <TestStates , TestEvents > transitions ) throws Exception {
206
+ transitions
207
+ .withExternal ()
208
+ .source (TestStates .S1 )
209
+ .target (TestStates .S2 )
210
+ .event (TestEvents .E1 )
211
+ .guard (testGuard1 ())
212
+ .and ()
213
+ .withExternal ()
214
+ .source (TestStates .S1 )
215
+ .target (TestStates .S2 )
216
+ .event (TestEvents .E2 )
217
+ .guard (testGuard2 ())
218
+ .and ()
219
+ .withExternal ()
220
+ .source (TestStates .S1 )
221
+ .target (TestStates .S2 )
222
+ .event (TestEvents .E3 );
223
+ }
224
+
225
+ @ Bean
226
+ public Guard <TestStates , TestEvents > testGuard1 () {
227
+ return new Guard <TestStates , TestEvents >() {
228
+
229
+ @ Override
230
+ public boolean evaluate (StateContext <TestStates , TestEvents > context ) {
231
+ throw new RuntimeException ();
232
+ }
233
+ };
234
+ }
235
+
236
+ @ Bean
237
+ public Guard <TestStates , TestEvents > testGuard2 () {
238
+ return new Guard <TestStates , TestEvents >() {
239
+
240
+ @ Override
241
+ public boolean evaluate (StateContext <TestStates , TestEvents > context ) {
242
+ throw new Error ();
243
+ }
244
+ };
245
+ }
246
+ }
173
247
}
0 commit comments