1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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              // Expected
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              // Expected
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             // 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     // Hello World example using the SCXML <log> action
128     public void testHelloWorld() {
129         // (1) Get a SCXMLExecutor
130         exec = SCXMLTestHelper.getExecutor(hello01);
131         // (2) Single, final state
132         assertEquals("hello", ((State) exec.getCurrentStatus().getStates().
133                 iterator().next()).getId());
134         assertTrue(exec.getCurrentStatus().isFinal());
135     }
136 
137     // Hello World example using a custom <hello> action
138     public void testCustomActionHelloWorld() {
139         // (1) Form a list of custom actions defined in the SCXML
140         //     document (and any included documents via "src" attributes)
141         CustomAction ca1 =
142             new CustomAction("http://my.custom-actions.domain/CUSTOM1",
143                              "hello", Hello.class);
144         // Register the same action under a different name, just to test
145         // multiple custom actions
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         // (2) Parse the document with a custom digester.
153         SCXML scxml = SCXMLTestHelper.digest(custom01, customActions);
154         // (3) Get a SCXMLExecutor
155         exec = SCXMLTestHelper.getExecutor(scxml);
156         // (4) Single, final state
157         assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
158                 iterator().next()).getId());
159         assertTrue(exec.getCurrentStatus().isFinal());
160     }
161 
162     // Hello World example using custom <my:hello> action
163     // as part of an external state source (src attribute)
164     public void testCustomActionExternalSrcHelloWorld() {
165         // (1) Form a list of custom actions defined in the SCXML
166         //     document (and any included documents via "src" attributes)
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         // (2) Parse the document with a custom digester.
173         SCXML scxml = SCXMLTestHelper.digest(external01, customActions);
174         // (3) Get a SCXMLExecutor
175         exec = SCXMLTestHelper.getExecutor(scxml);
176         // (4) Single, final state
177         assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
178             iterator().next()).getId());
179     }
180 
181     // The custom action defined by Hello.class should be called
182     // to execute() exactly 4 times upto this point
183     public void testCustomActionCallbacks() {
184         assertEquals(4, Hello.callbacks);
185     }
186 
187 }
188