1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }