    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.HashMap;
    21  import java.util.Map;
    22  import java.util.Set;
    23
    24  import junit.framework.Test;
    25  import junit.framework.TestCase;
    26  import junit.framework.TestSuite;
    27  import junit.textui.TestRunner;
    28
    29  import org.apache.commons.scxml.SCXMLExecutor;
    30  import org.apache.commons.scxml.SCXMLTestHelper;
    31  import org.apache.commons.scxml.TriggerEvent;
    32  import org.apache.commons.scxml.env.jexl.JexlContext;
    33  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
    34  import org.apache.commons.scxml.env.jsp.ELContext;
    35  import org.apache.commons.scxml.env.jsp.ELEvaluator;
    36  /***
    37   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
    38   */
    39  public class DatamodelTest extends TestCase {
    40      /***
    41       * Construct a new instance of SCXMLExecutorTest with
    42       * the specified name
    43       */
    44      public DatamodelTest(String name) {
    45          super(name);
    46      }
    47
    48      public static Test suite() {
    49          TestSuite suite = new TestSuite(DatamodelTest.class);
    50          suite.setName("SCXML Executor Tests");
    51          return suite;
    52      }
    53
    54      // Test data
    55      private URL datamodel01jexl, datamodel02jexl, datamodel04jexl, datamodel01jsp, datamodel02jsp;
    56      private SCXMLExecutor exec01, exec02;
    57
    58      /***
    59       * Set up instance variables required by this test case.
    60       */
    61      public void setUp() {
    62          datamodel01jexl = this.getClass().getClassLoader().
    63              getResource("org/apache/commons/scxml/env/jexl/datamodel-01.xml");
    64          datamodel02jexl = this.getClass().getClassLoader().
    65             getResource("org/apache/commons/scxml/env/jexl/datamodel-02.xml");
    66          datamodel04jexl = this.getClass().getClassLoader().
    67             getResource("org/apache/commons/scxml/env/jexl/datamodel-04.xml");
    68          datamodel01jsp = this.getClass().getClassLoader().
    69              getResource("org/apache/commons/scxml/env/jsp/datamodel-01.xml");
    70          datamodel02jsp = this.getClass().getClassLoader().
    71              getResource("org/apache/commons/scxml/env/jsp/datamodel-02.xml");
    72      }
    73
    74      /***
    75       * Tear down instance variables required by this test case.
    76       */
    77      public void tearDown() {
    78          datamodel01jexl = datamodel02jexl = datamodel01jsp = datamodel02jsp = null;
    79      }
    80
    81      /***
    82       * Test the stateless model, simultaneous executions
    83       */
    84      public void testDatamodelSimultaneousJexl() {
    85          exec01 = SCXMLTestHelper.getExecutor(datamodel01jexl,
    86              new JexlContext(), new JexlEvaluator());
    87          assertNotNull(exec01);
    88          exec02 = SCXMLTestHelper.getExecutor(datamodel01jexl,
    89              new JexlContext(), new JexlEvaluator());
    90          assertNotNull(exec02);
    91          assertFalse(exec01 == exec02);
    92          runtest();
    93      }
    94
    95      public void testDatamodelSimultaneousJsp() {
    96          exec01 = SCXMLTestHelper.getExecutor(datamodel01jsp,
    97              new ELContext(), new ELEvaluator());
    98          assertNotNull(exec01);
    99          exec02 = SCXMLTestHelper.getExecutor(datamodel01jsp,
    100             new ELContext(), new ELEvaluator());
    101         assertNotNull(exec02);
    102         assertFalse(exec01 == exec02);
    103         runtest();
    104     }
    105
    106     public void testDatamodelNamespacePrefixedXPaths() {
    107         exec01 = SCXMLTestHelper.getExecutor(datamodel02jexl,
    108             new JexlContext(), new JexlEvaluator());
    109         assertNotNull(exec01);
    110         exec02 = SCXMLTestHelper.getExecutor(datamodel02jsp,
    111             new ELContext(), new ELEvaluator());
    112         assertNotNull(exec02);
    113         assertFalse(exec01 == exec02);
    114         runtest();
    115     }
    116
    117     public void testDatamodel04Jexl() {
    118         exec01 = SCXMLTestHelper.getExecutor(datamodel04jexl,
    119             new JexlContext(), new JexlEvaluator());
    120         assertNotNull(exec01);
    121         Set currentStates = exec01.getCurrentStatus().getStates();
    122         assertEquals(1, currentStates.size());
    123         assertEquals("ten", ((State)currentStates.iterator().
    124             next()).getId());
    125         Map payload = new HashMap();
    126         payload.put("one", "1");
    127         payload.put("two", "2");
    128         TriggerEvent te = new TriggerEvent("ten.done", TriggerEvent.SIGNAL_EVENT, payload);
    129         SCXMLTestHelper.fireEvent(exec01, te);
    130         currentStates = exec01.getCurrentStatus().getStates();
    131         assertEquals(1, currentStates.size());
    132         assertEquals("twenty", ((State)currentStates.iterator().
    133             next()).getId());
    134         SCXMLTestHelper.fireEvent(exec01, "twenty.done");
    135         currentStates = exec01.getCurrentStatus().getStates();
    136         assertEquals(1, currentStates.size());
    137         assertEquals("thirty", ((State)currentStates.iterator().
    138             next()).getId());
    139     }
    140
    141     private void runtest() {
    142         try {
    143             //// Interleaved
    144             // exec01
    145             Set currentStates = exec01.getCurrentStatus().getStates();
    146             assertEquals(1, currentStates.size());
    147             assertEquals("ten", ((State)currentStates.iterator().
    148                 next()).getId());
    149             exec01 = SCXMLTestHelper.testExecutorSerializability(exec01);
    150             currentStates = fireEvent("ten.done", exec01);
    151             assertEquals(1, currentStates.size());
    152             assertEquals("twenty", ((State)currentStates.iterator().
    153                 next()).getId());
    154             // exec02
    155             currentStates = exec02.getCurrentStatus().getStates();
    156             assertEquals(1, currentStates.size());
    157             assertEquals("ten", ((State)currentStates.iterator().
    158                 next()).getId());
    159             // exec01
    160             currentStates = fireEvent("twenty.done", exec01);
    161             assertEquals(1, currentStates.size());
    162             assertEquals("thirty", ((State)currentStates.iterator().
    163                 next()).getId());
    164             exec01 = SCXMLTestHelper.testExecutorSerializability(exec01);
    165             // exec02
    166             currentStates = fireEvent("ten.done", exec02);
    167             assertEquals(1, currentStates.size());
    168             assertEquals("twenty", ((State)currentStates.iterator().
    169                 next()).getId());
    170             exec02 = SCXMLTestHelper.testExecutorSerializability(exec02);
    171             currentStates = fireEvent("twenty.done", exec02);
    172             assertEquals(1, currentStates.size());
    173             assertEquals("thirty", ((State)currentStates.iterator().
    174                 next()).getId());
    175             currentStates = fireEvent("thirty.done", exec02);
    176             assertEquals(1, currentStates.size());
    177             assertEquals("forty", ((State)currentStates.iterator().
    178                 next()).getId());
    179             // exec01
    180             currentStates = fireEvent("thirty.done", exec01);
    181             assertEquals(1, currentStates.size());
    182             assertEquals("forty", ((State)currentStates.iterator().
    183                 next()).getId());
    184         } catch (Exception e) {
    185             fail(e.getMessage());
    186         }
    187     }
    188
    189     private Set fireEvent(String name, SCXMLExecutor exec) {
    190         TriggerEvent[] evts = {new TriggerEvent(name,
    191                 TriggerEvent.SIGNAL_EVENT, null)};
    192         try {
    193             exec.triggerEvents(evts);
    194         } catch (Exception e) {
    195             fail(e.getMessage());
    196         }
    197         return exec.getCurrentStatus().getStates();
    198     }
    199
    200     public static void main(String args[]) {
    201         TestRunner.run(suite());
    202     }
    203 }
    204