velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r992140 - in /velocity/engine/branches/2.0_Exp: ./ src/changes/changes.xml src/java/org/apache/velocity/runtime/directive/Scope.java src/java/org/apache/velocity/runtime/directive/Stop.java src/test/org/apache/velocity/test/ScopeTestCase.java
Date Thu, 02 Sep 2010 22:37:18 GMT
Author: nbubna
Date: Thu Sep  2 22:37:18 2010
New Revision: 992140

URL: http://svn.apache.org/viewvc?rev=992140&view=rev
Log:
merge r992133 from trunk and update changes.xml

Modified:
    velocity/engine/branches/2.0_Exp/   (props changed)
    velocity/engine/branches/2.0_Exp/src/changes/changes.xml
    velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Scope.java
    velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Stop.java
  (props changed)
    velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ScopeTestCase.java

Propchange: velocity/engine/branches/2.0_Exp/
------------------------------------------------------------------------------
    svn:mergeinfo = /velocity/engine/trunk:992133

Modified: velocity/engine/branches/2.0_Exp/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/changes/changes.xml?rev=992140&r1=992139&r2=992140&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/changes/changes.xml (original)
+++ velocity/engine/branches/2.0_Exp/src/changes/changes.xml Thu Sep  2 22:37:18 2010
@@ -73,6 +73,14 @@
     </release>
 
     <release version="1.7" date="In Subversion">
+      <action type="add" dev="nbubna">
+    Add access to template and directive debugging info via $<scope>.info.
+      </action>
+
+      <action type="fix" dev="nbubna" issue="VELOCITY-766">
+    LogManager now catches UnsupportedOperationExceptions during LogChute init.
+      </action>
+
       <action type="fix" dev="nbubna" issue="VELOCITY-760" due-to="Jarkko Viinamäki">
     Ensure that DataSourceResourceLoader closes PreparedStatements.
       </action>

Modified: velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Scope.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Scope.java?rev=992140&r1=992139&r2=992140&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Scope.java
(original)
+++ velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Scope.java
Thu Sep  2 22:37:18 2010
@@ -23,6 +23,7 @@ import java.util.AbstractMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import org.apache.velocity.Template;
 
 /**
  * This handles context scoping and metadata for directives.
@@ -36,6 +37,7 @@ public class Scope extends AbstractMap
     private Map storage;
     private Object replaced;
     private Scope parent;
+    private Info info;
     protected final Object owner;
 
     public Scope(Object owner, Object previous)
@@ -110,7 +112,7 @@ public class Scope extends AbstractMap
      * instance and the topmost instance, plus one. This value
      * will never be negative or zero.
      */
-    public int getDepth()
+    protected int getDepth()
     {
         if (parent == null)
         {
@@ -156,4 +158,141 @@ public class Scope extends AbstractMap
         return replaced;
     }
 
+    /**
+     * Returns info about the current scope for debugging purposes.
+     */
+    public Info getInfo()
+    {
+        if (info == null)
+        {
+            info = new Info(this, owner);
+        }
+        return info;
+    }
+
+    /**
+     * Class to encapsulate and provide access to info about
+     * the current scope for debugging.
+     */
+    public static class Info
+    {
+        private Scope scope;
+        private Directive directive;
+        private Template template;
+
+        public Info(Scope scope, Object owner)
+        {
+            if (owner instanceof Directive)
+            {
+                directive = (Directive)owner;
+            }
+            if (owner instanceof Template)
+            {
+                template = (Template)owner;
+            }
+            this.scope = scope;
+        }
+
+        public String getName()
+        {
+            if (directive != null)
+            {
+                return directive.getName();
+            }
+            if (template != null)
+            {
+                return template.getName();
+            }
+            return null;
+        }
+
+        public String getType()
+        {
+            if (directive != null)
+            {
+                switch (directive.getType())
+                {
+                    case Directive.BLOCK:
+                        return "block";
+                    case Directive.LINE:
+                        return "line";
+                }
+            }
+            if (template != null)
+            {
+                return template.getEncoding();
+            }
+            return null;
+        }
+
+        public int getDepth()
+        {
+            return scope.getDepth();
+        }
+
+        public String getTemplate()
+        {
+            if (directive != null)
+            {
+                return directive.getTemplateName();
+            }
+            if (template != null)
+            {
+                return template.getName();
+            }
+            return null;
+        }
+
+        public int getLine()
+        {
+            if (directive != null)
+            {
+                return directive.getLine();
+            }
+            return 0;
+        }
+
+        public int getColumn()
+        {
+            if (directive != null)
+            {
+                return directive.getColumn();
+            }
+            return 0;
+        }
+
+        public String toString()
+        {
+            StringBuilder sb = new StringBuilder();
+            if (directive != null)
+            {
+                sb.append('#');
+            }
+            sb.append(getName());
+            sb.append("[type:").append(getType());
+            int depth = getDepth();
+            if (depth > 1)
+            {
+                sb.append(" depth:").append(depth);
+            }
+            if (template == null)
+            {
+                String vtl = getTemplate();
+                sb.append(" template:");
+                if (vtl.indexOf(" ") < 0)
+                {
+                    sb.append(vtl);
+                }
+                else
+                {
+                    sb.append('"').append(vtl).append('"');
+                }
+                sb.append(" line:").append(getLine());
+                sb.append(" column:").append(getColumn());
+            }
+            sb.append(']');
+            return sb.toString();
+        }
+    }
+
 }

Propchange: velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/directive/Stop.java
            ('svn:mergeinfo' removed)

Modified: velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ScopeTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ScopeTestCase.java?rev=992140&r1=992139&r2=992140&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ScopeTestCase.java
(original)
+++ velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/ScopeTestCase.java
Thu Sep  2 22:37:18 2010
@@ -1,325 +1,367 @@
-package org.apache.velocity.test;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.    
- */
-
-import java.util.HashMap;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-
-
-/**
- * This class tests the directive scope controls
- */
-public class ScopeTestCase extends BaseTestCase
-{
-    public ScopeTestCase(String name)
-    {
-       super(name);
-    }
-
-    protected void setUpEngine(VelocityEngine engine)
-    {
-        engine.setProperty("a.provide.scope.control", "true");
-        engine.setProperty("define.provide.scope.control", "true");
-        engine.setProperty("evaluate.provide.scope.control", "true");
-        engine.setProperty("foo.provide.scope.control", "true");
-        engine.setProperty("macro.provide.scope.control", "true");
-        engine.setProperty("template.provide.scope.control", "true");
-        engine.setProperty("vm.provide.scope.control", "true");
-    }
-
-    public void testRootTemplateMergeScope()
-    {
-        addTemplate("foo", "foo#break($template)bar");
-        assertTmplEquals("foo", "foo");
-        assertNull(context.get("template"));
-    }
-
-    public void testScopeGetLeakIntoInner()
-    {
-        addTemplate("foo", "#foreach($i in [1..1])#set($foreach.a=$i)"+
-                           "#foreach($j in [2..2])$foreach.a#set($foreach.a=$j)"+
-                           "#foreach($k in [3..3])$foreach.a#end#end#end");
-        assertTmplEquals("12", "foo");
-    }
-
-    public void testScopeGetLeakDoesntHideNullset()
-    {
-        addTemplate("a", "#macro(a)#set($macro.a='a')#b()$macro.a#end"+
-                         "#macro(b)$macro.a#set($macro.a=$null)$!macro.a#end"+
-                         "#a()");
-        assertTmplEquals("aa", "a");
-    }
-
-    public void testParseScope()
-    {
-        addTemplate("test", "$template.depth"+
-                            "$!parse.parent.depth"+
-                            "#set( $template.foo = 'bar' )"+
-                            "$template.foo"+
-                            "#break($template)"+
-                            "woogie");
-        assertEvalEquals("1bar", "#parse( 'test' )");
-        assertNull(context.get("template"));
-    }
-
-    public void testNestedParseScope()
-    {
-        HashMap grab = new HashMap();
-        context.put("grab", grab);
-
-        addTemplate("inner", "Inner depth: $template.depth"+
-                             "#set( $template.foo = '?' )"+
-                             "$!grab.put('inner',$template)"+
-                             "#break($template)$template.foo");
-        addTemplate("outer", "#set( $template.foo = '!' )"+
-                             "Outer depth: $template.depth "+
-                             "#parse('inner')"+
-                             "$!grab.put('outer', $template)"+
-                             "$template.foo");
-        assertEvalEquals("Outer depth: 1 Inner depth: 2!", "#parse('outer')");
-        // make extra sure that the outer control was restored after the stop
-        assertFalse(grab.get("inner") == grab.get("outer"));
-        // make sure the outer control was cleaned up
-        assertNull(context.get("template"));
-
-        addTemplate("3", "$template.topmost.foo#set( $template.topmost.foo = 'bar' )");
-        addTemplate("2", "#parse( '3' )$!parse.foo");
-        addTemplate("1", "#set( $template.foo = 'foo' )#parse('2')$template.foo");
-        assertEvalEquals("foobar", "#parse('1')$!parse");
-        // make sure the top control was cleaned up
-        assertNull(context.get("template"));
-    }
-
-    public void testForeachScope()
-    {
-        String template = "#foreach( $i in [0..2] )"+
-                          "#if( $i > 1 )#break($foreach)#end"+
-                          "$foreach.index:$foreach.count:$foreach.hasNext,"+
-                          "#end";
-        assertEvalEquals("0:1:true,1:2:true,", template);
-        assertNull(context.get("foreach"));
-    }
-
-    public void testNestedForeachScope()
-    {
-        String template = "#foreach( $i in [1..5] )"+
-                            "#foreach( $j in [1..2] )"+
-                              "#if ( $i > $foreach.count + $foreach.index + $foreach.depth
)#break($foreach.topmost)#end"+
-                            "#end"+
-                            "$i"+
-                          "#end";
-        assertEvalEquals("123", template);
-        assertNull(context.get("foreach"));
-    }
-
-    public void testMacroScope()
-    {
-        String template = "#macro( foo $i )"+
-                          "#if($i > 2 )#break($macro)#end"+
-                          "$i#end"+
-                          "#foo( 0 )#foo( 1 )#foo( 2 )";
-        assertEvalEquals("012", template);
-        assertNull(context.get("macro"));
-    }
-
-    public void testRecursiveMacroScope()
-    {
-        String template = "#macro( foo )$macro.depth"+
-                          "#if($macro.depth > 2 )#break($macro.topmost)#end"+
-                          "#foo()#end#foo()";
-        assertEvalEquals("123", template);
-        assertNull(context.get("macro"));
-    }
-
-    public void testNestedMacroScope()
-    {
-        String template = "#macro( a )$macro.depth#set($macro.c = 'a')$macro.c#end"+
-                          "#macro( b )#set($macro.c = 'b' )#a()$macro.c#end"+
-                          "#b()";
-        assertEvalEquals("2ab", template);
-        assertNull(context.get("macro"));
-    }
-
-    public void testBodyMacroScope()
-    {
-        String template = "#macro( foo $bar )$bodyContent$macro.bar#end"+
-                          "#@foo( 'bar' )#set( $macro.bar = 'foo'+$bar )"+
-                          "#set( $foo.d = $foo.depth )$foo.d #end";
-        assertEvalEquals("1 foobar", template);
-        assertNull(context.get("foo"));
-        assertNull(context.get("macro"));
-    }
-
-    public void testRecursiveBodyMacroScope()
-    {
-        engine.setProperty(RuntimeConstants.VM_MAX_DEPTH, "5");
-        String template = "#macro( foo )$bodyContent$macro.i#end"+
-                          "#@foo()#set( $macro.i = \"$!macro.i$foo.depth,\" )"+
-                          "$!bodyContent#end";
-        assertEvalEquals("1,2,3,4,5,", template);
-        assertNull(context.get("foo"));
-        assertNull(context.get("macro"));
-    }
-
-    public void testDefineScope()
-    {
-        String template = "#define( $foo )#set( $define.bar = 'bar'+$define.depth )$define.bar#end$foo";
-        assertEvalEquals("bar1", template);
-        assertNull(context.get("define"));
-    }
-
-    public void testNestedDefineScope()
-    {
-        String template = "#define($a)$b c#end"+
-                          "#define($b)$define.depth#break($define.topmost)#end"+
-                          "$a";
-        assertEvalEquals("2", template);
-        assertNull(context.get("define"));
-    }
-
-    public void testRecursiveDefineScope()
-    {
-        engine.setProperty(RuntimeConstants.DEFINE_DIRECTIVE_MAXDEPTH, "10");
-        String template = "#define($a)$define.depth"+
-                          "#if($define.depth == 5)#break($define)#end,$a#end$a";
-        assertEvalEquals("1,2,3,4,5", template);
-        assertNull(context.get("define"));
-    }
-
-    public void testRootEvaluateScope()
-    {
-        assertEvalEquals("1", "$evaluate.depth");
-        assertEvalEquals("foo", "foo#break($evaluate)bar");
-        assertNull(context.get("evaluate"));
-    }
-
-    public void testEvaluateScope()
-    {
-        context.put("h", "#");
-        context.put("d", "$");
-        String template = "${h}set( ${d}evaluate.foo = 'bar' )"+
-                          "${d}evaluate.foo ${d}evaluate.depth";
-        addTemplate("eval", "#evaluate(\""+template+"\")");
-        assertTmplEquals("bar 1", "eval");
-        assertNull(context.get("evaluate"));
-        assertNull(context.get("template"));
-    }
-
-    public void testNestedEvaluateScope()
-    {
-        context.put("h", "#");
-        context.put("d", "$");
-        addTemplate("e", "#evaluate(\"${h}evaluate( '${d}evaluate.depth${h}stop(${d}evaluate)
blah' )\")");
-        assertTmplEquals("2", "e");
-        assertNull(context.get("evaluate"));
-        assertNull(context.get("template"));
-    }
-
-    public void testTurningOffTemplateScope()
-    {
-        engine.setProperty("template."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
-        // root
-        addTemplate("test", "$template.depth");
-        assertTmplEquals("$template.depth", "test");
-        // #parse
-        assertEvalEquals("$template.depth", "#parse('test')");
-    }
-
-    public void testTurningOffEvaluateScope()
-    {
-        engine.setProperty("evaluate."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
-        // root
-        assertSchmoo("$evaluate.depth");
-        // #evaluate
-        assertEvalEquals("$evaluate.depth", "#evaluate( '$evaluate.depth' )");
-    }
-
-    public void testTurningOffMacroScope()
-    {
-        engine.setProperty("macro."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
-        engine.setProperty("foo."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
-        // macro definition
-        assertEvalEquals("$macro", "#macro(a)$macro#end#a()");
-        // macro body
-        assertEvalEquals("$macro $foo", "#macro(foo)$bodyContent#end#@foo()$macro $foo#end");
-    }
-
-    public void testTurningOffDefineScope()
-    {
-        engine.setProperty("define."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
-        assertEvalEquals("$define", "#define($a)$define#end$a");
-    }
-
-    public void testTurningOffForeachScope()
-    {
-        engine.setProperty("foreach."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
-        assertEvalEquals("$foreach$foreach", "#foreach($i in [0..1])$foreach#end");
-    }
-
-    public void testTemplateReplaced()
-    {
-        context.put("template", "foo");
-        addTemplate("test", "$template.replaced");
-        assertTmplEquals("foo", "test");
-        assertEvalEquals("foo", "#parse('test')");
-        assertContextValue("template", "foo");
-    }
-
-    public void testEvaluateReplaced()
-    {
-        context.put("evaluate","foo");
-        assertEvalEquals("foo", "$evaluate.replaced");
-        assertEvalEquals("foo", "#evaluate('$evaluate.replaced')");
-        assertContextValue("evaluate", "foo");
-    }
-
-    public void testMacroReplaced()
-    {
-        context.put("macro", "foo");
-        assertEvalEquals("foo foo foo", "$macro #macro(a)$macro.replaced#end#a() $macro");
-        assertContextValue("macro", "foo");
-    }
-
-    public void testForeachReplaced()
-    {
-        context.put("foreach", "foo");
-        assertEvalEquals("foofoofoo", "$foreach#foreach($i in [1..1])$foreach.replaced#end$foreach");
-        assertEquals("foo", context.get("foreach"));
-        context.put("foreach", "a");
-        assertEvalEquals("a", "#foreach($i in [1..1])#foreach($j in [1..1])$foreach.replaced#end#end");
-        assertContextValue("foreach", "a");
-    }
-
-    public void testDefineReplaced()
-    {
-        context.put("define", "a");
-        assertEvalEquals("a", "#define($a)$define.replaced#end$a");
-        assertContextValue("define", "a");
-    }
-
-    public void testBodyContentReplaced()
-    {
-        context.put("vm", "a");
-        assertEvalEquals("a", "#macro(vm)$bodyContent#end#@vm()$vm.replaced#end");
-        assertContextValue("vm", "a");
-    }
-
-}
+package org.apache.velocity.test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.    
+ */
+
+import java.util.HashMap;
+
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+
+/**
+ * This class tests the directive scope controls
+ */
+public class ScopeTestCase extends BaseTestCase
+{
+    public ScopeTestCase(String name)
+    {
+       super(name);
+    }
+
+    protected void setUpEngine(VelocityEngine engine)
+    {
+        engine.setProperty("a.provide.scope.control", "true");
+        engine.setProperty("define.provide.scope.control", "true");
+        engine.setProperty("evaluate.provide.scope.control", "true");
+        engine.setProperty("foo.provide.scope.control", "true");
+        engine.setProperty("macro.provide.scope.control", "true");
+        engine.setProperty("template.provide.scope.control", "true");
+        engine.setProperty("vm.provide.scope.control", "true");
+    }
+
+    public void testScopeGetLeakIntoInner()
+    {
+        addTemplate("foo", "#foreach($i in [1..1])#set($foreach.a=$i)"+
+                           "#foreach($j in [2..2])$foreach.a#set($foreach.a=$j)"+
+                           "#foreach($k in [3..3])$foreach.a#end#end$foreach.a#end");
+        assertTmplEquals("121", "foo");
+    }
+
+    public void testScopeGetLeakDoesntHideNullset()
+    {
+        addTemplate("a", "#macro(a)#set($macro.a='a')#b()$macro.a#end"+
+                         "#macro(b)$macro.a#set($macro.a=$null)$!macro.a#end"+
+                         "#a()");
+        assertTmplEquals("aa", "a");
+    }
+
+    public void testRootTemplateMergeScope()
+    {
+        addTemplate("foo", "foo#break($template)bar");
+        assertTmplEquals("foo", "foo");
+        assertNull(context.get("template"));
+    }
+
+    public void testParseScope()
+    {
+        addTemplate("test", "$template.info.depth"+
+                            "$!parse.parent.info.depth"+
+                            "#set( $template.foo = 'bar' )"+
+                            "$template.foo"+
+                            "#break($template)"+
+                            "woogie");
+        assertEvalEquals("1bar", "#parse( 'test' )");
+        assertNull(context.get("template"));
+    }
+
+    public void testNestedParseScope()
+    {
+        HashMap grab = new HashMap();
+        context.put("grab", grab);
+
+        addTemplate("inner", "Inner depth: $template.info.depth"+
+                             "#set( $template.foo = '?' )"+
+                             "$!grab.put('inner',$template)"+
+                             "#break($template)$template.foo");
+        addTemplate("outer", "#set( $template.foo = '!' )"+
+                             "Outer depth: $template.info.depth "+
+                             "#parse('inner')"+
+                             "$!grab.put('outer', $template)"+
+                             "$template.foo");
+        assertEvalEquals("Outer depth: 1 Inner depth: 2!", "#parse('outer')");
+        // make extra sure that the outer control was restored after the stop
+        assertFalse(grab.get("inner") == grab.get("outer"));
+        // make sure the outer control was cleaned up
+        assertNull(context.get("template"));
+
+        addTemplate("3", "$template.topmost.foo#set( $template.topmost.foo = 'bar' )");
+        addTemplate("2", "#parse( '3' )$!parse.foo");
+        addTemplate("1", "#set( $template.foo = 'foo' )#parse('2')$template.foo");
+        assertEvalEquals("foobar", "#parse('1')$!parse");
+        // make sure the top control was cleaned up
+        assertNull(context.get("template"));
+    }
+
+    public void testForeachScope()
+    {
+        String template = "#foreach( $i in [0..2] )"+
+                          "#if( $i > 1 )#break($foreach)#end"+
+                          "$foreach.index:$foreach.count:$foreach.hasNext,"+
+                          "#end";
+        assertEvalEquals("0:1:true,1:2:true,", template);
+        assertNull(context.get("foreach"));
+    }
+
+    public void testNestedForeachScope()
+    {
+        String template = "#foreach( $i in [1..5] )"+
+                            "#foreach( $j in [1..2] )"+
+                              "#if ( $i > $foreach.count + $foreach.index + $foreach.info.depth
)#break($foreach.topmost)#end"+
+                            "#end"+
+                            "$i"+
+                          "#end";
+        assertEvalEquals("123", template);
+        assertNull(context.get("foreach"));
+    }
+
+    public void testMacroScope()
+    {
+        String template = "#macro( foo $i )"+
+                          "#if($i > 2 )#break($macro)#end"+
+                          "$i#end"+
+                          "#foo( 0 )#foo( 1 )#foo( 2 )";
+        assertEvalEquals("012", template);
+        assertNull(context.get("macro"));
+    }
+
+    public void testRecursiveMacroScope()
+    {
+        String template = "#macro( foo )$macro.info.depth"+
+                          "#if($macro.info.depth > 2 )#break($macro.topmost)#end"+
+                          "#foo()#end#foo()";
+        assertEvalEquals("123", template);
+        assertNull(context.get("macro"));
+    }
+
+    public void testNestedMacroScope()
+    {
+        String template = "#macro( a )$macro.info.depth#set($macro.c = 'a')$macro.c#end"+
+                          "#macro( b )#set($macro.c = 'b' )#a()$macro.c#end"+
+                          "#b()";
+        assertEvalEquals("2ab", template);
+        assertNull(context.get("macro"));
+    }
+
+    public void testBodyMacroScope()
+    {
+        String template = "#macro( foo $bar )$bodyContent$macro.bar#end"+
+                          "#@foo( 'bar' )#set( $macro.bar = 'foo'+$bar )"+
+                          "#set( $foo.d = $foo.info.depth )$foo.d #end";
+        assertEvalEquals("1 foobar", template);
+        assertNull(context.get("foo"));
+        assertNull(context.get("macro"));
+    }
+
+    public void testRecursiveBodyMacroScope()
+    {
+        engine.setProperty(RuntimeConstants.VM_MAX_DEPTH, "5");
+        String template = "#macro( foo )$bodyContent$macro.i#end"+
+                          "#@foo()#set( $macro.i = \"$!macro.i$foo.info.depth,\" )"+
+                          "$!bodyContent#end";
+        assertEvalEquals("1,2,3,4,5,", template);
+        assertNull(context.get("foo"));
+        assertNull(context.get("macro"));
+    }
+
+    public void testDefineScope()
+    {
+        String template = "#define( $foo )#set( $define.bar = 'bar'+$define.info.depth )$define.bar#end$foo";
+        assertEvalEquals("bar1", template);
+        assertNull(context.get("define"));
+    }
+
+    public void testNestedDefineScope()
+    {
+        String template = "#define($a)$b c#end"+
+                          "#define($b)$define.info.depth#break($define.topmost)#end"+
+                          "$a";
+        assertEvalEquals("2", template);
+        assertNull(context.get("define"));
+    }
+
+    public void testRecursiveDefineScope()
+    {
+        engine.setProperty(RuntimeConstants.DEFINE_DIRECTIVE_MAXDEPTH, "10");
+        String template = "#define($a)$define.info.depth"+
+                          "#if($define.info.depth == 5)#break($define)#end,$a#end$a";
+        assertEvalEquals("1,2,3,4,5", template);
+        assertNull(context.get("define"));
+    }
+
+    public void testRootEvaluateScope()
+    {
+        assertEvalEquals("1", "$evaluate.info.depth");
+        assertEvalEquals("foo", "foo#break($evaluate)bar");
+        assertNull(context.get("evaluate"));
+    }
+
+    public void testEvaluateScope()
+    {
+        context.put("h", "#");
+        context.put("d", "$");
+        String template = "${h}set( ${d}evaluate.foo = 'bar' )"+
+                          "${d}evaluate.foo ${d}evaluate.info.depth";
+        addTemplate("eval", "#evaluate(\""+template+"\")");
+        assertTmplEquals("bar 1", "eval");
+        assertNull(context.get("evaluate"));
+        assertNull(context.get("template"));
+    }
+
+    public void testNestedEvaluateScope()
+    {
+        context.put("h", "#");
+        context.put("d", "$");
+        addTemplate("e", "#evaluate(\"${h}evaluate( '${d}evaluate.info.depth${h}stop(${d}evaluate)
blah' )\")");
+        assertTmplEquals("2", "e");
+        assertNull(context.get("evaluate"));
+        assertNull(context.get("template"));
+    }
+
+    public void testTurningOffTemplateScope()
+    {
+        engine.setProperty("template."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
+        // root
+        addTemplate("test", "$template.info.depth");
+        assertTmplEquals("$template.info.depth", "test");
+        // #parse
+        assertEvalEquals("$template.info.depth", "#parse('test')");
+    }
+
+    public void testTurningOffEvaluateScope()
+    {
+        engine.setProperty("evaluate."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
+        // root
+        assertSchmoo("$evaluate.info.depth");
+        // #evaluate
+        assertEvalEquals("$evaluate.info.depth", "#evaluate( '$evaluate.info.depth' )");
+    }
+
+    public void testTurningOffMacroScope()
+    {
+        engine.setProperty("macro."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
+        engine.setProperty("foo."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
+        // macro definition
+        assertEvalEquals("$macro", "#macro(a)$macro#end#a()");
+        // macro body
+        assertEvalEquals("$macro $foo", "#macro(foo)$bodyContent#end#@foo()$macro $foo#end");
+    }
+
+    public void testTurningOffDefineScope()
+    {
+        engine.setProperty("define."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
+        assertEvalEquals("$define", "#define($a)$define#end$a");
+    }
+
+    public void testTurningOffForeachScope()
+    {
+        engine.setProperty("foreach."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, "false");
+        assertEvalEquals("$foreach$foreach", "#foreach($i in [0..1])$foreach#end");
+    }
+
+    public void testTemplateReplaced()
+    {
+        context.put("template", "foo");
+        addTemplate("test", "$template.replaced");
+        assertTmplEquals("foo", "test");
+        assertEvalEquals("foo", "#parse('test')");
+        assertContextValue("template", "foo");
+    }
+
+    public void testEvaluateReplaced()
+    {
+        context.put("evaluate","foo");
+        assertEvalEquals("foo", "$evaluate.replaced");
+        assertEvalEquals("foo", "#evaluate('$evaluate.replaced')");
+        assertContextValue("evaluate", "foo");
+    }
+
+    public void testMacroReplaced()
+    {
+        context.put("macro", "foo");
+        assertEvalEquals("foo foo foo", "$macro #macro(a)$macro.replaced#end#a() $macro");
+        assertContextValue("macro", "foo");
+    }
+
+    public void testForeachReplaced()
+    {
+        context.put("foreach", "foo");
+        assertEvalEquals("foofoofoo", "$foreach#foreach($i in [1..1])$foreach.replaced#end$foreach");
+        assertEquals("foo", context.get("foreach"));
+        context.put("foreach", "a");
+        assertEvalEquals("a", "#foreach($i in [1..1])#foreach($j in [1..1])$foreach.replaced#end#end");
+        assertContextValue("foreach", "a");
+    }
+
+    public void testDefineReplaced()
+    {
+        context.put("define", "a");
+        assertEvalEquals("a", "#define($a)$define.replaced#end$a");
+        assertContextValue("define", "a");
+    }
+
+    public void testBodyContentReplaced()
+    {
+        context.put("vm", "a");
+        assertEvalEquals("a", "#macro(vm)$bodyContent#end#@vm()$vm.replaced#end");
+        assertContextValue("vm", "a");
+    }
+
+    public void testInfoDepth()
+    {
+        String template = "#foreach($i in [1..1])"+
+                            "#foreach($j in [0..0])"+
+                                "$foreach.info.depth"+
+                            "#end"+
+                          "#end";
+        assertEvalEquals("2", template);
+    }
+
+    public void testInfoName()
+    {
+        String template = "#foreach($i in [1..1])"+
+                            "$foreach.info.name #evaluate('$evaluate.info.name')"+
+                          "#end";
+        assertEvalEquals("foreach evaluate", template);
+    }
+
+    public void testInfoType()
+    {
+        addTemplate("info", "#foreach($i in [1..1])"+
+                                "$foreach.info.type"+
+                            "#end "+
+                            "#evaluate('$evaluate.info.type') "+
+                            "$template.info.type");
+        assertTmplEquals("block line utf-8", "info");
+    }
+
+    public void testInfoLineAndColumn()
+    {
+        String template = " #evaluate('$evaluate.info.line, $evaluate.info.column')";
+        assertEvalEquals(" 1, 2", template);
+        assertEvalEquals("\n\n   3, 4", "\n\n  "+template);
+    }
+
+    public void testInfoTemplate()
+    {
+        addTemplate("test", "#evaluate('$evaluate.info.template')");
+        assertTmplEquals("test", "test");
+        assertEvalEquals("test", "#parse('test')");
+    }
+
+}



Mime
View raw message