1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2.model;
18
19 import java.util.Set;
20
21 import org.apache.commons.scxml2.SCXMLExecutor;
22 import org.apache.commons.scxml2.SCXMLTestHelper;
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 public class HistoryTest {
27
28 @Test
29 public void testSetTypeDeep() {
30 History history = new History();
31 history.setType("deep");
32
33 Assert.assertTrue(history.isDeep());
34 }
35
36 @Test
37 public void testSetTypeNotDeep() {
38 History history = new History();
39 history.setType("shallow");
40
41 Assert.assertFalse(history.isDeep());
42 }
43
44 @Test
45 public void testShallowHistory01() throws Exception {
46 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-shallow-01.xml");
47 exec.go();
48 runHistoryFlow(exec);
49 }
50
51 @Test
52 public void testDeepHistory01() throws Exception {
53 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-deep-01.xml");
54 exec.go();
55 runHistoryFlow(exec);
56 }
57
58 @Test
59 public void testHistoryDefaults01() throws Exception {
60 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-default-01.xml");
61 exec.go();
62 Set<EnterableState> currentStates = exec.getStatus().getStates();
63 Assert.assertEquals(1, currentStates.size());
64 Assert.assertEquals("state11", currentStates.iterator().next().getId());
65 currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
66 Assert.assertEquals(1, currentStates.size());
67 Assert.assertEquals("state211", currentStates.iterator().next().getId());
68 currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
69 Assert.assertEquals(1, currentStates.size());
70 Assert.assertEquals("state31", currentStates.iterator().next().getId());
71 }
72
73 @Test
74 public void testHistoryParallel01() throws Exception {
75 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-parallel-01.xml");
76 exec.go();
77 Set<EnterableState> currentStates = exec.getStatus().getStates();
78 Assert.assertEquals(1, currentStates.size());
79 SCXMLTestHelper.assertState(exec, "off_call");
80 SCXMLTestHelper.assertPostTriggerStates(exec, "dial", new String[] { "talking", "on_call" });
81 SCXMLTestHelper.assertPostTriggerStates(exec, "consult", new String[] { "consult_talking", "on_consult" });
82
83 SCXMLTestHelper.assertPostTriggerStates(exec, "alternate", new String[] { "talking", "on_call" });
84
85 SCXMLTestHelper.assertPostTriggerStates(exec, "hold", new String[] { "held", "on_call" });
86 SCXMLTestHelper.assertPostTriggerStates(exec, "consult", new String[] { "consult_talking", "on_consult" });
87
88 SCXMLTestHelper.assertPostTriggerStates(exec, "alternate", new String[] { "held", "on_call" });
89 }
90
91 private void runHistoryFlow(SCXMLExecutor exec) throws Exception {
92 Set<EnterableState> currentStates = exec.getStatus().getStates();
93 Assert.assertEquals(1, currentStates.size());
94 Assert.assertEquals("phase1", currentStates.iterator().next().getId());
95 Assert.assertEquals("phase1", pauseAndResume(exec));
96 Assert.assertEquals("phase2", nextPhase(exec));
97
98 Assert.assertEquals("phase2", pauseAndResume(exec));
99 Assert.assertEquals("phase2", pauseAndResume(exec));
100 Assert.assertEquals("phase3", nextPhase(exec));
101 Assert.assertEquals("phase3", pauseAndResume(exec));
102 exec.reset();
103 currentStates = exec.getStatus().getStates();
104 Assert.assertEquals(1, currentStates.size());
105 Assert.assertEquals("phase1", currentStates.iterator().next().getId());
106 }
107
108 private String pauseAndResume(SCXMLExecutor exec) throws Exception {
109 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "flow.pause");
110 Assert.assertEquals(1, currentStates.size());
111 Assert.assertEquals("interrupted", currentStates.iterator().next().getId());
112 exec = SCXMLTestHelper.testInstanceSerializability(exec);
113 currentStates = SCXMLTestHelper.fireEvent(exec, "flow.resume");
114 Assert.assertEquals(1, currentStates.size());
115 SCXMLTestHelper.testInstanceSerializability(exec);
116 return currentStates.iterator().next().getId();
117 }
118
119 private String nextPhase(SCXMLExecutor exec) throws Exception {
120 Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.phase");
121 Assert.assertEquals(1, currentStates.size());
122 return currentStates.iterator().next().getId();
123 }
124
125 }