    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;
    18
    19  import java.net.URL;
    20  import java.util.HashSet;
    21  import java.util.Iterator;
    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.env.SimpleContext;
    30  import org.apache.commons.scxml.env.jsp.ELEvaluator;
    31  import org.apache.commons.scxml.model.SCXML;
    32  import org.apache.commons.scxml.model.State;
    33  import org.apache.commons.scxml.model.TransitionTarget;
    34  /***
    35   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
    36   */
    37  public class SCXMLExecutorTest extends TestCase {
    38      /***
    39       * Construct a new instance of SCXMLExecutorTest with
    40       * the specified name
    41       */
    42      public SCXMLExecutorTest(String name) {
    43          super(name);
    44      }
    45
    46      public static Test suite() {
    47          TestSuite suite = new TestSuite(SCXMLExecutorTest.class);
    48          suite.setName("SCXML Executor Tests");
    49          return suite;
    50      }
    51
    52      // Test data
    53      private URL microwave01jsp, microwave02jsp, microwave01jexl,
    54          microwave02jexl, microwave03jexl, microwave04jexl, transitions01,
    55          transitions02, transitions03, transitions04, prefix01, send01, send02;
    56      private SCXMLExecutor exec;
    57
    58      /***
    59       * Set up instance variables required by this test case.
    60       */
    61      public void setUp() {
    62          microwave01jsp = this.getClass().getClassLoader().
    63              getResource("org/apache/commons/scxml/env/jsp/microwave-01.xml");
    64          microwave02jsp = this.getClass().getClassLoader().
    65              getResource("org/apache/commons/scxml/env/jsp/microwave-02.xml");
    66          microwave01jexl = this.getClass().getClassLoader().
    67              getResource("org/apache/commons/scxml/env/jexl/microwave-01.xml");
    68          microwave02jexl = this.getClass().getClassLoader().
    69              getResource("org/apache/commons/scxml/env/jexl/microwave-02.xml");
    70          microwave03jexl = this.getClass().getClassLoader().
    71              getResource("org/apache/commons/scxml/env/jexl/microwave-03.xml");
    72          microwave04jexl = this.getClass().getClassLoader().
    73              getResource("org/apache/commons/scxml/env/jexl/microwave-04.xml");
    74          transitions01 = this.getClass().getClassLoader().
    75              getResource("org/apache/commons/scxml/transitions-01.xml");
    76          transitions02 = this.getClass().getClassLoader().
    77              getResource("org/apache/commons/scxml/transitions-02.xml");
    78          transitions03 = this.getClass().getClassLoader().
    79              getResource("org/apache/commons/scxml/transitions-03.xml");
    80          transitions04 = this.getClass().getClassLoader().
    81              getResource("org/apache/commons/scxml/transitions-04.xml");
    82          prefix01 = this.getClass().getClassLoader().
    83              getResource("org/apache/commons/scxml/prefix-01.xml");
    84          send01 = this.getClass().getClassLoader().
    85              getResource("org/apache/commons/scxml/send-01.xml");
    86          send02 = this.getClass().getClassLoader().
    87              getResource("org/apache/commons/scxml/send-02.xml");
    88      }
    89
    90      /***
    91       * Tear down instance variables required by this test case.
    92       */
    93      public void tearDown() {
    94          microwave01jsp = microwave02jsp = microwave01jexl = microwave02jexl =
    95              microwave04jexl = transitions01 = transitions02 = transitions03 =
    96              transitions04 = prefix01 = send01 = send02 = null;
    97      }
    98
    99      /***
    100      * Test the implementation
    101      */
    102     public void testSCXMLExecutorMicrowave01JspSample() {
    103         exec = SCXMLTestHelper.getExecutor(microwave01jsp,
    104             new SimpleContext(), new ELEvaluator());
    105         assertNotNull(exec);
    106         checkMicrowave01Sample();
    107     }
    108
    109     public void testSCXMLExecutorMicrowave02JspSample() {
    110         exec = SCXMLTestHelper.getExecutor(microwave02jsp,
    111             new SimpleContext(), new ELEvaluator());
    112         assertNotNull(exec);
    113         checkMicrowave02Sample();
    114     }
    115
    116     public void testSCXMLExecutorMicrowave01JexlSample() {
    117         exec = SCXMLTestHelper.getExecutor(microwave01jexl);
    118         assertNotNull(exec);
    119         checkMicrowave01Sample();
    120     }
    121
    122     public void testSCXMLExecutorMicrowave02JexlSample() {
    123         exec = SCXMLTestHelper.getExecutor(microwave02jexl);
    124         assertNotNull(exec);
    125         checkMicrowave02Sample();
    126     }
    127
    128     // Uses SCXMLParser (latest WD)
    129     public void testSCXMLExecutorMicrowave03JexlSample() {
    130         SCXML scxml = SCXMLTestHelper.parse(microwave03jexl);
    131         assertNotNull(scxml);
    132         exec = SCXMLTestHelper.getExecutor(scxml);
    133         assertNotNull(exec);
    134         checkMicrowave01Sample();
    135     }
    136
    137     // Uses SCXMLParser (latest WD)
    138     public void testSCXMLExecutorMicrowave04JexlSample() {
    139         SCXML scxml = SCXMLTestHelper.parse(microwave04jexl);
    140         assertNotNull(scxml);
    141         exec = SCXMLTestHelper.getExecutor(scxml);
    142         assertNotNull(exec);
    143         checkMicrowave02Sample();
    144     }
    145
    146     public void testSCXMLExecutorPrefix01Sample() {
    147         exec = SCXMLTestHelper.getExecutor(prefix01);
    148         assertNotNull(exec);
    149         Set currentStates = exec.getCurrentStatus().getStates();
    150         assertEquals(1, currentStates.size());
    151         assertEquals("ten", ((State)currentStates.iterator().
    152             next()).getId());
    153         currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    154         assertEquals(1, currentStates.size());
    155         assertEquals("twenty", ((State)currentStates.iterator().
    156             next()).getId());
    157     }
    158
    159     public void testSCXMLExecutorTransitions01Sample() {
    160         exec = SCXMLTestHelper.getExecutor(transitions01);
    161         assertNotNull(exec);
    162         try {
    163             Set currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    164             assertEquals(1, currentStates.size());
    165             assertEquals("twenty_one", ((State)currentStates.iterator().
    166                 next()).getId());
    167             currentStates = SCXMLTestHelper.fireEvent(exec, "twenty_one.done");
    168             assertEquals(1, currentStates.size());
    169             assertEquals("twenty_two", ((State)currentStates.iterator().
    170                 next()).getId());
    171             currentStates = SCXMLTestHelper.fireEvent(exec, "twenty_two.done");
    172             assertEquals(3, exec.getCurrentStatus().getStates().size());
    173         } catch (Exception e) {
    174             fail(e.getMessage());
    175         }
    176     }
    177
    178     public void testSCXMLExecutorTransitions02Sample() {
    179         exec = SCXMLTestHelper.getExecutor(transitions02);
    180         assertNotNull(exec);
    181         try {
    182             Set currentStates = SCXMLTestHelper.fireEvent(exec, "ten.stay");
    183             assertEquals(1, currentStates.size());
    184             assertEquals("ten", ((State)currentStates.iterator().
    185                 next()).getId());
    186             exec = SCXMLTestHelper.testExecutorSerializability(exec);
    187             currentStates = SCXMLTestHelper.fireEvent(exec, "ten.self");
    188             assertEquals(1, currentStates.size());
    189             assertEquals("ten", ((State)currentStates.iterator().
    190                 next()).getId());
    191             currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    192             assertEquals(1, currentStates.size());
    193             assertEquals("twenty", ((State)currentStates.iterator().
    194                 next()).getId());
    195         } catch (Exception e) {
    196             fail(e.getMessage());
    197         }
    198     }
    199
    200     public void testSCXMLExecutorTransitions03Sample() {
    201         exec = SCXMLTestHelper.getExecutor(transitions03);
    202         assertNotNull(exec);
    203         try {
    204             Set currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    205             assertEquals(3, currentStates.size());
    206             Set expected = new HashSet();
    207             expected.add("twenty_one_2");
    208             expected.add("twenty_two_2");
    209             expected.add("twenty_three_2");
    210             for (Iterator i = currentStates.iterator(); i.hasNext(); ) {
    211                 TransitionTarget tt = (TransitionTarget) i.next();
    212                 if (!expected.remove(tt.getId())) {
    213                     fail("'" + tt.getId()
    214                         + "' is not an expected current state ID");
    215                 }
    216             }
    217         } catch (Exception e) {
    218             fail(e.getMessage());
    219         }
    220     }
    221
    222     // Uses SCXMLParser (latest WD)
    223     public void testSCXMLExecutorTransitions04Sample() {
    224         SCXML scxml = SCXMLTestHelper.parse(transitions04);
    225         assertNotNull(scxml);
    226         exec = SCXMLTestHelper.getExecutor(scxml);
    227         assertNotNull(exec);
    228         try {
    229             Set currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    230             assertEquals(3, currentStates.size());
    231             Set expected = new HashSet();
    232             expected.add("twenty_one_1");
    233             expected.add("twenty_two_1");
    234             expected.add("twenty_three_1");
    235             for (Iterator i = currentStates.iterator(); i.hasNext(); ) {
    236                 TransitionTarget tt = (TransitionTarget) i.next();
    237                 if (!expected.remove(tt.getId())) {
    238                     fail("'" + tt.getId()
    239                         + "' is not an expected current state ID");
    240                 }
    241             }
    242             currentStates = SCXMLTestHelper.fireEvent(exec, "bar");
    243             assertEquals(1, currentStates.size());
    244             assertEquals("thirty", ((State)currentStates.iterator().
    245                 next()).getId());
    246         } catch (Exception e) {
    247             fail(e.getMessage());
    248         }
    249     }
    250
    251     public void testSend01Sample() {
    252         exec = SCXMLTestHelper.getExecutor(send01);
    253         assertNotNull(exec);
    254         try {
    255             Set currentStates = exec.getCurrentStatus().getStates();
    256             assertEquals(1, currentStates.size());
    257             assertEquals("ten", ((State)currentStates.iterator().
    258                 next()).getId());
    259             currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    260             assertEquals(1, currentStates.size());
    261             assertEquals("twenty", ((State)currentStates.iterator().
    262                 next()).getId());
    263         } catch (Exception e) {
    264             fail(e.getMessage());
    265         }
    266     }
    267
    268     public void testSend02TargettypeSCXMLSample() {
    269         exec = SCXMLTestHelper.getExecutor(send02);
    270         assertNotNull(exec);
    271         try {
    272             Set currentStates = exec.getCurrentStatus().getStates();
    273             assertEquals(1, currentStates.size());
    274             assertEquals("ninety", ((State)currentStates.iterator().
    275                 next()).getId());
    276             assertTrue(exec.getCurrentStatus().isFinal());
    277         } catch (Exception e) {
    278             fail(e.getMessage());
    279         }
    280     }
    281
    282     private void checkMicrowave01Sample() {
    283         try {
    284             Set currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
    285             assertEquals(1, currentStates.size());
    286             assertEquals("cooking", ((State)currentStates.iterator().
    287                 next()).getId());
    288         } catch (Exception e) {
    289             fail(e.getMessage());
    290         }
    291     }
    292
    293     private void checkMicrowave02Sample() {
    294         try {
    295             Set currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
    296             assertEquals(2, currentStates.size());
    297             String id = ((State)currentStates.iterator().next()).getId();
    298             assertTrue(id.equals("closed") || id.equals("cooking"));
    299         } catch (Exception e) {
    300             fail(e.getMessage());
    301         }
    302     }
    303
    304     public static void main(String args[]) {
    305         TestRunner.run(suite());
    306     }
    307 }
    308