001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml;
018:
019: import java.net.URL;
020: import java.util.Set;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.scxml.env.jsp.ELContext;
028: import org.apache.commons.scxml.env.jsp.ELEvaluator;
029: import org.apache.commons.scxml.model.State;
030:
031: /**
032: * Unit tests for namespace prefixes in XPaths pointing bits in a <data>.
033: */
034: public class NamespacePrefixedXPathsTest extends TestCase {
035:
036: /**
037: * Construct a new instance of NamespacePrefixedXPathsTest with
038: * the specified name
039: */
040: public NamespacePrefixedXPathsTest(String name) {
041: super (name);
042: }
043:
044: public static Test suite() {
045: TestSuite suite = new TestSuite(
046: NamespacePrefixedXPathsTest.class);
047: suite.setName("Namespace Prefixed XPaths Tests");
048: return suite;
049: }
050:
051: // Test data
052: private URL datamodel03jexl, datamodel03jsp;
053: private SCXMLExecutor exec01, exec02;
054:
055: /**
056: * Set up instance variables required by this test case.
057: */
058: public void setUp() {
059: datamodel03jexl = this .getClass().getClassLoader().getResource(
060: "org/apache/commons/scxml/env/jexl/datamodel-03.xml");
061: datamodel03jsp = this .getClass().getClassLoader().getResource(
062: "org/apache/commons/scxml/env/jsp/datamodel-03.xml");
063: exec01 = SCXMLTestHelper.getExecutor(datamodel03jexl);
064: exec02 = SCXMLTestHelper.getExecutor(datamodel03jsp,
065: new ELContext(), new ELEvaluator());
066: }
067:
068: /**
069: * Tear down instance variables required by this test case.
070: */
071: public void tearDown() {
072: datamodel03jexl = datamodel03jsp = null;
073: exec01 = exec02 = null;
074: }
075:
076: /**
077: * Test the XPath evaluation
078: */
079: // JEXL
080: public void testNamespacePrefixedXPathsJexl() {
081: runtest(exec01);
082: }
083:
084: // EL
085: public void testNamespacePrefixedXPathsEL() {
086: runtest(exec02);
087: }
088:
089: // Same test, since same documents (different expression languages)
090: private void runtest(SCXMLExecutor exec) {
091:
092: try {
093:
094: // must be in state "ten" at the onset
095: Set currentStates = exec.getCurrentStatus().getStates();
096: assertEquals(1, currentStates.size());
097: assertEquals("ten", ((State) currentStates.iterator()
098: .next()).getId());
099:
100: // should move to "twenty"
101: currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
102: assertEquals(1, currentStates.size());
103: assertEquals("twenty", ((State) currentStates.iterator()
104: .next()).getId());
105:
106: // This is set while exiting "ten"
107: Double retval = (Double) exec.getRootContext()
108: .get("retval");
109: assertEquals(Double.valueOf("11"), retval);
110:
111: // On to "thirty"
112: currentStates = SCXMLTestHelper.fireEvent(exec,
113: "twenty.done");
114: assertEquals(1, currentStates.size());
115: assertEquals("thirty", ((State) currentStates.iterator()
116: .next()).getId());
117: exec = SCXMLTestHelper.testExecutorSerializability(exec);
118:
119: // Tests XPath on SCXML actions, set while exiting "twenty"
120: String retvalstr = (String) exec.getRootContext().get(
121: "retval");
122: assertEquals("Equal to 20", retvalstr);
123:
124: // and so on ...
125: currentStates = SCXMLTestHelper.fireEvent(exec,
126: "thirty.done");
127: assertEquals(1, currentStates.size());
128: assertEquals("forty", ((State) currentStates.iterator()
129: .next()).getId());
130:
131: currentStates = SCXMLTestHelper.fireEvent(exec,
132: "forty.done");
133: assertEquals(1, currentStates.size());
134: assertEquals("fifty", ((State) currentStates.iterator()
135: .next()).getId());
136:
137: currentStates = SCXMLTestHelper.fireEvent(exec,
138: "fifty.done");
139: assertEquals(1, currentStates.size());
140: assertEquals("sixty", ((State) currentStates.iterator()
141: .next()).getId());
142:
143: currentStates = SCXMLTestHelper.fireEvent(exec,
144: "sixty.done");
145: assertEquals(1, currentStates.size());
146: assertEquals("seventy", ((State) currentStates.iterator()
147: .next()).getId());
148:
149: // done
150: assertTrue(exec.getCurrentStatus().isFinal());
151:
152: } catch (Exception e) {
153: fail(e.getMessage());
154: }
155:
156: }
157:
158: public static void main(String args[]) {
159: TestRunner.run(suite());
160: }
161:
162: }
|