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.env;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  
24  import org.apache.commons.scxml.SCXMLListener;
25  import org.apache.commons.scxml.model.State;
26  import org.apache.commons.scxml.model.Transition;
27  import org.apache.commons.scxml.model.TransitionTarget;
28  
29  /***
30   * Unit tests {@link org.apache.commons.scxml.AbstractSCXMLListener}.
31   */
32  public class AbstractSCXMLListenerTest extends TestCase {
33      /***
34       * Construct a new instance of AbstractSCXMLListenerTest with the specified name
35       */
36      public AbstractSCXMLListenerTest(String name) {
37          super(name);
38      }
39  
40      // Test data
41      private TransitionTarget to;
42      private TransitionTarget from;
43      private Transition transition;
44      private boolean heardOnEntry;
45      private boolean heardOnExit;
46      private boolean heardOnTransition;
47  
48      /***
49       * Set up instance variables required by this test case.
50       */
51      public void setUp() {
52          to = new State();
53          from = new State();
54          transition = new Transition();
55          heardOnEntry = false;
56          heardOnExit = false;
57          heardOnTransition = false;
58      }
59  
60      /***
61       * Tear down instance variables required by this test case.
62       */
63      public void tearDown() {
64          to = null;
65          from = null;
66          transition = null;
67      }
68  
69      public void testAbstractSCXMLListener0() {
70          SCXMLListener listener0 = new AbstractSCXMLListener() {
71                  /***
72                   * @see SCXMLListener#onEntry(TransitionTarget)
73                   */
74                  public void onEntry(TransitionTarget state) {
75                      heardOnEntry = true;
76                  }
77  
78                  /***
79                   * @see SCXMLListener#onExit(TransitionTarget)
80                   */
81                  public void onExit(TransitionTarget state) {
82                      heardOnExit = true;
83                  }
84  
85                  /***
86                   * @see SCXMLListener#onTransition(TransitionTarget,TransitionTarget,Transition)
87                   */
88                  public void onTransition(TransitionTarget from, TransitionTarget to,
89                                           Transition transition) {
90                      heardOnTransition = true;
91                  }
92              };
93  
94          assertFalse("heardOnEntry == false", heardOnEntry);
95          assertFalse("heardOnExit == false", heardOnExit);
96          assertFalse("heardOnTransition == false", heardOnTransition);
97          listener0.onEntry(to);
98          listener0.onExit(to);
99          listener0.onTransition(from, to, transition);
100         assertTrue("heardOnEntry", heardOnEntry);
101         assertTrue("heardOnExit", heardOnExit);
102         assertTrue("heardOnExit", heardOnTransition);
103     }
104 
105     public void testAbstractSCXMLListener1() {
106         SCXMLListener listener1 = new AbstractSCXMLListener() {
107                 /***
108                  * @see SCXMLListener#onEntry(TransitionTarget)
109                  */
110                 public void onEntry(TransitionTarget state) {
111                     heardOnEntry = true;
112                 }
113 
114                 /***
115                  * @see SCXMLListener#onExit(TransitionTarget)
116                  */
117                 public void onExit(TransitionTarget state) {
118                     heardOnExit = true;
119                 }
120             };
121 
122         assertFalse("heardOnEntry == false", heardOnEntry);
123         assertFalse("heardOnExit == false", heardOnExit);
124         assertFalse("heardOnTransition == false", heardOnTransition);
125         listener1.onEntry(to);
126         listener1.onExit(to);
127         listener1.onTransition(from, to, transition);
128         assertTrue("heardOnEntry", heardOnEntry);
129         assertTrue("heardOnExit", heardOnExit);
130         assertFalse("heardOnTransition == false", heardOnTransition);
131     }
132 
133     public void testAbstractSCXMLListener2() {
134         SCXMLListener listener2 = new AbstractSCXMLListener() {
135                 /***
136                  * @see SCXMLListener#onEntry(TransitionTarget)
137                  */
138                 public void onEntry(TransitionTarget state) {
139                     heardOnEntry = true;
140                 }
141             };
142 
143         assertFalse("heardOnEntry == false", heardOnEntry);
144         assertFalse("heardOnExit == false", heardOnExit);
145         assertFalse("heardOnTransition == false", heardOnTransition);
146         listener2.onEntry(to);
147         listener2.onExit(to);
148         listener2.onTransition(from, to, transition);
149         assertTrue("heardOnEntry", heardOnEntry);
150         assertFalse("heardOnExit == false", heardOnExit);
151         assertFalse("heardOnTransition == false", heardOnTransition);
152     }
153 
154     public void testAbstractSCXMLListener3() {
155         SCXMLListener listener3 = new AbstractSCXMLListener() {
156                 // empty
157             };
158 
159         assertFalse("heardOnEntry == false", heardOnEntry);
160         assertFalse("heardOnExit == false", heardOnExit);
161         assertFalse("heardOnTransition == false", heardOnTransition);
162         listener3.onEntry(to);
163         listener3.onExit(to);
164         listener3.onTransition(from, to, transition);
165         assertFalse("heardOnEntry == false", heardOnEntry);
166         assertFalse("heardOnExit == false", heardOnExit);
167         assertFalse("heardOnTransition == false", heardOnTransition);
168     }
169 
170     public static Test suite() {
171         TestSuite suite = new TestSuite(AbstractSCXMLListenerTest.class);
172         suite.setName("AbstractSCXMLListener Tests");
173         return suite;
174     }
175 
176     public static void main(String args[]) {
177         TestRunner.run(suite());
178     }
179 }