    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;
    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.env.SimpleContext;
    27  import org.apache.commons.scxml.env.jsp.ELEvaluator;
    28  import org.apache.commons.scxml.model.State;
    29  /***
    30   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
    31   */
    32  public class SCXMLExecutorTest extends TestCase {
    33      /***
    34       * Construct a new instance of SCXMLExecutorTest with
    35       * the specified name
    36       */
    37      public SCXMLExecutorTest(String name) {
    38          super(name);
    39      }
    40
    41      public static Test suite() {
    42          TestSuite suite = new TestSuite(SCXMLExecutorTest.class);
    43          suite.setName("SCXML Executor Tests");
    44          return suite;
    45      }
    46
    47      // Test data
    48      private URL microwave01jsp, microwave02jsp, microwave01jexl,
    49          microwave02jexl, transitions01, send02;
    50      private SCXMLExecutor exec;
    51
    52      /***
    53       * Set up instance variables required by this test case.
    54       */
    55      public void setUp() {
    56          microwave01jsp = this.getClass().getClassLoader().
    57              getResource("org/apache/commons/scxml/env/jsp/microwave-01.xml");
    58          microwave02jsp = this.getClass().getClassLoader().
    59              getResource("org/apache/commons/scxml/env/jsp/microwave-02.xml");
    60          microwave01jexl = this.getClass().getClassLoader().
    61              getResource("org/apache/commons/scxml/env/jexl/microwave-01.xml");
    62          microwave02jexl = this.getClass().getClassLoader().
    63              getResource("org/apache/commons/scxml/env/jexl/microwave-02.xml");
    64          transitions01 = this.getClass().getClassLoader().
    65              getResource("org/apache/commons/scxml/transitions-01.xml");
    66          send02 = this.getClass().getClassLoader().
    67              getResource("org/apache/commons/scxml/send-02.xml");
    68      }
    69
    70      /***
    71       * Tear down instance variables required by this test case.
    72       */
    73      public void tearDown() {
    74          microwave01jsp = microwave02jsp = microwave01jexl = microwave02jexl =
    75              transitions01 = send02 = null;
    76      }
    77
    78      /***
    79       * Test the implementation
    80       */
    81      public void testSCXMLExecutorMicrowave01JspSample() {
    82          exec = SCXMLTestHelper.getExecutor(microwave01jsp,
    83              new SimpleContext(), new ELEvaluator());
    84          assertNotNull(exec);
    85          checkMicrowave01Sample();
    86      }
    87
    88      public void testSCXMLExecutorMicrowave02JspSample() {
    89          exec = SCXMLTestHelper.getExecutor(microwave02jsp,
    90              new SimpleContext(), new ELEvaluator());
    91          assertNotNull(exec);
    92          checkMicrowave02Sample();
    93      }
    94
    95      public void testSCXMLExecutorMicrowave01JexlSample() {
    96          exec = SCXMLTestHelper.getExecutor(microwave01jexl);
    97          assertNotNull(exec);
    98          checkMicrowave01Sample();
    99      }
    100
    101     public void testSCXMLExecutorMicrowave02JexlSample() {
    102         exec = SCXMLTestHelper.getExecutor(microwave02jexl);
    103         assertNotNull(exec);
    104         checkMicrowave02Sample();
    105     }
    106
    107     public void testSCXMLExecutorTransitions01Sample() {
    108         exec = SCXMLTestHelper.getExecutor(transitions01);
    109         assertNotNull(exec);
    110         try {
    111             Set currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    112             assertEquals(1, currentStates.size());
    113             assertEquals("twenty_one", ((State)currentStates.iterator().
    114                 next()).getId());
    115             currentStates = SCXMLTestHelper.fireEvent(exec, "twenty_one.done");
    116             assertEquals(1, currentStates.size());
    117             assertEquals("twenty_two", ((State)currentStates.iterator().
    118                 next()).getId());
    119             currentStates = SCXMLTestHelper.fireEvent(exec, "twenty_two.done");
    120             assertEquals(3, exec.getCurrentStatus().getStates().size());
    121         } catch (Exception e) {
    122             fail(e.getMessage());
    123         }
    124     }
    125
    126     public void testSendTargettypeSCXMLSample() {
    127         exec = SCXMLTestHelper.getExecutor(send02);
    128         assertNotNull(exec);
    129         try {
    130             Set currentStates = exec.getCurrentStatus().getStates();
    131             assertEquals(1, currentStates.size());
    132             assertEquals("ninety", ((State)currentStates.iterator().
    133                 next()).getId());
    134         } catch (Exception e) {
    135             fail(e.getMessage());
    136         }
    137     }
    138
    139     private void checkMicrowave01Sample() {
    140         try {
    141             Set currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
    142             assertEquals(1, currentStates.size());
    143             assertEquals("cooking", ((State)currentStates.iterator().
    144                 next()).getId());
    145         } catch (Exception e) {
    146             fail(e.getMessage());
    147         }
    148     }
    149
    150     private void checkMicrowave02Sample() {
    151         try {
    152             Set currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
    153             assertEquals(2, currentStates.size());
    154             String id = ((State)currentStates.iterator().next()).getId();
    155             assertTrue(id.equals("closed") || id.equals("cooking"));
    156         } catch (Exception e) {
    157             fail(e.getMessage());
    158         }
    159     }
    160
    161     public static void main(String args[]) {
    162         TestRunner.run(suite());
    163     }
    164 }
    165