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
30 public class CustomActionTest extends TestCase {
31
32 public CustomActionTest(String testName) {
33 super(testName);
34 }
35
36 public static Test suite() {
37 return new TestSuite(CustomActionTest.class);
38 }
39
40 public static void main(String args[]) {
41 String[] testCaseName = { CustomActionTest.class.getName()};
42 junit.textui.TestRunner.main(testCaseName);
43 }
44
45 private URL hello01, custom01, external01, override01;
46 private SCXMLExecutor exec;
47
48 /***
49 * Set up instance variables required by this test case.
50 */
51 public void setUp() {
52 hello01 = this.getClass().getClassLoader().
53 getResource("org/apache/commons/scxml/hello-world.xml");
54 custom01 = this.getClass().getClassLoader().
55 getResource("org/apache/commons/scxml/custom-hello-world-01.xml");
56 external01 = this.getClass().getClassLoader().
57 getResource("org/apache/commons/scxml/external-hello-world.xml");
58 override01 = this.getClass().getClassLoader().
59 getResource("org/apache/commons/scxml/custom-hello-world-03.xml");
60 }
61
62 /***
63 * Tear down instance variables required by this test case.
64 */
65 public void tearDown() {
66 hello01 = custom01 = external01 = null;
67 exec = null;
68 }
69
70 public void testAddGoodCustomAction01() {
71 try {
72 new CustomAction("http://my.actions.domain/CUSTOM", "hello",
73 Hello.class);
74 } catch (IllegalArgumentException iae) {
75 fail("Failed to add custom action "Hello"");
76 }
77 }
78
79 public void testAddBadCustomAction01() {
80 try {
81 new CustomAction(null, "hello", Hello.class);
82 fail("Added custom action with illegal namespace");
83 } catch (IllegalArgumentException iae) {
84
85 }
86 }
87
88 public void testAddBadCustomAction02() {
89 try {
90 new CustomAction(" ", "hello", Hello.class);
91 fail("Added custom action with illegal namespace");
92 } catch (IllegalArgumentException iae) {
93
94 }
95 }
96
97 public void testAddBadCustomAction03() {
98 try {
99 new CustomAction("http://my.actions.domain/CUSTOM", "",
100 Hello.class);
101 fail("Added custom action with illegal local name");
102 } catch (IllegalArgumentException iae) {
103
104 }
105 }
106
107 public void testAddBadCustomAction04() {
108 try {
109 new CustomAction("http://my.actions.domain/CUSTOM", " ",
110 Hello.class);
111 fail("Added custom action with illegal local name");
112 } catch (IllegalArgumentException iae) {
113
114 }
115 }
116
117 public void testAddBadCustomAction05() {
118 try {
119 new CustomAction("http://my.actions.domain/CUSTOM", "foo",
120 this.getClass());
121 fail("Added custom action which is not an Action class subtype");
122 } catch (IllegalArgumentException iae) {
123
124 }
125 }
126
127 public void testAddBadCustomAction06() {
128 try {
129 new CustomAction("http://www.w3.org/2005/07/scxml", "foo",
130 this.getClass());
131 fail("Added custom action in the SCXML namespace");
132 } catch (IllegalArgumentException iae) {
133
134 }
135 }
136
137
138 public void testHelloWorld() {
139
140 exec = SCXMLTestHelper.getExecutor(hello01);
141
142 assertEquals("hello", ((State) exec.getCurrentStatus().getStates().
143 iterator().next()).getId());
144 assertTrue(exec.getCurrentStatus().isFinal());
145 }
146
147
148 public void testCustomActionHelloWorld() {
149
150
151 CustomAction ca1 =
152 new CustomAction("http://my.custom-actions.domain/CUSTOM1",
153 "hello", Hello.class);
154
155
156 CustomAction ca2 =
157 new CustomAction("http://my.custom-actions.domain/CUSTOM2",
158 "bar", Hello.class);
159 List customActions = new ArrayList();
160 customActions.add(ca1);
161 customActions.add(ca2);
162
163 SCXML scxml = SCXMLTestHelper.digest(custom01, customActions);
164
165 exec = SCXMLTestHelper.getExecutor(scxml);
166
167 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
168 iterator().next()).getId());
169 assertTrue(exec.getCurrentStatus().isFinal());
170 }
171
172
173
174 public void testCustomActionExternalSrcHelloWorld() {
175
176
177 CustomAction ca =
178 new CustomAction("http://my.custom-actions.domain/CUSTOM",
179 "hello", Hello.class);
180 List customActions = new ArrayList();
181 customActions.add(ca);
182
183 SCXML scxml = SCXMLTestHelper.digest(external01, customActions);
184
185 exec = SCXMLTestHelper.getExecutor(scxml);
186
187 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
188 iterator().next()).getId());
189 }
190
191
192
193 public void testCustomActionOverrideLocalName() {
194
195 CustomAction ca =
196 new CustomAction("http://my.custom-actions.domain/CUSTOM",
197 "send", Hello.class);
198 List customActions = new ArrayList();
199 customActions.add(ca);
200
201 SCXML scxml = SCXMLTestHelper.digest(override01, customActions);
202
203 exec = SCXMLTestHelper.getExecutor(scxml);
204
205 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
206 iterator().next()).getId());
207 }
208
209
210
211 public void testCustomActionCallbacks() {
212 assertEquals(5, Hello.callbacks);
213 }
214
215 }
216