Author: wglass
Date: Thu Jan  6 06:42:07 2005
New Revision: 124382

URL: http://svn.apache.org/viewcvs?view=rev&rev=124382
Log:
Allow absolute paths with FileResourceLoader

http://issues.apache.org/bugzilla/show_bug.cgi?id=17379
Added:
   
jakarta/velocity/trunk/src/java/org/apache/velocity/test/AbsoluteFileResourceLoaderTest.java
   jakarta/velocity/trunk/test/absolute/   (props changed)
   jakarta/velocity/trunk/test/absolute/absolute.vm
   jakarta/velocity/trunk/test/absolute/compare/
   jakarta/velocity/trunk/test/absolute/compare/absolute.cmp
Modified:
   jakarta/velocity/trunk/build/testcases.xml
   
jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java

Modified: jakarta/velocity/trunk/build/testcases.xml
Url: 
http://svn.apache.org/viewcvs/jakarta/velocity/trunk/build/testcases.xml?view=diff&rev=124382&p1=jakarta/velocity/trunk/build/testcases.xml&r1=124381&p2=jakarta/velocity/trunk/build/testcases.xml&r2=124382
==============================================================================
--- jakarta/velocity/trunk/build/testcases.xml  (original)
+++ jakarta/velocity/trunk/build/testcases.xml  Thu Jan  6 06:42:07 2005
@@ -53,6 +53,7 @@
 
   <target name="test-all" depends="
                                    test-template,
+                                   test-absolutefileloader,
                                    test-velocityapp,
                                    test-introspect,
                                    test-introspect2,
@@ -87,6 +88,17 @@
       <classpath refid="classpath"/>
     </java>
    </target>
+   
+   <target name="test-absolutefileloader">
+    <echo message="Running absolute file resource loader test..."/>
+
+    <java classname="${velocity.test.runner}" fork="yes"
+          failonerror="${testbed.failonerror}">
+      <arg value="org.apache.velocity.test.AbsoluteFileResourceLoaderTest"/>
+      <classpath refid="classpath"/>
+  </java>
+  </target>
+   
 
  <target name="test-eventhandling">
     <echo message="Running Event Handler tests..."/>

Modified: 
jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
Url: 
http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java?view=diff&rev=124382&p1=jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java&r1=124381&p2=jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java&r2=124382
==============================================================================
--- 
jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
 (original)
+++ 
jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
 Thu Jan  6 06:42:07 2005
@@ -36,8 +36,9 @@
 /**
  * A loader for templates stored on the file system.
  *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Will Glass-Husain</a>
  * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: FileResourceLoader.java,v 1.23 2004/03/20 03:35:51 dlr Exp $
+ * @version $Id$
  */
 public class FileResourceLoader extends ResourceLoader
 {
@@ -85,6 +86,7 @@
     public synchronized InputStream getResourceStream(String templateName)
         throws ResourceNotFoundException
     {
+
         /*
          * Make sure we have a valid templateName.
          */
@@ -161,7 +163,7 @@
     {
         try 
         {
-            File file = new File( path, template );   
+            File file = getFile(path,template);
         
             if ( file.canRead() )
             {
@@ -206,13 +208,13 @@
         for (int i = 0; currentFile == null && i < paths.size(); i++)
         {
             String testPath = (String) paths.get(i);
-            File testFile = new File(testPath, fileName);
+            File testFile = getFile(testPath, fileName);
             if (testFile.canRead())
             {
                 currentFile = testFile;
             }
         }
-        File file = new File(path, fileName);
+        File file = getFile(path, fileName);
         if (currentFile == null || !file.exists())
         {
             /*
@@ -244,7 +246,7 @@
     public long getLastModified(Resource resource)
     {
         String path = (String) templatePaths.get(resource.getName());
-        File file = new File(path, resource.getName());
+        File file = getFile(path, resource.getName());
 
         if (file.canRead())
         {
@@ -254,5 +256,21 @@
         {
             return 0;
         }            
+    }
+
+
+    /**
+     * Create a File based on either a relative path if given, or absolute 
path otherwise
+     */
+    private File getFile(String path, String template)
+    {
+        File file = null;
+
+        if( "".equals(path) )
+            file = new File( template );
+        else
+            file = new File ( path, template );
+
+        return file;
     }
 }

Added: 
jakarta/velocity/trunk/src/java/org/apache/velocity/test/AbsoluteFileResourceLoaderTest.java
Url: 
http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/test/AbsoluteFileResourceLoaderTest.java?view=auto&rev=124382
==============================================================================
--- (empty file)
+++ 
jakarta/velocity/trunk/src/java/org/apache/velocity/test/AbsoluteFileResourceLoaderTest.java
        Thu Jan  6 06:42:07 2005
@@ -0,0 +1,149 @@
+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.BufferedWriter;

+import java.io.FileOutputStream;

+import java.io.OutputStreamWriter;

+import java.io.Writer;

+import java.io.File;

+

+import java.util.Properties;

+

+import org.apache.velocity.VelocityContext;

+

+import org.apache.velocity.Template;

+import org.apache.velocity.app.Velocity;

+import org.apache.velocity.runtime.RuntimeSingleton;

+import org.apache.velocity.test.provider.TestProvider;

+import org.apache.velocity.util.StringUtils;

+import org.apache.velocity.runtime.VelocimacroFactory;

+

+import junit.framework.TestCase;

+

+/**

+ * Test absolute path for the file loader

+ *

+ * @author <a href="mailto:[EMAIL PROTECTED]">Will Glass-Husain</a>

+ * @version $Id: MultipleFileResourcePathTest.java,v 1.8 2001/10/22 03:53:26 
jon Exp $

+ */

+public class AbsoluteFileResourceLoaderTest extends BaseTestCase

+{

+     /**

+     * VTL file extension.

+     */

+    private static final String TMPL_FILE_EXT = "vm";

+

+    /**

+     * Comparison file extension.

+     */

+    private static final String CMP_FILE_EXT = "cmp";

+

+    /**

+     * Comparison file extension.

+     */

+    private static final String RESULT_FILE_EXT = "res";

+

+    /**

+     * Path to template file.  This will get combined with the application 
directory to form an absolute path

+     */

+    private final static String TEMPLATE_PATH = "../test/absolute/absolute";

+

+    /**

+     * Results relative to the build directory.

+     */

+    private static final String RESULTS_DIR = "../test/absolute/results";

+

+    /**

+     * Results relative to the build directory.

+     */

+    private static final String COMPARE_DIR = "../test/absolute/compare";

+

+    /**

+     * Default constructor.

+     */

+    AbsoluteFileResourceLoaderTest()

+    {

+        super("AbsoluteFileResourceLoaderTest");

+

+        try

+        {

+            assureResultsDirectoryExists(RESULTS_DIR);

+

+

+            // signify we want to use an absolute path

+            Velocity.addProperty(

+                Velocity.FILE_RESOURCE_LOADER_PATH, "");

+

+            Velocity.init();

+        }

+        catch (Exception e)

+        {

+            System.err.println("Cannot setup AbsoluteFileResourceLoaderTest!");

+            e.printStackTrace();

+            System.exit(1);

+        }

+    }

+

+    public static junit.framework.Test suite ()

+    {

+        return new AbsoluteFileResourceLoaderTest();

+    }

+

+    /**

+     * Runs the test.

+     */

+    public void runTest ()

+    {

+        try

+        {

+

+            String curdir = System.getProperty("user.dir");

+            String f = getFileName(curdir, TEMPLATE_PATH, TMPL_FILE_EXT);

+

+            System.out.println("Retrieving template at absolute path: " + f);

+

+            Template template1 = RuntimeSingleton.getTemplate(f);

+

+            FileOutputStream fos1 =

+                new FileOutputStream (

+                    getFileName(RESULTS_DIR, "absolute", RESULT_FILE_EXT));

+

+            Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));

+

+            /*

+             *  put the Vector into the context, and merge both

+             */

+

+            VelocityContext context = new VelocityContext();

+

+            template1.merge(context, writer1);

+            writer1.flush();

+            writer1.close();

+

+            if (!isMatch(RESULTS_DIR, COMPARE_DIR, "absolute",

+                    RESULT_FILE_EXT, CMP_FILE_EXT))

+            {

+                fail("Output incorrect.");

+            }

+        }

+        catch (Exception e)

+        {

+            fail(e.getMessage());

+        }

+    }

+}


Added: jakarta/velocity/trunk/test/absolute/absolute.vm
Url: 
http://svn.apache.org/viewcvs/jakarta/velocity/trunk/test/absolute/absolute.vm?view=auto&rev=124382
==============================================================================
--- (empty file)
+++ jakarta/velocity/trunk/test/absolute/absolute.vm    Thu Jan  6 06:42:07 2005
@@ -0,0 +1,12 @@
+#*

+

[EMAIL PROTECTED] absolute.vm

+

+This template is used for Velocity regression testing.

+If you alter this template make sure you change the

+corresponding comparison file so that the regression

+test doesn't fail incorrectly.

+

+*#

+

+I am absolute.vm


Added: jakarta/velocity/trunk/test/absolute/compare/absolute.cmp
Url: 
http://svn.apache.org/viewcvs/jakarta/velocity/trunk/test/absolute/compare/absolute.cmp?view=auto&rev=124382
==============================================================================
--- (empty file)
+++ jakarta/velocity/trunk/test/absolute/compare/absolute.cmp   Thu Jan  6 
06:42:07 2005
@@ -0,0 +1,3 @@
+

+

+I am absolute.vm


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to