    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.scxml;
    18
    19  import java.net.URL;
    20  import java.util.Set;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestCase;
    24  import junit.framework.TestSuite;
    25  import junit.textui.TestRunner;
    26
    27  import org.apache.commons.scxml.model.State;
    28  /***
    29   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
    30   * Testing special variable "_eventdata"
    31   */
    32  public class EventDataTest extends TestCase {
    33      /***
    34       * Construct a new instance of SCXMLExecutorTest with
    35       * the specified name
    36       */
    37      public EventDataTest(String name) {
    38          super(name);
    39      }
    40
    41      public static Test suite() {
    42          TestSuite suite = new TestSuite(EventDataTest.class);
    43          suite.setName("SCXML Executor Tests, _eventdata special variable");
    44          return suite;
    45      }
    46
    47      // Test data
    48      private URL eventdata01, eventdata02;
    49      private SCXMLExecutor exec;
    50
    51      /***
    52       * Set up instance variables required by this test case.
    53       */
    54      public void setUp() {
    55          eventdata01 = this.getClass().getClassLoader().
    56              getResource("org/apache/commons/scxml/env/jexl/eventdata-01.xml");
    57          eventdata02 = this.getClass().getClassLoader().
    58              getResource("org/apache/commons/scxml/env/jexl/eventdata-02.xml");
    59      }
    60
    61      /***
    62       * Tear down instance variables required by this test case.
    63       */
    64      public void tearDown() {
    65          eventdata01 = eventdata02 = null;
    66      }
    67
    68      /***
    69       * Test the SCXML documents, usage of "_eventdata"
    70       */
    71      public void testEventdata01Sample() {
    72      	exec = SCXMLTestHelper.getExecutor(eventdata01);
    73          assertNotNull(exec);
    74          try {
    75              Set currentStates = exec.getCurrentStatus().getStates();
    76              assertEquals(1, currentStates.size());
    77              assertEquals("state1", ((State)currentStates.iterator().
    78                  next()).getId());
    79              TriggerEvent te = new TriggerEvent("event.foo",
    80                  TriggerEvent.SIGNAL_EVENT, new Integer(3));
    81              currentStates = SCXMLTestHelper.fireEvent(exec, te);
    82              assertEquals(1, currentStates.size());
    83              assertEquals("state3", ((State)currentStates.iterator().
    84                  next()).getId());
    85              TriggerEvent[] evts = new TriggerEvent[] { te,
    86                  new TriggerEvent("event.bar", TriggerEvent.SIGNAL_EVENT,
    87                  new Integer(6))};
    88              currentStates = SCXMLTestHelper.fireEvents(exec, evts);
    89              assertEquals(1, currentStates.size());
    90              assertEquals("state6", ((State)currentStates.iterator().
    91                  next()).getId());
    92              te = new TriggerEvent("event.baz",
    93                  TriggerEvent.SIGNAL_EVENT, new Integer(7));
    94              currentStates = SCXMLTestHelper.fireEvent(exec, te);
    95              assertEquals(1, currentStates.size());
    96              assertEquals("state7", ((State)currentStates.iterator().
    97                  next()).getId());
    98          } catch (Exception e) {
    99              fail(e.getMessage());
    100         }
    101     }
    102
    103     public void testEventdata02Sample() {
    104     	exec = SCXMLTestHelper.getExecutor(eventdata02);
    105         assertNotNull(exec);
    106         try {
    107             Set currentStates = exec.getCurrentStatus().getStates();
    108             assertEquals(1, currentStates.size());
    109             assertEquals("state0", ((State)currentStates.iterator().
    110                 next()).getId());
    111             TriggerEvent te1 = new TriggerEvent("connection.alerting",
    112                 TriggerEvent.SIGNAL_EVENT, "line2");
    113             currentStates = SCXMLTestHelper.fireEvent(exec, te1);
    114             assertEquals(1, currentStates.size());
    115             assertEquals("state2", ((State)currentStates.iterator().
    116                 next()).getId());
    117             TriggerEvent te2 = new TriggerEvent("connection.alerting",
    118                 TriggerEvent.SIGNAL_EVENT,
    119                 new ConnectionAlertingPayload(4));
    120             currentStates = SCXMLTestHelper.fireEvent(exec, te2);
    121             assertEquals(1, currentStates.size());
    122             assertEquals("state4", ((State)currentStates.iterator().
    123                 next()).getId());
    124         } catch (Exception e) {
    125             fail(e.getMessage());
    126         }
    127     }
    128
    129     public static class ConnectionAlertingPayload {
    130         private int line;
    131         public ConnectionAlertingPayload(int line) {
    132             this.line = line;
    133         }
    134         public void setLine(int line) {
    135             this.line = line;
    136         }
    137         public int getLine() {
    138             return line;
    139         }
    140     }
    141
    142     public static void main(String args[]) {
    143         TestRunner.run(suite());
    144     }
    145 }