Author: nbubna
Date: Tue Sep  2 14:28:16 2008
New Revision: 691385

URL: http://svn.apache.org/viewvc?rev=691385&view=rev
Log:
VELOCITY-467 add option for #foreach to bail on bad iterator() impl's (thanks 
to Adrian Tarau for persistence on this :)

Added:
    
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
   (with props)
Modified:
    
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java
    
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java

Modified: 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=691385&r1=691384&r2=691385&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java
 (original)
+++ 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java
 Tue Sep  2 14:28:16 2008
@@ -126,6 +126,12 @@
     /** Maximum allowed number of loops. */
     String MAX_NUMBER_LOOPS = "directive.foreach.maxloops";
 
+    /**
+     * Whether to throw an exception or just skip bad iterables. Default is 
true.
+     * @since 1.6
+     */
+    String SKIP_INVALID_ITERATOR = "directive.foreach.skip.invalid";
+
     /** if set to true then allows #set to accept null values in the right 
hand side. */
     String SET_NULL_ALLOWED = "directive.set.null.allowed";
 

Modified: 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java?rev=691385&r1=691384&r2=691385&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java
 (original)
+++ 
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java
 Tue Sep  2 14:28:16 2008
@@ -367,6 +367,11 @@
     private int maxNbrLoops;
 
     /**
+     * Whether or not to throw an Exception if the iterator is null.
+     */
+    private boolean skipInvalidIterator;
+
+    /**
      * The reference name used to access each
      * of the elements in the list object. It
      * is the $item in the following:
@@ -405,6 +410,8 @@
         {
             maxNbrLoops = Integer.MAX_VALUE;
         }
+        skipInvalidIterator =
+            rsvc.getBoolean(RuntimeConstants.SKIP_INVALID_ITERATOR, true);
 
         /*
          *  this is really the only thing we can do here as everything
@@ -474,14 +481,25 @@
         }
         catch(Exception ee)
         {
-            String msg = "Error getting iterator for #foreach";
+            String msg = "Error getting iterator for #foreach at "+uberInfo;
             rsvc.getLog().error(msg, ee);
             throw new VelocityException(msg, ee);
         }
 
         if (i == null)
         {
-            return false;
+            if (skipInvalidIterator)
+            {
+                return false;
+            }
+            else
+            {
+                String msg = "Uberspect returned a null iterator for #foreach 
at "
+                    + uberInfo + ".  " + node.jjtGetChild(2).literal() + " (" 
+ listObject
+                    + ") is either of wrong type or has an invalid iterator() 
implementation.";
+                rsvc.getLog().error(msg);
+                throw new VelocityException(msg);
+            }
         }
 
         int counter = counterInitialValue;

Added: 
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java?rev=691385&view=auto
==============================================================================
--- 
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
 (added)
+++ 
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
 Tue Sep  2 14:28:16 2008
@@ -0,0 +1,109 @@
+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.Collections;
+import java.util.Iterator;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.exception.VelocityException;
+import org.apache.velocity.runtime.RuntimeConstants;
+
+/**
+ * This class tests support for strict foreach mode.
+ */
+public class StrictForeachTestCase extends BaseEvalTestCase
+{
+    public StrictForeachTestCase(String name)
+    {
+       super(name);
+    }
+
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        engine.setProperty(RuntimeConstants.SKIP_INVALID_ITERATOR, 
Boolean.FALSE);
+        context.put("good", new GoodIterable());
+        context.put("bad", new BadIterable());
+        context.put("ugly", new UglyIterable());
+    }
+
+    public void testGood()
+    {
+        try
+        {
+            evaluate("#foreach( $i in $good )$i#end");
+        }
+        catch (VelocityException ve)
+        {
+            fail("Doing #foreach on $good should not have exploded!");
+        }
+    }
+
+    public void testBad()
+    {
+        try
+        {
+            evaluate("#foreach( $i in $bad )$i#end");
+            fail("Doing #foreach on $bad should have exploded!");
+        }
+        catch (VelocityException ve)
+        {
+            // success!
+        }
+    }
+
+    public void testUgly()
+    {
+        try
+        {
+            evaluate("#foreach( $i in $ugly )$i#end");
+            fail("Doing #foreach on $ugly should have exploded!");
+        }
+        catch (VelocityException ve)
+        {
+            // success!
+        }
+    }
+
+
+    public static class GoodIterable
+    {
+        public Iterator iterator()
+        {
+            return Collections.emptyList().iterator();
+        }
+    }
+
+    public static class BadIterable
+    {
+        public Object iterator()
+        {
+            return new Object();
+        }
+    }
+
+    public static class UglyIterable
+    {
+        public Iterator iterator()
+        {
+            return null;
+        }
+    }
+}

Propchange: 
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: 
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Propchange: 
velocity/engine/trunk/src/test/org/apache/velocity/test/StrictForeachTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to