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;
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.env.SimpleContext;
028: import org.apache.commons.scxml.env.jsp.ELEvaluator;
029: import org.apache.commons.scxml.model.State;
030:
031: /**
032: * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
033: */
034: public class SCXMLExecutorTest extends TestCase {
035: /**
036: * Construct a new instance of SCXMLExecutorTest with
037: * the specified name
038: */
039: public SCXMLExecutorTest(String name) {
040: super (name);
041: }
042:
043: public static Test suite() {
044: TestSuite suite = new TestSuite(SCXMLExecutorTest.class);
045: suite.setName("SCXML Executor Tests");
046: return suite;
047: }
048:
049: // Test data
050: private URL microwave01jsp, microwave02jsp, microwave01jexl,
051: microwave02jexl, transitions01, transitions02, prefix01,
052: send01, send02;
053: private SCXMLExecutor exec;
054:
055: /**
056: * Set up instance variables required by this test case.
057: */
058: public void setUp() {
059: microwave01jsp = this .getClass().getClassLoader().getResource(
060: "org/apache/commons/scxml/env/jsp/microwave-01.xml");
061: microwave02jsp = this .getClass().getClassLoader().getResource(
062: "org/apache/commons/scxml/env/jsp/microwave-02.xml");
063: microwave01jexl = this .getClass().getClassLoader().getResource(
064: "org/apache/commons/scxml/env/jexl/microwave-01.xml");
065: microwave02jexl = this .getClass().getClassLoader().getResource(
066: "org/apache/commons/scxml/env/jexl/microwave-02.xml");
067: transitions01 = this .getClass().getClassLoader().getResource(
068: "org/apache/commons/scxml/transitions-01.xml");
069: transitions02 = this .getClass().getClassLoader().getResource(
070: "org/apache/commons/scxml/transitions-02.xml");
071: prefix01 = this .getClass().getClassLoader().getResource(
072: "org/apache/commons/scxml/prefix-01.xml");
073: send01 = this .getClass().getClassLoader().getResource(
074: "org/apache/commons/scxml/send-01.xml");
075: send02 = this .getClass().getClassLoader().getResource(
076: "org/apache/commons/scxml/send-02.xml");
077: }
078:
079: /**
080: * Tear down instance variables required by this test case.
081: */
082: public void tearDown() {
083: microwave01jsp = microwave02jsp = microwave01jexl = microwave02jexl = transitions01 = transitions02 = prefix01 = send01 = send02 = null;
084: }
085:
086: /**
087: * Test the implementation
088: */
089: public void testSCXMLExecutorMicrowave01JspSample() {
090: exec = SCXMLTestHelper.getExecutor(microwave01jsp,
091: new SimpleContext(), new ELEvaluator());
092: assertNotNull(exec);
093: checkMicrowave01Sample();
094: }
095:
096: public void testSCXMLExecutorMicrowave02JspSample() {
097: exec = SCXMLTestHelper.getExecutor(microwave02jsp,
098: new SimpleContext(), new ELEvaluator());
099: assertNotNull(exec);
100: checkMicrowave02Sample();
101: }
102:
103: public void testSCXMLExecutorMicrowave01JexlSample() {
104: exec = SCXMLTestHelper.getExecutor(microwave01jexl);
105: assertNotNull(exec);
106: checkMicrowave01Sample();
107: }
108:
109: public void testSCXMLExecutorMicrowave02JexlSample() {
110: exec = SCXMLTestHelper.getExecutor(microwave02jexl);
111: assertNotNull(exec);
112: checkMicrowave02Sample();
113: }
114:
115: public void testSCXMLExecutorPrefix01Sample() {
116: exec = SCXMLTestHelper.getExecutor(prefix01);
117: assertNotNull(exec);
118: Set currentStates = exec.getCurrentStatus().getStates();
119: assertEquals(1, currentStates.size());
120: assertEquals("ten", ((State) currentStates.iterator().next())
121: .getId());
122: currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
123: assertEquals(1, currentStates.size());
124: assertEquals("twenty",
125: ((State) currentStates.iterator().next()).getId());
126: }
127:
128: public void testSCXMLExecutorTransitions01Sample() {
129: exec = SCXMLTestHelper.getExecutor(transitions01);
130: assertNotNull(exec);
131: try {
132: Set currentStates = SCXMLTestHelper.fireEvent(exec,
133: "ten.done");
134: assertEquals(1, currentStates.size());
135: assertEquals("twenty_one", ((State) currentStates
136: .iterator().next()).getId());
137: currentStates = SCXMLTestHelper.fireEvent(exec,
138: "twenty_one.done");
139: assertEquals(1, currentStates.size());
140: assertEquals("twenty_two", ((State) currentStates
141: .iterator().next()).getId());
142: currentStates = SCXMLTestHelper.fireEvent(exec,
143: "twenty_two.done");
144: assertEquals(3, exec.getCurrentStatus().getStates().size());
145: } catch (Exception e) {
146: fail(e.getMessage());
147: }
148: }
149:
150: public void testSCXMLExecutorTransitions02Sample() {
151: exec = SCXMLTestHelper.getExecutor(transitions02);
152: assertNotNull(exec);
153: try {
154: Set currentStates = SCXMLTestHelper.fireEvent(exec,
155: "ten.stay");
156: assertEquals(1, currentStates.size());
157: assertEquals("ten", ((State) currentStates.iterator()
158: .next()).getId());
159: exec = SCXMLTestHelper.testExecutorSerializability(exec);
160: currentStates = SCXMLTestHelper.fireEvent(exec, "ten.self");
161: assertEquals(1, currentStates.size());
162: assertEquals("ten", ((State) currentStates.iterator()
163: .next()).getId());
164: currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
165: assertEquals(1, currentStates.size());
166: assertEquals("twenty", ((State) currentStates.iterator()
167: .next()).getId());
168: } catch (Exception e) {
169: fail(e.getMessage());
170: }
171: }
172:
173: public void testSend01Sample() {
174: exec = SCXMLTestHelper.getExecutor(send01);
175: assertNotNull(exec);
176: try {
177: Set currentStates = exec.getCurrentStatus().getStates();
178: assertEquals(1, currentStates.size());
179: assertEquals("ten", ((State) currentStates.iterator()
180: .next()).getId());
181: currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
182: assertEquals(1, currentStates.size());
183: assertEquals("twenty", ((State) currentStates.iterator()
184: .next()).getId());
185: } catch (Exception e) {
186: fail(e.getMessage());
187: }
188: }
189:
190: public void testSend02TargettypeSCXMLSample() {
191: exec = SCXMLTestHelper.getExecutor(send02);
192: assertNotNull(exec);
193: try {
194: Set currentStates = exec.getCurrentStatus().getStates();
195: assertEquals(1, currentStates.size());
196: assertEquals("ninety", ((State) currentStates.iterator()
197: .next()).getId());
198: } catch (Exception e) {
199: fail(e.getMessage());
200: }
201: }
202:
203: private void checkMicrowave01Sample() {
204: try {
205: Set currentStates = SCXMLTestHelper.fireEvent(exec,
206: "turn_on");
207: assertEquals(1, currentStates.size());
208: assertEquals("cooking", ((State) currentStates.iterator()
209: .next()).getId());
210: } catch (Exception e) {
211: fail(e.getMessage());
212: }
213: }
214:
215: private void checkMicrowave02Sample() {
216: try {
217: Set currentStates = SCXMLTestHelper.fireEvent(exec,
218: "turn_on");
219: assertEquals(2, currentStates.size());
220: String id = ((State) currentStates.iterator().next())
221: .getId();
222: assertTrue(id.equals("closed") || id.equals("cooking"));
223: } catch (Exception e) {
224: fail(e.getMessage());
225: }
226: }
227:
228: public static void main(String args[]) {
229: TestRunner.run(suite());
230: }
231: }
|