    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, eventdata03;
    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          eventdata03 = this.getClass().getClassLoader().
    60              getResource("org/apache/commons/scxml/env/jexl/eventdata-03.xml");
    61      }
    62
    63      /***
    64       * Tear down instance variables required by this test case.
    65       */
    66      public void tearDown() {
    67          eventdata01 = eventdata02 = eventdata03 = null;
    68      }
    69
    70      /***
    71       * Test the SCXML documents, usage of "_eventdata"
    72       */
    73      public void testEventdata01Sample() {
    74      	exec = SCXMLTestHelper.getExecutor(eventdata01);
    75          assertNotNull(exec);
    76          try {
    77              Set currentStates = exec.getCurrentStatus().getStates();
    78              assertEquals(1, currentStates.size());
    79              assertEquals("state1", ((State)currentStates.iterator().
    80                  next()).getId());
    81              TriggerEvent te = new TriggerEvent("event.foo",
    82                  TriggerEvent.SIGNAL_EVENT, new Integer(3));
    83              currentStates = SCXMLTestHelper.fireEvent(exec, te);
    84              assertEquals(1, currentStates.size());
    85              assertEquals("state3", ((State)currentStates.iterator().
    86                  next()).getId());
    87              TriggerEvent[] evts = new TriggerEvent[] { te,
    88                  new TriggerEvent("event.bar", TriggerEvent.SIGNAL_EVENT,
    89                  new Integer(6))};
    90              currentStates = SCXMLTestHelper.fireEvents(exec, evts);
    91              assertEquals(1, currentStates.size());
    92              assertEquals("state6", ((State)currentStates.iterator().
    93                  next()).getId());
    94              te = new TriggerEvent("event.baz",
    95                  TriggerEvent.SIGNAL_EVENT, new Integer(7));
    96              currentStates = SCXMLTestHelper.fireEvent(exec, te);
    97              assertEquals(1, currentStates.size());
    98              assertEquals("state7", ((State)currentStates.iterator().
    99                  next()).getId());
    100         } catch (Exception e) {
    101             fail(e.getMessage());
    102         }
    103     }
    104
    105     public void testEventdata02Sample() {
    106     	exec = SCXMLTestHelper.getExecutor(eventdata02);
    107         assertNotNull(exec);
    108         try {
    109             Set currentStates = exec.getCurrentStatus().getStates();
    110             assertEquals(1, currentStates.size());
    111             assertEquals("state0", ((State)currentStates.iterator().
    112                 next()).getId());
    113             TriggerEvent te1 = new TriggerEvent("connection.alerting",
    114                 TriggerEvent.SIGNAL_EVENT, "line2");
    115             currentStates = SCXMLTestHelper.fireEvent(exec, te1);
    116             assertEquals(1, currentStates.size());
    117             assertEquals("state2", ((State)currentStates.iterator().
    118                 next()).getId());
    119             TriggerEvent te2 = new TriggerEvent("connection.alerting",
    120                 TriggerEvent.SIGNAL_EVENT,
    121                 new ConnectionAlertingPayload(4));
    122             currentStates = SCXMLTestHelper.fireEvent(exec, te2);
    123             assertEquals(1, currentStates.size());
    124             assertEquals("state4", ((State)currentStates.iterator().
    125                 next()).getId());
    126         } catch (Exception e) {
    127             fail(e.getMessage());
    128         }
    129     }
    130
    131     public void testEventdata03Sample() {
    132         exec = SCXMLTestHelper.getExecutor(eventdata03);
    133         assertNotNull(exec);
    134         try {
    135             Set currentStates = exec.getCurrentStatus().getStates();
    136             assertEquals(1, currentStates.size());
    137             assertEquals("ten", ((State)currentStates.iterator().
    138                 next()).getId());
    139             TriggerEvent te = new TriggerEvent("event.foo",
    140                 TriggerEvent.SIGNAL_EVENT);
    141             currentStates = SCXMLTestHelper.fireEvent(exec, te);
    142             assertEquals(1, currentStates.size());
    143             assertEquals("thirty", ((State)currentStates.iterator().
    144                 next()).getId());
    145         } catch (Exception e) {
    146             fail(e.getMessage());
    147         }
    148     }
    149
    150     public static class ConnectionAlertingPayload {
    151         private int line;
    152         public ConnectionAlertingPayload(int line) {
    153             this.line = line;
    154         }
    155         public void setLine(int line) {
    156             this.line = line;
    157         }
    158         public int getLine() {
    159             return line;
    160         }
    161     }
    162
    163     public static void main(String args[]) {
    164         TestRunner.run(suite());
    165     }
    166 }