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; 018 019import java.util.Set; 020 021import org.apache.commons.scxml2.env.Tracer; 022import org.apache.commons.scxml2.model.EnterableState; 023import org.apache.commons.scxml2.model.SCXML; 024import org.junit.Assert; 025import org.junit.Test; 026/** 027 * Unit tests {@link org.apache.commons.scxml2.SCXMLExecutor}. 028 * Testing special variable "_event.data" 029 */ 030public class EventDataTest { 031 032 /** 033 * Test the SCXML documents, usage of "_event.data" 034 */ 035 @Test 036 public void testEventdata01Sample() throws Exception { 037 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/eventdata-01.xml"); 038 exec.go(); 039 Set<EnterableState> currentStates = exec.getStatus().getStates(); 040 Assert.assertEquals(1, currentStates.size()); 041 Assert.assertEquals("state1", currentStates.iterator().next().getId()); 042 TriggerEvent te = new TriggerEvent("event.foo", 043 TriggerEvent.SIGNAL_EVENT, new Integer(3)); 044 currentStates = SCXMLTestHelper.fireEvent(exec, te); 045 Assert.assertEquals(1, currentStates.size()); 046 Assert.assertEquals("state3", currentStates.iterator().next().getId()); 047 TriggerEvent[] evts = new TriggerEvent[] { te, 048 new TriggerEvent("event.bar", TriggerEvent.SIGNAL_EVENT, 049 new Integer(6))}; 050 currentStates = SCXMLTestHelper.fireEvents(exec, evts); 051 Assert.assertEquals(1, currentStates.size()); 052 Assert.assertEquals("state6", currentStates.iterator().next().getId()); 053 te = new TriggerEvent("event.baz", 054 TriggerEvent.SIGNAL_EVENT, new Integer(7)); 055 currentStates = SCXMLTestHelper.fireEvent(exec, te); 056 Assert.assertEquals(1, currentStates.size()); 057 Assert.assertEquals("state7", currentStates.iterator().next().getId()); 058 } 059 060 @Test 061 public void testEventdata02Sample() throws Exception { 062 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/eventdata-02.xml"); 063 exec.go(); 064 Set<EnterableState> currentStates = exec.getStatus().getStates(); 065 Assert.assertEquals(1, currentStates.size()); 066 Assert.assertEquals("state0", currentStates.iterator().next().getId()); 067 TriggerEvent te1 = new TriggerEvent("connection.alerting", 068 TriggerEvent.SIGNAL_EVENT, "line2"); 069 currentStates = SCXMLTestHelper.fireEvent(exec, te1); 070 Assert.assertEquals(1, currentStates.size()); 071 Assert.assertEquals("state2", currentStates.iterator().next().getId()); 072 TriggerEvent te2 = new TriggerEvent("connection.alerting", 073 TriggerEvent.SIGNAL_EVENT, 074 new ConnectionAlertingPayload(4)); 075 currentStates = SCXMLTestHelper.fireEvent(exec, te2); 076 Assert.assertEquals(1, currentStates.size()); 077 Assert.assertEquals("state4", currentStates.iterator().next().getId()); 078 } 079 080 @Test 081 public void testEventdata03Sample() throws Exception { 082 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/eventdata-03.xml"); 083 exec.go(); 084 Set<EnterableState> currentStates = exec.getStatus().getStates(); 085 Assert.assertEquals(1, currentStates.size()); 086 Assert.assertEquals("ten", currentStates.iterator().next().getId()); 087 TriggerEvent te = new TriggerEvent("event.foo", 088 TriggerEvent.SIGNAL_EVENT); 089 currentStates = SCXMLTestHelper.fireEvent(exec, te); 090 Assert.assertEquals(1, currentStates.size()); 091 Assert.assertEquals("thirty", currentStates.iterator().next().getId()); 092 } 093 094 @Test 095 public void testEventdata04Sample() throws Exception { 096 SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/env/jexl/eventdata-03.xml"); 097 Tracer trc = new Tracer(); 098 SCXMLExecutor exec = new SCXMLExecutor(null, null, trc); 099 exec.addListener(scxml, trc); 100 exec.setStateMachine(scxml); 101 exec.go(); 102 Thread.sleep(200); // let the 100 delay lapse 103 } 104 105 public static class ConnectionAlertingPayload { 106 private int line; 107 public ConnectionAlertingPayload(int line) { 108 this.line = line; 109 } 110 public void setLine(int line) { 111 this.line = line; 112 } 113 public int getLine() { 114 return line; 115 } 116 } 117}