velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r702298 - in /velocity/engine/trunk/src: java/org/apache/velocity/runtime/directive/RuntimeMacro.java test/org/apache/velocity/test/StrictReferenceTestCase.java
Date Mon, 06 Oct 2008 22:54:56 GMT
Author: nbubna
Date: Mon Oct  6 15:54:55 2008
New Revision: 702298

URL: http://svn.apache.org/viewvc?rev=702298&view=rev
Log:
VELOCITY-618 be more careful about macros in strict ref mode

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/StrictReferenceTestCase.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=702298&r1=702297&r2=702298&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
Mon Oct  6 15:54:55 2008
@@ -24,6 +24,7 @@
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.runtime.parser.node.Node;
 import org.apache.velocity.runtime.parser.Token;
+import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.RuntimeServices;
 import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.exception.ParseErrorException;
@@ -66,6 +67,11 @@
     private Node node = null;
 
     /**
+     * Indicates if we are running in strict reference mode.
+     */
+    protected boolean strictRef = false;
+    
+    /**
      * Create a RuntimeMacro instance. Macro name and source
      * template stored for later use.
      *
@@ -119,6 +125,16 @@
         super.init(rs, context, node);
         rsvc = rs;
         this.node = node;
+        
+        /**
+         * Only check for strictRef setting if this really looks like a macro,
+         * so strict mode doesn't balk at things like #E0E0E0 in a template.
+         */
+        Token t = node.getLastToken();
+        if (t.image.charAt(0) == ')')
+        {
+            strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
+        }
     }
 
     /**
@@ -220,12 +236,19 @@
             }
             catch (TemplateInitException die)
             {
-                Info info = new Info(sourceTemplate, node.getLine(), node.getColumn());
+              Info info = new Info(sourceTemplate, node.getLine(), node.getColumn());
 
-                throw new ParseErrorException(die.getMessage(), info);
+                throw new ParseErrorException(die.getMessage() + " at " + info.getTemplateName()
+                    + "[" + info.getLine() + "," + info.getColumn() + "]", info);
             }
             return vmProxy.render(context, writer, node);
         }
+        else if (strictRef)
+        {
+            Info info = new Info(sourceTemplate, node.getLine(), node.getColumn());
+            throw new ParseErrorException("Macro '#" + macroName + "' is not defined at "+
+                info.getTemplateName() + "[" + info.getLine() + "," + info.getColumn() +
"]", info);
+        }
 
         /**
          * If we cannot find an implementation write the literal text

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/StrictReferenceTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/StrictReferenceTestCase.java?rev=702298&r1=702297&r2=702298&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/StrictReferenceTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/StrictReferenceTestCase.java Mon
Oct  6 15:54:55 2008
@@ -1,6 +1,7 @@
 package org.apache.velocity.test;
 
 import org.apache.velocity.exception.MethodInvocationException;
+import org.apache.velocity.exception.ParseErrorException;
 import org.apache.velocity.runtime.RuntimeConstants;
 
 /**
@@ -22,7 +23,7 @@
         context.put("bar", null);
         context.put("TRUE", Boolean.TRUE);
     }
-
+    
 
     /**
      * Test the modified behavior of #if in strict mode.  Mainly, that
@@ -58,8 +59,8 @@
     {
         evaluate("$bar");
         assertEvalEquals("true", "#if($bar == $NULL)true#end");
-        assertEvalEquals("false", "#set($foobar = $NULL)#if(!$foobar)false#end");
-        assertEvalEquals("13", "#set($list = [1, $NULL, 3])#foreach($item in $list)#if($item)$item#end#end");
+        assertEvalEquals("true", "#set($foobar = $NULL)#if($foobar == $NULL)true#end");
+        assertEvalEquals("13", "#set($list = [1, $NULL, 3])#foreach($item in $list)#if($item
!= $NULL)$item#end#end");
     }
     
     /**
@@ -137,7 +138,25 @@
         evaluate("$fargo.next.nullVal");
         evaluate("#foreach($item in $fargo.nullVal)#end");
     }
-        
+
+    /**
+     * Make sure undefined macros throw exceptions
+     */
+    public void testMacros()
+    {
+        assertParseEx("#bogus()");
+        assertParseEx("#bogus (  )");
+        assertParseEx("#bogus( $a )");        
+        assertParseEx("abc#bogus ( $a )a ");
+
+        assertEvalEquals(" true ", "#macro(test1) true #end#test1()");
+        assertEvalEquals(" true ", "#macro(test2 $a) $a #end#test2 ( \"true\")");
+        assertEvalEquals("#CCFFEE", "#CCFFEE");
+        assertEvalEquals("#F - ()", "#F - ()");
+        assertEvalEquals("#F{}", "#F{}");
+    }
+    
+    
     /**
      * Assert that we get a MethodInvocationException when calling evaluate
      */
@@ -146,6 +165,14 @@
         assertEvalException(template, MethodInvocationException.class);
     }
 
+    /**
+     * Assert that we get a MethodInvocationException when calling evaluate
+     */
+    public void assertParseEx(String template)
+    {
+        assertEvalException(template, ParseErrorException.class);
+    }
+
 
     public static class Fargo
     {



Mime
View raw message