You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by wg...@apache.org on 2005/02/01 07:55:38 UTC
svn commit: r149378 - in jakarta/velocity/trunk: build/testcases.xml
src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
src/java/org/apache/velocity/test/ForeachMethodCallTestCase.java
src/java/org/apache/velocity/test/provider/ForeachMethodCallHelper.java
Author: wglass
Date: Mon Jan 31 22:55:36 2005
New Revision: 149378
URL: http://svn.apache.org/viewcvs?view=rev&rev=149378
Log:
Fixed invalid caching of methods within foreach (where the parameters for a given method call might change). http://issues.apache.org/bugzilla/show_bug.cgi?id=13637
Added:
jakarta/velocity/trunk/src/java/org/apache/velocity/test/ForeachMethodCallTestCase.java
jakarta/velocity/trunk/src/java/org/apache/velocity/test/provider/ForeachMethodCallHelper.java
Modified:
jakarta/velocity/trunk/build/testcases.xml
jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
Modified: jakarta/velocity/trunk/build/testcases.xml
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/build/testcases.xml?view=diff&r1=149377&r2=149378
==============================================================================
--- jakarta/velocity/trunk/build/testcases.xml (original)
+++ jakarta/velocity/trunk/build/testcases.xml Mon Jan 31 22:55:36 2005
@@ -76,6 +76,7 @@
test-servlet,
test-parser,
test-resourceinstance,
+ test-foreachmethodcall,
test-anakia
"/>
@@ -412,6 +413,16 @@
<echo message="Running Resource Loader instance tests..."/>
<java classname="${velocity.test.runner}" fork="yes" failonerror="${testbed.failonerror}">
<arg value="org.apache.velocity.test.ResourceLoaderInstanceTest"/>
+ <classpath>
+ <path refid="classpath"/>
+ </classpath>
+ </java>
+ </target>
+
+ <target name="test-foreachmethodcall">
+ <echo message="Running Foreach Method call tests..."/>
+ <java classname="${velocity.test.runner}" fork="yes" failonerror="${testbed.failonerror}">
+ <arg value="org.apache.velocity.test.ForeachMethodCallTestCase"/>
<classpath>
<path refid="classpath"/>
</classpath>
Modified: jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java?view=diff&r1=149377&r2=149378
==============================================================================
--- jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java (original)
+++ jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java Mon Jan 31 22:55:36 2005
@@ -41,7 +41,7 @@
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
- * @version $Id: ASTMethod.java,v 1.28 2004/03/20 03:35:51 dlr Exp $
+ * @version $Id$
*/
public class ASTMethod extends SimpleNode
{
@@ -105,10 +105,27 @@
try
{
/*
- * check the cache
+ * sadly, we do need recalc the values of the args, as this can
+ * change from visit to visit
*/
- IntrospectionCacheData icd = context.icacheGet( this );
+ Class[] paramClasses = new Class[paramCount];
+
+ for (int j = 0; j < paramCount; j++) {
+ params[j] = jjtGetChild(j + 1).value(context);
+
+ if (params[j] != null)
+ {
+ paramClasses[j] = params[j].getClass();
+ }
+ }
+
+ /*
+ * check the cache
+ */
+
+ MethodCacheKey mck = new MethodCacheKey(paramClasses);
+ IntrospectionCacheData icd = context.icacheGet( mck );
Class c = o.getClass();
/*
@@ -119,16 +136,9 @@
if ( icd != null && icd.contextData == c )
{
- /*
- * sadly, we do need recalc the values of the args, as this can
- * change from visit to visit
- */
-
- for (int j = 0; j < paramCount; j++)
- params[j] = jjtGetChild(j + 1).value(context);
/*
- * and get the method from the cache
+ * get the method from the cache
*/
method = (VelMethod) icd.thingy;
@@ -150,7 +160,8 @@
icd = new IntrospectionCacheData();
icd.contextData = c;
icd.thingy = method;
- context.icachePut( this, icd );
+
+ context.icachePut( mck, icd );
}
}
@@ -264,4 +275,31 @@
return null;
}
}
+
+ /**
+ * Internal class used as key for method cache. Combines
+ * ASTMethod fields with array of parameter classes.
+ */
+ class MethodCacheKey
+ {
+ Class[] params;
+
+ MethodCacheKey(Class[] params)
+ {
+ this.params = params;
+ }
+
+ public boolean equals(Object o)
+ {
+ return (o != null) &&
+ (o instanceof MethodCacheKey) &&
+ (this.hashCode() == o.hashCode());
+ }
+
+ public int hashCode()
+ {
+ return params.hashCode() * 37 + ASTMethod.this.hashCode();
+ }
+ }
+
}
Added: jakarta/velocity/trunk/src/java/org/apache/velocity/test/ForeachMethodCallTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/test/ForeachMethodCallTestCase.java?view=auto&rev=149378
==============================================================================
--- jakarta/velocity/trunk/src/java/org/apache/velocity/test/ForeachMethodCallTestCase.java (added)
+++ jakarta/velocity/trunk/src/java/org/apache/velocity/test/ForeachMethodCallTestCase.java Mon Jan 31 22:55:36 2005
@@ -0,0 +1,90 @@
+package org.apache.velocity.test;
+
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.test.provider.ForeachMethodCallHelper;
+
+/**
+ * This class tests proper method execution during a Foreach loop with items
+ * of varying class.
+ *
+ * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
+ * @version $Id: VelocimacroTestCase.java 75959 2004-03-19 17:13:40Z dlr $
+ */
+public class ForeachMethodCallTestCase extends TestCase
+{
+ private String template1 = "#foreach ( $item in $col )$helper.getFoo($item) #end";
+ private String result1 = "int 100 str STRVALUE ";
+
+ public ForeachMethodCallTestCase()
+ {
+ super("ForeachMethodCallTestCase");
+
+ try
+ {
+ Velocity.init();
+ }
+ catch (Exception e)
+ {
+ System.err.println("Cannot setup ForeachMethodCallTestCase!");
+ System.exit(1);
+ }
+ }
+
+ public static junit.framework.Test suite()
+ {
+ return new ForeachMethodCallTestCase();
+ }
+
+ /**
+ * Runs the test.
+ */
+ public void runTest ()
+ {
+ VelocityContext context = new VelocityContext();
+ List col = new ArrayList();
+ col.add(new Integer(100));
+ col.add("STRVALUE");
+ context.put("helper", new ForeachMethodCallHelper());
+ context.put("col", col);
+
+ try
+ {
+ StringWriter writer = new StringWriter();
+ Velocity.evaluate(context, writer, "test", template1);
+
+ String out = writer.toString();
+
+ if( !result1.equals( out ) )
+ {
+ fail("output incorrect.");
+ }
+ }
+ catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+}
Added: jakarta/velocity/trunk/src/java/org/apache/velocity/test/provider/ForeachMethodCallHelper.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/test/provider/ForeachMethodCallHelper.java?view=auto&rev=149378
==============================================================================
--- jakarta/velocity/trunk/src/java/org/apache/velocity/test/provider/ForeachMethodCallHelper.java (added)
+++ jakarta/velocity/trunk/src/java/org/apache/velocity/test/provider/ForeachMethodCallHelper.java Mon Jan 31 22:55:36 2005
@@ -0,0 +1,28 @@
+package org.apache.velocity.test.provider;
+
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * Provides overloaded methods for testing method execution within a foreach
+ * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
+ * @version $Id: VelocimacroTestCase.java 75959 2004-03-19 17:13:40Z dlr $
+ */
+public class ForeachMethodCallHelper
+{
+ public String getFoo(Integer v) { return "int "+v; }
+ public String getFoo(String v) { return "str "+v; }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org