This is an automated email from the ASF dual-hosted git repository. cbrisson pushed a commit to branch VELOCITY-944 in repository https://gitbox.apache.org/repos/asf/velocity-engine.git
commit 603630c3d9557a9247bd9b533d2f7d6ea22c7f09 Author: Claude Brisson <[email protected]> AuthorDate: Mon Oct 14 07:00:41 2024 +0200 Fix VELOCITY-944 --- .../apache/velocity/runtime/RuntimeInstance.java | 2 + .../velocity/runtime/VelocimacroManager.java | 4 +- .../org/apache/velocity/test/BaseTestCase.java | 26 +++++------ .../velocity/test/issues/Velocity944TestCase.java | 50 ++++++++++++++++++++++ 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java index ada01e64..e77f2df8 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java @@ -1499,6 +1499,7 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices try { + ica.setCurrentResource(nodeTree.getTemplate()); try { nodeTree.init(ica, this); @@ -1554,6 +1555,7 @@ public class RuntimeInstance implements RuntimeConstants, RuntimeServices finally { ica.popCurrentTemplateName(); + ica.setCurrentResource(null); if (isScopeControlEnabled(evaluateScopeName)) { Object obj = ica.get(evaluateScopeName); diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java index 63dc92b9..d4dbcbff 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/VelocimacroManager.java @@ -36,7 +36,7 @@ import java.util.concurrent.ConcurrentHashMap; * * <ul> * <li>flat - all allowable VMs are in the global namespace</li> - * <li>local - inline VMs are added to it's own template namespace</li> + * <li>local - inline VMs are added to their own template namespace</li> * </ul> * * Thanks to <a href="mailto:[email protected]">Jose Alberto Fernandez</a> @@ -190,7 +190,7 @@ public class VelocimacroManager if( usingNamespaces() && template != null ) { MacroEntry me = (MacroEntry)template.getMacros().get(vmName); - if( template.getMacros().size() > 0 && me != null ) + if(!template.getMacros().isEmpty() && me != null ) { return me.getProxy(); } diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/BaseTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/BaseTestCase.java index 1d43a835..9f5b67fc 100644 --- a/velocity-engine-core/src/test/java/org/apache/velocity/test/BaseTestCase.java +++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/BaseTestCase.java @@ -113,10 +113,10 @@ public abstract class BaseTestCase extends TestCase implements TemplateTestBase return repo; } - protected void addTemplate(String name, String template) + protected void addTemplate(String name, String vtl) { - info("Template '"+name+"': "+template); - getStringRepository().putStringResource(name, template); + info("Template '"+name+"': "+vtl); + getStringRepository().putStringResource(name, vtl); } protected void removeTemplate(String name) @@ -202,18 +202,18 @@ public abstract class BaseTestCase extends TestCase implements TemplateTestBase /** * Ensure that a template renders as expected. */ - protected void assertEvalEquals(String expected, String template) + protected void assertEvalEquals(String expected, String vtl) { - assertEvalEquals(expected, template, engine); + assertEvalEquals(expected, vtl, engine); } /** * Ensure that a template renders as expected against the provided engine. */ - protected void assertEvalEquals(String expected, String template, VelocityEngine ve) + protected void assertEvalEquals(String expected, String vtl, VelocityEngine ve) { info("Expectation: "+expected); - assertEquals(expected, evaluate(template, ve)); + assertEquals(expected, evaluate(vtl, ve)); } /** @@ -314,25 +314,25 @@ public abstract class BaseTestCase extends TestCase implements TemplateTestBase /** * Evaluate the specified String as a template and return the result as a String. */ - protected String evaluate(String template) + protected String evaluate(String vtl) { - return evaluate(template, engine); + return evaluate(vtl, engine); } /** * Evaluate the specified String as a template against the provided engine and return the result as a String. */ - protected String evaluate(String template, VelocityEngine ve) + protected String evaluate(String vtl, VelocityEngine ve) { StringWriter writer = new StringWriter(); try { - info("Template: "+template); + info("Template: "+vtl); // use template as its own name, since our templates are short // unless it's not that short, then shorten it... - String name = (template.length() <= 15) ? template : template.substring(0,15); - ve.evaluate(context, writer, name, template); + String name = (vtl.length() <= 15) ? vtl : vtl.substring(0,15); + ve.evaluate(context, writer, name, vtl); String result = writer.toString(); info("Result: "+result); diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity944TestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity944TestCase.java new file mode 100644 index 00000000..b3169c98 --- /dev/null +++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/issues/Velocity944TestCase.java @@ -0,0 +1,50 @@ +package org.apache.velocity.test.issues; + +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.runtime.resource.loader.StringResourceLoader; +import org.apache.velocity.runtime.resource.util.StringResourceRepository; +import org.apache.velocity.test.BaseTestCase; + +public class Velocity944TestCase extends BaseTestCase { + public Velocity944TestCase(String name) { + super(name); + } + + @Override + protected void setUpEngine(VelocityEngine engine) { + engine.setProperty("velocimacro.inline.local_scope", "true"); + } + + @Override + protected void setUpContext(VelocityContext context) { + context.put("someObj", new SomeClass()); + } + + public void testVelocity944StringLiteral() throws Exception { + assertEvalEquals( + "<span>foo</span> some text <span>bar</span>", + "#macro( m $v )<span>$v</span>#end#m( 'foo' ) some text #set($str=\"#m( 'bar' )\")$str" + ); + } + + public void testVelocity944ReferenceArgument() throws Exception { + assertEvalEquals( + "<span>foo</span> some text <span>bar</span>", + "#macro( m $v )<span>$v</span>#end#m( 'foo' ) some text ${someObj.someMethod(\"#m( 'bar' )\")}" + ); + } + + public void testVelocity944LoadedTemplate() throws Exception { + addTemplate("velocity944", "#macro( m $v )<span>$v</span>#end#m( 'foo' ) some text ${someObj.someMethod(\"#m( 'bar' )\")}"); + assertTmplEquals("<span>foo</span> some text <span>bar</span>", "velocity944"); + } + + public static class SomeClass + { + public String someMethod(final String value) + { + return value; + } + } +}
