1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml.invoke;
18  
19  import java.net.URL;
20  import java.util.Set;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  import junit.textui.TestRunner;
26  
27  import org.apache.commons.scxml.SCXMLExecutor;
28  import org.apache.commons.scxml.SCXMLTestHelper;
29  import org.apache.commons.scxml.env.SimpleDispatcher;
30  import org.apache.commons.scxml.env.SimpleErrorHandler;
31  import org.apache.commons.scxml.env.SimpleErrorReporter;
32  import org.apache.commons.scxml.env.jexl.JexlContext;
33  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
34  import org.apache.commons.scxml.io.SCXMLParser;
35  import org.apache.commons.scxml.model.SCXML;
36  import org.apache.commons.scxml.model.State;
37  
38  /***
39   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
40   * Testing <invoke>
41   */
42  public class InvokeTest extends TestCase {
43      /***
44       * Construct a new instance of SCXMLExecutorTest with
45       * the specified name
46       */
47      public InvokeTest(String name) {
48          super(name);
49      }
50  
51      public static Test suite() {
52          TestSuite suite = new TestSuite(InvokeTest.class);
53          suite.setName("SCXML Executor Tests, wildcard event match");
54          return suite;
55      }
56  
57      // Test data
58      private URL invoke01, invoke02, invoke03;
59      private SCXMLExecutor exec;
60  
61      /***
62       * Set up instance variables required by this test case.
63       */
64      public void setUp() {
65          invoke01 = this.getClass().getClassLoader().
66              getResource("org/apache/commons/scxml/invoke/invoker-01.xml");
67          invoke02 = this.getClass().getClassLoader().
68              getResource("org/apache/commons/scxml/invoke/invoker-02.xml");
69          invoke03 = this.getClass().getClassLoader().
70              getResource("org/apache/commons/scxml/invoke/invoker-03.xml");
71      }
72  
73      /***
74       * Tear down instance variables required by this test case.
75       */
76      public void tearDown() {
77          invoke01 = invoke02 = invoke03 = null;
78      }
79  
80      /***
81       * Test the SCXML documents, usage of &lt;invoke&gt;
82       */
83      public void testInvoke01Sample() {
84          try {
85              SCXML scxml = SCXMLParser.parse(invoke01,
86                  new SimpleErrorHandler());
87              exec = new SCXMLExecutor(new JexlEvaluator(), new SimpleDispatcher(),
88                  new SimpleErrorReporter());
89              assertNotNull(exec);
90              exec.setRootContext(new JexlContext());
91              exec.setStateMachine(scxml);
92              exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
93              exec.go();
94              Set currentStates = exec.getCurrentStatus().getStates();
95              assertEquals(1, currentStates.size());
96              assertEquals("invoker", ((State)currentStates.iterator().
97                  next()).getId());
98          } catch (Exception e) {
99              e.printStackTrace();
100             fail(e.getMessage());
101         }
102     }
103 
104     public void testInvoke02Sample() {
105         try {
106             SCXML scxml = SCXMLParser.parse(invoke02,
107                 new SimpleErrorHandler());
108             exec = new SCXMLExecutor(new JexlEvaluator(), new SimpleDispatcher(),
109                 new SimpleErrorReporter());
110             assertNotNull(exec);
111             exec.setRootContext(new JexlContext());
112             exec.setStateMachine(scxml);
113             exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
114             exec.go();
115             Set currentStates = exec.getCurrentStatus().getStates();
116             assertEquals(1, currentStates.size());
117         } catch (Exception e) {
118             e.printStackTrace();
119             fail(e.getMessage());
120         }
121     }
122 
123     public void testInvoke03Sample() {
124         try {
125             SCXML scxml = SCXMLParser.parse(invoke03,
126                 new SimpleErrorHandler());
127             exec = new SCXMLExecutor(new JexlEvaluator(), new SimpleDispatcher(),
128                 new SimpleErrorReporter());
129             assertNotNull(exec);
130             exec.setRootContext(new JexlContext());
131             exec.setStateMachine(scxml);
132             exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
133             exec.go();
134             Set currentStates = exec.getCurrentStatus().getStates();
135             assertEquals(1, currentStates.size());
136             SCXMLTestHelper.fireEvent(exec, "s1.next");
137             SCXMLTestHelper.fireEvent(exec, "state1.next");
138         } catch (Exception e) {
139             e.printStackTrace();
140             fail(e.getMessage());
141         }
142     }
143 
144     public static void main(String args[]) {
145         TestRunner.run(suite());
146     }
147 }
148