1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml.model;
18
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.scxml.SCXMLExecutor;
28 import org.apache.commons.scxml.SCXMLTestHelper;
29 import org.apache.commons.scxml.env.jsp.ELEvaluator;
30
31 public class CustomActionTest extends TestCase {
32
33 public CustomActionTest(String testName) {
34 super(testName);
35 }
36
37 public static Test suite() {
38 return new TestSuite(CustomActionTest.class);
39 }
40
41 public static void main(String args[]) {
42 String[] testCaseName = { CustomActionTest.class.getName()};
43 junit.textui.TestRunner.main(testCaseName);
44 }
45
46 private URL hello01, custom01, external01, override01, payload01, payload02;
47 private SCXMLExecutor exec;
48
49 /***
50 * Set up instance variables required by this test case.
51 */
52 public void setUp() {
53 hello01 = this.getClass().getClassLoader().
54 getResource("org/apache/commons/scxml/hello-world.xml");
55 custom01 = this.getClass().getClassLoader().
56 getResource("org/apache/commons/scxml/custom-hello-world-01.xml");
57 external01 = this.getClass().getClassLoader().
58 getResource("org/apache/commons/scxml/external-hello-world.xml");
59 override01 = this.getClass().getClassLoader().
60 getResource("org/apache/commons/scxml/custom-hello-world-03.xml");
61 payload01 = this.getClass().getClassLoader().
62 getResource("org/apache/commons/scxml/custom-hello-world-04-jexl.xml");
63 payload02 = this.getClass().getClassLoader().
64 getResource("org/apache/commons/scxml/custom-hello-world-04-el.xml");
65 }
66
67 /***
68 * Tear down instance variables required by this test case.
69 */
70 public void tearDown() {
71 hello01 = custom01 = external01 = payload01 = payload02 = null;
72 exec = null;
73 }
74
75 public void testAddGoodCustomAction01() {
76 try {
77 new CustomAction("http://my.actions.domain/CUSTOM", "hello",
78 Hello.class);
79 } catch (IllegalArgumentException iae) {
80 fail("Failed to add custom action "Hello"");
81 }
82 }
83
84 public void testAddBadCustomAction01() {
85 try {
86 new CustomAction(null, "hello", Hello.class);
87 fail("Added custom action with illegal namespace");
88 } catch (IllegalArgumentException iae) {
89
90 }
91 }
92
93 public void testAddBadCustomAction02() {
94 try {
95 new CustomAction(" ", "hello", Hello.class);
96 fail("Added custom action with illegal namespace");
97 } catch (IllegalArgumentException iae) {
98
99 }
100 }
101
102 public void testAddBadCustomAction03() {
103 try {
104 new CustomAction("http://my.actions.domain/CUSTOM", "",
105 Hello.class);
106 fail("Added custom action with illegal local name");
107 } catch (IllegalArgumentException iae) {
108
109 }
110 }
111
112 public void testAddBadCustomAction04() {
113 try {
114 new CustomAction("http://my.actions.domain/CUSTOM", " ",
115 Hello.class);
116 fail("Added custom action with illegal local name");
117 } catch (IllegalArgumentException iae) {
118
119 }
120 }
121
122 public void testAddBadCustomAction05() {
123 try {
124 new CustomAction("http://my.actions.domain/CUSTOM", "foo",
125 this.getClass());
126 fail("Added custom action which is not an Action class subtype");
127 } catch (IllegalArgumentException iae) {
128
129 }
130 }
131
132 public void testAddBadCustomAction06() {
133 try {
134 new CustomAction("http://www.w3.org/2005/07/scxml", "foo",
135 this.getClass());
136 fail("Added custom action in the SCXML namespace");
137 } catch (IllegalArgumentException iae) {
138
139 }
140 }
141
142
143 public void testHelloWorld() {
144
145 exec = SCXMLTestHelper.getExecutor(hello01);
146
147 assertEquals("hello", ((State) exec.getCurrentStatus().getStates().
148 iterator().next()).getId());
149 assertTrue(exec.getCurrentStatus().isFinal());
150 }
151
152
153 public void testCustomActionHelloWorld() {
154
155
156 CustomAction ca1 =
157 new CustomAction("http://my.custom-actions.domain/CUSTOM1",
158 "hello", Hello.class);
159
160
161 CustomAction ca2 =
162 new CustomAction("http://my.custom-actions.domain/CUSTOM2",
163 "bar", Hello.class);
164 List customActions = new ArrayList();
165 customActions.add(ca1);
166 customActions.add(ca2);
167
168 SCXML scxml = SCXMLTestHelper.digest(custom01, customActions);
169
170 exec = SCXMLTestHelper.getExecutor(scxml);
171
172 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
173 iterator().next()).getId());
174 assertTrue(exec.getCurrentStatus().isFinal());
175 }
176
177
178
179 public void testCustomActionExternalSrcHelloWorld() {
180
181
182 CustomAction ca =
183 new CustomAction("http://my.custom-actions.domain/CUSTOM",
184 "hello", Hello.class);
185 List customActions = new ArrayList();
186 customActions.add(ca);
187
188 SCXML scxml = SCXMLTestHelper.digest(external01, customActions);
189
190 exec = SCXMLTestHelper.getExecutor(scxml);
191
192 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
193 iterator().next()).getId());
194 }
195
196
197
198 public void testCustomActionOverrideLocalName() {
199
200 CustomAction ca =
201 new CustomAction("http://my.custom-actions.domain/CUSTOM",
202 "send", Hello.class);
203 List customActions = new ArrayList();
204 customActions.add(ca);
205
206 SCXML scxml = SCXMLTestHelper.digest(override01, customActions);
207
208 exec = SCXMLTestHelper.getExecutor(scxml);
209
210 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
211 iterator().next()).getId());
212 }
213
214
215
216 public void testCustomActionCallbacks() {
217 assertEquals(5, Hello.callbacks);
218 }
219
220
221
222 public void testCustomActionEventPayloadHelloWorldJexl() {
223
224
225 CustomAction ca =
226 new CustomAction("http://my.custom-actions.domain/CUSTOM",
227 "hello", Hello.class);
228 List customActions = new ArrayList();
229 customActions.add(ca);
230
231 SCXML scxml = SCXMLTestHelper.digest(payload01, customActions);
232
233 exec = SCXMLTestHelper.getExecutor(scxml);
234
235 assertEquals("Invalid intermediate state",
236 "custom1", ((State) exec.getCurrentStatus().getStates().
237 iterator().next()).getId());
238
239 assertEquals("Missing helloName1 in root context", "custom04a",
240 (String) exec.getRootContext().get("helloName1"));
241
242 SCXMLTestHelper.fireEvent(exec, "custom.next");
243
244 assertEquals("Missing helloName1 in root context", "custom04b",
245 (String) exec.getRootContext().get("helloName1"));
246 assertEquals("Invalid final state",
247 "end", ((State) exec.getCurrentStatus().getStates().
248 iterator().next()).getId());
249 assertTrue(exec.getCurrentStatus().isFinal());
250 }
251
252
253
254 public void testCustomActionEventPayloadHelloWorldEL() {
255
256
257 CustomAction ca =
258 new CustomAction("http://my.custom-actions.domain/CUSTOM",
259 "hello", Hello.class);
260 List customActions = new ArrayList();
261 customActions.add(ca);
262
263 SCXML scxml = SCXMLTestHelper.digest(payload02, customActions);
264
265 exec = SCXMLTestHelper.getExecutor(new ELEvaluator(), scxml);
266
267 assertEquals("Invalid final state",
268 "custom", ((State) exec.getCurrentStatus().getStates().
269 iterator().next()).getId());
270
271 assertEquals("Missing helloName1 in root context", "custom04",
272 (String) exec.getRootContext().get("helloName1"));
273 }
274
275 }
276