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.Map;
20  import java.util.Set;
21  
22  import org.apache.commons.scxml2.env.SimpleDispatcher;
23  import org.apache.commons.scxml2.model.EnterableState;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * Unit tests
30   */
31  public class WizardsTest {
32  
33      /**
34       * Test the wizard style SCXML documents, and send usage
35       */
36      @Test
37      public void testWizard01Sample() throws Exception {
38          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/wizard-01.xml");
39          exec.go();
40          Assert.assertNotNull(exec);
41          Set<EnterableState> currentStates = exec.getStatus().getStates();
42          Assert.assertEquals(1, currentStates.size());
43          Assert.assertEquals("state1", currentStates.iterator().next().getId());
44          exec = SCXMLTestHelper.testInstanceSerializability(exec);
45          currentStates = SCXMLTestHelper.fireEvent(exec, "event2");
46          Assert.assertEquals(1, currentStates.size());
47          Assert.assertEquals("state2", currentStates.iterator().next().getId());
48          currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
49          Assert.assertEquals(1, currentStates.size());
50          Assert.assertEquals("state4", currentStates.iterator().next().getId());
51          currentStates = SCXMLTestHelper.fireEvent(exec, "event3");
52          Assert.assertEquals(1, currentStates.size());
53          Assert.assertEquals("state3", currentStates.iterator().next().getId());
54          exec = SCXMLTestHelper.testInstanceSerializability(exec);
55          currentStates = SCXMLTestHelper.fireEvent(exec, "event3"); // ensure we stay put
56          Assert.assertEquals(1, currentStates.size());
57          Assert.assertEquals("state3", currentStates.iterator().next().getId());
58      }
59  
60      @Test
61      public void testWizard02Sample() throws Exception {
62          SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/wizard-02.xml");
63          exec.setEventdispatcher(new TestEventDispatcher());
64          exec.go();
65          // If you change this, you must also change
66          // the TestEventDispatcher
67          Set<EnterableState> currentStates = exec.getStatus().getStates();
68          Assert.assertEquals(1, currentStates.size());
69          Assert.assertEquals("state2", currentStates.iterator().next().getId());
70          exec = SCXMLTestHelper.testInstanceSerializability(exec);
71          currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
72          Assert.assertEquals(1, currentStates.size());
73          Assert.assertEquals("state4", currentStates.iterator().next().getId());
74      }
75  
76      static class TestEventDispatcher extends SimpleDispatcher {
77          private static final long serialVersionUID = 1L;
78          // If you change this, you must also change testWizard02Sample()
79  
80          int callback = 0;
81  
82          @SuppressWarnings("unchecked")
83          public void send(Map<String, SCXMLIOProcessor> ioProcessors, String id, String target, String type,
84                  String event, Object data, Object hints, long delay) {
85              if ("foo".equals(type)) {
86                  Map<String, Object> params = (Map<String, Object>)data;
87                  int i = ((Integer) params.get("aValue"));
88                  switch (callback) {
89                      case 0:
90                          Assert.assertTrue(i == 2); // state2
91                          callback++;
92                          break;
93                      case 1:
94                          Assert.assertTrue(i == 4); // state4
95                          callback++;
96                          break;
97                      default:
98                          Assert.fail("More than 2 TestEventDispatcher <send> callbacks for type \"foo\"");
99                  }
100             }
101             else {
102                 super.send(ioProcessors, id, target, type, event, data, hints, delay);
103             }
104         }
105         public void cancel(String sendId) {
106             // should never be called
107             Assert.fail("<cancel> TestEventDispatcher callback unexpected");
108         }
109     }
110 }