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;
18  
19  import java.io.Serializable;
20  import java.net.URL;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  import junit.textui.TestRunner;
29  
30  import org.apache.commons.scxml.env.Tracer;
31  import org.apache.commons.scxml.env.jexl.JexlContext;
32  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
33  import org.apache.commons.scxml.model.SCXML;
34  import org.apache.commons.scxml.model.State;
35  /***
36   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
37   */
38  public class WizardsTest extends TestCase {
39      /***
40       * Construct a new instance of SCXMLExecutorTest with
41       * the specified name
42       */
43      public WizardsTest(String name) {
44          super(name);
45      }
46  
47      public static Test suite() {
48          TestSuite suite = new TestSuite(WizardsTest.class);
49          suite.setName("SCXML Executor Tests, Wizard Style documents");
50          return suite;
51      }
52  
53      // Test data
54      private URL wizard01, wizard02;
55      private SCXMLExecutor exec;
56  
57      /***
58       * Set up instance variables required by this test case.
59       */
60      public void setUp() {
61          wizard01 = this.getClass().getClassLoader().
62              getResource("org/apache/commons/scxml/env/jexl/wizard-01.xml");
63          wizard02 = this.getClass().getClassLoader().
64              getResource("org/apache/commons/scxml/env/jexl/wizard-02.xml");
65      }
66  
67      /***
68       * Tear down instance variables required by this test case.
69       */
70      public void tearDown() {
71          wizard01 = wizard02 = null;
72      }
73  
74      /***
75       * Test the wizard style SCXML documents, and send usage
76       */
77      public void testWizard01Sample() {
78      	exec = SCXMLTestHelper.getExecutor(wizard01);
79          assertNotNull(exec);
80          try {
81              Set currentStates = exec.getCurrentStatus().getStates();
82              assertEquals(1, currentStates.size());
83              assertEquals("state1", ((State)currentStates.iterator().
84                  next()).getId());
85              exec = SCXMLTestHelper.testExecutorSerializability(exec);
86              currentStates = SCXMLTestHelper.fireEvent(exec, "event2");
87              assertEquals(1, currentStates.size());
88              assertEquals("state2", ((State)currentStates.iterator().
89                  next()).getId());
90              currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
91              assertEquals(1, currentStates.size());
92              assertEquals("state4", ((State)currentStates.iterator().
93                  next()).getId());
94              currentStates = SCXMLTestHelper.fireEvent(exec, "event3");
95              assertEquals(1, currentStates.size());
96              assertEquals("state3", ((State)currentStates.iterator().
97                  next()).getId());
98              exec = SCXMLTestHelper.testExecutorSerializability(exec);
99              currentStates = SCXMLTestHelper.fireEvent(exec, "event3"); // ensure we stay put
100             assertEquals(1, currentStates.size());
101             assertEquals("state3", ((State)currentStates.iterator().
102                 next()).getId());
103         } catch (Exception e) {
104             fail(e.getMessage());
105         }
106     }
107 
108     public void testWizard02Sample() {
109         SCXML scxml = SCXMLTestHelper.digest(wizard02);
110         exec = SCXMLTestHelper.getExecutor(new JexlContext(),
111             new JexlEvaluator(), scxml, new TestEventDispatcher(),
112             new Tracer());
113         assertNotNull(exec);
114         try {
115             // If you change this, you must also change
116             // the TestEventDispatcher
117             Set currentStates = exec.getCurrentStatus().getStates();
118             assertEquals(1, currentStates.size());
119             assertEquals("state2", ((State)currentStates.iterator().
120                 next()).getId());
121             exec = SCXMLTestHelper.testExecutorSerializability(exec);
122             currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
123             assertEquals(1, currentStates.size());
124             assertEquals("state4", ((State)currentStates.iterator().
125                 next()).getId());
126         } catch (Exception e) {
127             fail(e.getMessage());
128         }
129     }
130 
131     static class TestEventDispatcher implements EventDispatcher, Serializable {
132         private static final long serialVersionUID = 1L;
133         // If you change this, you must also change testWizard02Sample()
134         int callback = 0;
135         public void send(String sendId, String target, String targetType,
136                 String event, Map params, Object hints, long delay,
137                 List externalNodes) {
138             int i = ((Integer) params.get("aValue")).intValue();
139             switch (callback) {
140                 case 0:
141                     assertTrue(i == 2); // state2
142                     callback++;
143                     break;
144                 case 1:
145                     assertTrue(i == 4); // state4
146                     callback++;
147                     break;
148                 default:
149                     fail("More than 2 TestEventDispatcher <send> callbacks");
150             }
151         }
152         public void cancel(String sendId) {
153             // should never be called
154             fail("<cancel> TestEventDispatcher callback unexpected");
155         }
156     }
157 
158     public static void main(String args[]) {
159         TestRunner.run(suite());
160     }
161 }