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.model; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Set; 022 023import org.apache.commons.scxml2.SCXMLExecutor; 024import org.apache.commons.scxml2.SCXMLTestHelper; 025import org.apache.commons.scxml2.TriggerEvent; 026import org.junit.Assert; 027import org.junit.Test; 028/** 029 * Unit tests {@link org.apache.commons.scxml2.SCXMLExecutor}. 030 */ 031public class DatamodelTest { 032 033 /** 034 * Test the stateless model, simultaneous executions 035 */ 036 @Test 037 public void testDatamodelSimultaneousJexl() throws Exception { 038 SCXMLExecutor exec01 = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-01.xml"); 039 exec01.go(); 040 SCXMLExecutor exec02 = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-01.xml"); 041 exec02.go(); 042 Assert.assertFalse(exec01 == exec02); 043 runtest(exec01, exec02); 044 } 045 046 @Test 047 public void testDatamodelNamespacePrefixedXPaths() throws Exception { 048 SCXMLExecutor exec01 = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-02.xml"); 049 exec01.go(); 050 SCXMLExecutor exec02 = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-02.xml"); 051 exec02.go(); 052 Assert.assertFalse(exec01 == exec02); 053 runtest(exec01, exec02); 054 } 055 056 @Test 057 public void testDatamodel04Jexl() throws Exception { 058 SCXMLExecutor exec01 = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-04.xml"); 059 exec01.go(); 060 Set<EnterableState> currentStates = exec01.getStatus().getStates(); 061 Assert.assertEquals(1, currentStates.size()); 062 Assert.assertEquals("ten", currentStates.iterator().next().getId()); 063 Map<String, Object> payload = new HashMap<String, Object>(); 064 payload.put("one", "1"); 065 payload.put("two", "2"); 066 TriggerEvent te = new TriggerEvent("done.state.ten", TriggerEvent.SIGNAL_EVENT, payload); 067 SCXMLTestHelper.fireEvent(exec01, te); 068 currentStates = exec01.getStatus().getStates(); 069 Assert.assertEquals(1, currentStates.size()); 070 Assert.assertEquals("twenty", currentStates.iterator().next().getId()); 071 SCXMLTestHelper.fireEvent(exec01, "done.state.twenty"); 072 currentStates = exec01.getStatus().getStates(); 073 Assert.assertEquals(1, currentStates.size()); 074 Assert.assertEquals("thirty", currentStates.iterator().next().getId()); 075 } 076 077 @Test 078 public void testDatamodel05Jexl() throws Exception { 079 SCXMLExecutor exec01 = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/env/jexl/datamodel-05.xml"); 080 exec01.go(); 081 SCXMLTestHelper.assertState(exec01, "end"); 082 } 083 084 private void runtest(SCXMLExecutor exec01, SCXMLExecutor exec02) throws Exception { 085 //// Interleaved 086 // exec01 087 Set<EnterableState> currentStates = exec01.getStatus().getStates(); 088 Assert.assertEquals(1, currentStates.size()); 089 Assert.assertEquals("ten", currentStates.iterator().next().getId()); 090 exec01 = SCXMLTestHelper.testInstanceSerializability(exec01); 091 currentStates = fireEvent("done.state.ten", exec01); 092 Assert.assertEquals(1, currentStates.size()); 093 Assert.assertEquals("twenty", currentStates.iterator().next().getId()); 094 // exec02 095 currentStates = exec02.getStatus().getStates(); 096 Assert.assertEquals(1, currentStates.size()); 097 Assert.assertEquals("ten", currentStates.iterator().next().getId()); 098 // exec01 099 currentStates = fireEvent("done.state.twenty", exec01); 100 Assert.assertEquals(1, currentStates.size()); 101 Assert.assertEquals("thirty", currentStates.iterator().next().getId()); 102 exec01 = SCXMLTestHelper.testInstanceSerializability(exec01); 103 // exec02 104 currentStates = fireEvent("done.state.ten", exec02); 105 Assert.assertEquals(1, currentStates.size()); 106 Assert.assertEquals("twenty", currentStates.iterator().next().getId()); 107 exec02 = SCXMLTestHelper.testInstanceSerializability(exec02); 108 currentStates = fireEvent("done.state.twenty", exec02); 109 Assert.assertEquals(1, currentStates.size()); 110 Assert.assertEquals("thirty", currentStates.iterator().next().getId()); 111 currentStates = fireEvent("done.state.thirty", exec02); 112 Assert.assertEquals(1, currentStates.size()); 113 Assert.assertEquals("forty", currentStates.iterator().next().getId()); 114 // exec01 115 currentStates = fireEvent("done.state.thirty", exec01); 116 Assert.assertEquals(1, currentStates.size()); 117 Assert.assertEquals("forty", currentStates.iterator().next().getId()); 118 } 119 120 private Set<EnterableState> fireEvent(String name, SCXMLExecutor exec) throws Exception { 121 TriggerEvent[] evts = {new TriggerEvent(name, 122 TriggerEvent.SIGNAL_EVENT, null)}; 123 exec.triggerEvents(evts); 124 return exec.getStatus().getStates(); 125 } 126} 127