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