Author: wglass Date: Sat Sep 23 16:43:43 2006 New Revision: 449333 URL: http://svn.apache.org/viewvc?view=rev&rev=449333 Log: allow macros to get outside references, even with localscope setting. Thanks to Stephen Habermann. VELOCITY-459
Added: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/context/ jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/context/VMContextLocalscopeTestCase.java Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/context/VMContext.java Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/context/VMContext.java URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/context/VMContext.java?view=diff&rev=449333&r1=449332&r2=449333 ============================================================================== --- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/context/VMContext.java (original) +++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/context/VMContext.java Sat Sep 23 16:43:43 2006 @@ -34,7 +34,7 @@ * in the put() and get() methods. * * Further, this context also supports the 'VM local context' mode, where - * any get() or put() of references that aren't args to the VM are considered + * any put() of references that aren't args to the VM are considered * local to the vm, protecting the global context. * * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a> @@ -190,31 +190,19 @@ } else { - if(localcontextscope) - { - /* - * if we have localcontextscope mode, then just - * put in the local context - */ + /* + * always try the local context then innerContext--even if localcontextscope + */ - o = localcontext.get( key ); - } - else + o = localcontext.get( key ); + + if ( o == null) { /* - * try the local context - */ - - o = localcontext.get( key ); + * last chance + */ - if ( o == null) - { - /* - * last chance - */ - - o = innerContext.get( key ); - } + o = innerContext.get( key ); } } Added: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/context/VMContextLocalscopeTestCase.java URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/context/VMContextLocalscopeTestCase.java?view=auto&rev=449333 ============================================================================== --- jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/context/VMContextLocalscopeTestCase.java (added) +++ jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/context/VMContextLocalscopeTestCase.java Sat Sep 23 16:43:43 2006 @@ -0,0 +1,64 @@ +package org.apache.velocity.test.context; + +/* + * Copyright 2001-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Tests scope of velocimacros with localscope setting. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Stephen Habermann</a> + * @version $Id: Handler2.java 447976 2006-09-19 21:29:45Z henning $ + */ +import junit.framework.TestCase; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.context.InternalContextAdapterImpl; +import org.apache.velocity.context.VMContext; +import org.apache.velocity.runtime.RuntimeConstants; +import org.apache.velocity.runtime.RuntimeInstance; + +public class VMContextLocalscopeTestCase extends TestCase { + + private RuntimeInstance instance; + + public void setUp() throws Exception + { + this.instance = new RuntimeInstance(); + this.instance.setProperty(RuntimeConstants.VM_CONTEXT_LOCALSCOPE, Boolean.TRUE); + this.instance.init(); + } + + public void testLocalscopePutDoesntLeakButGetDoes() + { + VelocityContext base = new VelocityContext(); + base.put("outsideVar", "value1"); + + VMContext vm = new VMContext(new InternalContextAdapterImpl(base), this.instance); + vm.put("newLocalVar", "value2"); + + // New variable put doesn't leak + assertNull(base.get("newLocalVar")); + assertEquals("value2", vm.get("newLocalVar")); + + // But we can still get to "outsideVar" + assertEquals("value1", vm.get("outsideVar")); + + // If we decide to try and set outsideVar it won't leak + vm.put("outsideVar", "value3"); + assertEquals("value3", vm.get("outsideVar")); + assertEquals("value1", base.get("outsideVar")); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]