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.net.URL;
20  import java.util.Set;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  import junit.textui.TestRunner;
26  
27  import org.apache.commons.scxml.env.SimpleContext;
28  import org.apache.commons.scxml.env.jsp.ELEvaluator;
29  import org.apache.commons.scxml.model.State;
30  /***
31   * Unit tests {@link org.apache.commons.scxml.SCXMLExecutor}.
32   */
33  public class SCXMLExecutorTest extends TestCase {
34      /***
35       * Construct a new instance of SCXMLExecutorTest with
36       * the specified name
37       */
38      public SCXMLExecutorTest(String name) {
39          super(name);
40      }
41  
42      public static Test suite() {
43          TestSuite suite = new TestSuite(SCXMLExecutorTest.class);
44          suite.setName("SCXML Executor Tests");
45          return suite;
46      }
47  
48      // Test data
49      private URL microwave01jsp, microwave02jsp, microwave01jexl,
50          microwave02jexl, transitions01, transitions02, prefix01, send01, send02;
51      private SCXMLExecutor exec;
52  
53      /***
54       * Set up instance variables required by this test case.
55       */
56      public void setUp() {
57          microwave01jsp = this.getClass().getClassLoader().
58              getResource("org/apache/commons/scxml/env/jsp/microwave-01.xml");
59          microwave02jsp = this.getClass().getClassLoader().
60              getResource("org/apache/commons/scxml/env/jsp/microwave-02.xml");
61          microwave01jexl = this.getClass().getClassLoader().
62              getResource("org/apache/commons/scxml/env/jexl/microwave-01.xml");
63          microwave02jexl = this.getClass().getClassLoader().
64              getResource("org/apache/commons/scxml/env/jexl/microwave-02.xml");
65          transitions01 = this.getClass().getClassLoader().
66              getResource("org/apache/commons/scxml/transitions-01.xml");
67          transitions02 = this.getClass().getClassLoader().
68              getResource("org/apache/commons/scxml/transitions-02.xml");
69          prefix01 = this.getClass().getClassLoader().
70              getResource("org/apache/commons/scxml/prefix-01.xml");
71          send01 = this.getClass().getClassLoader().
72              getResource("org/apache/commons/scxml/send-01.xml");
73          send02 = this.getClass().getClassLoader().
74              getResource("org/apache/commons/scxml/send-02.xml");
75      }
76  
77      /***
78       * Tear down instance variables required by this test case.
79       */
80      public void tearDown() {
81          microwave01jsp = microwave02jsp = microwave01jexl = microwave02jexl =
82              transitions01 = transitions02 = prefix01 = send01 = send02 = null;
83      }
84  
85      /***
86       * Test the implementation
87       */
88      public void testSCXMLExecutorMicrowave01JspSample() {
89          exec = SCXMLTestHelper.getExecutor(microwave01jsp,
90              new SimpleContext(), new ELEvaluator());
91          assertNotNull(exec);
92          checkMicrowave01Sample();
93      }
94  
95      public void testSCXMLExecutorMicrowave02JspSample() {
96          exec = SCXMLTestHelper.getExecutor(microwave02jsp,
97              new SimpleContext(), new ELEvaluator());
98          assertNotNull(exec);
99          checkMicrowave02Sample();
100     }
101 
102     public void testSCXMLExecutorMicrowave01JexlSample() {
103         exec = SCXMLTestHelper.getExecutor(microwave01jexl);
104         assertNotNull(exec);
105         checkMicrowave01Sample();
106     }
107 
108     public void testSCXMLExecutorMicrowave02JexlSample() {
109         exec = SCXMLTestHelper.getExecutor(microwave02jexl);
110         assertNotNull(exec);
111         checkMicrowave02Sample();
112     }
113 
114     public void testSCXMLExecutorPrefix01Sample() {
115         exec = SCXMLTestHelper.getExecutor(prefix01);
116         assertNotNull(exec);
117         Set currentStates = exec.getCurrentStatus().getStates();
118         assertEquals(1, currentStates.size());
119         assertEquals("ten", ((State)currentStates.iterator().
120             next()).getId());
121         currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
122         assertEquals(1, currentStates.size());
123         assertEquals("twenty", ((State)currentStates.iterator().
124             next()).getId());
125     }
126 
127     public void testSCXMLExecutorTransitions01Sample() {
128         exec = SCXMLTestHelper.getExecutor(transitions01);
129         assertNotNull(exec);
130         try {
131             Set currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
132             assertEquals(1, currentStates.size());
133             assertEquals("twenty_one", ((State)currentStates.iterator().
134                 next()).getId());
135             currentStates = SCXMLTestHelper.fireEvent(exec, "twenty_one.done");
136             assertEquals(1, currentStates.size());
137             assertEquals("twenty_two", ((State)currentStates.iterator().
138                 next()).getId());
139             currentStates = SCXMLTestHelper.fireEvent(exec, "twenty_two.done");
140             assertEquals(3, exec.getCurrentStatus().getStates().size());
141         } catch (Exception e) {
142             fail(e.getMessage());
143         }
144     }
145 
146     public void testSCXMLExecutorTransitions02Sample() {
147         exec = SCXMLTestHelper.getExecutor(transitions02);
148         assertNotNull(exec);
149         try {
150             Set currentStates = SCXMLTestHelper.fireEvent(exec, "ten.stay");
151             assertEquals(1, currentStates.size());
152             assertEquals("ten", ((State)currentStates.iterator().
153                 next()).getId());
154             exec = SCXMLTestHelper.testExecutorSerializability(exec);
155             currentStates = SCXMLTestHelper.fireEvent(exec, "ten.self");
156             assertEquals(1, currentStates.size());
157             assertEquals("ten", ((State)currentStates.iterator().
158                 next()).getId());
159             currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
160             assertEquals(1, currentStates.size());
161             assertEquals("twenty", ((State)currentStates.iterator().
162                 next()).getId());
163         } catch (Exception e) {
164             fail(e.getMessage());
165         }
166     }
167 
168     public void testSend01Sample() {
169         exec = SCXMLTestHelper.getExecutor(send01);
170         assertNotNull(exec);
171         try {
172             Set currentStates = exec.getCurrentStatus().getStates();
173             assertEquals(1, currentStates.size());
174             assertEquals("ten", ((State)currentStates.iterator().
175                 next()).getId());
176             currentStates = SCXMLTestHelper.fireEvent(exec, "ten.done");
177             assertEquals(1, currentStates.size());
178             assertEquals("twenty", ((State)currentStates.iterator().
179                 next()).getId());
180         } catch (Exception e) {
181             fail(e.getMessage());
182         }
183     }
184 
185     public void testSend02TargettypeSCXMLSample() {
186         exec = SCXMLTestHelper.getExecutor(send02);
187         assertNotNull(exec);
188         try {
189             Set currentStates = exec.getCurrentStatus().getStates();
190             assertEquals(1, currentStates.size());
191             assertEquals("ninety", ((State)currentStates.iterator().
192                 next()).getId());
193         } catch (Exception e) {
194             fail(e.getMessage());
195         }
196     }
197 
198     private void checkMicrowave01Sample() {
199         try {
200             Set currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
201             assertEquals(1, currentStates.size());
202             assertEquals("cooking", ((State)currentStates.iterator().
203                 next()).getId());
204         } catch (Exception e) {
205             fail(e.getMessage());
206         }
207     }
208 
209     private void checkMicrowave02Sample() {
210         try {
211             Set currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
212             assertEquals(2, currentStates.size());
213             String id = ((State)currentStates.iterator().next()).getId();
214             assertTrue(id.equals("closed") || id.equals("cooking"));
215         } catch (Exception e) {
216             fail(e.getMessage());
217         }
218     }
219 
220     public static void main(String args[]) {
221         TestRunner.run(suite());
222     }
223 }
224