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 */
017package org.apache.commons.scxml2.invoke;
018
019import java.util.Set;
020
021import org.apache.commons.scxml2.SCXMLExecutor;
022import org.apache.commons.scxml2.SCXMLTestHelper;
023import org.apache.commons.scxml2.env.SimpleDispatcher;
024import org.apache.commons.scxml2.env.SimpleErrorReporter;
025import org.apache.commons.scxml2.io.SCXMLReader;
026import org.apache.commons.scxml2.model.EnterableState;
027import org.apache.commons.scxml2.model.SCXML;
028import org.junit.Assert;
029import org.junit.Test;
030
031/**
032 * Unit tests {@link org.apache.commons.scxml2.SCXMLExecutor}.
033 * Testing <invoke>
034 */
035public class InvokeTest {
036
037    /**
038     * Test the SCXML documents, usage of &lt;invoke&gt;
039     */    
040    @Test
041    public void testInvoke01Sample() throws Exception {
042        SCXML scxml = SCXMLReader.read(SCXMLTestHelper.getResource("org/apache/commons/scxml2/invoke/invoker-01.xml"));
043        SCXMLExecutor exec = new SCXMLExecutor(null, new SimpleDispatcher(), new SimpleErrorReporter());
044        exec.setStateMachine(scxml);
045        exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
046        exec.go();
047        Set<EnterableState> currentStates = exec.getStatus().getStates();
048        Assert.assertEquals(1, currentStates.size());
049        Assert.assertEquals("invoker", currentStates.iterator().next().getId());
050    }
051    
052    @Test
053    public void testInvoke02Sample() throws Exception {
054        SCXML scxml = SCXMLReader.read(SCXMLTestHelper.getResource("org/apache/commons/scxml2/invoke/invoker-02.xml"));
055        SCXMLExecutor exec = new SCXMLExecutor(null, new SimpleDispatcher(), new SimpleErrorReporter());
056        exec.setStateMachine(scxml);
057        exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
058        exec.go();
059        Set<EnterableState> currentStates = exec.getStatus().getStates();
060        Assert.assertEquals(1, currentStates.size());
061    }
062    
063    @Test
064    public void testInvoke03Sample() throws Exception {
065        SCXML scxml = SCXMLReader.read(SCXMLTestHelper.getResource("org/apache/commons/scxml2/invoke/invoker-03.xml"));
066        SCXMLExecutor exec = new SCXMLExecutor(null, new SimpleDispatcher(), new SimpleErrorReporter());
067        exec.setStateMachine(scxml);
068        exec.registerInvokerClass("scxml", SimpleSCXMLInvoker.class);
069        exec.go();
070        Set<EnterableState> currentStates = exec.getStatus().getStates();
071        Assert.assertEquals(1, currentStates.size());
072        SCXMLTestHelper.fireEvent(exec, "s1.next");
073        SCXMLTestHelper.fireEvent(exec, "state1.next");
074    }
075}
076