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.invoke;
18  
19  import java.util.Map;
20  
21  import org.apache.commons.scxml2.SCXMLExecutor;
22  import org.apache.commons.scxml2.SCXMLIOProcessor;
23  import org.apache.commons.scxml2.SCXMLTestHelper;
24  import org.apache.commons.scxml2.TriggerEvent;
25  import org.apache.commons.scxml2.model.ModelException;
26  import org.junit.After;
27  import org.junit.Assert;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.Node;
32  
33  // Tests for 4.3.1 in WD-scxml-20080516
34  public class InvokeParamNameTest {
35  
36      private SCXMLExecutor exec;
37  
38      static String lastSource;
39      static Map<String, Object> lastParams;
40      
41      @Before
42      public void setUp() throws Exception {
43          exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/invoke/invoker-04.xml");
44          exec.registerInvokerClass("x-test", DummyInvoker.class);
45          exec.go();
46      }
47      
48      @After
49      public void tearDown() {
50          exec.unregisterInvokerClass("x-test");    
51      }
52      
53      private void trigger() throws ModelException {
54          lastParams = null;
55          lastSource = null;
56          exec.triggerEvent(new TriggerEvent("test.trigger",
57              TriggerEvent.SIGNAL_EVENT)); 
58      }
59      
60      // Tests "param" element with "name" and "expr" attribute    
61      @Test
62      public void testNameAndExpr() throws Exception {
63          trigger();
64          Assert.assertTrue(lastSource.endsWith("TestSrc"));
65          final Map.Entry<String, Object> e =
66              lastParams.entrySet().iterator().next();
67          Assert.assertEquals("ding", e.getKey());
68          Assert.assertEquals("foo", e.getValue());
69      }
70  
71      // Tests "param" element with a "name" attribute and "expr" attribute locating a node
72      @Test
73      public void testSoleNameLocation() throws Exception {
74          trigger(); trigger();
75          final Element e = (Element)lastParams.values().iterator().next();
76          Assert.assertNotNull(e);
77          Assert.assertEquals("bar", e.getNodeName());
78          Assert.assertEquals(Node.TEXT_NODE, e.getFirstChild().getNodeType());
79          Assert.assertEquals("foo", e.getFirstChild().getNodeValue());
80      }
81  
82      public static class DummyInvoker implements Invoker {
83  
84          private String invokeId;
85  
86          @Override
87          public void invoke(String source, Map<String, Object> params)
88          throws InvokerException {
89              lastSource = source;
90              lastParams = params;
91          }
92  
93          public String lastSource() {
94              return lastSource;
95          }
96  
97          public Map<String, Object> lastParams() {
98              return lastParams;
99          }
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 }