View Javadoc
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.scxml2;
18  
19  import java.util.Set;
20  
21  import org.apache.commons.scxml2.model.EnterableState;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Unit tests for namespace prefixes in XPaths pointing bits in a <data>.
27   */
28  public class NamespacePrefixedXPathsTest {
29  
30      /**
31       * Test the XPath evaluation
32       */
33      // JEXL
34      @Test
35      public void testNamespacePrefixedXPathsJexl() throws Exception {
36          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-03.xml");
37          exec.go();
38          runtest(exec);
39      }
40  
41      // Same test, since same documents (different expression languages)
42      private void runtest(SCXMLExecutor exec) throws Exception {
43          // must be in state "ten" at the onset
44          Set<EnterableState> currentStates = exec.getStatus().getStates();
45          Assert.assertEquals(1, currentStates.size());
46          Assert.assertEquals("ten", currentStates.iterator().next().getId());
47  
48          // should move to "twenty"
49          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
50          Assert.assertEquals(1, currentStates.size());
51          Assert.assertEquals("twenty", currentStates.iterator().next().getId());
52  
53          // This is set while exiting "ten"
54          Double retval = (Double) exec.getGlobalContext().get("retval");
55          Assert.assertEquals(Double.valueOf("11"), retval);
56  
57          // On to "thirty"
58          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.twenty");
59          Assert.assertEquals(1, currentStates.size());
60          Assert.assertEquals("thirty", currentStates.iterator().next().getId());
61          exec = SCXMLTestHelper.testInstanceSerializability(exec);
62  
63          // Tests XPath on SCXML actions, set while exiting "twenty"
64          String retvalstr = (String) exec.getGlobalContext().get("retval");
65          Assert.assertEquals("Equal to 20", retvalstr);
66  
67          // and so on ...
68          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.thirty");
69          Assert.assertEquals(1, currentStates.size());
70          Assert.assertEquals("forty", currentStates.iterator().next().getId());
71  
72          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.forty");
73          Assert.assertEquals(1, currentStates.size());
74          Assert.assertEquals("fifty", currentStates.iterator().next().getId());
75  
76          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.fifty");
77          Assert.assertEquals(1, currentStates.size());
78          Assert.assertEquals("sixty", (currentStates.iterator().
79              next()).getId());
80  
81          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.sixty");
82          Assert.assertEquals(1, currentStates.size());
83          Assert.assertEquals("seventy", currentStates.iterator().next().getId());
84  
85          // done
86          Assert.assertTrue(exec.getStatus().isFinal());
87      }
88  }
89