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.HashSet; 020import java.util.Set; 021 022import org.apache.commons.jexl2.JexlContext; 023import org.apache.commons.scxml2.env.SimpleContext; 024import org.apache.commons.scxml2.env.jexl.JexlEvaluator; 025import org.apache.commons.scxml2.model.EnterableState; 026import org.apache.commons.scxml2.model.History; 027import org.apache.commons.scxml2.model.State; 028import org.junit.Assert; 029import org.junit.Before; 030import org.junit.Test; 031 032public class SCInstanceTest { 033 034 private SCXMLExecutor executor; 035 private SCInstance instance; 036 037 @Before 038 public void setUp() { 039 executor = new SCXMLExecutor(); 040 instance = executor.getSCInstance(); 041 } 042 043 @Test 044 public void testGetRootContextNull() { 045 Assert.assertNull(instance.getRootContext()); 046 } 047 048 @Test 049 public void testGetRootContext() { 050 Context context = new SimpleContext(); 051 context.set("name", "value"); 052 053 instance.setRootContext(context); 054 Assert.assertEquals("value", instance.getRootContext().get("name")); 055 } 056 057 @Test 058 public void testGetRootContextEvaluator() throws Exception { 059 Evaluator evaluator = new JexlEvaluator(); 060 061 executor.setEvaluator(evaluator); 062 063 Assert.assertTrue(instance.getRootContext() instanceof JexlContext); 064 } 065 066 @Test 067 public void testGetContext() { 068 State target = new State(); 069 target.setId("1"); 070 071 Context context = new SimpleContext(); 072 context.set("name", "value"); 073 074 instance.setContext(target, context); 075 076 Assert.assertEquals("value", instance.getContext(target).get("name")); 077 } 078 079 @Test 080 public void testGetContextNullParent() throws Exception { 081 State target = new State(); 082 target.setId("1"); 083 084 Context context = new SimpleContext(); 085 context.set("name", "value"); 086 instance.setRootContext(context); 087 088 Evaluator evaluator = new JexlEvaluator(); 089 executor.setEvaluator(evaluator); 090 091 Assert.assertEquals("value", instance.getContext(target).get("name")); 092 Assert.assertEquals("value", instance.lookupContext(target).get("name")); 093 } 094 095 @Test 096 public void testGetContextParent() throws Exception { 097 State target = new State(); 098 target.setId("1"); 099 100 State parent = new State(); 101 parent.setId("parent"); 102 103 target.setParent(parent); 104 105 Context context = new SimpleContext(); 106 context.set("name", "value"); 107 instance.setRootContext(context); 108 109 Evaluator evaluator = new JexlEvaluator(); 110 executor.setEvaluator(evaluator); 111 112 Assert.assertEquals("value", instance.getContext(target).get("name")); 113 Assert.assertEquals("value", instance.lookupContext(target).get("name")); 114 } 115 116 @Test 117 public void testGetLastConfigurationNull() { 118 History history = new History(); 119 120 Set<EnterableState> returnConfiguration = instance.getLastConfiguration(history); 121 122 Assert.assertEquals(0, returnConfiguration.size()); 123 } 124 125 @Test 126 public void testGetLastConfiguration() { 127 History history = new History(); 128 history.setId("1"); 129 130 Set<EnterableState> configuration = new HashSet<EnterableState>(); 131 EnterableState tt1 = new State(); 132 EnterableState tt2 = new State(); 133 configuration.add(tt1); 134 configuration.add(tt2); 135 136 instance.setLastConfiguration(history, configuration); 137 138 Set<EnterableState> returnConfiguration = instance.getLastConfiguration(history); 139 140 Assert.assertEquals(2, returnConfiguration.size()); 141 Assert.assertTrue(returnConfiguration.contains(tt1)); 142 Assert.assertTrue(returnConfiguration.contains(tt2)); 143 } 144 145 @Test 146 public void testIsEmpty() { 147 Assert.assertTrue(instance.getLastConfiguration(new History()).isEmpty()); 148 } 149 150 @Test 151 public void testIsEmptyFalse() { 152 History history = new History(); 153 history.setId("1"); 154 155 Set<EnterableState> configuration = new HashSet<EnterableState>(); 156 EnterableState tt1 = new State(); 157 configuration.add(tt1); 158 159 instance.setLastConfiguration(history, configuration); 160 161 Assert.assertFalse(instance.getLastConfiguration(history).isEmpty()); 162 } 163 164 @Test 165 public void testReset() { 166 History history = new History(); 167 history.setId("1"); 168 169 Set<EnterableState> configuration = new HashSet<EnterableState>(); 170 EnterableState tt1 = new State(); 171 configuration.add(tt1); 172 173 instance.setLastConfiguration(history, configuration); 174 175 instance.resetConfiguration(history); 176 177 Assert.assertTrue(instance.getLastConfiguration(history).isEmpty()); 178 } 179 180}