You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by wg...@apache.org on 2007/09/18 06:35:45 UTC

svn commit: r576689 - in /velocity/engine/trunk: src/java/org/apache/velocity/runtime/directive/ src/test/org/apache/velocity/test/ test/macrolibs/compare/ test/parsemacros/ test/parsemacros/compare/

Author: wglass
Date: Mon Sep 17 21:35:43 2007
New Revision: 576689

URL: http://svn.apache.org/viewvc?rev=576689&view=rev
Log:
Final fix to VELOCITY-362. Ensures that if duplicate macros are included, the second one takes precedence.

Added:
    velocity/engine/trunk/test/macrolibs/compare/vm_library_duplicate.cmp   (with props)
    velocity/engine/trunk/test/parsemacros/compare/parseMacro3.cmp   (with props)
    velocity/engine/trunk/test/parsemacros/parseMacro3.vm   (with props)
Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/ParseWithMacroLibsTestCase.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/VMLibraryTestCase.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java?rev=576689&r1=576688&r2=576689&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/RuntimeMacro.java Mon Sep 17 21:35:43 2007
@@ -189,7 +189,7 @@
             List macroLibraries = context.getMacroLibraries();
             if (macroLibraries != null)
             {
-                for (int i = 0; i < macroLibraries.size(); i++)
+                for (int i = macroLibraries.size() - 1; i >= 0; i--)
                 {
                     o = rsvc.getVelocimacro(macroName,
                             (String)macroLibraries.get(i));

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/ParseWithMacroLibsTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/ParseWithMacroLibsTestCase.java?rev=576689&r1=576688&r2=576689&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/ParseWithMacroLibsTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/ParseWithMacroLibsTestCase.java Mon Sep 17 21:35:43 2007
@@ -192,6 +192,8 @@
         
         return ve;
     }
+    
+    
     /**
      * Test whether the literal text is given if a definition cannot be
      * found for a macro.
@@ -241,4 +243,55 @@
             fail("Processed template did not match expected output");
         }
     }
+
+
+    /**
+     * Test that if a macro is duplicated, the second one takes precendence
+     *
+     * @throws Exception
+     */
+    public void testDuplicateDefinitions()
+            throws Exception
+    {
+        /*
+         *  ve1: local scope, cache on
+         */
+        VelocityEngine ve1 = new VelocityEngine();
+        
+        ve1.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
+        ve1.setProperty("velocimacro.permissions.allow.inline.to.replace.global",
+                Boolean.FALSE);
+        ve1.setProperty("file.resource.loader.cache", Boolean.TRUE);
+        ve1.setProperty(
+                Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
+        ve1.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
+        ve1.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
+                TEST_COMPARE_DIR + "/parsemacros");
+        ve1.init();
+        
+        assureResultsDirectoryExists(RESULT_DIR);
+
+        FileOutputStream fos = new FileOutputStream (getFileName(
+                RESULT_DIR, "parseMacro3", RESULT_FILE_EXT));
+
+        VelocityContext context = new VelocityContext();
+
+        Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
+
+        Template template = ve1.getTemplate("parseMacro3.vm");
+        template.merge(context, writer);
+
+        /**
+         * Write to the file
+         */
+        writer.flush();
+        writer.close();
+
+        if (!isMatch(RESULT_DIR, COMPARE_DIR, "parseMacro3",
+                RESULT_FILE_EXT,CMP_FILE_EXT))
+        {
+            fail("Processed template did not match expected output");
+        }
+    }
+
 }

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/VMLibraryTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/VMLibraryTestCase.java?rev=576689&r1=576688&r2=576689&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/VMLibraryTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/VMLibraryTestCase.java Mon Sep 17 21:35:43 2007
@@ -240,6 +240,52 @@
     }
 
     /**
+     * Runs the tests with global namespace.
+     */
+    public void testVelociMacroLibWithDuplicateDefinitions()
+            throws Exception
+    {
+        assureResultsDirectoryExists(RESULT_DIR);
+        /**
+         * Clear the file before proceeding
+         */
+        File file = new File(getFileName(
+                RESULT_DIR, "vm_library_duplicate", RESULT_FILE_EXT));
+        if (file.exists())
+        {
+            file.delete();
+        }
+
+        /**
+         * Create a file output stream for appending
+         */
+        FileOutputStream fos = new FileOutputStream (getFileName(
+                RESULT_DIR, "vm_library_duplicate", RESULT_FILE_EXT), true);
+
+        List templateList = new ArrayList();
+        VelocityContext context = new VelocityContext();
+        Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
+
+        templateList.add("vm_library1.vm");
+        templateList.add("vm_library2.vm");
+
+        Template template = ve1.getTemplate("vm_library.vm");
+        template.merge(context, writer, templateList);
+
+        /**
+         * Write to the file
+         */
+        writer.flush();
+        writer.close();
+
+        if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library_duplicate",
+                RESULT_FILE_EXT,CMP_FILE_EXT))
+        {
+            fail("Processed template did not match expected output");
+        }
+    }
+
+    /**
      * Test whether the literal text is given if a definition cannot be
      * found for a macro.
      *
@@ -274,4 +320,6 @@
             fail("Processed template did not match expected output");
         }
     }
+
+
 }

Added: velocity/engine/trunk/test/macrolibs/compare/vm_library_duplicate.cmp
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/test/macrolibs/compare/vm_library_duplicate.cmp?rev=576689&view=auto
==============================================================================
--- velocity/engine/trunk/test/macrolibs/compare/vm_library_duplicate.cmp (added)
+++ velocity/engine/trunk/test/macrolibs/compare/vm_library_duplicate.cmp Mon Sep 17 21:35:43 2007
@@ -0,0 +1,6 @@
+This is a test file for loading macro libs programatically
+
+call foo
+86
+no macro definition
+#abc(2)

Propchange: velocity/engine/trunk/test/macrolibs/compare/vm_library_duplicate.cmp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/engine/trunk/test/macrolibs/compare/vm_library_duplicate.cmp
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: velocity/engine/trunk/test/parsemacros/compare/parseMacro3.cmp
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/test/parsemacros/compare/parseMacro3.cmp?rev=576689&view=auto
==============================================================================
--- velocity/engine/trunk/test/parsemacros/compare/parseMacro3.cmp (added)
+++ velocity/engine/trunk/test/parsemacros/compare/parseMacro3.cmp Mon Sep 17 21:35:43 2007
@@ -0,0 +1,4 @@
+
+
+8 6
+

Propchange: velocity/engine/trunk/test/parsemacros/compare/parseMacro3.cmp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/engine/trunk/test/parsemacros/compare/parseMacro3.cmp
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: velocity/engine/trunk/test/parsemacros/parseMacro3.vm
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/test/parsemacros/parseMacro3.vm?rev=576689&view=auto
==============================================================================
--- velocity/engine/trunk/test/parsemacros/parseMacro3.vm (added)
+++ velocity/engine/trunk/test/parsemacros/parseMacro3.vm Mon Sep 17 21:35:43 2007
@@ -0,0 +1,5 @@
+#parse("vm_library1.vm")
+#parse("vm_library2.vm")
+#foo(1) #bar(2)
+
+

Propchange: velocity/engine/trunk/test/parsemacros/parseMacro3.vm
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/engine/trunk/test/parsemacros/parseMacro3.vm
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision