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: import junit.textui.TestRunner;
026:
027: import org.apache.commons.scxml.SCXMLExecutor;
028: import org.apache.commons.scxml.SCXMLTestHelper;
029: import org.apache.commons.scxml.TriggerEvent;
030: import org.apache.commons.scxml.env.jsp.ELContext;
031: import org.apache.commons.scxml.env.jsp.ELEvaluator;
032:
033: /**
034: * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
035: */
036: public class StatelessModelTest extends TestCase {
037: /**
038: * Construct a new instance of SCXMLExecutorTest with
039: * the specified name
040: */
041: public StatelessModelTest(String name) {
042: super (name);
043: }
044:
045: public static Test suite() {
046: TestSuite suite = new TestSuite(StatelessModelTest.class);
047: suite.setName("SCXML Executor Tests");
048: return suite;
049: }
050:
051: // Test data
052: private URL stateless01jexl, stateless01jsp;
053: private SCXML scxml01jexl, scxml01jsp;
054: private SCXMLExecutor exec01, exec02;
055:
056: /**
057: * Set up instance variables required by this test case.
058: */
059: public void setUp() {
060: stateless01jexl = this .getClass().getClassLoader().getResource(
061: "org/apache/commons/scxml/env/jexl/stateless-01.xml");
062: stateless01jsp = this .getClass().getClassLoader().getResource(
063: "org/apache/commons/scxml/env/jsp/stateless-01.xml");
064: scxml01jexl = SCXMLTestHelper.digest(stateless01jexl);
065: scxml01jsp = SCXMLTestHelper.digest(stateless01jsp);
066: }
067:
068: /**
069: * Tear down instance variables required by this test case.
070: */
071: public void tearDown() {
072: stateless01jexl = null;
073: }
074:
075: /**
076: * Test the stateless model, simultaneous executions, JEXL expressions
077: */
078: public void testStatelessModelSimultaneousJexl() {
079: // parse once, use many times
080: exec01 = SCXMLTestHelper.getExecutor(scxml01jexl);
081: assertNotNull(exec01);
082: exec02 = SCXMLTestHelper.getExecutor(scxml01jexl);
083: assertNotNull(exec02);
084: assertFalse(exec01 == exec02);
085: runSimultaneousTest();
086: }
087:
088: /**
089: * Test the stateless model, sequential executions, JEXL expressions
090: */
091: public void testStatelessModelSequentialJexl() {
092: // rinse and repeat
093: for (int i = 0; i < 3; i++) {
094: exec01 = SCXMLTestHelper.getExecutor(scxml01jexl);
095: assertNotNull(exec01);
096: runSequentialTest();
097: }
098: }
099:
100: /**
101: * Test the stateless model, simultaneous executions, EL expressions
102: */
103: public void testStatelessModelSimultaneousEl() {
104: // parse once, use many times
105: exec01 = SCXMLTestHelper.getExecutor(scxml01jsp,
106: new ELContext(), new ELEvaluator());
107: assertNotNull(exec01);
108: exec02 = SCXMLTestHelper.getExecutor(scxml01jsp,
109: new ELContext(), new ELEvaluator());
110: assertNotNull(exec02);
111: assertFalse(exec01 == exec02);
112: runSimultaneousTest();
113: }
114:
115: /**
116: * Test the stateless model, sequential executions, EL expressions
117: */
118: public void testStatelessModelSequentialEl() {
119: // rinse and repeat
120: for (int i = 0; i < 3; i++) {
121: exec01 = SCXMLTestHelper.getExecutor(scxml01jsp,
122: new ELContext(), new ELEvaluator());
123: assertNotNull(exec01);
124: runSequentialTest();
125: }
126: }
127:
128: private void runSimultaneousTest() {
129: try {
130: //// Interleaved
131: // exec01
132: Set currentStates = exec01.getCurrentStatus().getStates();
133: assertEquals(1, currentStates.size());
134: assertEquals("ten", ((State) currentStates.iterator()
135: .next()).getId());
136: currentStates = fireEvent("ten.done", exec01);
137: assertEquals(1, currentStates.size());
138: assertEquals("twenty", ((State) currentStates.iterator()
139: .next()).getId());
140: // exec02
141: currentStates = exec02.getCurrentStatus().getStates();
142: assertEquals(1, currentStates.size());
143: assertEquals("ten", ((State) currentStates.iterator()
144: .next()).getId());
145: // exec01
146: currentStates = fireEvent("twenty.done", exec01);
147: assertEquals(1, currentStates.size());
148: assertEquals("thirty", ((State) currentStates.iterator()
149: .next()).getId());
150: // exec02
151: currentStates = fireEvent("ten.done", exec02);
152: assertEquals(1, currentStates.size());
153: assertEquals("twenty", ((State) currentStates.iterator()
154: .next()).getId());
155: currentStates = fireEvent("twenty.done", exec02);
156: assertEquals(1, currentStates.size());
157: assertEquals("thirty", ((State) currentStates.iterator()
158: .next()).getId());
159: } catch (Exception e) {
160: fail(e.getMessage());
161: }
162: }
163:
164: private void runSequentialTest() {
165: try {
166: Set currentStates = exec01.getCurrentStatus().getStates();
167: assertEquals(1, currentStates.size());
168: assertEquals("ten", ((State) currentStates.iterator()
169: .next()).getId());
170: currentStates = fireEvent("ten.done", exec01);
171: assertEquals(1, currentStates.size());
172: assertEquals("twenty", ((State) currentStates.iterator()
173: .next()).getId());
174: currentStates = fireEvent("twenty.done", exec01);
175: assertEquals(1, currentStates.size());
176: assertEquals("thirty", ((State) currentStates.iterator()
177: .next()).getId());
178: } catch (Exception e) {
179: fail(e.getMessage());
180: }
181: }
182:
183: private Set fireEvent(String name, SCXMLExecutor exec) {
184: TriggerEvent[] evts = { new TriggerEvent(name,
185: TriggerEvent.SIGNAL_EVENT, null) };
186: try {
187: exec.triggerEvents(evts);
188: } catch (Exception e) {
189: fail(e.getMessage());
190: }
191: return exec.getCurrentStatus().getStates();
192: }
193:
194: public static void main(String args[]) {
195: TestRunner.run(suite());
196: }
197: }
|