You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by by...@apache.org on 2008/12/28 10:23:39 UTC
svn commit: r729711 - in /velocity/engine/trunk/src:
java/org/apache/velocity/runtime/directive/Foreach.java
test/org/apache/velocity/test/ForeachTestCase.java
Author: byron
Date: Sun Dec 28 01:23:38 2008
New Revision: 729711
URL: http://svn.apache.org/viewvc?rev=729711&view=rev
Log:
VELOCITY-658 fix hasNext variable in nested foreach blocks, thanks Jarkko Viinamäki
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java
velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java
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=729711&r1=729710&r2=729711&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 Sun Dec 28 01:23:38 2008
@@ -329,7 +329,8 @@
*/
Object o = context.get(elementKey);
Object savedCounter = context.get(counterName);
-
+ Object nextFlag = context.get(hasNextName);
+
/*
* Instantiate the null holder context if a null value
* is returned by the foreach iterator. Only one instance is
@@ -406,6 +407,18 @@
context.remove(elementKey);
}
+ /*
+ * restores the "hasNext" boolean flag if it exists
+ */
+ if( nextFlag != null )
+ {
+ context.put(hasNextName, nextFlag);
+ }
+ else
+ {
+ context.remove(hasNextName);
+ }
+
return true;
}
}
Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java?rev=729711&r1=729710&r2=729711&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java Sun Dec 28 01:23:38 2008
@@ -38,41 +38,24 @@
* @author Daniel Rall
* @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
*/
-public class ForeachTestCase extends TestCase
+public class ForeachTestCase extends BaseEvalTestCase
{
- private VelocityContext context;
-
public ForeachTestCase(String name)
{
super(name);
}
- public void setUp()
- throws Exception
- {
- // Limit the loop to three iterations.
- Velocity.setProperty(RuntimeConstants.MAX_NUMBER_LOOPS,
- new Integer(3));
-
- Velocity.setProperty(
- Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
-
- Velocity.init();
-
- context = new VelocityContext();
- }
-
/**
* Tests limiting of the number of loop iterations.
*/
public void testMaxNbrLoopsConstraint()
throws Exception
{
- StringWriter writer = new StringWriter();
- String template = "#foreach ($item in [1..10])$item #end";
- Velocity.evaluate(context, writer, "test", template);
- assertEquals("Max number loops not enforced",
- "1 2 3 ", writer.toString());
+ // Limit the loop to three iterations.
+ engine.setProperty(RuntimeConstants.MAX_NUMBER_LOOPS,
+ new Integer(3));
+
+ assertEvalEquals("1 2 3 ", "#foreach ($item in [1..10])$item #end");
}
/**
@@ -88,12 +71,7 @@
context.put("helper", new ForeachMethodCallHelper());
context.put("col", col);
- StringWriter writer = new StringWriter();
- Velocity.evaluate(context, writer, "test",
- "#foreach ( $item in $col )$helper.getFoo($item) " +
- "#end");
- assertEquals("Method calls while looping over varying classes failed",
- "int 100 str STRVALUE ", writer.toString());
+ assertEvalEquals("int 100 str STRVALUE ", "#foreach ( $item in $col )$helper.getFoo($item) #end");
}
/**
@@ -106,11 +84,7 @@
{
context.put("iterable", new MyIterable());
- StringWriter writer = new StringWriter();
- String template = "#foreach ($i in $iterable)$i #end";
- Velocity.evaluate(context, writer, "test", template);
- assertEquals("Failed to call iterator() method",
- "1 2 3 ", writer.toString());
+ assertEvalEquals("1 2 3 ", "#foreach ($i in $iterable)$i #end");
}
public void testNotReallyIterableIteratorMethod()
@@ -118,10 +92,7 @@
{
context.put("nri", new NotReallyIterable());
- StringWriter writer = new StringWriter();
- String template = "#foreach ($i in $nri)$i #end";
- Velocity.evaluate(context, writer, "test", template);
- assertEquals("", writer.toString());
+ assertEvalEquals("", "#foreach ($i in $nri)$i #end");
}
public void testVelocityHasNextProperty()
@@ -132,13 +103,27 @@
list.add("test2");
list.add("test3");
context.put("list", list);
-
- StringWriter writer = new StringWriter();
- String template = "#foreach ($value in $list)$value #if( $velocityHasNext )SEPARATOR #end#end";
- Velocity.evaluate(context, writer, "test", template);
- assertEquals("test1 SEPARATOR test2 SEPARATOR test3 ", writer.toString());
+ assertEvalEquals("test1 SEPARATOR test2 SEPARATOR test3 ", "#foreach ($value in $list)$value #if( $velocityHasNext )SEPARATOR #end#end");
}
+ public void testNestedVelocityHasNextProperty()
+ throws Exception
+ {
+ List list = new ArrayList();
+ list.add("test1");
+ list.add("test2");
+ list.add("test3");
+ list.add("test4");
+ context.put("list", list);
+ List list2 = new ArrayList();
+ list2.add("a1");
+ list2.add("a2");
+ list2.add("a3");
+ context.put("list2", list2);
+
+ assertEvalEquals("test1 (a1;a2;a3)-test2 (a1;a2;a3)-test3 (a1;a2;a3)-test4 (a1;a2;a3)", "#foreach ($value in $list)$value (#foreach ($val in $list2)$val#if( $velocityHasNext );#end#end)#if( $velocityHasNext )-#end#end");
+ }
+
public static class MyIterable
{
private List foo;