1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
104 }
105
106 @Override
107 public void parentEvent(TriggerEvent evt) throws InvokerException {
108
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
124 }
125
126 @Override
127 public SCXMLIOProcessor getChildIOProcessor() {
128
129 return null;
130 }
131 }
132
133 }