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 org.apache.velocity.VelocityContext;
23  import org.apache.velocity.context.InternalContextAdapterImpl;
24  import org.apache.velocity.context.ProxyVMContext;
25  import org.apache.velocity.runtime.RuntimeConstants;
26  import org.apache.velocity.runtime.RuntimeInstance;
27  
28  /**
29   * Tests scope of velocimacros with localscope setting. 
30   * 
31   * @author <a href="mailto:stephenh@chase3000.com">Stephen Habermann</a>
32   * @version $Id: VMContextLocalscopeTestCase.java 747235 2009-02-24 00:32:03Z nbubna $
33   */
34  public class VMContextLocalscopeTestCase extends BaseTestCase {
35  
36      public VMContextLocalscopeTestCase(String name)
37      {
38          super(name);
39      }
40  
41      public void testViaEval()
42      {
43          engine.setProperty(RuntimeConstants.VM_CONTEXT_LOCALSCOPE, Boolean.TRUE);
44          assertEvalEquals("$a", "#macro(a)#set($a = 'b')#end#a$a");
45          context.put("b", "b");
46          assertEvalEquals("b", "#macro(b)$b#set($b = 'c')#end#b");
47          assertContextValue("b", "b");
48      }
49  
50      public void testLocalscopePutDoesntLeakButGetDoes() 
51      {
52          RuntimeInstance instance = new RuntimeInstance();
53          instance.setProperty(RuntimeConstants.VM_CONTEXT_LOCALSCOPE, Boolean.TRUE);
54          instance.init();
55  
56          VelocityContext base = new VelocityContext();
57          base.put("outsideVar", "value1");
58  
59          ProxyVMContext vm =
60              new ProxyVMContext(new InternalContextAdapterImpl(base), instance, true);
61          vm.put("newLocalVar", "value2");
62  
63          // New variable put doesn't leak
64          assertNull(base.get("newLocalVar"));
65          assertEquals("value2", vm.get("newLocalVar"));
66  
67          // But we can still get to "outsideVar"
68          assertEquals("value1", vm.get("outsideVar"));
69  
70          // If we decide to try and set outsideVar it won't leak
71          vm.put("outsideVar", "value3");
72          assertEquals("value3", vm.get("outsideVar"));
73          assertEquals("value1", base.get("outsideVar"));
74      }
75  
76  }