    1   /*
    2    * Copyright 2006 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.scxml.env;
    17
    18  import java.util.HashMap;
    19  import java.util.Map;
    20
    21  import junit.framework.Test;
    22  import junit.framework.TestCase;
    23  import junit.framework.TestSuite;
    24
    25  public class SimpleContextTest extends TestCase {
    26
    27      public SimpleContextTest(String testName) {
    28          super(testName);
    29      }
    30
    31      public static Test suite() {
    32          return new TestSuite(SimpleContextTest.class);
    33      }
    34
    35      public static void main(String args[]) {
    36          String[] testCaseName = { SimpleContextTest.class.getName()};
    37          junit.textui.TestRunner.main(testCaseName);
    38      }
    39
    40      private SimpleContext context;
    41
    42      protected void setUp() throws Exception {
    43          context = new SimpleContext();
    44      }
    45
    46      public void testHasTrue() {
    47          Map vars = new HashMap();
    48          vars.put("key", "value");
    49
    50          context.setVars(vars);
    51
    52          assertTrue(context.has("key"));
    53      }
    54
    55      public void testHasNullParent() {
    56          Map vars = new HashMap();
    57          vars.put("key", "value");
    58
    59          context.setVars(vars);
    60
    61          assertFalse(context.has("differentKey"));
    62      }
    63
    64      public void testHasParentWrongKey() {
    65          Map parentVars = new HashMap();
    66          parentVars.put("key", "value");
    67
    68          SimpleContext parentContext = new SimpleContext(parentVars);
    69
    70          Map vars = new HashMap();
    71          vars.put("key", "value");
    72
    73          context.setVars(vars);
    74          context = new SimpleContext(parentContext, parentVars);
    75
    76          assertFalse(context.has("differentKey"));
    77      }
    78
    79      public void testHasParentCorrectKey() {
    80          Map parentVars = new HashMap();
    81          parentVars.put("differentKey", "value");
    82
    83          SimpleContext parentContext = new SimpleContext(parentVars);
    84
    85          Map vars = new HashMap();
    86          vars.put("key", "value");
    87
    88          context.setVars(vars);
    89          context = new SimpleContext(parentContext, parentVars);
    90
    91          assertTrue(context.has("differentKey"));
    92      }
    93
    94      public void testGetNull() {
    95          Object value = context.get("key");
    96
    97          assertNull(value);
    98      }
    99
    100     public void testGetValue() {
    101         Map vars = new HashMap();
    102         vars.put("key", "value");
    103
    104         context.setVars(vars);
    105
    106         assertEquals("value", context.get("key"));
    107     }
    108
    109     public void testGetParentValue() {
    110         Map parentVars = new HashMap();
    111         parentVars.put("differentKey", "differentValue");
    112
    113         SimpleContext parentContext = new SimpleContext(parentVars);
    114
    115         Map vars = new HashMap();
    116         vars.put("key", "value");
    117
    118         context.setVars(vars);
    119         context = new SimpleContext(parentContext, parentVars);
    120
    121         assertEquals("differentValue", context.get("differentKey"));
    122     }
    123
    124     public void testGetParentNull() {
    125         Map vars = new HashMap();
    126         vars.put("key", "value");
    127
    128         context.setVars(vars);
    129
    130         assertNull(context.get("differentKey"));
    131     }
    132
    133     public void testGetParentWrongValue() {
    134         Map parentVars = new HashMap();
    135         parentVars.put("differentKey", "differentValue");
    136
    137         SimpleContext parentContext = new SimpleContext(parentVars);
    138
    139         Map vars = new HashMap();
    140         vars.put("key", "value");
    141
    142         context.setVars(vars);
    143         context = new SimpleContext(parentContext, parentVars);
    144
    145         assertNull(context.get("reallyDifferentKey"));
    146     }
    147
    148     public void testSetVarsChangeValue() {
    149         Map vars = new HashMap();
    150         vars.put("key", "value");
    151
    152         context.setVars(vars);
    153
    154         context.set("key", "newValue");
    155
    156         assertEquals("newValue", context.get("key"));
    157     }
    158
    159     public void testSetVarsEmpty() {
    160         Map vars = new HashMap();
    161         context.setVars(vars);
    162
    163         context.set("key", "newValue");
    164
    165         assertEquals("newValue", context.get("key"));
    166     }
    167
    168     public void testSetVarsParent() {
    169         Map parentVars = new HashMap();
    170         parentVars.put("differentKey", "differentValue");
    171
    172         SimpleContext parentContext = new SimpleContext(parentVars);
    173
    174         Map vars = new HashMap();
    175         vars.put("key", "value");
    176
    177         context.setVars(vars);
    178         context = new SimpleContext(parentContext, parentVars);
    179
    180         context.set("differentKey", "newValue");
    181
    182         assertEquals("newValue", context.get("differentKey"));
    183     }
    184 }