You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2016/07/14 09:34:46 UTC
svn commit: r1752609 - in /velocity/engine/trunk/velocity-engine-core/src:
main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
test/java/org/apache/velocity/test/InvalidEventHandlerTestCase.java
Author: cbrisson
Date: Thu Jul 14 09:34:46 2016
New Revision: 1752609
URL: http://svn.apache.org/viewvc?rev=1752609&view=rev
Log:
quiet references should never trigger invalid reference events
Modified:
velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/InvalidEventHandlerTestCase.java
Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=1752609&r1=1752608&r2=1752609&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java Thu Jul 14 09:34:46 2016
@@ -243,9 +243,9 @@ public class ASTReference extends Simple
{
/*
* do not trigger an invalid reference if the reference is present, but with a null value
- * don't either inside an #if/#elseif evaluation context
+ * don't either for a quiet reference or inside an #if/#elseif evaluation context
*/
- if (!context.containsKey(rootString) && !onlyTestingReference)
+ if (!context.containsKey(rootString) && referenceType != QUIET_REFERENCE && !onlyTestingReference)
{
return EventHandlerUtil.invalidGetMethod(rsvc, context,
"$" + rootString, null, null, uberInfo);
@@ -290,9 +290,11 @@ public class ASTReference extends Simple
{
// do not call bad reference handler if the getter is present
// (it means the getter has been called and returned null)
- // do not either if the *last* child failed while testing the reference
+ // do not either for a quiet reference or if the *last* child failed while testing the reference
Object getter = context.icacheGet(jjtGetChild(i));
- if (getter == null && (!onlyTestingReference || i < jjtGetNumChildren() - 1))
+ if (getter == null &&
+ referenceType != QUIET_REFERENCE &&
+ (!onlyTestingReference || i < jjtGetNumChildren() - 1))
{
failedChild = i;
break;
@@ -306,9 +308,10 @@ public class ASTReference extends Simple
{
/*
* do not trigger an invalid reference if the reference is present, but with a null value
- * don't either inside an #if/#elseif evaluation context when there's no child
+ * don't either for a quiet reference,
+ * or inside an #if/#elseif evaluation context when there's no child
*/
- if (!context.containsKey(rootString) && (!onlyTestingReference || jjtGetNumChildren() > 0))
+ if (!context.containsKey(rootString) && referenceType != QUIET_REFERENCE && (!onlyTestingReference || jjtGetNumChildren() > 0))
{
result = EventHandlerUtil.invalidGetMethod(rsvc, context,
"$" + rootString, previousResult, null, uberInfo);
Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/InvalidEventHandlerTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/InvalidEventHandlerTestCase.java?rev=1752609&r1=1752608&r2=1752609&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/InvalidEventHandlerTestCase.java (original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/InvalidEventHandlerTestCase.java Thu Jul 14 09:34:46 2016
@@ -238,7 +238,13 @@ extends TestCase
ve.evaluate( context, w, "mystring", s );
fail("Expected exception.");
} catch (RuntimeException e) {}
-
+
+ // good object, bad method, quiet reference
+ s = "$!a1.afternoon()";
+ w = new StringWriter();
+ ve.evaluate( context, w, "mystring", s );
+ assertEquals("", w.toString());
+
// bad object, bad method -- fails on get
s = "$zz.daylight()";
w = new StringWriter();
@@ -247,6 +253,12 @@ extends TestCase
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // bad object, bad method, quiet reference
+ s = "$!zz.daylight()";
+ w = new StringWriter();
+ ve.evaluate( context, w, "mystring", s );
+ assertEquals("", w.toString());
+
// change result
s = "$b1.baby()";
w = new StringWriter();
@@ -283,6 +295,12 @@ extends TestCase
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // same one as a quiet reference should not fail
+ s = "$!a1.foobar";
+ w = new StringWriter();
+ ve.evaluate( context, w, "mystring", s );
+ assertEquals("",w.toString());
+
// same one inside an #if statement should not fail
s = "#if($a1.foobar)yes#{else}no#end";
w = new StringWriter();
@@ -298,6 +316,12 @@ extends TestCase
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // same one as a quiet reference should not fail
+ s = "$!a2.foobar";
+ w = new StringWriter();
+ ve.evaluate( context, w, "mystring", s );
+ assertEquals("",w.toString());
+
// same one inside an #if statement should still fail
s = "#if($a2.foobar)yes#{else}no#end";
w = new StringWriter();
@@ -320,6 +344,13 @@ extends TestCase
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // bad object, no property as quiet reference should not fail
+ s = "$!a3";
+ w = new StringWriter();
+ ve.evaluate(context, w, "mystring", s);
+ result = w.toString();
+ assertEquals("", result);
+
// bad object, no property as #if condition should not fail
s = "#if($a3)yes#{else}no#end";
w = new StringWriter();