View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml2;
18  
19  import java.util.Set;
20  
21  import org.apache.commons.scxml2.env.Tracer;
22  import org.apache.commons.scxml2.model.EnterableState;
23  import org.apache.commons.scxml2.model.SCXML;
24  import org.junit.Assert;
25  import org.junit.Test;
26  /**
27   * Unit tests {@link org.apache.commons.scxml2.SCXMLExecutor}.
28   * Testing special variable "_event.data"
29   */
30  public class EventDataTest {
31  
32      /**
33       * Test the SCXML documents, usage of "_event.data"
34       */
35      @Test
36      public void testEventdata01Sample() throws Exception {
37      	SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/eventdata-01.xml");
38          exec.go();
39          Set<EnterableState> currentStates = exec.getStatus().getStates();
40          Assert.assertEquals(1, currentStates.size());
41          Assert.assertEquals("state1", currentStates.iterator().next().getId());
42          TriggerEvent te = new TriggerEvent("event.foo",
43              TriggerEvent.SIGNAL_EVENT, new Integer(3));
44          currentStates = SCXMLTestHelper.fireEvent(exec, te);
45          Assert.assertEquals(1, currentStates.size());
46          Assert.assertEquals("state3", currentStates.iterator().next().getId());
47          TriggerEvent[] evts = new TriggerEvent[] { te,
48              new TriggerEvent("event.bar", TriggerEvent.SIGNAL_EVENT,
49              new Integer(6))};
50          currentStates = SCXMLTestHelper.fireEvents(exec, evts);
51          Assert.assertEquals(1, currentStates.size());
52          Assert.assertEquals("state6", currentStates.iterator().next().getId());
53          te = new TriggerEvent("event.baz",
54              TriggerEvent.SIGNAL_EVENT, new Integer(7));
55          currentStates = SCXMLTestHelper.fireEvent(exec, te);
56          Assert.assertEquals(1, currentStates.size());
57          Assert.assertEquals("state7", currentStates.iterator().next().getId());
58      }
59  
60      @Test
61      public void testEventdata02Sample() throws Exception {
62          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/eventdata-02.xml");
63          exec.go();
64          Set<EnterableState> currentStates = exec.getStatus().getStates();
65          Assert.assertEquals(1, currentStates.size());
66          Assert.assertEquals("state0", currentStates.iterator().next().getId());
67          TriggerEvent te1 = new TriggerEvent("connection.alerting",
68              TriggerEvent.SIGNAL_EVENT, "line2");
69          currentStates = SCXMLTestHelper.fireEvent(exec, te1);
70          Assert.assertEquals(1, currentStates.size());
71          Assert.assertEquals("state2", currentStates.iterator().next().getId());
72          TriggerEvent te2 = new TriggerEvent("connection.alerting",
73              TriggerEvent.SIGNAL_EVENT,
74              new ConnectionAlertingPayload(4));
75          currentStates = SCXMLTestHelper.fireEvent(exec, te2);
76          Assert.assertEquals(1, currentStates.size());
77          Assert.assertEquals("state4", currentStates.iterator().next().getId());
78      }
79  
80      @Test
81      public void testEventdata03Sample() throws Exception {
82          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/eventdata-03.xml");
83          exec.go();
84          Set<EnterableState> currentStates = exec.getStatus().getStates();
85          Assert.assertEquals(1, currentStates.size());
86          Assert.assertEquals("ten", currentStates.iterator().next().getId());
87          TriggerEvent te = new TriggerEvent("event.foo",
88              TriggerEvent.SIGNAL_EVENT);
89          currentStates = SCXMLTestHelper.fireEvent(exec, te);
90          Assert.assertEquals(1, currentStates.size());
91          Assert.assertEquals("thirty", currentStates.iterator().next().getId());
92      }
93  
94      @Test
95      public void testEventdata04Sample() throws Exception {
96          SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/env/jexl/eventdata-03.xml");
97          Tracer trc = new Tracer();
98          SCXMLExecutor exec = new SCXMLExecutor(null, null, trc);
99          exec.addListener(scxml, trc);
100         exec.setStateMachine(scxml);
101         exec.go();
102         Thread.sleep(200); // let the 100 delay lapse
103     }
104 
105     public static class ConnectionAlertingPayload {
106         private int line;
107         public ConnectionAlertingPayload(int line) {
108             this.line = line;
109         }
110         public void setLine(int line) {
111             this.line = line;
112         }
113         public int getLine() {
114             return line;
115         }
116     }
117 }