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.Map; 020import java.util.Set; 021 022import org.apache.commons.scxml2.env.SimpleDispatcher; 023import org.apache.commons.scxml2.model.EnterableState; 024 025import org.junit.Assert; 026import org.junit.Test; 027 028/** 029 * Unit tests 030 */ 031public class WizardsTest { 032 033 /** 034 * Test the wizard style SCXML documents, and send usage 035 */ 036 @Test 037 public void testWizard01Sample() throws Exception { 038 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/wizard-01.xml"); 039 exec.go(); 040 Assert.assertNotNull(exec); 041 Set<EnterableState> currentStates = exec.getStatus().getStates(); 042 Assert.assertEquals(1, currentStates.size()); 043 Assert.assertEquals("state1", currentStates.iterator().next().getId()); 044 exec = SCXMLTestHelper.testInstanceSerializability(exec); 045 currentStates = SCXMLTestHelper.fireEvent(exec, "event2"); 046 Assert.assertEquals(1, currentStates.size()); 047 Assert.assertEquals("state2", currentStates.iterator().next().getId()); 048 currentStates = SCXMLTestHelper.fireEvent(exec, "event4"); 049 Assert.assertEquals(1, currentStates.size()); 050 Assert.assertEquals("state4", currentStates.iterator().next().getId()); 051 currentStates = SCXMLTestHelper.fireEvent(exec, "event3"); 052 Assert.assertEquals(1, currentStates.size()); 053 Assert.assertEquals("state3", currentStates.iterator().next().getId()); 054 exec = SCXMLTestHelper.testInstanceSerializability(exec); 055 currentStates = SCXMLTestHelper.fireEvent(exec, "event3"); // ensure we stay put 056 Assert.assertEquals(1, currentStates.size()); 057 Assert.assertEquals("state3", currentStates.iterator().next().getId()); 058 } 059 060 @Test 061 public void testWizard02Sample() throws Exception { 062 SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/wizard-02.xml"); 063 exec.setEventdispatcher(new TestEventDispatcher()); 064 exec.go(); 065 // If you change this, you must also change 066 // the TestEventDispatcher 067 Set<EnterableState> currentStates = exec.getStatus().getStates(); 068 Assert.assertEquals(1, currentStates.size()); 069 Assert.assertEquals("state2", currentStates.iterator().next().getId()); 070 exec = SCXMLTestHelper.testInstanceSerializability(exec); 071 currentStates = SCXMLTestHelper.fireEvent(exec, "event4"); 072 Assert.assertEquals(1, currentStates.size()); 073 Assert.assertEquals("state4", currentStates.iterator().next().getId()); 074 } 075 076 static class TestEventDispatcher extends SimpleDispatcher { 077 private static final long serialVersionUID = 1L; 078 // If you change this, you must also change testWizard02Sample() 079 080 int callback = 0; 081 082 @SuppressWarnings("unchecked") 083 public void send(Map<String, SCXMLIOProcessor> ioProcessors, String id, String target, String type, 084 String event, Object data, Object hints, long delay) { 085 if ("foo".equals(type)) { 086 Map<String, Object> params = (Map<String, Object>)data; 087 int i = ((Integer) params.get("aValue")); 088 switch (callback) { 089 case 0: 090 Assert.assertTrue(i == 2); // state2 091 callback++; 092 break; 093 case 1: 094 Assert.assertTrue(i == 4); // state4 095 callback++; 096 break; 097 default: 098 Assert.fail("More than 2 TestEventDispatcher <send> callbacks for type \"foo\""); 099 } 100 } 101 else { 102 super.send(ioProcessors, id, target, type, event, data, hints, delay); 103 } 104 } 105 public void cancel(String sendId) { 106 // should never be called 107 Assert.fail("<cancel> TestEventDispatcher callback unexpected"); 108 } 109 } 110}