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.model.State;
028:
029: /**
030: * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
031: * Testing special variable "_eventdata"
032: */
033: public class EventDataTest extends TestCase {
034: /**
035: * Construct a new instance of SCXMLExecutorTest with
036: * the specified name
037: */
038: public EventDataTest(String name) {
039: super (name);
040: }
041:
042: public static Test suite() {
043: TestSuite suite = new TestSuite(EventDataTest.class);
044: suite
045: .setName("SCXML Executor Tests, _eventdata special variable");
046: return suite;
047: }
048:
049: // Test data
050: private URL eventdata01, eventdata02;
051: private SCXMLExecutor exec;
052:
053: /**
054: * Set up instance variables required by this test case.
055: */
056: public void setUp() {
057: eventdata01 = this .getClass().getClassLoader().getResource(
058: "org/apache/commons/scxml/env/jexl/eventdata-01.xml");
059: eventdata02 = this .getClass().getClassLoader().getResource(
060: "org/apache/commons/scxml/env/jexl/eventdata-02.xml");
061: }
062:
063: /**
064: * Tear down instance variables required by this test case.
065: */
066: public void tearDown() {
067: eventdata01 = eventdata02 = null;
068: }
069:
070: /**
071: * Test the SCXML documents, usage of "_eventdata"
072: */
073: public void testEventdata01Sample() {
074: exec = SCXMLTestHelper.getExecutor(eventdata01);
075: assertNotNull(exec);
076: try {
077: Set currentStates = exec.getCurrentStatus().getStates();
078: assertEquals(1, currentStates.size());
079: assertEquals("state1", ((State) currentStates.iterator()
080: .next()).getId());
081: TriggerEvent te = new TriggerEvent("event.foo",
082: TriggerEvent.SIGNAL_EVENT, new Integer(3));
083: currentStates = SCXMLTestHelper.fireEvent(exec, te);
084: assertEquals(1, currentStates.size());
085: assertEquals("state3", ((State) currentStates.iterator()
086: .next()).getId());
087: TriggerEvent[] evts = new TriggerEvent[] {
088: te,
089: new TriggerEvent("event.bar",
090: TriggerEvent.SIGNAL_EVENT, new Integer(6)) };
091: currentStates = SCXMLTestHelper.fireEvents(exec, evts);
092: assertEquals(1, currentStates.size());
093: assertEquals("state6", ((State) currentStates.iterator()
094: .next()).getId());
095: te = new TriggerEvent("event.baz",
096: TriggerEvent.SIGNAL_EVENT, new Integer(7));
097: currentStates = SCXMLTestHelper.fireEvent(exec, te);
098: assertEquals(1, currentStates.size());
099: assertEquals("state7", ((State) currentStates.iterator()
100: .next()).getId());
101: } catch (Exception e) {
102: fail(e.getMessage());
103: }
104: }
105:
106: public void testEventdata02Sample() {
107: exec = SCXMLTestHelper.getExecutor(eventdata02);
108: assertNotNull(exec);
109: try {
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: } catch (Exception e) {
128: fail(e.getMessage());
129: }
130: }
131:
132: public static class ConnectionAlertingPayload {
133: private int line;
134:
135: public ConnectionAlertingPayload(int line) {
136: this .line = line;
137: }
138:
139: public void setLine(int line) {
140: this .line = line;
141: }
142:
143: public int getLine() {
144: return line;
145: }
146: }
147:
148: public static void main(String args[]) {
149: TestRunner.run(suite());
150: }
151: }
|