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.invoke;
018
019import java.util.Map;
020
021import org.apache.commons.scxml2.SCXMLExecutor;
022import org.apache.commons.scxml2.SCXMLIOProcessor;
023import org.apache.commons.scxml2.SCXMLTestHelper;
024import org.apache.commons.scxml2.TriggerEvent;
025import org.apache.commons.scxml2.model.ModelException;
026import org.junit.After;
027import org.junit.Assert;
028import org.junit.Before;
029import org.junit.Test;
030import org.w3c.dom.Element;
031import org.w3c.dom.Node;
032
033// Tests for 4.3.1 in WD-scxml-20080516
034public class InvokeParamNameTest {
035
036    private SCXMLExecutor exec;
037
038    static String lastSource;
039    static Map<String, Object> lastParams;
040    
041    @Before
042    public void setUp() throws Exception {
043        exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/invoke/invoker-04.xml");
044        exec.registerInvokerClass("x-test", DummyInvoker.class);
045        exec.go();
046    }
047    
048    @After
049    public void tearDown() {
050        exec.unregisterInvokerClass("x-test");    
051    }
052    
053    private void trigger() throws ModelException {
054        lastParams = null;
055        lastSource = null;
056        exec.triggerEvent(new TriggerEvent("test.trigger",
057            TriggerEvent.SIGNAL_EVENT)); 
058    }
059    
060    // Tests "param" element with "name" and "expr" attribute    
061    @Test
062    public void testNameAndExpr() throws Exception {
063        trigger();
064        Assert.assertTrue(lastSource.endsWith("TestSrc"));
065        final Map.Entry<String, Object> e =
066            lastParams.entrySet().iterator().next();
067        Assert.assertEquals("ding", e.getKey());
068        Assert.assertEquals("foo", e.getValue());
069    }
070
071    // Tests "param" element with a "name" attribute and "expr" attribute locating a node
072    @Test
073    public void testSoleNameLocation() throws Exception {
074        trigger(); trigger();
075        final Element e = (Element)lastParams.values().iterator().next();
076        Assert.assertNotNull(e);
077        Assert.assertEquals("bar", e.getNodeName());
078        Assert.assertEquals(Node.TEXT_NODE, e.getFirstChild().getNodeType());
079        Assert.assertEquals("foo", e.getFirstChild().getNodeValue());
080    }
081
082    public static class DummyInvoker implements Invoker {
083
084        private String invokeId;
085
086        @Override
087        public void invoke(String source, Map<String, Object> params)
088        throws InvokerException {
089            lastSource = source;
090            lastParams = params;
091        }
092
093        public String lastSource() {
094            return lastSource;
095        }
096
097        public Map<String, Object> lastParams() {
098            return lastParams;
099        }
100
101        @Override
102        public void cancel() throws InvokerException {
103            // Not needed
104        }
105
106        @Override
107        public void parentEvent(TriggerEvent evt) throws InvokerException {
108            // Not needed
109        }
110
111        @Override
112        public String getInvokeId() {
113            return invokeId;
114        }
115
116        @Override
117        public void setInvokeId(String invokeId) {
118            this.invokeId = invokeId;
119        }
120
121        @Override
122        public void setParentSCXMLExecutor(SCXMLExecutor parentSCXMLExecutor) {
123            // Not needed
124        }
125
126        @Override
127        public SCXMLIOProcessor getChildIOProcessor() {
128            // not used
129            return null;
130        }
131    }
132
133}