View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // Next line uses history to go back to on call and talking
83          SCXMLTestHelper.assertPostTriggerStates(exec, "alternate", new String[] { "talking", "on_call" });
84          // Hold
85          SCXMLTestHelper.assertPostTriggerStates(exec, "hold", new String[] { "held", "on_call" });
86          SCXMLTestHelper.assertPostTriggerStates(exec, "consult", new String[] { "consult_talking", "on_consult" });
87          // Next line uses history to go back to on call and on hold
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          // pause and resume couple of times for good measure
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 }