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.SimpleScheduler;
028 import org.apache.commons.scxml.env.Tracer;
029 import org.apache.commons.scxml.env.jexl.JexlEvaluator;
030 import org.apache.commons.scxml.model.SCXML;
031 import org.apache.commons.scxml.model.State;
032 /**
033 * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
034 * Testing special variable "_eventdata"
035 */
036 public class EventDataTest extends TestCase {
037 /**
038 * Construct a new instance of SCXMLExecutorTest with
039 * the specified name
040 */
041 public EventDataTest(String name) {
042 super(name);
043 }
044
045 public static Test suite() {
046 TestSuite suite = new TestSuite(EventDataTest.class);
047 suite.setName("SCXML Executor Tests, _eventdata special variable");
048 return suite;
049 }
050
051 // Test data
052 private URL eventdata01, eventdata02, eventdata03, eventdata04;
053 private SCXMLExecutor exec;
054
055 /**
056 * Set up instance variables required by this test case.
057 */
058 public void setUp() {
059 eventdata01 = this.getClass().getClassLoader().
060 getResource("org/apache/commons/scxml/env/jexl/eventdata-01.xml");
061 eventdata02 = this.getClass().getClassLoader().
062 getResource("org/apache/commons/scxml/env/jexl/eventdata-02.xml");
063 eventdata03 = this.getClass().getClassLoader().
064 getResource("org/apache/commons/scxml/env/jexl/eventdata-03.xml");
065 eventdata04 = this.getClass().getClassLoader().
066 getResource("org/apache/commons/scxml/env/jexl/eventdata-04.xml");
067 }
068
069 /**
070 * Tear down instance variables required by this test case.
071 */
072 public void tearDown() {
073 eventdata01 = eventdata02 = eventdata03 = eventdata04 = null;
074 }
075
076 /**
077 * Test the SCXML documents, usage of "_eventdata"
078 */
079 public void testEventdata01Sample() throws Exception {
080 exec = SCXMLTestHelper.getExecutor(eventdata01);
081 assertNotNull(exec);
082 Set currentStates = exec.getCurrentStatus().getStates();
083 assertEquals(1, currentStates.size());
084 assertEquals("state1", ((State)currentStates.iterator().
085 next()).getId());
086 TriggerEvent te = new TriggerEvent("event.foo",
087 TriggerEvent.SIGNAL_EVENT, new Integer(3));
088 currentStates = SCXMLTestHelper.fireEvent(exec, te);
089 assertEquals(1, currentStates.size());
090 assertEquals("state3", ((State)currentStates.iterator().
091 next()).getId());
092 TriggerEvent[] evts = new TriggerEvent[] { te,
093 new TriggerEvent("event.bar", TriggerEvent.SIGNAL_EVENT,
094 new Integer(6))};
095 currentStates = SCXMLTestHelper.fireEvents(exec, evts);
096 assertEquals(1, currentStates.size());
097 assertEquals("state6", ((State)currentStates.iterator().
098 next()).getId());
099 te = new TriggerEvent("event.baz",
100 TriggerEvent.SIGNAL_EVENT, new Integer(7));
101 currentStates = SCXMLTestHelper.fireEvent(exec, te);
102 assertEquals(1, currentStates.size());
103 assertEquals("state7", ((State)currentStates.iterator().
104 next()).getId());
105 }
106
107 public void testEventdata02Sample() throws Exception {
108 exec = SCXMLTestHelper.getExecutor(eventdata02);
109 assertNotNull(exec);
110 Set currentStates = exec.getCurrentStatus().getStates();
111 assertEquals(1, currentStates.size());
112 assertEquals("state0", ((State)currentStates.iterator().
113 next()).getId());
114 TriggerEvent te1 = new TriggerEvent("connection.alerting",
115 TriggerEvent.SIGNAL_EVENT, "line2");
116 currentStates = SCXMLTestHelper.fireEvent(exec, te1);
117 assertEquals(1, currentStates.size());
118 assertEquals("state2", ((State)currentStates.iterator().
119 next()).getId());
120 TriggerEvent te2 = new TriggerEvent("connection.alerting",
121 TriggerEvent.SIGNAL_EVENT,
122 new ConnectionAlertingPayload(4));
123 currentStates = SCXMLTestHelper.fireEvent(exec, te2);
124 assertEquals(1, currentStates.size());
125 assertEquals("state4", ((State)currentStates.iterator().
126 next()).getId());
127 }
128
129 public void testEventdata03Sample() throws Exception {
130 exec = SCXMLTestHelper.getExecutor(eventdata03);
131 assertNotNull(exec);
132 Set currentStates = exec.getCurrentStatus().getStates();
133 assertEquals(1, currentStates.size());
134 assertEquals("ten", ((State)currentStates.iterator().
135 next()).getId());
136 TriggerEvent te = new TriggerEvent("event.foo",
137 TriggerEvent.SIGNAL_EVENT);
138 currentStates = SCXMLTestHelper.fireEvent(exec, te);
139 assertEquals(1, currentStates.size());
140 assertEquals("thirty", ((State)currentStates.iterator().
141 next()).getId());
142 }
143
144 public void testEventdata04Sample() throws Exception {
145 SCXML scxml = SCXMLTestHelper.parse(eventdata04);
146 Tracer trc = new Tracer();
147 exec = new SCXMLExecutor(new JexlEvaluator(), null, trc);
148 assertNotNull(exec);
149 exec.setEventdispatcher(new SimpleScheduler(exec));
150 exec.addListener(scxml, trc);
151 exec.setStateMachine(scxml);
152 exec.go();
153 Thread.sleep(200); // let the 100 delay lapse
154 }
155
156 public static class ConnectionAlertingPayload {
157 private int line;
158 public ConnectionAlertingPayload(int line) {
159 this.line = line;
160 }
161 public void setLine(int line) {
162 this.line = line;
163 }
164 public int getLine() {
165 return line;
166 }
167 }
168
169 public static void main(String args[]) {
170 TestRunner.run(suite());
171 }
172 }