You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2010/08/30 21:03:37 UTC
svn commit: r990898 - in /velocity/tools/trunk/src:
main/java/org/apache/velocity/tools/generic/LoopTool.java
test/java/org/apache/velocity/tools/generic/LoopToolTests.java
test/java/org/apache/velocity/tools/generic/Veltools128Tests.java
Author: nbubna
Date: Mon Aug 30 19:03:36 2010
New Revision: 990898
URL: http://svn.apache.org/viewvc?rev=990898&view=rev
Log:
VELTOOLS-128 fix sync problems
Added:
velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java (with props)
Modified:
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java
velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/LoopToolTests.java
Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java?rev=990898&r1=990897&r2=990898&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java Mon Aug 30 19:03:36 2010
@@ -94,6 +94,7 @@ public class LoopTool
{
private Stack<ManagedIterator> iterators = new Stack<ManagedIterator>();
private ManagedIterator last;
+ private Map<String,Object> lastSyncedValues;
/**
* <p>Tells the LoopTool to watch the specified Array, Collection, Map,
@@ -401,6 +402,14 @@ public class LoopTool
return iterator.get(key);
}
}
+ if (lastSyncedValues != null)
+ {
+ Object syncedValue = lastSyncedValues.get(key);
+ if (syncedValue != null)
+ {
+ return syncedValue;
+ }
+ }
// shortest key would be "last_X" where X is the loop name
if (key == null || key.length() < 6)
{
@@ -553,7 +562,9 @@ public class LoopTool
*/
protected ManagedIterator pop()
{
- return iterators.pop();
+ ManagedIterator i = iterators.pop();
+ this.lastSyncedValues = i.getLastSyncedValues();
+ return i;
}
@@ -739,9 +750,6 @@ public class LoopTool
}
}
}
-
- // ok, looks like we have a next that met all the conditions
- shiftSynced();
return true;
}
@@ -839,6 +847,8 @@ public class LoopTool
Object value = this.next;
// clear the cache
this.next = null;
+ // call next on synced ones
+ shiftSynced();
// return the no-longer-cached value
return value;
}
@@ -905,7 +915,7 @@ public class LoopTool
* <p>Adds another iterator to be kept in sync with the one
* being managed by this instance. The values of the parallel
* iterator can be retrieved from the LoopTool under the
- * name s"synced" (e.g. $loop.synched or $loop.get('synced'))
+ * name s"synced" (e.g. $loop.synced or $loop.get('synced'))
* and are automatically updated for each iteration by this instance.
* </p><p><b>NOTE</b>: if you are sync'ing multiple iterators
* with the same managed iterator, you must use
@@ -948,6 +958,20 @@ public class LoopTool
return this;
}
+ public Map<String,Object> getLastSyncedValues()
+ {
+ if (synced == null)
+ {
+ return null;
+ }
+ Map<String,Object> syncs = new HashMap<String,Object>();
+ for (String key : synced.keySet())
+ {
+ syncs.put(key, synced.get(key).get());
+ }
+ return syncs;
+ }
+
@Override
public String toString()
{
Modified: velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/LoopToolTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/LoopToolTests.java?rev=990898&r1=990897&r2=990898&view=diff
==============================================================================
--- velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/LoopToolTests.java (original)
+++ velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/LoopToolTests.java Mon Aug 30 19:03:36 2010
@@ -306,16 +306,18 @@ public class LoopToolTests {
// is, of course, impossible in a template, but it
// makes writing a reasonable test for this method a lot
// easier.
- //NOTE: this reliese on the default name for synced iterators
+ //NOTE: this relies on the default name for synced iterators
// being "synced", for i being "loop0", and for j being "loop1"
Iterator i = loop.watch(ARRAY).sync(other);
Iterator j = loop.watch(other).sync(ARRAY);
while (i.hasNext() && j.hasNext())
{
+ Object ival = i.next();
+ Object jval = j.next();
// i and loop.synced (aka loop.get("loop1","synced")) should match
- assertEquals(i.next(), loop.get("synced"));
+ assertEquals(ival, loop.get("synced"));
// j and loop.get("loop0","synced") should match
- assertEquals(j.next(), loop.get("loop0", "synced"));
+ assertEquals(jval, loop.get("loop0", "synced"));
}
}
Added: velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java?rev=990898&view=auto
==============================================================================
--- velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java (added)
+++ velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java Mon Aug 30 19:03:36 2010
@@ -0,0 +1,48 @@
+package org.apache.velocity.tools.generic;
+
+/*
+ * 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 org.apache.velocity.VelocityContext;
+
+/**
+ * This class tests VELOCITY-762.
+ */
+public class Veltools128Tests extends BaseTestCase
+{
+ public Veltools128Tests(String name)
+ {
+ super(name);
+ }
+
+ protected void setUpContext(VelocityContext context)
+ {
+ context.put("loop", new LoopTool());
+ }
+
+ public void testLoopToolSync()
+ {
+ String template =
+ "#foreach( $item in $loop.watch([1..3]).sync([3..5], 'other') )"+
+ "$item:$loop.other "+
+ "#end";
+ assertEvalEquals("1:3 2:4 3:5 ", template);
+ }
+
+}
Propchange: velocity/tools/trunk/src/test/java/org/apache/velocity/tools/generic/Veltools128Tests.java
------------------------------------------------------------------------------
svn:executable = *