1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.scxml.model;
17  
18  import java.net.URL;
19  import java.util.Set;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  import org.apache.commons.scxml.SCXMLExecutor;
26  import org.apache.commons.scxml.SCXMLTestHelper;
27  
28  public class HistoryTest extends TestCase {
29      /***
30       * Construct a new instance of HistoryTest with
31       * the specified name
32       */
33      public HistoryTest(String testName) {
34          super(testName);
35      }
36  
37      public static Test suite() {
38          return new TestSuite(HistoryTest.class);
39      }
40  
41      public static void main(String args[]) {
42          String[] testCaseName = { HistoryTest.class.getName()};
43          junit.textui.TestRunner.main(testCaseName);
44      }
45  
46      // Test data
47      private History history;
48      private URL shallow01, deep01, defaults01;
49      private SCXMLExecutor exec;
50  
51      /***
52       * Set up instance variables required by this test case.
53       */   
54      public void setUp() {
55          history = new History();
56          shallow01 = this.getClass().getClassLoader().
57              getResource("org/apache/commons/scxml/history-shallow-01.xml");
58          deep01 = this.getClass().getClassLoader().
59              getResource("org/apache/commons/scxml/history-deep-01.xml");
60          defaults01 = this.getClass().getClassLoader().
61              getResource("org/apache/commons/scxml/history-default-01.xml");
62      }
63  
64      /***
65       * Tear down instance variables required by this test case.
66       */
67      public void tearDown() {
68          history = null;
69          shallow01 = deep01 = defaults01 = null;
70          exec = null;
71      }
72  
73      /***
74       * Test the implementation
75       */
76      public void testSetTypeDeep() {
77          history.setType("deep");
78          
79          assertTrue(history.isDeep());
80      }
81      
82      public void testSetTypeNotDeep() {
83          history.setType("shallow");
84          
85          assertFalse(history.isDeep());
86      }
87  
88      public void testShallowHistory01() {
89          exec = SCXMLTestHelper.getExecutor(shallow01);
90          runHistoryFlow();
91      }
92  
93      public void testDeepHistory01() {
94          exec = SCXMLTestHelper.getExecutor(deep01);
95          runHistoryFlow();
96      }
97  
98      public void testHistoryDefaults01() {
99          exec = SCXMLTestHelper.getExecutor(defaults01);
100         Set currentStates = exec.getCurrentStatus().getStates();
101         assertEquals(1, currentStates.size());
102         assertEquals("state11", ((State)currentStates.iterator().
103             next()).getId());
104         currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
105         assertEquals(1, currentStates.size());
106         assertEquals("state211", ((State)currentStates.iterator().
107             next()).getId());
108         currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
109         assertEquals(1, currentStates.size());
110         assertEquals("state31", ((State)currentStates.iterator().
111             next()).getId());
112     }
113 
114     private void runHistoryFlow() {
115         Set currentStates = exec.getCurrentStatus().getStates();
116         assertEquals(1, currentStates.size());
117         assertEquals("phase1", ((State)currentStates.iterator().
118             next()).getId());
119         assertEquals("phase1", pauseAndResume());
120         assertEquals("phase2", nextPhase());
121         // pause and resume couple of times for good measure
122         assertEquals("phase2", pauseAndResume());
123         assertEquals("phase2", pauseAndResume());
124         assertEquals("phase3", nextPhase());
125         assertEquals("phase3", pauseAndResume());
126         try {
127             exec.reset();
128         } catch (ModelException me) {
129             fail(me.getMessage());
130         }
131         currentStates = exec.getCurrentStatus().getStates();
132         assertEquals(1, currentStates.size());
133         assertEquals("phase1", ((State)currentStates.iterator().
134             next()).getId());
135     }
136 
137     private String pauseAndResume() {
138         Set currentStates = SCXMLTestHelper.fireEvent(exec, "flow.pause");
139         assertEquals(1, currentStates.size());
140         assertEquals("interrupted", ((State)currentStates.iterator().
141             next()).getId());
142         currentStates = SCXMLTestHelper.fireEvent(exec, "flow.resume");
143         assertEquals(1, currentStates.size());
144         return ((State)currentStates.iterator().next()).getId();
145     }
146 
147     private String nextPhase() {
148         Set currentStates = SCXMLTestHelper.fireEvent(exec, "phase.done");
149         assertEquals(1, currentStates.size());
150         return ((State)currentStates.iterator().next()).getId();
151     }
152 
153 }