View Javadoc
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.scxml2;
18  
19  import java.util.Set;
20  
21  import org.apache.commons.scxml2.model.EnterableState;
22  import org.apache.commons.scxml2.model.TransitionTarget;
23  import org.junit.Assert;
24  import org.junit.Test;
25  /**
26   * Unit tests for testing conflict resolution amongst multiple transitions
27   * within the {@link org.apache.commons.scxml2.SCXMLExecutor}'s default
28   * semantics.
29   *
30   * Upto v0.6, non-deterministic behavior leads to an error condition. Based
31   * on the February 2007 WD, such non-determinism should now be resolved
32   * based on document order and heirarchy of states within the state machine.
33   * This class tests various such cases where more than one candidate
34   * transition exists at a particular point, and tie-breaking rules are used
35   * to make progress, rather than resulting in error conditions.
36   */
37  public class TieBreakerTest {
38  
39      @Test
40      public void testTieBreaker01() throws Exception {
41          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/tie-breaker-01.xml");
42          exec.go();
43          Set<EnterableState> currentStates = exec.getStatus().getStates();
44          Assert.assertEquals(1, currentStates.size());
45          Assert.assertEquals("ten", currentStates.iterator().next().getId());
46          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
47          Assert.assertEquals(1, currentStates.size());
48          Assert.assertEquals("twenty", currentStates.iterator().next().getId());
49      }
50  
51      @Test
52      public void testTieBreaker02() throws Exception {
53          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/tie-breaker-02.xml");
54          exec.go();
55          Set<EnterableState> currentStates = exec.getStatus().getStates();
56          Assert.assertEquals(1, currentStates.size());
57          Assert.assertEquals("eleven", currentStates.iterator().next().getId());
58          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
59          Assert.assertEquals(1, currentStates.size());
60          Assert.assertEquals("thirty", currentStates.iterator().next().getId());
61      }
62  
63      @Test
64      public void testTieBreaker03() throws Exception {
65          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/tie-breaker-03.xml");
66          exec.go();
67          Set<EnterableState> currentStates = exec.getStatus().getStates();
68          Assert.assertEquals(1, currentStates.size());
69          Assert.assertEquals("eleven", currentStates.iterator().next().getId());
70          currentStates = SCXMLTestHelper.fireEvent(exec, "done.state.ten");
71          Assert.assertEquals(1, currentStates.size());
72          Assert.assertEquals("forty", currentStates.iterator().next().getId());
73      }
74  
75      @Test
76      public void testTieBreaker04() throws Exception {
77          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/tie-breaker-04.xml");
78          exec.go();
79          Set<EnterableState> currentStates = SCXMLTestHelper.fireEvent(exec, "event_2");
80          Assert.assertEquals(1, currentStates.size());
81          currentStates = SCXMLTestHelper.fireEvent(exec, "event_1");
82          Assert.assertEquals(1, currentStates.size());
83      }
84  
85      @Test
86      public void testTieBreaker05() throws Exception {
87          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/tie-breaker-05.xml");
88          exec.go();
89          Set<EnterableState> currentStates = exec.getStatus().getStates();
90          Assert.assertEquals(3, currentStates.size());
91          for (TransitionTarget tt : currentStates) {
92              String id = tt.getId();
93              Assert.assertTrue(id.equals("s11") || id.equals("s212")
94                  || id.equals("s2111"));
95          }
96          currentStates = SCXMLTestHelper.fireEvent(exec, "event1");
97          Assert.assertEquals(3, currentStates.size());
98          for (TransitionTarget tt : currentStates) {
99              String id = tt.getId();
100             Assert.assertTrue(id.equals("s12") || id.equals("s212")
101                 || id.equals("s2112"));
102         }
103     }
104 
105     @Test
106     public void testTieBreaker06() throws Exception {
107         SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/tie-breaker-06.xml");
108         exec.go();
109         Set<EnterableState> currentStates = exec.getStatus().getStates();
110         Assert.assertEquals(1, currentStates.size());
111     }
112 }