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.env.SimpleDispatcher;
29  import org.apache.commons.scxml.env.SimpleErrorHandler;
30  import org.apache.commons.scxml.env.SimpleErrorReporter;
31  import org.apache.commons.scxml.env.jexl.JexlContext;
32  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
33  import org.apache.commons.scxml.io.SCXMLDigester;
34  import org.apache.commons.scxml.model.SCXML;
35  import org.apache.commons.scxml.model.State;
36  
37  /***
38   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
39   * Testing <invoke>
40   */
41  public class InvokeTest extends TestCase {
42      /***
43       * Construct a new instance of SCXMLExecutorTest with
44       * the specified name
45       */
46      public InvokeTest(String name) {
47          super(name);
48      }
49  
50      public static Test suite() {
51          TestSuite suite = new TestSuite(InvokeTest.class);
52          suite.setName("SCXML Executor Tests, wildcard event match");
53          return suite;
54      }
55  
56      // Test data
57      private URL invoke01;
58      private SCXMLExecutor exec;
59  
60      /***
61       * Set up instance variables required by this test case.
62       */
63      public void setUp() {
64          invoke01 = this.getClass().getClassLoader().
65              getResource("org/apache/commons/scxml/invoke/invoker-01.xml");
66      }
67  
68      /***
69       * Tear down instance variables required by this test case.
70       */
71      public void tearDown() {
72          invoke01 = null;
73      }
74  
75      /***
76       * Test the SCXML documents, usage of &lt;invoke&gt;
77       */
78      public void testInvoke01Sample() {
79          try {
80              SCXML scxml = SCXMLDigester.digest(invoke01,
81                  new SimpleErrorHandler());
82              exec = new SCXMLExecutor(new JexlEvaluator(), new SimpleDispatcher(),
83                  new SimpleErrorReporter());
84              assertNotNull(exec);
85              exec.setRootContext(new JexlContext());
86              exec.setStateMachine(scxml);
87              exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
88              exec.go();
89              Set currentStates = exec.getCurrentStatus().getStates();
90              assertEquals(1, currentStates.size());
91              assertEquals("invoker", ((State)currentStates.iterator().
92                  next()).getId());
93          } catch (Exception e) {
94              e.printStackTrace();
95              fail(e.getMessage());
96          }
97      }
98  
99      public static void main(String args[]) {
100         TestRunner.run(suite());
101     }
102 }
103