    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.invoke;
    17
    18  import java.net.URL;
    19  import java.util.Set;
    20
    21  import junit.framework.Test;
    22  import junit.framework.TestCase;
    23  import junit.framework.TestSuite;
    24  import junit.textui.TestRunner;
    25
    26  import org.apache.commons.scxml.SCXMLExecutor;
    27  import org.apache.commons.scxml.SCXMLTestHelper;
    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  import org.apache.commons.scxml.model.TransitionTarget;
    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