Author: wglass
Date: Tue Nov 3 05:32:31 2009
New Revision: 832302
URL: http://svn.apache.org/viewvc?rev=832302&view=rev
Log:
include ! in reference name when calling InvalidReferenceEventHandler, e.g.
$!foo instead of $foo. VELOCITY-740.
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
velocity/engine/trunk/src/test/org/apache/velocity/test/InvalidEventHandlerTestCase.java
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java?rev=832302&r1=832301&r2=832302&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/app/event/InvalidReferenceEventHandler.java
Tue Nov 3 05:32:31 2009
@@ -44,7 +44,7 @@
* returned.
*
* @param context the context when the reference was found invalid
- * @param reference string with complete invalid reference
+ * @param reference string with complete invalid reference. If silent
reference, will start with $!
* @param object the object referred to, or null if not found
* @param property the property name from the reference
* @param info contains template, line, column details
@@ -77,7 +77,7 @@
* the chain until the first non-null value is returned.
*
* @param context the context when the reference was found invalid
- * @param reference string with complete invalid reference
+ * @param reference string with complete invalid reference. . If silent
reference, will start with $!
* @param object the object referred to, or null if not found
* @param method the name of the (non-existent) method
* @param info contains template, line, column details
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=832302&r1=832301&r2=832302&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
Tue Nov 3 05:32:31 2009
@@ -242,7 +242,7 @@
if (result == null && !strictRef)
{
return EventHandlerUtil.invalidGetMethod(rsvc, context,
- "$" + rootString, null, null, uberInfo);
+ getDollarBang() + rootString, null, null, uberInfo);
}
/*
@@ -291,11 +291,11 @@
if (failedChild == -1)
{
result = EventHandlerUtil.invalidGetMethod(rsvc, context,
- "$" + rootString, previousResult, null, uberInfo);
+ getDollarBang() + rootString, previousResult,
null, uberInfo);
}
else
{
- StringBuffer name = new
StringBuffer("$").append(rootString);
+ StringBuffer name = new
StringBuffer(getDollarBang()).append(rootString);
for (int i = 0; i <= failedChild; i++)
{
Node node = jjtGetChild(i);
@@ -1010,4 +1010,9 @@
}
return obj;
}
+
+ public String getDollarBang()
+ {
+ return (referenceType == QUIET_REFERENCE) ? "$!" : "$";
+ }
}
Modified:
velocity/engine/trunk/src/test/org/apache/velocity/test/InvalidEventHandlerTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/InvalidEventHandlerTestCase.java?rev=832302&r1=832301&r2=832302&view=diff
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/InvalidEventHandlerTestCase.java
(original)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/InvalidEventHandlerTestCase.java
Tue Nov 3 05:32:31 2009
@@ -187,8 +187,10 @@
{
VelocityContext context = new VelocityContext(vc);
context.put("a1",new Integer(5));
+ context.put("b1",new Integer(5));
context.put("a4",new Integer(5));
- context.put("b1","abc");
+ context.put("b4",new Integer(5));
+ context.put("z1","abc");
String s;
Writer w;
@@ -201,6 +203,14 @@
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // good object, bad method
+ s = "$!b1.afternoon()";
+ w = new StringWriter();
+ try {
+ ve.evaluate( context, w, "mystring", s );
+ fail("Expected exception.");
+ } catch (RuntimeException e) {}
+
// bad object, bad method -- fails on get
s = "$zz.daylight()";
w = new StringWriter();
@@ -210,7 +220,7 @@
} catch (RuntimeException e) {}
// change result
- s = "$b1.baby()";
+ s = "$z1.baby()";
w = new StringWriter();
ve.evaluate( context, w, "mystring", s );
assertEquals("www",w.toString());
@@ -229,11 +239,13 @@
VelocityContext context = new VelocityContext(vc);
context.put("a1",new Integer(5));
+ context.put("b1",new Integer(5));
context.put("a4",new Integer(5));
- context.put("b1","abc");
+ context.put("b4",new Integer(5));
+ context.put("z1","abc");
// normal - should be no calls to handler
- String s = "$a1 $a1.intValue() $b1 $b1.length() #set($c1 = '5')";
+ String s = "$a1 $a1.intValue() $z1 $z1.length() #set($c1 = '5')";
Writer w = new StringWriter();
ve.evaluate( context, w, "mystring", s );
@@ -245,6 +257,14 @@
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // good object, bad property / silent
+ s = "$!b1.foobar";
+ w = new StringWriter();
+ try {
+ ve.evaluate( context, w, "mystring", s );
+ fail("Expected exception.");
+ } catch (RuntimeException e) {}
+
// bad object, bad property
s = "$a2.foobar";
w = new StringWriter();
@@ -253,6 +273,14 @@
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // bad object, bad property / silent
+ s = "$!b2.foobar";
+ w = new StringWriter();
+ try {
+ ve.evaluate( context, w, "mystring", s );
+ fail("Expected exception.");
+ } catch (RuntimeException e) {}
+
// bad object, no property
s = "$a3";
w = new StringWriter();
@@ -261,13 +289,21 @@
fail("Expected exception.");
} catch (RuntimeException e) {}
+ // bad object, no property / silent
+ s = "$!b3";
+ w = new StringWriter();
+ try {
+ ve.evaluate( context, w, "mystring", s );
+ fail("Expected exception.");
+ } catch (RuntimeException e) {}
+
// good object, bad property; change the value
s = "$a4.foobar";
w = new StringWriter();
ve.evaluate( context, w, "mystring", s );
result = w.toString();
assertEquals("zzz", result);
-
+
}
@@ -314,6 +350,30 @@
throw new RuntimeException("expected exception");
}
+ // good object, bad property
+ else if (reference.equals("$!b1.foobar"))
+ {
+ assertEquals(new Integer(5),object);
+ assertEquals("foobar",property);
+ throw new RuntimeException("expected exception");
+ }
+
+ // good object, bad property
+ else if (reference.equals("$a1.foobar"))
+ {
+ assertEquals(new Integer(5),object);
+ assertEquals("foobar",property);
+ throw new RuntimeException("expected exception");
+ }
+
+ // good object, bad property
+ else if (reference.equals("$!b1.foobar"))
+ {
+ assertEquals(new Integer(5),object);
+ assertEquals("foobar",property);
+ throw new RuntimeException("expected exception");
+ }
+
// bad object, bad property
else if (reference.equals("$a2"))
{
@@ -321,7 +381,15 @@
assertNull(property);
throw new RuntimeException("expected exception");
}
-
+
+ // bad object, bad property
+ else if (reference.equals("$!b2"))
+ {
+ assertNull(object);
+ assertNull(property);
+ throw new RuntimeException("expected exception");
+ }
+
// bad object, no property
else if (reference.equals("$a3"))
{
@@ -330,6 +398,14 @@
throw new RuntimeException("expected exception");
}
+ // bad object, no property
+ else if (reference.equals("$!b3"))
+ {
+ assertNull(object);
+ assertNull(property);
+ throw new RuntimeException("expected exception");
+ }
+
// good object, bad property; change the value
else if (reference.equals("$a4.foobar"))
{
@@ -389,11 +465,24 @@
// good reference, bad method
if (object.getClass().equals(Integer.class))
{
- assertEquals("$a1.afternoon()",reference);
- assertEquals("afternoon",method);
- throw new RuntimeException("expected exception");
+ if (reference.equals("$a1.afternoon()"))
+ {
+ assertEquals("afternoon",method);
+ throw new RuntimeException("expected exception");
+ }
+ else if (reference.equals("$!b1.afternoon()"))
+ {
+ assertEquals("afternoon",method);
+ throw new RuntimeException("expected exception");
+ }
+ else
+ {
+ fail("Unexpected invalid method. " + method);
+
+ }
}
+
else if (object.getClass().equals(String.class) &&
"baby".equals(method))
{
return "www";