    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.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.env.jsp.ELContext;
    28  import org.apache.commons.scxml.env.jsp.ELEvaluator;
    29  import org.apache.commons.scxml.model.State;
    30
    31  /***
    32   * Unit tests for namespace prefixes in XPaths pointing bits in a &lt;data&gt;.
    33   */
    34  public class NamespacePrefixedXPathsTest extends TestCase {
    35
    36      /***
    37       * Construct a new instance of NamespacePrefixedXPathsTest with
    38       * the specified name
    39       */
    40      public NamespacePrefixedXPathsTest(String name) {
    41          super(name);
    42      }
    43
    44      public static Test suite() {
    45          TestSuite suite = new TestSuite(NamespacePrefixedXPathsTest.class);
    46          suite.setName("Namespace Prefixed XPaths Tests");
    47          return suite;
    48      }
    49
    50      // Test data
    51      private URL datamodel03jexl, datamodel03jsp;
    52      private SCXMLExecutor exec01, exec02;
    53
    54      /***
    55       * Set up instance variables required by this test case.
    56       */
    57      public void setUp() {
    58          datamodel03jexl = this.getClass().getClassLoader().
    59              getResource("org/apache/commons/scxml/env/jexl/datamodel-03.xml");
    60          datamodel03jsp = this.getClass().getClassLoader().
    61              getResource("org/apache/commons/scxml/env/jsp/datamodel-03.xml");
    62          exec01 = SCXMLTestHelper.getExecutor(datamodel03jexl);
    63          exec02 = SCXMLTestHelper.getExecutor(datamodel03jsp, new ELContext(), new ELEvaluator());
    64      }
    65
    66      /***
    67       * Tear down instance variables required by this test case.
    68       */
    69      public void tearDown() {
    70          datamodel03jexl = datamodel03jsp = null;
    71          exec01 = exec02 = null;
    72      }
    73
    74      /***
    75       * Test the XPath evaluation
    76       */
    77      // JEXL
    78      public void testNamespacePrefixedXPathsJexl() {
    79          runtest(exec01);
    80      }
    81
    82      // EL
    83      public void testNamespacePrefixedXPathsEL() {
    84          runtest(exec02);
    85      }
    86
    87      // Same test, since same documents (different expression languages)
    88      private void runtest(SCXMLExecutor exec) {
    89
    90          try {
    91
    92              // must be in state "ten" at the onset
    93              Set currentStates = exec.getCurrentStatus().getStates();
    94              assertEquals(1, currentStates.size());
    95              assertEquals("ten", ((State)currentStates.iterator().
    96                  next()).getId());
    97
    98              // should move to "twenty"
    99              currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
    100             assertEquals(1, currentStates.size());
    101             assertEquals("twenty", ((State)currentStates.iterator().
    102                 next()).getId());
    103
    104             // This is set while exiting "ten"
    105             Double retval = (Double) exec.getRootContext().get("retval");
    106             assertEquals(Double.valueOf("11"), retval);
    107
    108             // On to "thirty"
    109             currentStates = SCXMLTestHelper.fireEvent(exec, "twenty.done");
    110             assertEquals(1, currentStates.size());
    111             assertEquals("thirty", ((State)currentStates.iterator().
    112                 next()).getId());
    113             exec = SCXMLTestHelper.testExecutorSerializability(exec);
    114
    115             // Tests XPath on SCXML actions, set while exiting "twenty"
    116             String retvalstr = (String) exec.getRootContext().get("retval");
    117             assertEquals("Equal to 20", retvalstr);
    118
    119             // and so on ...
    120             currentStates = SCXMLTestHelper.fireEvent(exec, "thirty.done");
    121             assertEquals(1, currentStates.size());
    122             assertEquals("forty", ((State)currentStates.iterator().
    123                 next()).getId());
    124
    125             currentStates = SCXMLTestHelper.fireEvent(exec, "forty.done");
    126             assertEquals(1, currentStates.size());
    127             assertEquals("fifty", ((State)currentStates.iterator().
    128                 next()).getId());
    129
    130             currentStates = SCXMLTestHelper.fireEvent(exec, "fifty.done");
    131             assertEquals(1, currentStates.size());
    132             assertEquals("sixty", ((State)currentStates.iterator().
    133                 next()).getId());
    134
    135             currentStates = SCXMLTestHelper.fireEvent(exec, "sixty.done");
    136             assertEquals(1, currentStates.size());
    137             assertEquals("seventy", ((State)currentStates.iterator().
    138                 next()).getId());
    139
    140             // done
    141             assertTrue(exec.getCurrentStatus().isFinal());
    142
    143         } catch (Exception e) {
    144             fail(e.getMessage());
    145         }
    146
    147     }
    148
    149     public static void main(String args[]) {
    150         TestRunner.run(suite());
    151     }
    152
    153 }
    154