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 */
017package org.apache.commons.scxml2;
018
019import java.util.Set;
020
021import org.apache.commons.scxml2.model.EnterableState;
022import org.junit.Assert;
023import org.junit.Test;
024
025/**
026 * Unit tests for namespace prefixes in XPaths pointing bits in a <data>.
027 */
028public class NamespacePrefixedXPathsTest {
029
030    /**
031     * Test the XPath evaluation
032     */
033    // JEXL
034    @Test
035    public void testNamespacePrefixedXPathsJexl() throws Exception {
036        SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-03.xml");
037        exec.go();
038        runtest(exec);
039    }
040
041    // Same test, since same documents (different expression languages)
042    private void runtest(SCXMLExecutor exec) throws Exception {
043        // must be in state "ten" at the onset
044        Set<EnterableState> currentStates = exec.getStatus().getStates();
045        Assert.assertEquals(1, currentStates.size());
046        Assert.assertEquals("ten", currentStates.iterator().next().getId());
047
048        // should move to "twenty"
049        currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
050        Assert.assertEquals(1, currentStates.size());
051        Assert.assertEquals("twenty", currentStates.iterator().next().getId());
052
053        // This is set while exiting "ten"
054        Double retval = (Double) exec.getGlobalContext().get("retval");
055        Assert.assertEquals(Double.valueOf("11"), retval);
056
057        // On to "thirty"
058        currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.twenty");
059        Assert.assertEquals(1, currentStates.size());
060        Assert.assertEquals("thirty", currentStates.iterator().next().getId());
061        exec = SCXMLTestHelper.testInstanceSerializability(exec);
062
063        // Tests XPath on SCXML actions, set while exiting "twenty"
064        String retvalstr = (String) exec.getGlobalContext().get("retval");
065        Assert.assertEquals("Equal to 20", retvalstr);
066
067        // and so on ...
068        currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.thirty");
069        Assert.assertEquals(1, currentStates.size());
070        Assert.assertEquals("forty", currentStates.iterator().next().getId());
071
072        currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.forty");
073        Assert.assertEquals(1, currentStates.size());
074        Assert.assertEquals("fifty", currentStates.iterator().next().getId());
075
076        currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.fifty");
077        Assert.assertEquals(1, currentStates.size());
078        Assert.assertEquals("sixty", (currentStates.iterator().
079            next()).getId());
080
081        currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.sixty");
082        Assert.assertEquals(1, currentStates.size());
083        Assert.assertEquals("seventy", currentStates.iterator().next().getId());
084
085        // done
086        Assert.assertTrue(exec.getStatus().isFinal());
087    }
088}
089