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.env;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: public class SimpleContextTest extends TestCase {
027:
028: public SimpleContextTest(String testName) {
029: super (testName);
030: }
031:
032: public static Test suite() {
033: return new TestSuite(SimpleContextTest.class);
034: }
035:
036: public static void main(String args[]) {
037: String[] testCaseName = { SimpleContextTest.class.getName() };
038: junit.textui.TestRunner.main(testCaseName);
039: }
040:
041: private SimpleContext context;
042:
043: protected void setUp() throws Exception {
044: context = new SimpleContext();
045: }
046:
047: public void testHasTrue() {
048: Map vars = new HashMap();
049: vars.put("key", "value");
050:
051: context.setVars(vars);
052:
053: assertTrue(context.has("key"));
054: }
055:
056: public void testHasNullParent() {
057: Map vars = new HashMap();
058: vars.put("key", "value");
059:
060: context.setVars(vars);
061:
062: assertFalse(context.has("differentKey"));
063: }
064:
065: public void testHasParentWrongKey() {
066: Map parentVars = new HashMap();
067: parentVars.put("key", "value");
068:
069: SimpleContext parentContext = new SimpleContext(parentVars);
070:
071: Map vars = new HashMap();
072: vars.put("key", "value");
073:
074: context.setVars(vars);
075: context = new SimpleContext(parentContext, parentVars);
076:
077: assertFalse(context.has("differentKey"));
078: }
079:
080: public void testHasParentCorrectKey() {
081: Map parentVars = new HashMap();
082: parentVars.put("differentKey", "value");
083:
084: SimpleContext parentContext = new SimpleContext(parentVars);
085:
086: Map vars = new HashMap();
087: vars.put("key", "value");
088:
089: context.setVars(vars);
090: context = new SimpleContext(parentContext, parentVars);
091:
092: assertTrue(context.has("differentKey"));
093: }
094:
095: public void testGetNull() {
096: Object value = context.get("key");
097:
098: assertNull(value);
099: }
100:
101: public void testGetValue() {
102: Map vars = new HashMap();
103: vars.put("key", "value");
104:
105: context.setVars(vars);
106:
107: assertEquals("value", context.get("key"));
108: }
109:
110: public void testGetParentValue() {
111: Map parentVars = new HashMap();
112: parentVars.put("differentKey", "differentValue");
113:
114: SimpleContext parentContext = new SimpleContext(parentVars);
115:
116: Map vars = new HashMap();
117: vars.put("key", "value");
118:
119: context.setVars(vars);
120: context = new SimpleContext(parentContext, parentVars);
121:
122: assertEquals("differentValue", context.get("differentKey"));
123: }
124:
125: public void testGetParentNull() {
126: Map vars = new HashMap();
127: vars.put("key", "value");
128:
129: context.setVars(vars);
130:
131: assertNull(context.get("differentKey"));
132: }
133:
134: public void testGetParentWrongValue() {
135: Map parentVars = new HashMap();
136: parentVars.put("differentKey", "differentValue");
137:
138: SimpleContext parentContext = new SimpleContext(parentVars);
139:
140: Map vars = new HashMap();
141: vars.put("key", "value");
142:
143: context.setVars(vars);
144: context = new SimpleContext(parentContext, parentVars);
145:
146: assertNull(context.get("reallyDifferentKey"));
147: }
148:
149: public void testSetVarsChangeValue() {
150: Map vars = new HashMap();
151: vars.put("key", "value");
152:
153: context.setVars(vars);
154:
155: context.set("key", "newValue");
156:
157: assertEquals("newValue", context.get("key"));
158: }
159:
160: public void testSetVarsEmpty() {
161: Map vars = new HashMap();
162: context.setVars(vars);
163:
164: context.set("key", "newValue");
165:
166: assertEquals("newValue", context.get("key"));
167: }
168:
169: public void testSetVarsParent() {
170: Map parentVars = new HashMap();
171: parentVars.put("differentKey", "differentValue");
172:
173: SimpleContext parentContext = new SimpleContext(parentVars);
174:
175: Map vars = new HashMap();
176: vars.put("key", "value");
177:
178: context.setVars(vars);
179: context = new SimpleContext(parentContext, parentVars);
180:
181: context.set("differentKey", "newValue");
182:
183: assertEquals("newValue", context.get("differentKey"));
184: }
185: }
|