1   /*
2    * Copyright 2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.scxml;
17  
18  import java.net.URL;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  import junit.textui.TestRunner;
27  
28  import org.apache.commons.scxml.env.Tracer;
29  import org.apache.commons.scxml.env.jexl.JexlContext;
30  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
31  import org.apache.commons.scxml.model.SCXML;
32  import org.apache.commons.scxml.model.State;
33  /***
34   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
35   */
36  public class WizardsTest extends TestCase {
37      /***
38       * Construct a new instance of SCXMLExecutorTest with
39       * the specified name
40       */
41      public WizardsTest(String name) {
42          super(name);
43      }
44  
45      public static Test suite() {
46          TestSuite suite = new TestSuite(WizardsTest.class);
47          suite.setName("SCXML Executor Tests, Wizard Style documents");
48          return suite;
49      }
50  
51      // Test data
52      private URL wizard01, wizard02;
53      private SCXMLExecutor exec;
54  
55      /***
56       * Set up instance variables required by this test case.
57       */
58      public void setUp() {
59          wizard01 = this.getClass().getClassLoader().
60              getResource("org/apache/commons/scxml/env/jexl/wizard-01.xml");
61          wizard02 = this.getClass().getClassLoader().
62              getResource("org/apache/commons/scxml/env/jexl/wizard-02.xml");
63      }
64  
65      /***
66       * Tear down instance variables required by this test case.
67       */
68      public void tearDown() {
69          wizard01 = wizard02 = null;
70      }
71  
72      /***
73       * Test the wizard style SCXML documents, and send usage
74       */
75      public void testWizard01Sample() {
76      	exec = SCXMLTestHelper.getExecutor(wizard01);
77          assertNotNull(exec);
78          try {
79              Set currentStates = exec.getCurrentStatus().getStates();
80              assertEquals(1, currentStates.size());
81              assertEquals("state1", ((State)currentStates.iterator().
82                  next()).getId());
83              currentStates = SCXMLTestHelper.fireEvent(exec, "event2");
84              assertEquals(1, currentStates.size());
85              assertEquals("state2", ((State)currentStates.iterator().
86                  next()).getId());
87              currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
88              assertEquals(1, currentStates.size());
89              assertEquals("state4", ((State)currentStates.iterator().
90                  next()).getId());
91              currentStates = SCXMLTestHelper.fireEvent(exec, "event3");
92              assertEquals(1, currentStates.size());
93              assertEquals("state3", ((State)currentStates.iterator().
94                  next()).getId());
95              currentStates = SCXMLTestHelper.fireEvent(exec, "event3"); // ensure we stay put
96              assertEquals(1, currentStates.size());
97              assertEquals("state3", ((State)currentStates.iterator().
98                  next()).getId());
99          } catch (Exception e) {
100             fail(e.getMessage());
101         }
102     }
103 
104     public void testWizard02Sample() {
105         SCXML scxml = SCXMLTestHelper.digest(wizard02);
106         exec = SCXMLTestHelper.getExecutor(new JexlContext(),
107             new JexlEvaluator(), scxml, new TestEventDispatcher(),
108             new Tracer());
109         assertNotNull(exec);
110         try {
111             // If you change this, you must also change
112             // the TestEventDispatcher
113             Set currentStates = exec.getCurrentStatus().getStates();
114             assertEquals(1, currentStates.size());
115             assertEquals("state2", ((State)currentStates.iterator().
116                 next()).getId());
117             currentStates = SCXMLTestHelper.fireEvent(exec, "event4");
118             assertEquals(1, currentStates.size());
119             assertEquals("state4", ((State)currentStates.iterator().
120                 next()).getId());
121         } catch (Exception e) {
122             fail(e.getMessage());
123         }
124     }
125 
126     class TestEventDispatcher implements EventDispatcher {
127         // If you change this, you must also change testWizard02Sample()
128         int callback = 0;
129         public void send(String sendId, String target, String targetType,
130                 String event, Map params, Object hints, long delay,
131                 List externalNodes) {
132             int i = ((Integer) params.get("aValue")).intValue();
133             switch (callback) {
134                 case 0:
135                     assertTrue(i == 2); // state2
136                     callback++;
137                     break;
138                 case 1:
139                     assertTrue(i == 4); // state4
140                     callback++;
141                     break;
142                 default:
143                     fail("More than 2 TestEventDispatcher <send> callbacks");
144             }
145         }
146         public void cancel(String sendId) {
147             // should never be called
148             fail("<cancel> TestEventDispatcher callback unexpected");
149         }
150     }
151 
152     public static void main(String args[]) {
153         TestRunner.run(suite());
154     }
155 }