velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r809822 - in /velocity/engine/branches/2.0_Exp/src: java/org/apache/velocity/app/event/implement/IncludeNotFound.java test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
Date Tue, 01 Sep 2009 05:30:56 GMT
Author: nbubna
Date: Tue Sep  1 05:30:56 2009
New Revision: 809822

URL: http://svn.apache.org/viewvc?rev=809822&view=rev
Log:
VELOCITY-734 merge feature addition and test to 2.x branch (thanks to chad davis)

Modified:
    velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
    velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java

Modified: velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java?rev=809822&r1=809821&r2=809822&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
(original)
+++ velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/app/event/implement/IncludeNotFound.java
Tue Sep  1 05:30:56 2009
@@ -20,7 +20,9 @@
  */
 
 import org.apache.velocity.app.event.IncludeEventHandler;
+import org.apache.velocity.context.Context;
 import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.util.ContextAware;
 import org.apache.velocity.util.RuntimeServicesAware;
 import org.apache.velocity.util.StringUtils;
 
@@ -36,17 +38,24 @@
  * eventhandler.include.notfound = error.vm
  * </PRE>
  * </code>
+ * </p><p>
+ * The name of the missing resource is put into the Velocity context, under the 
+ * key "missingResource", so that the "notfound" template can report the missing 
+ * resource with a Velocity reference, like: 
+ * <code>$missingResource</code>
+ * </p>
  *
  * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
  * @version $Id$
  * @since 1.5
  */
-public class IncludeNotFound implements IncludeEventHandler,RuntimeServicesAware {
+public class IncludeNotFound implements IncludeEventHandler,RuntimeServicesAware,ContextAware
{
 
     private static final String DEFAULT_NOT_FOUND = "notfound.vm";
     private static final String PROPERTY_NOT_FOUND = "eventhandler.include.notfound";
     private RuntimeServices rs = null;
     String notfound;
+    Context context;
 
     /**
      * Chseck to see if included file exists, and display "not found" page if it
@@ -70,10 +79,10 @@
         boolean exists = (rs.getLoaderNameForResource(includeResourcePath) != null);
         if (!exists)
         {
+            context.put("missingResource", includeResourcePath);
             if (rs.getLoaderNameForResource(notfound) != null)
             {
                 return notfound;
-
             }
             else
             {
@@ -83,7 +92,6 @@
                 rs.getLog().error("Can't find include not found page: " + notfound);
                 return null;
             }
-
         }
         else
             return includeResourcePath;
@@ -97,6 +105,13 @@
     {
          this.rs = rs;
          notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND));
-     }
+    }
 
+    /** 
+     * @see org.apache.velocity.util.ContextAware#setContext(org.apache.velocity.context.Context)
+     */ 
+    public void setContext(Context context)
+    {
+        this.context = context;
+    }
 }

Modified: velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java?rev=809822&r1=809821&r2=809822&view=diff
==============================================================================
--- velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
(original)
+++ velocity/engine/branches/2.0_Exp/src/test/org/apache/velocity/test/BuiltInEventHandlerTestCase.java
Tue Sep  1 05:30:56 2009
@@ -93,9 +93,10 @@
         super(name);
     }
 
-    public void setUp()
+    public void setUp() throws Exception
     {
         assureResultsDirectoryExists(RESULTS_DIR);
+        super.setUp();
     }
 
     public static Test suite()
@@ -391,6 +392,14 @@
 
     }
 
+    public void testIncludeNotFoundMissingResourceName() throws Exception
+    {
+        // uses base test support
+        engine.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, "org.apache.velocity.app.event.implement.IncludeNotFound");
+        addTemplate("notfound.vm", "$missingResource");
+        assertEvalEquals("foo", "#parse('foo')");
+    }
+
     public void testIncludeRelativePath() throws Exception
     {
         VelocityEngine ve = new VelocityEngine();



Mime
View raw message