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.io.Serializable;
020: import java.net.URL;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028: import junit.textui.TestRunner;
029:
030: import org.apache.commons.scxml.env.Tracer;
031: import org.apache.commons.scxml.env.jexl.JexlContext;
032: import org.apache.commons.scxml.env.jexl.JexlEvaluator;
033: import org.apache.commons.scxml.model.SCXML;
034: import org.apache.commons.scxml.model.State;
035:
036: /**
037: * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
038: */
039: public class WizardsTest extends TestCase {
040: /**
041: * Construct a new instance of SCXMLExecutorTest with
042: * the specified name
043: */
044: public WizardsTest(String name) {
045: super (name);
046: }
047:
048: public static Test suite() {
049: TestSuite suite = new TestSuite(WizardsTest.class);
050: suite.setName("SCXML Executor Tests, Wizard Style documents");
051: return suite;
052: }
053:
054: // Test data
055: private URL wizard01, wizard02;
056: private SCXMLExecutor exec;
057:
058: /**
059: * Set up instance variables required by this test case.
060: */
061: public void setUp() {
062: wizard01 = this .getClass().getClassLoader().getResource(
063: "org/apache/commons/scxml/env/jexl/wizard-01.xml");
064: wizard02 = this .getClass().getClassLoader().getResource(
065: "org/apache/commons/scxml/env/jexl/wizard-02.xml");
066: }
067:
068: /**
069: * Tear down instance variables required by this test case.
070: */
071: public void tearDown() {
072: wizard01 = wizard02 = null;
073: }
074:
075: /**
076: * Test the wizard style SCXML documents, and send usage
077: */
078: public void testWizard01Sample() {
079: exec = SCXMLTestHelper.getExecutor(wizard01);
080: assertNotNull(exec);
081: try {
082: Set currentStates = exec.getCurrentStatus().getStates();
083: assertEquals(1, currentStates.size());
084: assertEquals("state1", ((State) currentStates.iterator()
085: .next()).getId());
086: exec = SCXMLTestHelper.testExecutorSerializability(exec);
087: currentStates = SCXMLTestHelper.fireEvent(exec, "event2");
088: assertEquals(1, currentStates.size());
089: assertEquals("state2", ((State) currentStates.iterator()
090: .next()).getId());
091: currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
092: assertEquals(1, currentStates.size());
093: assertEquals("state4", ((State) currentStates.iterator()
094: .next()).getId());
095: currentStates = SCXMLTestHelper.fireEvent(exec, "event3");
096: assertEquals(1, currentStates.size());
097: assertEquals("state3", ((State) currentStates.iterator()
098: .next()).getId());
099: exec = SCXMLTestHelper.testExecutorSerializability(exec);
100: currentStates = SCXMLTestHelper.fireEvent(exec, "event3"); // ensure we stay put
101: assertEquals(1, currentStates.size());
102: assertEquals("state3", ((State) currentStates.iterator()
103: .next()).getId());
104: } catch (Exception e) {
105: fail(e.getMessage());
106: }
107: }
108:
109: public void testWizard02Sample() {
110: SCXML scxml = SCXMLTestHelper.digest(wizard02);
111: exec = SCXMLTestHelper.getExecutor(new JexlContext(),
112: new JexlEvaluator(), scxml, new TestEventDispatcher(),
113: new Tracer());
114: assertNotNull(exec);
115: try {
116: // If you change this, you must also change
117: // the TestEventDispatcher
118: Set currentStates = exec.getCurrentStatus().getStates();
119: assertEquals(1, currentStates.size());
120: assertEquals("state2", ((State) currentStates.iterator()
121: .next()).getId());
122: exec = SCXMLTestHelper.testExecutorSerializability(exec);
123: currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
124: assertEquals(1, currentStates.size());
125: assertEquals("state4", ((State) currentStates.iterator()
126: .next()).getId());
127: } catch (Exception e) {
128: fail(e.getMessage());
129: }
130: }
131:
132: static class TestEventDispatcher implements EventDispatcher,
133: Serializable {
134: private static final long serialVersionUID = 1L;
135: // If you change this, you must also change testWizard02Sample()
136: int callback = 0;
137:
138: public void send(String sendId, String target,
139: String targetType, String event, Map params,
140: Object hints, long delay, List externalNodes) {
141: int i = ((Integer) params.get("aValue")).intValue();
142: switch (callback) {
143: case 0:
144: assertTrue(i == 2); // state2
145: callback++;
146: break;
147: case 1:
148: assertTrue(i == 4); // state4
149: callback++;
150: break;
151: default:
152: fail("More than 2 TestEventDispatcher <send> callbacks");
153: }
154: }
155:
156: public void cancel(String sendId) {
157: // should never be called
158: fail("<cancel> TestEventDispatcher callback unexpected");
159: }
160: }
161:
162: public static void main(String args[]) {
163: TestRunner.run(suite());
164: }
165: }
|