1   package org.apache.velocity.test.misc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.velocity.VelocityContext;
26  import org.apache.velocity.context.Context;
27  
28  /**
29   * Used for testing EvaluateContext.  For testing purposes, this is a case insensitive
30   * context.  
31   * 
32   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
33   * @version $Id: TestContext.java 522413 2007-03-26 04:34:15Z wglass $
34   */
35  public class TestContext implements Context
36  {
37      Context innerContext = new VelocityContext();
38      Map originalKeys = new HashMap();
39      
40      public boolean containsKey(Object key)
41      {
42          return innerContext.containsKey(normalizeKey(key));
43      }
44  
45      public Object get(String key)
46      {
47          return innerContext.get(normalizeKey(key));
48      }
49  
50      public Object[] getKeys()
51      {
52          return originalKeys.values().toArray();
53      }
54  
55      public Object put(String key, Object value)
56      {
57          String normalizedKey = normalizeKey(key);
58          originalKeys.put(key, normalizedKey);
59          return innerContext.put(normalizedKey, value);
60      }
61  
62      public Object remove(Object key)
63      {
64          originalKeys.remove(key);
65          return innerContext.remove(normalizeKey(key));
66      }
67  
68      private String normalizeKey(Object key)
69      {
70          if (key == null)
71          {
72              return null;
73          }
74          else if (key.toString() == null)
75          {
76              return null;
77          }
78          else
79          {
80              return key.toString().toUpperCase();
81          }
82      }
83  }