001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml.model;
018:
019: import java.net.URL;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.apache.commons.scxml.SCXMLExecutor;
028: import org.apache.commons.scxml.SCXMLTestHelper;
029:
030: public class CustomActionTest extends TestCase {
031:
032: public CustomActionTest(String testName) {
033: super (testName);
034: }
035:
036: public static Test suite() {
037: return new TestSuite(CustomActionTest.class);
038: }
039:
040: public static void main(String args[]) {
041: String[] testCaseName = { CustomActionTest.class.getName() };
042: junit.textui.TestRunner.main(testCaseName);
043: }
044:
045: private URL hello01, custom01, external01, override01;
046: private SCXMLExecutor exec;
047:
048: /**
049: * Set up instance variables required by this test case.
050: */
051: public void setUp() {
052: hello01 = this .getClass().getClassLoader().getResource(
053: "org/apache/commons/scxml/hello-world.xml");
054: custom01 = this .getClass().getClassLoader().getResource(
055: "org/apache/commons/scxml/custom-hello-world-01.xml");
056: external01 = this .getClass().getClassLoader().getResource(
057: "org/apache/commons/scxml/external-hello-world.xml");
058: override01 = this .getClass().getClassLoader().getResource(
059: "org/apache/commons/scxml/custom-hello-world-03.xml");
060: }
061:
062: /**
063: * Tear down instance variables required by this test case.
064: */
065: public void tearDown() {
066: hello01 = custom01 = external01 = null;
067: exec = null;
068: }
069:
070: public void testAddGoodCustomAction01() {
071: try {
072: new CustomAction("http://my.actions.domain/CUSTOM",
073: "hello", Hello.class);
074: } catch (IllegalArgumentException iae) {
075: fail("Failed to add custom action "Hello"");
076: }
077: }
078:
079: public void testAddBadCustomAction01() {
080: try {
081: new CustomAction(null, "hello", Hello.class);
082: fail("Added custom action with illegal namespace");
083: } catch (IllegalArgumentException iae) {
084: // Expected
085: }
086: }
087:
088: public void testAddBadCustomAction02() {
089: try {
090: new CustomAction(" ", "hello", Hello.class);
091: fail("Added custom action with illegal namespace");
092: } catch (IllegalArgumentException iae) {
093: // Expected
094: }
095: }
096:
097: public void testAddBadCustomAction03() {
098: try {
099: new CustomAction("http://my.actions.domain/CUSTOM", "",
100: Hello.class);
101: fail("Added custom action with illegal local name");
102: } catch (IllegalArgumentException iae) {
103: // Expected
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: // Expected
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: // Expected
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: // Expected
134: }
135: }
136:
137: // Hello World example using the SCXML <log> action
138: public void testHelloWorld() {
139: // (1) Get a SCXMLExecutor
140: exec = SCXMLTestHelper.getExecutor(hello01);
141: // (2) Single, final state
142: assertEquals("hello", ((State) exec.getCurrentStatus()
143: .getStates().iterator().next()).getId());
144: assertTrue(exec.getCurrentStatus().isFinal());
145: }
146:
147: // Hello World example using a custom <hello> action
148: public void testCustomActionHelloWorld() {
149: // (1) Form a list of custom actions defined in the SCXML
150: // document (and any included documents via "src" attributes)
151: CustomAction ca1 = new CustomAction(
152: "http://my.custom-actions.domain/CUSTOM1", "hello",
153: Hello.class);
154: // Register the same action under a different name, just to test
155: // multiple custom actions
156: CustomAction ca2 = new CustomAction(
157: "http://my.custom-actions.domain/CUSTOM2", "bar",
158: Hello.class);
159: List customActions = new ArrayList();
160: customActions.add(ca1);
161: customActions.add(ca2);
162: // (2) Parse the document with a custom digester.
163: SCXML scxml = SCXMLTestHelper.digest(custom01, customActions);
164: // (3) Get a SCXMLExecutor
165: exec = SCXMLTestHelper.getExecutor(scxml);
166: // (4) Single, final state
167: assertEquals("custom", ((State) exec.getCurrentStatus()
168: .getStates().iterator().next()).getId());
169: assertTrue(exec.getCurrentStatus().isFinal());
170: }
171:
172: // Hello World example using custom <my:hello> action
173: // as part of an external state source (src attribute)
174: public void testCustomActionExternalSrcHelloWorld() {
175: // (1) Form a list of custom actions defined in the SCXML
176: // document (and any included documents via "src" attributes)
177: CustomAction ca = new CustomAction(
178: "http://my.custom-actions.domain/CUSTOM", "hello",
179: Hello.class);
180: List customActions = new ArrayList();
181: customActions.add(ca);
182: // (2) Parse the document with a custom digester.
183: SCXML scxml = SCXMLTestHelper.digest(external01, customActions);
184: // (3) Get a SCXMLExecutor
185: exec = SCXMLTestHelper.getExecutor(scxml);
186: // (4) Single, final state
187: assertEquals("custom", ((State) exec.getCurrentStatus()
188: .getStates().iterator().next()).getId());
189: }
190:
191: // Hello World example using custom <my:send> action
192: // (overriding SCXML local name "send")
193: public void testCustomActionOverrideLocalName() {
194: // (1) List of custom actions, use same local name as SCXML action
195: CustomAction ca = new CustomAction(
196: "http://my.custom-actions.domain/CUSTOM", "send",
197: Hello.class);
198: List customActions = new ArrayList();
199: customActions.add(ca);
200: // (2) Parse the document with a custom digester.
201: SCXML scxml = SCXMLTestHelper.digest(override01, customActions);
202: // (3) Get a SCXMLExecutor
203: exec = SCXMLTestHelper.getExecutor(scxml);
204: // (4) Single, final state
205: assertEquals("custom", ((State) exec.getCurrentStatus()
206: .getStates().iterator().next()).getId());
207: }
208:
209: // The custom action defined by Hello.class should be called
210: // to execute() exactly 5 times upto this point
211: public void testCustomActionCallbacks() {
212: assertEquals(5, Hello.callbacks);
213: }
214:
215: }
|