001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml.model;
018:
019: import java.net.URL;
020: import java.util.Set;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.scxml.SCXMLExecutor;
027: import org.apache.commons.scxml.SCXMLTestHelper;
028:
029: public class HistoryTest extends TestCase {
030: /**
031: * Construct a new instance of HistoryTest with
032: * the specified name
033: */
034: public HistoryTest(String testName) {
035: super (testName);
036: }
037:
038: public static Test suite() {
039: return new TestSuite(HistoryTest.class);
040: }
041:
042: public static void main(String args[]) {
043: String[] testCaseName = { HistoryTest.class.getName() };
044: junit.textui.TestRunner.main(testCaseName);
045: }
046:
047: // Test data
048: private History history;
049: private URL shallow01, deep01, defaults01;
050: private SCXMLExecutor exec;
051:
052: /**
053: * Set up instance variables required by this test case.
054: */
055: public void setUp() {
056: history = new History();
057: shallow01 = this .getClass().getClassLoader().getResource(
058: "org/apache/commons/scxml/history-shallow-01.xml");
059: deep01 = this .getClass().getClassLoader().getResource(
060: "org/apache/commons/scxml/history-deep-01.xml");
061: defaults01 = this .getClass().getClassLoader().getResource(
062: "org/apache/commons/scxml/history-default-01.xml");
063: }
064:
065: /**
066: * Tear down instance variables required by this test case.
067: */
068: public void tearDown() {
069: history = null;
070: shallow01 = deep01 = defaults01 = null;
071: exec = null;
072: }
073:
074: /**
075: * Test the implementation
076: */
077: public void testSetTypeDeep() {
078: history.setType("deep");
079:
080: assertTrue(history.isDeep());
081: }
082:
083: public void testSetTypeNotDeep() {
084: history.setType("shallow");
085:
086: assertFalse(history.isDeep());
087: }
088:
089: public void testShallowHistory01() {
090: exec = SCXMLTestHelper.getExecutor(shallow01);
091: runHistoryFlow();
092: }
093:
094: public void testDeepHistory01() {
095: exec = SCXMLTestHelper.getExecutor(deep01);
096: runHistoryFlow();
097: }
098:
099: public void testHistoryDefaults01() {
100: exec = SCXMLTestHelper.getExecutor(defaults01);
101: Set currentStates = exec.getCurrentStatus().getStates();
102: assertEquals(1, currentStates.size());
103: assertEquals("state11", ((State) currentStates.iterator()
104: .next()).getId());
105: currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
106: assertEquals(1, currentStates.size());
107: assertEquals("state211", ((State) currentStates.iterator()
108: .next()).getId());
109: currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
110: assertEquals(1, currentStates.size());
111: assertEquals("state31", ((State) currentStates.iterator()
112: .next()).getId());
113: }
114:
115: private void runHistoryFlow() {
116: Set currentStates = exec.getCurrentStatus().getStates();
117: assertEquals(1, currentStates.size());
118: assertEquals("phase1",
119: ((State) currentStates.iterator().next()).getId());
120: assertEquals("phase1", pauseAndResume());
121: assertEquals("phase2", nextPhase());
122: // pause and resume couple of times for good measure
123: assertEquals("phase2", pauseAndResume());
124: assertEquals("phase2", pauseAndResume());
125: assertEquals("phase3", nextPhase());
126: assertEquals("phase3", pauseAndResume());
127: try {
128: exec.reset();
129: } catch (ModelException me) {
130: fail(me.getMessage());
131: }
132: currentStates = exec.getCurrentStatus().getStates();
133: assertEquals(1, currentStates.size());
134: assertEquals("phase1",
135: ((State) currentStates.iterator().next()).getId());
136: }
137:
138: private String pauseAndResume() {
139: Set currentStates = SCXMLTestHelper.fireEvent(exec,
140: "flow.pause");
141: assertEquals(1, currentStates.size());
142: assertEquals("interrupted", ((State) currentStates.iterator()
143: .next()).getId());
144: exec = SCXMLTestHelper.testExecutorSerializability(exec);
145: currentStates = SCXMLTestHelper.fireEvent(exec, "flow.resume");
146: assertEquals(1, currentStates.size());
147: exec = SCXMLTestHelper.testExecutorSerializability(exec);
148: return ((State) currentStates.iterator().next()).getId();
149: }
150:
151: private String nextPhase() {
152: Set currentStates = SCXMLTestHelper.fireEvent(exec,
153: "phase.done");
154: assertEquals(1, currentStates.size());
155: return ((State) currentStates.iterator().next()).getId();
156: }
157:
158: }
|