    1   /*
    2    * Copyright 2006 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.scxml;
    17
    18  import java.net.URL;
    19  import java.util.Set;
    20
    21  import junit.framework.Test;
    22  import junit.framework.TestCase;
    23  import junit.framework.TestSuite;
    24  import junit.textui.TestRunner;
    25
    26  import org.apache.commons.scxml.model.State;
    27  /***
    28   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
    29   * Testing special variable "_eventdata"
    30   */
    31  public class EventDataTest extends TestCase {
    32      /***
    33       * Construct a new instance of SCXMLExecutorTest with
    34       * the specified name
    35       */
    36      public EventDataTest(String name) {
    37          super(name);
    38      }
    39
    40      public static Test suite() {
    41          TestSuite suite = new TestSuite(EventDataTest.class);
    42          suite.setName("SCXML Executor Tests, _eventdata special variable");
    43          return suite;
    44      }
    45
    46      // Test data
    47      private URL eventdata01, eventdata02;
    48      private SCXMLExecutor exec;
    49
    50      /***
    51       * Set up instance variables required by this test case.
    52       */
    53      public void setUp() {
    54          eventdata01 = this.getClass().getClassLoader().
    55              getResource("org/apache/commons/scxml/env/jexl/eventdata-01.xml");
    56          eventdata02 = this.getClass().getClassLoader().
    57              getResource("org/apache/commons/scxml/env/jexl/eventdata-02.xml");
    58      }
    59
    60      /***
    61       * Tear down instance variables required by this test case.
    62       */
    63      public void tearDown() {
    64          eventdata01 = eventdata02 = null;
    65      }
    66
    67      /***
    68       * Test the SCXML documents, usage of "_eventdata"
    69       */
    70      public void testEventdata01Sample() {
    71      	exec = SCXMLTestHelper.getExecutor(eventdata01);
    72          assertNotNull(exec);
    73          try {
    74              Set currentStates = exec.getCurrentStatus().getStates();
    75              assertEquals(1, currentStates.size());
    76              assertEquals("state1", ((State)currentStates.iterator().
    77                  next()).getId());
    78              TriggerEvent te = new TriggerEvent("event.foo",
    79                  TriggerEvent.SIGNAL_EVENT, new Integer(3));
    80              currentStates = SCXMLTestHelper.fireEvent(exec, te);
    81              assertEquals(1, currentStates.size());
    82              assertEquals("state3", ((State)currentStates.iterator().
    83                  next()).getId());
    84              TriggerEvent[] evts = new TriggerEvent[] { te,
    85                  new TriggerEvent("event.bar", TriggerEvent.SIGNAL_EVENT,
    86                  new Integer(6))};
    87              currentStates = SCXMLTestHelper.fireEvents(exec, evts);
    88              assertEquals(1, currentStates.size());
    89              assertEquals("state6", ((State)currentStates.iterator().
    90                  next()).getId());
    91              te = new TriggerEvent("event.baz",
    92                  TriggerEvent.SIGNAL_EVENT, new Integer(7));
    93              currentStates = SCXMLTestHelper.fireEvent(exec, te);
    94              assertEquals(1, currentStates.size());
    95              assertEquals("state7", ((State)currentStates.iterator().
    96                  next()).getId());
    97          } catch (Exception e) {
    98              fail(e.getMessage());
    99          }
    100     }
    101
    102     public void testEventdata02Sample() {
    103     	exec = SCXMLTestHelper.getExecutor(eventdata02);
    104         assertNotNull(exec);
    105         try {
    106             Set currentStates = exec.getCurrentStatus().getStates();
    107             assertEquals(1, currentStates.size());
    108             assertEquals("state0", ((State)currentStates.iterator().
    109                 next()).getId());
    110             TriggerEvent te1 = new TriggerEvent("connection.alerting",
    111                 TriggerEvent.SIGNAL_EVENT, "line2");
    112             currentStates = SCXMLTestHelper.fireEvent(exec, te1);
    113             assertEquals(1, currentStates.size());
    114             assertEquals("state2", ((State)currentStates.iterator().
    115                 next()).getId());
    116             TriggerEvent te2 = new TriggerEvent("connection.alerting",
    117                 TriggerEvent.SIGNAL_EVENT,
    118                 new ConnectionAlertingPayload(4));
    119             currentStates = SCXMLTestHelper.fireEvent(exec, te2);
    120             assertEquals(1, currentStates.size());
    121             assertEquals("state4", ((State)currentStates.iterator().
    122                 next()).getId());
    123         } catch (Exception e) {
    124             fail(e.getMessage());
    125         }
    126     }
    127
    128     public static class ConnectionAlertingPayload {
    129         private int line;
    130         public ConnectionAlertingPayload(int line) {
    131             this.line = line;
    132         }
    133         public void setLine(int line) {
    134             this.line = line;
    135         }
    136         public int getLine() {
    137             return line;
    138         }
    139     }
    140
    141     public static void main(String args[]) {
    142         TestRunner.run(suite());
    143     }
    144 }