1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml.env;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 public class SimpleContextTest extends TestCase {
27
28 public SimpleContextTest(String testName) {
29 super(testName);
30 }
31
32 public static Test suite() {
33 return new TestSuite(SimpleContextTest.class);
34 }
35
36 public static void main(String args[]) {
37 String[] testCaseName = { SimpleContextTest.class.getName()};
38 junit.textui.TestRunner.main(testCaseName);
39 }
40
41 private SimpleContext context;
42
43 protected void setUp() throws Exception {
44 context = new SimpleContext();
45 }
46
47 public void testHasTrue() {
48 Map vars = new HashMap();
49 vars.put("key", "value");
50
51 context.setVars(vars);
52
53 assertTrue(context.has("key"));
54 }
55
56 public void testHasNullParent() {
57 Map vars = new HashMap();
58 vars.put("key", "value");
59
60 context.setVars(vars);
61
62 assertFalse(context.has("differentKey"));
63 }
64
65 public void testHasParentWrongKey() {
66 Map parentVars = new HashMap();
67 parentVars.put("key", "value");
68
69 SimpleContext parentContext = new SimpleContext(parentVars);
70
71 Map vars = new HashMap();
72 vars.put("key", "value");
73
74 context.setVars(vars);
75 context = new SimpleContext(parentContext, parentVars);
76
77 assertFalse(context.has("differentKey"));
78 }
79
80 public void testHasParentCorrectKey() {
81 Map parentVars = new HashMap();
82 parentVars.put("differentKey", "value");
83
84 SimpleContext parentContext = new SimpleContext(parentVars);
85
86 Map vars = new HashMap();
87 vars.put("key", "value");
88
89 context.setVars(vars);
90 context = new SimpleContext(parentContext, parentVars);
91
92 assertTrue(context.has("differentKey"));
93 }
94
95 public void testGetNull() {
96 Object value = context.get("key");
97
98 assertNull(value);
99 }
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 }