1   package org.apache.velocity.test;
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 junit.framework.TestCase;
23  
24  import org.apache.velocity.VelocityContext;
25  import org.apache.velocity.context.EvaluateContext;
26  import org.apache.velocity.context.InternalContextAdapterImpl;
27  import org.apache.velocity.runtime.RuntimeConstants;
28  import org.apache.velocity.runtime.RuntimeInstance;
29  import org.apache.velocity.test.misc.TestContext;
30  
31  /**
32   * Tests scope of EvaluateContext.  
33   * 
34   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
35   * @version $Id: EvaluateContextTestCase.java 753027 2009-03-12 21:47:56Z nbubna $
36   */
37  public class EvaluateContextTestCase extends TestCase
38  {
39      public void testLocalscopePutDoesntLeakButGetDoes() 
40      throws Exception
41      {
42          RuntimeInstance instance;
43          
44          instance = new RuntimeInstance();
45          instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, VelocityContext.class.getName());
46          instance.init();
47  
48          VelocityContext base = new VelocityContext();
49          base.put("outsideVar", "value1");
50  
51          EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
52          evc.put("newLocalVar", "value2");
53  
54          // New variable put doesn't leak
55          assertNull(base.get("newLocalVar"));
56          assertEquals("value2", evc.get("newLocalVar"));
57  
58          // But we can still get to "outsideVar"
59          assertEquals("value1", evc.get("outsideVar"));
60  
61          // If we decide to try and set outsideVar it won't leak
62          evc.put("outsideVar", "value3");
63          assertEquals("value3", evc.get("outsideVar"));
64          assertEquals("value1", base.get("outsideVar"));
65          
66          assertEquals(2, evc.getKeys().length);
67      }
68  
69      /**
70       * Test that local context can be configured.
71       * @throws Exception
72       */
73      public void testSetLocalContext()
74      throws Exception
75      {
76          RuntimeInstance instance = new RuntimeInstance();
77          instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, TestContext.class.getName());
78          instance.init();
79  
80          VelocityContext base = new VelocityContext();
81          base.put("outsideVar", "value1");
82          EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
83  
84          // original entry
85          assertEquals(1,evc.getKeys().length);
86          
87          // original plus local entry
88          evc.put("test","result");
89          assertEquals(2,evc.getKeys().length);
90          
91          // local context is case insensitive, so the count remains the same
92          evc.put("TEST","result");
93          assertEquals(2,evc.getKeys().length);
94  
95          assertEquals("result",evc.get("test"));
96          assertEquals("result",evc.get("TEst"));
97      
98          assertNull(evc.get("OUTSIDEVAR"));
99      }
100 
101     public void testSetLocalContextWithErrors()
102     throws Exception
103     {
104         VelocityContext base = new VelocityContext();
105 
106         try 
107         {
108             // initialize with bad class name
109             RuntimeInstance instance = new RuntimeInstance();
110             instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, "org.apache");
111             instance.init();
112             EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
113             fail ("Expected an exception");
114         }
115         catch (Exception e) {}
116         
117         try 
118         {
119             // initialize with class not implementing Context
120             RuntimeInstance instance = new RuntimeInstance();
121             instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, org.apache.velocity.test.EvaluateContextTestCase.class.getName());
122             instance.init();
123             EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
124             fail ("Expected an exception");
125         }
126         catch (Exception e) {}
127     }       
128 }