1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2;
18
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.commons.scxml2.model.EnterableState;
25 import org.apache.commons.scxml2.model.TransitionTarget;
26 import org.junit.Assert;
27 import org.junit.Test;
28
29
30
31 public class SCXMLExecutorTest {
32
33
34
35
36 @Test
37 public void testSCXMLExecutorMicrowave01JexlSample() throws Exception {
38 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/microwave-01.xml");
39 exec.go();
40 checkMicrowave01Sample(exec);
41 }
42
43 @Test
44 public void testSCXMLExecutorMicrowave02JexlSample() throws Exception {
45 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/microwave-02.xml");
46 exec.go();
47 checkMicrowave02Sample(exec);
48 }
49
50 @Test
51 public void testSCXMLExecutorMicrowave03JexlSample() throws Exception {
52 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/microwave-03.xml");
53 exec.go();
54 checkMicrowave01Sample(exec);
55 }
56
57 @Test
58 public void testSCXMLExecutorMicrowave04JexlSample() throws Exception {
59 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/microwave-04.xml");
60 exec.go();
61 checkMicrowave02Sample(exec);
62 }
63
64 @Test
65 public void testSCXMLExecutorMicrowave05JexlSample() throws Exception {
66 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/microwave-05.xml");
67 exec.go();
68 checkMicrowave02Sample(exec);
69 }
70
71 @Test
72 public void testSCXMLExecutorMicrowave01grvSample() throws Exception {
73 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/groovy/microwave-01.xml");
74 exec.go();
75 checkMicrowave01Sample(exec);
76 }
77
78 @Test
79 public void testSCXMLExecutorMicrowave02grvSample() throws Exception {
80 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/groovy/microwave-02.xml");
81 exec.go();
82 checkMicrowave02Sample(exec);
83 }
84
85 @Test
86 public void testSCXMLExecutorMicrowave03grvSample() throws Exception {
87 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/groovy/microwave-03.xml");
88 exec.go();
89 checkMicrowave01Sample(exec);
90 }
91
92 @Test
93 public void testSCXMLExecutorMicrowave04grvSample() throws Exception {
94 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/groovy/microwave-04.xml");
95 exec.go();
96 checkMicrowave02Sample(exec);
97 }
98
99 @Test
100 public void testSCXMLExecutorMicrowave05grvSample() throws Exception {
101 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/groovy/microwave-05.xml");
102 exec.go();
103 checkMicrowave02Sample(exec);
104 }
105
106 @Test
107 public void testSCXMLExecutorPrefix01Sample() throws Exception {
108 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/prefix-01.xml");
109 exec.go();
110 Set<EnterableState> currentStates = exec.getStatus().getStates();
111 Assert.assertEquals(1, currentStates.size());
112 Assert.assertEquals("ten", currentStates.iterator().next().getId());
113 currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
114 Assert.assertEquals(1, currentStates.size());
115 Assert.assertEquals("twenty", currentStates.iterator().next().getId());
116 }
117
118 @Test
119 public void testSCXMLExecutorTransitions01Sample() throws Exception {
120 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-01.xml");
121 exec.go();
122 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
123 Assert.assertEquals(1, currentStates.size());
124 Assert.assertEquals("twenty_one", currentStates.iterator().next().getId());
125 currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.twenty_one");
126 Assert.assertEquals(1, currentStates.size());
127 Assert.assertEquals("twenty_two", currentStates.iterator().next().getId());
128 SCXMLTestHelper.fireEvent(exec, "done.state.twenty_two");
129 Assert.assertEquals(3, exec.getStatus().getStates().size());
130 }
131
132 @Test
133 public void testSCXMLExecutorTransitions02Sample() throws Exception {
134 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-02.xml");
135 exec.go();
136 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "ten.stay");
137 Assert.assertEquals(1, currentStates.size());
138 Assert.assertEquals("ten", currentStates.iterator().next().getId());
139 exec = SCXMLTestHelper.testInstanceSerializability(exec);
140 currentStates = SCXMLTestHelper.fireEvent(exec, "ten.self");
141 Assert.assertEquals(1, currentStates.size());
142 Assert.assertEquals("ten", currentStates.iterator().next().getId());
143 currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
144 Assert.assertEquals(1, currentStates.size());
145 Assert.assertEquals("twenty", currentStates.iterator().next().getId());
146 }
147
148 @Test
149 public void testSCXMLExecutorTransitions03Sample() throws Exception {
150 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-03.xml");
151 exec.go();
152 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
153 Assert.assertEquals(3, currentStates.size());
154 Set<String> expected = new HashSet<String>();
155 expected.add("twenty_one_2");
156 expected.add("twenty_two_2");
157 expected.add("twenty_three_2");
158 for (TransitionTarget tt : currentStates) {
159 if (!expected.remove(tt.getId())) {
160 Assert.fail("'" + tt.getId()
161 + "' is not an expected current state ID");
162 }
163 }
164 }
165
166 @Test
167 public void testSCXMLExecutorTransitions04Sample() throws Exception {
168 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-04.xml");
169 exec.go();
170 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
171 Assert.assertEquals(3, currentStates.size());
172 Set<String> expected = new HashSet<String>();
173 expected.add("twenty_one_1");
174 expected.add("twenty_two_1");
175 expected.add("twenty_three_1");
176 for (TransitionTarget tt : currentStates) {
177 if (!expected.remove(tt.getId())) {
178 Assert.fail("'" + tt.getId()
179 + "' is not an expected current state ID");
180 }
181 }
182 currentStates = SCXMLTestHelper.fireEvent(exec, "bar");
183 Assert.assertEquals(1, currentStates.size());
184 Assert.assertEquals("thirty", (currentStates.iterator().
185 next()).getId());
186 }
187
188 @Test
189 public void testSCXMLExecutorTransitions05Sample() throws Exception {
190 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-05.xml");
191 exec.go();
192 SCXMLTestHelper.assertPostTriggerState(exec, "foo", "end");
193 }
194
195 @Test
196 public void testSCXMLExecutorTransitions06Sample() throws Exception {
197 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-06.xml");
198 exec.go();
199 SCXMLTestHelper.assertPostTriggerStates(exec, "start", new String[]{"one", "two"});
200 SCXMLTestHelper.assertPostTriggerState(exec, "onetwo_three", "three");
201 SCXMLTestHelper.assertPostTriggerStates(exec, "three_one", new String[]{"one", "two"});
202 SCXMLTestHelper.assertPostTriggerState(exec, "two_four", "four");
203 }
204
205 @Test
206 public void testSend01Sample() throws Exception {
207 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/send-01.xml");
208 exec.go();
209 Set<EnterableState> currentStates = exec.getStatus().getStates();
210 Assert.assertEquals(1, currentStates.size());
211 Assert.assertEquals("ten", currentStates.iterator().next().getId());
212 currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
213 Assert.assertEquals(1, currentStates.size());
214 Assert.assertEquals("twenty", currentStates.iterator().next().getId());
215 }
216
217 @Test
218 public void testSend02TypeSCXMLSample() throws Exception {
219 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/send-02.xml");
220 exec.go();
221 Set<EnterableState> currentStates = exec.getStatus().getStates();
222 Assert.assertEquals(1, currentStates.size());
223 Assert.assertEquals("ninety", currentStates.iterator().next().getId());
224 Assert.assertTrue(exec.getStatus().isFinal());
225 }
226
227 @Test
228 public void testSCXMLExecutorTransitionsWithCond01Sample() throws Exception {
229 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-with-cond-01.xml");
230 exec.go();
231 Map<String, Object> payload = new HashMap<String, Object>();
232
233
234 payload.put("keyed", Boolean.TRUE);
235 SCXMLTestHelper.assertPostTriggerState(exec, "open", payload, "opened");
236
237 SCXMLTestHelper.assertPostTriggerState(exec, "close", payload, "closed");
238
239
240 payload.put("keyed", Boolean.FALSE);
241 SCXMLTestHelper.assertPostTriggerState(exec, "open", payload, "closed");
242
243
244 payload.clear();
245 SCXMLTestHelper.assertPostTriggerState(exec, "open", payload, "closed");
246
247
248 SCXMLTestHelper.assertPostTriggerState(exec, "open", null, "closed");
249
250
251 SCXMLTestHelper.assertPostTriggerState(exec, "lock", null, "locked");
252
253 SCXMLTestHelper.assertPostTriggerState(exec, "unlock", null, "locked");
254 }
255
256 @Test
257 public void testSCXMLExecutorSystemEventVariable() throws Exception {
258 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-event-variable.xml");
259 exec.go();
260 Map<String, Object> payload = new HashMap<String, Object>();
261 payload.put("keyed", Boolean.TRUE);
262 SCXMLTestHelper.assertPostTriggerState(exec, "open", payload, "opened");
263 }
264
265 @Test
266 public void testSCXMLExecutorSetConfiguration() throws Exception {
267 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/transitions-01.xml");
268 exec.go();
269 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
270 Assert.assertEquals(1, currentStates.size());
271 Assert.assertEquals("twenty_one", currentStates.iterator().next().getId());
272 currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.twenty_one");
273 Assert.assertEquals(1, currentStates.size());
274 Assert.assertEquals("twenty_two", currentStates.iterator().next().getId());
275 Set<String> stateIds = new HashSet<String>();
276 stateIds.add("twenty_one");
277 exec.setConfiguration(stateIds);
278 Assert.assertEquals(1, exec.getStatus().getStates().size());
279 SCXMLTestHelper.fireEvent(exec, "done.state.twenty_one");
280 Assert.assertEquals(1, currentStates.size());
281 Assert.assertEquals("twenty_two", currentStates.iterator().next().getId());
282 }
283
284 private void checkMicrowave01Sample(SCXMLExecutor exec) throws Exception {
285 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
286 Assert.assertEquals(1, currentStates.size());
287 Assert.assertEquals("cooking", currentStates.iterator().next().getId());
288 }
289
290 private void checkMicrowave02Sample(SCXMLExecutor exec) throws Exception {
291 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
292 Assert.assertEquals(2, currentStates.size());
293 String id = (currentStates.iterator().next()).getId();
294 Assert.assertTrue(id.equals("closed") || id.equals("cooking"));
295 }
296 }