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.model;
018
019import java.util.Set;
020
021import org.apache.commons.scxml2.SCXMLExecutor;
022import org.apache.commons.scxml2.SCXMLTestHelper;
023import org.junit.Assert;
024import org.junit.Test;
025
026public class HistoryTest {
027
028    @Test
029    public void testSetTypeDeep() {
030        History history = new History();
031        history.setType("deep");
032        
033        Assert.assertTrue(history.isDeep());
034    }
035        
036    @Test
037    public void testSetTypeNotDeep() {
038        History history = new History();
039        history.setType("shallow");
040        
041        Assert.assertFalse(history.isDeep());
042    }
043    
044    @Test
045    public void testShallowHistory01() throws Exception {
046        SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-shallow-01.xml");
047        exec.go();
048        runHistoryFlow(exec);
049    }
050    
051    @Test
052    public void testDeepHistory01() throws Exception {
053        SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-deep-01.xml");
054        exec.go();
055        runHistoryFlow(exec);
056    }
057    
058    @Test
059    public void testHistoryDefaults01() throws Exception {
060        SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-default-01.xml");
061        exec.go();
062        Set<EnterableState> currentStates = exec.getStatus().getStates();
063        Assert.assertEquals(1, currentStates.size());
064        Assert.assertEquals("state11", currentStates.iterator().next().getId());
065        currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
066        Assert.assertEquals(1, currentStates.size());
067        Assert.assertEquals("state211", currentStates.iterator().next().getId());
068        currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
069        Assert.assertEquals(1, currentStates.size());
070        Assert.assertEquals("state31", currentStates.iterator().next().getId());
071    }
072    
073    @Test
074    public void testHistoryParallel01() throws Exception {
075        SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/history-parallel-01.xml");
076        exec.go();
077        Set<EnterableState> currentStates = exec.getStatus().getStates();
078        Assert.assertEquals(1, currentStates.size());
079        SCXMLTestHelper.assertState(exec, "off_call");
080        SCXMLTestHelper.assertPostTriggerStates(exec, "dial", new String[] { "talking", "on_call" });
081        SCXMLTestHelper.assertPostTriggerStates(exec, "consult", new String[] { "consult_talking", "on_consult" });
082        // Next line uses history to go back to on call and talking
083        SCXMLTestHelper.assertPostTriggerStates(exec, "alternate", new String[] { "talking", "on_call" });
084        // Hold
085        SCXMLTestHelper.assertPostTriggerStates(exec, "hold", new String[] { "held", "on_call" });
086        SCXMLTestHelper.assertPostTriggerStates(exec, "consult", new String[] { "consult_talking", "on_consult" });
087        // Next line uses history to go back to on call and on hold
088        SCXMLTestHelper.assertPostTriggerStates(exec, "alternate", new String[] { "held", "on_call" });
089    }
090
091    private void runHistoryFlow(SCXMLExecutor exec) throws Exception {
092        Set<EnterableState> currentStates = exec.getStatus().getStates();
093        Assert.assertEquals(1, currentStates.size());
094        Assert.assertEquals("phase1", currentStates.iterator().next().getId());
095        Assert.assertEquals("phase1", pauseAndResume(exec));
096        Assert.assertEquals("phase2", nextPhase(exec));
097        // pause and resume couple of times for good measure
098        Assert.assertEquals("phase2", pauseAndResume(exec));
099        Assert.assertEquals("phase2", pauseAndResume(exec));
100        Assert.assertEquals("phase3", nextPhase(exec));
101        Assert.assertEquals("phase3", pauseAndResume(exec));
102        exec.reset();
103        currentStates = exec.getStatus().getStates();
104        Assert.assertEquals(1, currentStates.size());
105        Assert.assertEquals("phase1", currentStates.iterator().next().getId());
106    }
107
108    private String pauseAndResume(SCXMLExecutor exec) throws Exception {
109        Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "flow.pause");
110        Assert.assertEquals(1, currentStates.size());
111        Assert.assertEquals("interrupted", currentStates.iterator().next().getId());
112        exec = SCXMLTestHelper.testInstanceSerializability(exec);
113        currentStates = SCXMLTestHelper.fireEvent(exec, "flow.resume");
114        Assert.assertEquals(1, currentStates.size());
115        SCXMLTestHelper.testInstanceSerializability(exec);
116        return currentStates.iterator().next().getId();
117    }
118
119    private String nextPhase(SCXMLExecutor exec) throws Exception {
120        Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.phase");
121        Assert.assertEquals(1, currentStates.size());
122        return currentStates.iterator().next().getId();
123    }
124
125}