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