    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.model;
    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.SCXMLExecutor;
    28  import org.apache.commons.scxml.SCXMLTestHelper;
    29  import org.apache.commons.scxml.TriggerEvent;
    30  import org.apache.commons.scxml.env.jsp.ELContext;
    31  import org.apache.commons.scxml.env.jsp.ELEvaluator;
    32  /***
    33   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
    34   */
    35  public class StatelessModelTest extends TestCase {
    36      /***
    37       * Construct a new instance of SCXMLExecutorTest with
    38       * the specified name
    39       */
    40      public StatelessModelTest(String name) {
    41          super(name);
    42      }
    43
    44      public static Test suite() {
    45          TestSuite suite = new TestSuite(StatelessModelTest.class);
    46          suite.setName("SCXML Executor Tests");
    47          return suite;
    48      }
    49
    50      // Test data
    51      private URL stateless01jexl, stateless01jsp;
    52      private SCXML scxml01jexl, scxml01jsp;
    53      private SCXMLExecutor exec01, exec02;
    54
    55      /***
    56       * Set up instance variables required by this test case.
    57       */
    58      public void setUp() {
    59          stateless01jexl = this.getClass().getClassLoader().
    60              getResource("org/apache/commons/scxml/env/jexl/stateless-01.xml");
    61          stateless01jsp = this.getClass().getClassLoader().
    62              getResource("org/apache/commons/scxml/env/jsp/stateless-01.xml");
    63          scxml01jexl = SCXMLTestHelper.digest(stateless01jexl);
    64          scxml01jsp = SCXMLTestHelper.digest(stateless01jsp);
    65      }
    66
    67      /***
    68       * Tear down instance variables required by this test case.
    69       */
    70      public void tearDown() {
    71          stateless01jexl = null;
    72      }
    73
    74      /***
    75       * Test the stateless model, simultaneous executions, JEXL expressions
    76       */
    77      public void testStatelessModelSimultaneousJexl() {
    78      	// parse once, use many times
    79          exec01 = SCXMLTestHelper.getExecutor(scxml01jexl);
    80          assertNotNull(exec01);
    81          exec02 = SCXMLTestHelper.getExecutor(scxml01jexl);
    82          assertNotNull(exec02);
    83          assertFalse(exec01 == exec02);
    84          runSimultaneousTest();
    85      }
    86
    87      /***
    88       * Test the stateless model, sequential executions, JEXL expressions
    89       */
    90      public void testStatelessModelSequentialJexl() {
    91          // rinse and repeat
    92          for (int i = 0; i < 3; i++) {
    93              exec01 = SCXMLTestHelper.getExecutor(scxml01jexl);
    94              assertNotNull(exec01);
    95              runSequentialTest();
    96          }
    97      }
    98
    99      /***
    100      * Test the stateless model, simultaneous executions, EL expressions
    101      */
    102     public void testStatelessModelSimultaneousEl() {
    103     	// parse once, use many times
    104         exec01 = SCXMLTestHelper.getExecutor(scxml01jsp,
    105             new ELContext(), new ELEvaluator());
    106         assertNotNull(exec01);
    107         exec02 = SCXMLTestHelper.getExecutor(scxml01jsp,
    108             new ELContext(), new ELEvaluator());
    109         assertNotNull(exec02);
    110         assertFalse(exec01 == exec02);
    111         runSimultaneousTest();
    112     }
    113
    114     /***
    115      * Test the stateless model, sequential executions, EL expressions
    116      */
    117     public void testStatelessModelSequentialEl() {
    118         // rinse and repeat
    119         for (int i = 0; i < 3; i++) {
    120             exec01 = SCXMLTestHelper.getExecutor(scxml01jsp,
    121                 new ELContext(), new ELEvaluator());
    122             assertNotNull(exec01);
    123             runSequentialTest();
    124         }
    125     }
    126
    127     private void runSimultaneousTest() {
    128         try {
    129             //// Interleaved
    130             // exec01
    131             Set currentStates = exec01.getCurrentStatus().getStates();
    132             assertEquals(1, currentStates.size());
    133             assertEquals("ten", ((State)currentStates.iterator().
    134                 next()).getId());
    135             currentStates = fireEvent("ten.done", exec01);
    136             assertEquals(1, currentStates.size());
    137             assertEquals("twenty", ((State)currentStates.iterator().
    138                 next()).getId());
    139             // exec02
    140             currentStates = exec02.getCurrentStatus().getStates();
    141             assertEquals(1, currentStates.size());
    142             assertEquals("ten", ((State)currentStates.iterator().
    143                 next()).getId());
    144             // exec01
    145             currentStates = fireEvent("twenty.done", exec01);
    146             assertEquals(1, currentStates.size());
    147             assertEquals("thirty", ((State)currentStates.iterator().
    148                 next()).getId());
    149             // exec02
    150             currentStates = fireEvent("ten.done", exec02);
    151             assertEquals(1, currentStates.size());
    152             assertEquals("twenty", ((State)currentStates.iterator().
    153                 next()).getId());
    154             currentStates = fireEvent("twenty.done", exec02);
    155             assertEquals(1, currentStates.size());
    156             assertEquals("thirty", ((State)currentStates.iterator().
    157                 next()).getId());
    158         } catch (Exception e) {
    159             fail(e.getMessage());
    160         }
    161     }
    162
    163     private void runSequentialTest() {
    164         try {
    165             Set currentStates = exec01.getCurrentStatus().getStates();
    166             assertEquals(1, currentStates.size());
    167             assertEquals("ten", ((State)currentStates.iterator().
    168                 next()).getId());
    169             currentStates = fireEvent("ten.done", exec01);
    170             assertEquals(1, currentStates.size());
    171             assertEquals("twenty", ((State)currentStates.iterator().
    172                 next()).getId());
    173             currentStates = fireEvent("twenty.done", exec01);
    174             assertEquals(1, currentStates.size());
    175             assertEquals("thirty", ((State)currentStates.iterator().
    176                 next()).getId());
    177         } catch (Exception e) {
    178             fail(e.getMessage());
    179         }
    180     }
    181
    182     private Set fireEvent(String name, SCXMLExecutor exec) {
    183         TriggerEvent[] evts = {new TriggerEvent(name,
    184                 TriggerEvent.SIGNAL_EVENT, null)};
    185         try {
    186             exec.triggerEvents(evts);
    187         } catch (Exception e) {
    188             fail(e.getMessage());
    189         }
    190         return exec.getCurrentStatus().getStates();
    191     }
    192
    193     public static void main(String args[]) {
    194         TestRunner.run(suite());
    195     }
    196 }
    197