    1   /*
    2    * Copyright 2005 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.model;
    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.SCXMLExecutor;
    27  import org.apache.commons.scxml.SCXMLTestHelper;
    28  import org.apache.commons.scxml.TriggerEvent;
    29  import org.apache.commons.scxml.env.jexl.JexlContext;
    30  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
    31  import org.apache.commons.scxml.env.jsp.ELContext;
    32  import org.apache.commons.scxml.env.jsp.ELEvaluator;
    33  /***
    34   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
    35   */
    36  public class DatamodelTest extends TestCase {
    37      /***
    38       * Construct a new instance of SCXMLExecutorTest with
    39       * the specified name
    40       */
    41      public DatamodelTest(String name) {
    42          super(name);
    43      }
    44
    45      public static Test suite() {
    46          TestSuite suite = new TestSuite(DatamodelTest.class);
    47          suite.setName("SCXML Executor Tests");
    48          return suite;
    49      }
    50
    51      // Test data
    52      private URL datamodel01jexl, datamodel01jsp;
    53      private SCXMLExecutor exec01, exec02;
    54
    55      /***
    56       * Set up instance variables required by this test case.
    57       */
    58      public void setUp() {
    59          datamodel01jexl = this.getClass().getClassLoader().
    60              getResource("org/apache/commons/scxml/env/jexl/datamodel-01.xml");
    61          datamodel01jsp = this.getClass().getClassLoader().
    62              getResource("org/apache/commons/scxml/env/jsp/datamodel-01.xml");
    63      }
    64
    65      /***
    66       * Tear down instance variables required by this test case.
    67       */
    68      public void tearDown() {
    69          datamodel01jexl = datamodel01jsp = null;
    70      }
    71
    72      /***
    73       * Test the stateless model, simultaneous executions
    74       */
    75      public void testDatamodelSimultaneousJexl() {
    76          exec01 = SCXMLTestHelper.getExecutor(datamodel01jexl,
    77              new JexlContext(), new JexlEvaluator());
    78          assertNotNull(exec01);
    79          exec02 = SCXMLTestHelper.getExecutor(datamodel01jexl,
    80              new JexlContext(), new JexlEvaluator());
    81          assertNotNull(exec02);
    82          assertFalse(exec01 == exec02);
    83          runtest();
    84      }
    85
    86      public void testDatamodelSimultaneousJsp() {
    87          exec01 = SCXMLTestHelper.getExecutor(datamodel01jsp,
    88              new ELContext(), new ELEvaluator());
    89          assertNotNull(exec01);
    90          exec02 = SCXMLTestHelper.getExecutor(datamodel01jsp,
    91              new ELContext(), new ELEvaluator());
    92          assertNotNull(exec02);
    93          assertFalse(exec01 == exec02);
    94          runtest();
    95      }
    96
    97      private void runtest() {
    98          try {
    99              //// Interleaved
    100             // exec01
    101             Set currentStates = exec01.getCurrentStatus().getStates();
    102             assertEquals(1, currentStates.size());
    103             assertEquals("ten", ((State)currentStates.iterator().
    104                 next()).getId());
    105             currentStates = fireEvent("ten.done", exec01);
    106             assertEquals(1, currentStates.size());
    107             assertEquals("twenty", ((State)currentStates.iterator().
    108                 next()).getId());
    109             // exec02
    110             currentStates = exec02.getCurrentStatus().getStates();
    111             assertEquals(1, currentStates.size());
    112             assertEquals("ten", ((State)currentStates.iterator().
    113                 next()).getId());
    114             // exec01
    115             currentStates = fireEvent("twenty.done", exec01);
    116             assertEquals(1, currentStates.size());
    117             assertEquals("thirty", ((State)currentStates.iterator().
    118                 next()).getId());
    119             // exec02
    120             currentStates = fireEvent("ten.done", exec02);
    121             assertEquals(1, currentStates.size());
    122             assertEquals("twenty", ((State)currentStates.iterator().
    123                 next()).getId());
    124             currentStates = fireEvent("twenty.done", exec02);
    125             assertEquals(1, currentStates.size());
    126             assertEquals("thirty", ((State)currentStates.iterator().
    127                 next()).getId());
    128             currentStates = fireEvent("thirty.done", exec02);
    129             assertEquals(1, currentStates.size());
    130             assertEquals("forty", ((State)currentStates.iterator().
    131                 next()).getId());
    132             // exec01
    133             currentStates = fireEvent("thirty.done", exec01);
    134             assertEquals(1, currentStates.size());
    135             assertEquals("forty", ((State)currentStates.iterator().
    136                 next()).getId());
    137         } catch (Exception e) {
    138             fail(e.getMessage());
    139         }
    140     }
    141
    142     private Set fireEvent(String name, SCXMLExecutor exec) {
    143         TriggerEvent[] evts = {new TriggerEvent(name,
    144                 TriggerEvent.SIGNAL_EVENT, null)};
    145         try {
    146             exec.triggerEvents(evts);
    147         } catch (Exception e) {
    148             fail(e.getMessage());
    149         }
    150         return exec.getCurrentStatus().getStates();
    151     }
    152
    153     public static void main(String args[]) {
    154         TestRunner.run(suite());
    155     }
    156 }
    157