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 */ 017package org.apache.commons.scxml2.env; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.junit.Assert; 023import org.junit.Before; 024import org.junit.Test; 025 026public class SimpleContextTest { 027 028 private SimpleContext context; 029 030 @Before 031 public void setUp() throws Exception { 032 context = new SimpleContext(); 033 } 034 035 @Test 036 public void testHasTrue() { 037 Map<String, Object> vars = new HashMap<String, Object>(); 038 vars.put("key", "value"); 039 040 context.setVars(vars); 041 042 Assert.assertTrue(context.has("key")); 043 } 044 045 @Test 046 public void testHasNullParent() { 047 Map<String, Object> vars = new HashMap<String, Object>(); 048 vars.put("key", "value"); 049 050 context.setVars(vars); 051 052 Assert.assertFalse(context.has("differentKey")); 053 } 054 055 @Test 056 public void testHasParentWrongKey() { 057 Map<String, Object> parentVars = new HashMap<String, Object>(); 058 parentVars.put("key", "value"); 059 060 SimpleContext parentContext = new SimpleContext(null, parentVars); 061 062 Map<String, Object> vars = new HashMap<String, Object>(); 063 vars.put("key", "value"); 064 065 context.setVars(vars); 066 context = new SimpleContext(parentContext, parentVars); 067 068 Assert.assertFalse(context.has("differentKey")); 069 } 070 071 @Test 072 public void testHasParentCorrectKey() { 073 Map<String, Object> parentVars = new HashMap<String, Object>(); 074 parentVars.put("differentKey", "value"); 075 076 SimpleContext parentContext = new SimpleContext(null, parentVars); 077 078 Map<String, Object> vars = new HashMap<String, Object>(); 079 vars.put("key", "value"); 080 081 context.setVars(vars); 082 context = new SimpleContext(parentContext, parentVars); 083 084 Assert.assertTrue(context.has("differentKey")); 085 } 086 087 @Test 088 public void testGetNull() { 089 Object value = context.get("key"); 090 091 Assert.assertNull(value); 092 } 093 094 @Test 095 public void testGetValue() { 096 Map<String, Object> vars = new HashMap<String, Object>(); 097 vars.put("key", "value"); 098 099 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}