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