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