    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.model;
    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
    26  import org.apache.commons.scxml.SCXMLExecutor;
    27  import org.apache.commons.scxml.SCXMLTestHelper;
    28
    29  public class HistoryTest extends TestCase {
    30      /***
    31       * Construct a new instance of HistoryTest with
    32       * the specified name
    33       */
    34      public HistoryTest(String testName) {
    35          super(testName);
    36      }
    37
    38      public static Test suite() {
    39          return new TestSuite(HistoryTest.class);
    40      }
    41
    42      public static void main(String args[]) {
    43          String[] testCaseName = { HistoryTest.class.getName()};
    44          junit.textui.TestRunner.main(testCaseName);
    45      }
    46
    47      // Test data
    48      private History history;
    49      private URL shallow01, deep01, defaults01;
    50      private SCXMLExecutor exec;
    51
    52      /***
    53       * Set up instance variables required by this test case.
    54       */
    55      public void setUp() {
    56          history = new History();
    57          shallow01 = this.getClass().getClassLoader().
    58              getResource("org/apache/commons/scxml/history-shallow-01.xml");
    59          deep01 = this.getClass().getClassLoader().
    60              getResource("org/apache/commons/scxml/history-deep-01.xml");
    61          defaults01 = this.getClass().getClassLoader().
    62              getResource("org/apache/commons/scxml/history-default-01.xml");
    63      }
    64
    65      /***
    66       * Tear down instance variables required by this test case.
    67       */
    68      public void tearDown() {
    69          history = null;
    70          shallow01 = deep01 = defaults01 = null;
    71          exec = null;
    72      }
    73
    74      /***
    75       * Test the implementation
    76       */
    77      public void testSetTypeDeep() {
    78          history.setType("deep");
    79
    80          assertTrue(history.isDeep());
    81      }
    82
    83      public void testSetTypeNotDeep() {
    84          history.setType("shallow");
    85
    86          assertFalse(history.isDeep());
    87      }
    88
    89      public void testShallowHistory01() {
    90          exec = SCXMLTestHelper.getExecutor(shallow01);
    91          runHistoryFlow();
    92      }
    93
    94      public void testDeepHistory01() {
    95          exec = SCXMLTestHelper.getExecutor(deep01);
    96          runHistoryFlow();
    97      }
    98
    99      public void testHistoryDefaults01() {
    100         exec = SCXMLTestHelper.getExecutor(defaults01);
    101         Set currentStates = exec.getCurrentStatus().getStates();
    102         assertEquals(1, currentStates.size());
    103         assertEquals("state11", ((State)currentStates.iterator().
    104             next()).getId());
    105         currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
    106         assertEquals(1, currentStates.size());
    107         assertEquals("state211", ((State)currentStates.iterator().
    108             next()).getId());
    109         currentStates = SCXMLTestHelper.fireEvent(exec, "state.next");
    110         assertEquals(1, currentStates.size());
    111         assertEquals("state31", ((State)currentStates.iterator().
    112             next()).getId());
    113     }
    114
    115     private void runHistoryFlow() {
    116         Set currentStates = exec.getCurrentStatus().getStates();
    117         assertEquals(1, currentStates.size());
    118         assertEquals("phase1", ((State)currentStates.iterator().
    119             next()).getId());
    120         assertEquals("phase1", pauseAndResume());
    121         assertEquals("phase2", nextPhase());
    122         // pause and resume couple of times for good measure
    123         assertEquals("phase2", pauseAndResume());
    124         assertEquals("phase2", pauseAndResume());
    125         assertEquals("phase3", nextPhase());
    126         assertEquals("phase3", pauseAndResume());
    127         try {
    128             exec.reset();
    129         } catch (ModelException me) {
    130             fail(me.getMessage());
    131         }
    132         currentStates = exec.getCurrentStatus().getStates();
    133         assertEquals(1, currentStates.size());
    134         assertEquals("phase1", ((State)currentStates.iterator().
    135             next()).getId());
    136     }
    137
    138     private String pauseAndResume() {
    139         Set currentStates = SCXMLTestHelper.fireEvent(exec, "flow.pause");
    140         assertEquals(1, currentStates.size());
    141         assertEquals("interrupted", ((State)currentStates.iterator().
    142             next()).getId());
    143         exec = SCXMLTestHelper.testExecutorSerializability(exec);
    144         currentStates = SCXMLTestHelper.fireEvent(exec, "flow.resume");
    145         assertEquals(1, currentStates.size());
    146         exec = SCXMLTestHelper.testExecutorSerializability(exec);
    147         return ((State)currentStates.iterator().next()).getId();
    148     }
    149
    150     private String nextPhase() {
    151         Set currentStates = SCXMLTestHelper.fireEvent(exec, "phase.done");
    152         assertEquals(1, currentStates.size());
    153         return ((State)currentStates.iterator().next()).getId();
    154     }
    155
    156 }