View Javadoc
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.scxml2.env;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.junit.Assert;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  public class SimpleContextTest {
27  
28      private SimpleContext context;
29  
30      @Before
31      public void setUp() throws Exception {
32          context = new SimpleContext();
33      }
34      
35      @Test
36      public void testHasTrue() {
37          Map<String, Object> vars = new HashMap<String, Object>();
38          vars.put("key", "value");
39          
40          context.setVars(vars);
41          
42          Assert.assertTrue(context.has("key"));
43      }
44  
45      @Test
46      public void testHasNullParent() {
47          Map<String, Object> vars = new HashMap<String, Object>();
48          vars.put("key", "value");
49          
50          context.setVars(vars);
51          
52          Assert.assertFalse(context.has("differentKey"));
53      }
54      
55      @Test
56      public void testHasParentWrongKey() {
57          Map<String, Object> parentVars = new HashMap<String, Object>();
58          parentVars.put("key", "value");
59          
60          SimpleContext parentContext = new SimpleContext(null, parentVars);
61          
62          Map<String, Object> vars = new HashMap<String, Object>();
63          vars.put("key", "value");
64          
65          context.setVars(vars);
66          context = new SimpleContext(parentContext, parentVars);
67          
68          Assert.assertFalse(context.has("differentKey"));
69      }
70  
71      @Test
72      public void testHasParentCorrectKey() {
73          Map<String, Object> parentVars = new HashMap<String, Object>();
74          parentVars.put("differentKey", "value");
75          
76          SimpleContext parentContext = new SimpleContext(null, parentVars);
77          
78          Map<String, Object> vars = new HashMap<String, Object>();
79          vars.put("key", "value");
80          
81          context.setVars(vars);
82          context = new SimpleContext(parentContext, parentVars);
83          
84          Assert.assertTrue(context.has("differentKey"));
85      }
86      
87      @Test
88      public void testGetNull() {
89          Object value = context.get("key");
90          
91          Assert.assertNull(value);
92      }
93      
94      @Test
95      public void testGetValue() {
96          Map<String, Object> vars = new HashMap<String, Object>();
97          vars.put("key", "value");
98          
99          context.setVars(vars);
100         
101         Assert.assertEquals("value", context.get("key"));
102     }
103     
104     @Test
105     public void testGetParentValue() {
106         Map<String, Object> parentVars = new HashMap<String, Object>();
107         parentVars.put("differentKey", "differentValue");
108         
109         SimpleContext parentContext = new SimpleContext(null, parentVars);
110         
111         Map<String, Object> vars = new HashMap<String, Object>();
112         vars.put("key", "value");
113         
114         context.setVars(vars);
115         context = new SimpleContext(parentContext, parentVars);
116         
117         Assert.assertEquals("differentValue", context.get("differentKey"));
118     }
119     
120     @Test
121     public void testGetParentNull() {
122         Map<String, Object> vars = new HashMap<String, Object>();
123         vars.put("key", "value");
124         
125         context.setVars(vars);
126         
127         Assert.assertNull(context.get("differentKey"));
128     }
129     
130     @Test
131     public void testGetParentWrongValue() {
132         Map<String, Object> parentVars = new HashMap<String, Object>();
133         parentVars.put("differentKey", "differentValue");
134         
135         SimpleContext parentContext = new SimpleContext(null, parentVars);
136         
137         Map<String, Object> vars = new HashMap<String, Object>();
138         vars.put("key", "value");
139         
140         context.setVars(vars);
141         context = new SimpleContext(parentContext, parentVars);
142         
143         Assert.assertNull(context.get("reallyDifferentKey"));
144     }
145 
146     @Test
147     public void testSetVarsChangeValue() {
148         Map<String, Object> vars = new HashMap<String, Object>();
149         vars.put("key", "value");
150         
151         context.setVars(vars);
152         
153         context.set("key", "newValue");
154         
155         Assert.assertEquals("newValue", context.get("key"));
156     }
157 
158     @Test
159     public void testSetVarsEmpty() {
160         Map<String, Object> vars = new HashMap<String, Object>();
161         context.setVars(vars);
162         
163         context.set("key", "newValue");
164         
165         Assert.assertEquals("newValue", context.get("key"));
166     }
167     
168     @Test
169     public void testSetVarsParent() {
170         Map<String, Object> parentVars = new HashMap<String, Object>();
171         parentVars.put("differentKey", "differentValue");
172         
173         SimpleContext parentContext = new SimpleContext(null, parentVars);
174         
175         Map<String, Object> vars = new HashMap<String, Object>();
176         vars.put("key", "value");
177         
178         context.setVars(vars);
179         context = new SimpleContext(parentContext, parentVars);
180         
181         context.set("differentKey", "newValue");
182         
183         Assert.assertEquals("newValue", context.get("differentKey"));
184     }
185 }