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  import org.apache.velocity.VelocityContext;
24  import org.apache.velocity.context.InternalContextAdapterImpl;
25  import org.apache.velocity.context.ProxyVMContext;
26  import org.apache.velocity.runtime.RuntimeConstants;
27  import org.apache.velocity.runtime.RuntimeInstance;
28  
29  /**
30   * Tests scope of velocimacros with localscope setting. 
31   * 
32   * @author <a href="mailto:stephenh@chase3000.com">Stephen Habermann</a>
33   * @version $Id: VMContextLocalscopeTestCase.java 685433 2008-08-13 04:18:27Z nbubna $
34   */
35  public class VMContextLocalscopeTestCase extends TestCase {
36  
37      private RuntimeInstance instance;
38  
39      public void setUp() throws Exception 
40      {
41          this.instance = new RuntimeInstance();
42          this.instance.setProperty(RuntimeConstants.VM_CONTEXT_LOCALSCOPE, Boolean.TRUE);
43          this.instance.init();
44      }
45  
46      public void testLocalscopePutDoesntLeakButGetDoes() 
47      {
48          VelocityContext base = new VelocityContext();
49          base.put("outsideVar", "value1");
50  
51          ProxyVMContext vm = new ProxyVMContext(new InternalContextAdapterImpl(base), this.instance, true);
52          vm.put("newLocalVar", "value2");
53  
54          // New variable put doesn't leak
55          assertNull(base.get("newLocalVar"));
56          assertEquals("value2", vm.get("newLocalVar"));
57  
58          // But we can still get to "outsideVar"
59          assertEquals("value1", vm.get("outsideVar"));
60  
61          // If we decide to try and set outsideVar it won't leak
62          vm.put("outsideVar", "value3");
63          assertEquals("value3", vm.get("outsideVar"));
64          assertEquals("value1", base.get("outsideVar"));
65      }
66  
67  }