001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.scxml2.env; 018 019import org.apache.commons.scxml2.SCXMLListener; 020import org.apache.commons.scxml2.model.EnterableState; 021import org.apache.commons.scxml2.model.State; 022import org.apache.commons.scxml2.model.Transition; 023import org.apache.commons.scxml2.model.TransitionTarget; 024import org.junit.After; 025import org.junit.Assert; 026import org.junit.Before; 027import org.junit.Test; 028 029/** 030 * Unit tests {@link org.apache.commons.scxml2.env.AbstractSCXMLListener}. 031 */ 032public class AbstractSCXMLListenerTest { 033 034 // Test data 035 private State to; 036 private State from; 037 private Transition transition; 038 private boolean heardOnEntry; 039 private boolean heardOnExit; 040 private boolean heardOnTransition; 041 042 /** 043 * Set up instance variables required by this test case. 044 */ 045 @Before 046 public void setUp() { 047 to = new State(); 048 from = new State(); 049 transition = new Transition(); 050 heardOnEntry = false; 051 heardOnExit = false; 052 heardOnTransition = false; 053 } 054 055 /** 056 * Tear down instance variables required by this test case. 057 */ 058 @After 059 public void tearDown() { 060 to = null; 061 from = null; 062 transition = null; 063 } 064 065 @Test 066 public void testAbstractSCXMLListener0() { 067 SCXMLListener listener0 = new AbstractSCXMLListener() { 068 /** 069 * @see SCXMLListener#onEntry(EnterableState) 070 */ 071 @Override 072 public void onEntry(EnterableState state) { 073 heardOnEntry = true; 074 } 075 076 /** 077 * @see SCXMLListener#onExit(EnterableState) 078 */ 079 @Override 080 public void onExit(EnterableState state) { 081 heardOnExit = true; 082 } 083 084 /** 085 * @see SCXMLListener#onTransition(TransitionTarget,TransitionTarget,Transition,String) 086 */ 087 @Override 088 public void onTransition(TransitionTarget from, TransitionTarget to, 089 Transition transition, String event) { 090 heardOnTransition = true; 091 } 092 }; 093 094 Assert.assertFalse("heardOnEntry == false", heardOnEntry); 095 Assert.assertFalse("heardOnExit == false", heardOnExit); 096 Assert.assertFalse("heardOnTransition == false", heardOnTransition); 097 listener0.onEntry(to); 098 listener0.onExit(to); 099 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}