1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.scxml.model;
17
18 import java.net.URL;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.digester.Digester;
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;
46 private Digester digester;
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 }
60
61 /***
62 * Tear down instance variables required by this test case.
63 */
64 public void tearDown() {
65 hello01 = custom01 = external01 = null;
66 digester = 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
128 public void testHelloWorld() {
129
130 exec = SCXMLTestHelper.getExecutor(hello01);
131
132 assertEquals("hello", ((State) exec.getCurrentStatus().getStates().
133 iterator().next()).getId());
134 assertTrue(exec.getCurrentStatus().isFinal());
135 }
136
137
138 public void testCustomActionHelloWorld() {
139
140
141 CustomAction ca1 =
142 new CustomAction("http://my.custom-actions.domain/CUSTOM1",
143 "hello", Hello.class);
144
145
146 CustomAction ca2 =
147 new CustomAction("http://my.custom-actions.domain/CUSTOM2",
148 "bar", Hello.class);
149 List customActions = new ArrayList();
150 customActions.add(ca1);
151 customActions.add(ca2);
152
153 SCXML scxml = SCXMLTestHelper.digest(custom01, customActions);
154
155 exec = SCXMLTestHelper.getExecutor(scxml);
156
157 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
158 iterator().next()).getId());
159 assertTrue(exec.getCurrentStatus().isFinal());
160 }
161
162
163
164 public void testCustomActionExternalSrcHelloWorld() {
165
166
167 CustomAction ca =
168 new CustomAction("http://my.custom-actions.domain/CUSTOM",
169 "hello", Hello.class);
170 List customActions = new ArrayList();
171 customActions.add(ca);
172
173 SCXML scxml = SCXMLTestHelper.digest(external01, customActions);
174
175 exec = SCXMLTestHelper.getExecutor(scxml);
176
177 assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
178 iterator().next()).getId());
179 }
180
181
182
183 public void testCustomActionCallbacks() {
184 assertEquals(4, Hello.callbacks);
185 }
186
187 }
188