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 2006/12/28 14:35:49 UTC

svn commit: r490718 - in /velocity/engine/trunk/src/test/org/apache/velocity/test: BaseTestCase.java MacroForwardDefineTestCase.java misc/TestLogChute.java

Author: wglass
Date: Thu Dec 28 05:35:49 2006
New Revision: 490718

URL: http://svn.apache.org/viewvc?view=rev&rev=490718
Log:
Use a in-memory logger to check log files, not log4j.  Fixes VELOCITY-508.

Added:
    velocity/engine/trunk/src/test/org/apache/velocity/test/misc/TestLogChute.java   (with props)
Modified:
    velocity/engine/trunk/src/test/org/apache/velocity/test/BaseTestCase.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/MacroForwardDefineTestCase.java

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/BaseTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/BaseTestCase.java?view=diff&rev=490718&r1=490717&r2=490718
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/BaseTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/BaseTestCase.java Thu Dec 28 05:35:49 2006
@@ -168,6 +168,26 @@
         String result = StringUtils.fileContentsToString
                 (getFileName(resultsDir, baseFileName, resultExt, true));
 
+        return isMatch(result,compareDir,baseFileName,compareExt);
+    }
+
+    
+    /**
+     * Returns whether the processed template matches the
+     * content of the provided comparison file.
+     *
+     * @return Whether the output matches the contents
+     *         of the comparison file.
+     *
+     * @exception Exception Test failure condition.
+     */
+    protected boolean isMatch (
+                               String result,
+                               String compareDir,
+                               String baseFileName,
+                               String compareExt)
+        throws Exception
+    {
         String compare = StringUtils.fileContentsToString
                 (getFileName(compareDir, baseFileName, compareExt, true));
 
@@ -179,7 +199,7 @@
                            normalizeNewlines( compare ) );
     }
 
-    /**
+        /**
      * Turns a base file name into a test case name.
      *
      * @param s The base file name.

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/MacroForwardDefineTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/MacroForwardDefineTestCase.java?view=diff&rev=490718&r1=490717&r2=490718
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/MacroForwardDefineTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/MacroForwardDefineTestCase.java Thu Dec 28 05:35:49 2006
@@ -27,6 +27,8 @@
 import junit.framework.TestSuite;
 
 import org.apache.velocity.app.Velocity;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.test.misc.TestLogChute;
 
 /**
  * Make sure that a forward referenced macro inside another macro definition does
@@ -56,6 +58,11 @@
     private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/macroforwarddefine/compare";
 
     /**
+     * Collects the log messages.
+     */
+    private TestLogChute logger = new TestLogChute();
+    
+    /**
      * Default constructor.
      */
     public MacroForwardDefineTestCase(String name)
@@ -67,15 +74,19 @@
         throws Exception
     {
         assureResultsDirectoryExists(RESULTS_DIR);
-        
-        InputStream stream = new FileInputStream(FILE_RESOURCE_LOADER_PATH + "/velocity.properties");
-        Properties p = new Properties();
-        p.load(stream);
-        
-        p.setProperty("file.resource.loader.path", FILE_RESOURCE_LOADER_PATH );
-        p.setProperty("runtime.log", RESULTS_DIR + "/velocity.log");
+                
+        // use Velocity.setProperty (instead of properties file) so that we can use actual instance of log
+        Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER,"file");
+        Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH );
+        Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID,"true");
+        Velocity.setProperty(RuntimeConstants.VM_LIBRARY, "macros.vm");
+
+        // actual instance of logger
+        logger = new TestLogChute();
+        Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,logger);
+        Velocity.setProperty("runtime.log.logsystem.test.level", "error");
 
-        Velocity.init(p);
+        Velocity.init();
     }
 
     public static Test suite()
@@ -86,8 +97,7 @@
     public void testLogResult()
         throws Exception
     {
-        if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "velocity.log",
-                        null, "cmp"))
+        if ( !isMatch(logger.getLog(), COMPARE_DIR, "velocity.log", "cmp"))
         {
             fail("Output incorrect.");
         }

Added: velocity/engine/trunk/src/test/org/apache/velocity/test/misc/TestLogChute.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/misc/TestLogChute.java?view=auto&rev=490718
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/misc/TestLogChute.java (added)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/misc/TestLogChute.java Thu Dec 28 05:35:49 2006
@@ -0,0 +1,160 @@
+package org.apache.velocity.test.misc;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.log.LogChute;
+
+/**
+ * LogChute implementation that creates a String in memory.  Used to test
+ * log information.
+ *
+ * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
+ * @version $Id$
+ */
+public class TestLogChute implements LogChute
+{
+    public static final String TEST_LOGGER_LEVEL = "runtime.log.logsystem.test.level";
+
+    private StringBuffer log = new StringBuffer();
+
+    private int logLevel;
+    
+    public void init(RuntimeServices rs) throws Exception
+    {
+        String level = rs.getString(TEST_LOGGER_LEVEL, "debug");
+        logLevel = getLevelNumber(level, LogChute.DEBUG_ID);
+    }
+
+    public void log(int level, String message)
+    {
+        if (level >= logLevel)
+        {
+            String levelName;
+            levelName = getLevelName(level);
+            log.append(" [").append(levelName).append("] ");
+            log.append(message);
+            log.append("\n");
+        }
+    }
+    
+    /**
+     * Return the stored log messages to date.
+     * @return log messages
+     */
+    public String getLog()
+    {
+        return log.toString();
+    }
+
+    /**
+     * Return the name corresponding to each level
+     * @param level integer level
+     * @return String level name
+     */
+    private String getLevelName(int level)
+    {
+        String levelName;
+        if (level == LogChute.DEBUG_ID)
+        {
+            levelName = "debug";
+        }
+        else if (level == LogChute.INFO_ID)
+        {
+            levelName = "info";
+        }
+        else if (level == LogChute.TRACE_ID)
+        {
+            levelName = "trace";
+        }
+        else if (level == LogChute.WARN_ID)
+        {
+            levelName = "warn";
+        }
+        else if (level == LogChute.ERROR_ID)
+        {
+            levelName = "error";
+        }
+        else 
+        {
+            levelName = "";
+        }
+
+        return levelName;
+    }
+
+    /**
+     * Return the integer level correspoding to the string number, or use the default
+     * @param level name
+     * @param defaultLevel the default if the name does not exist
+     * @return integer level
+     */
+    private int getLevelNumber(String level, int defaultLevel)
+    {
+        if (level == null)
+        {
+            return defaultLevel;
+        }
+        else if (level.equalsIgnoreCase("DEBUG"))
+        {
+            return LogChute.DEBUG_ID;
+        }
+        else if (level.equalsIgnoreCase("ERROR"))
+        {
+            return LogChute.ERROR_ID;
+        }
+        else if (level.equalsIgnoreCase("INFO"))
+        {
+            return LogChute.INFO_ID;
+        }
+        else if (level.equalsIgnoreCase("TRACE"))
+        {
+            return LogChute.TRACE_ID;
+        }
+        else if (level.equalsIgnoreCase("WARN"))
+        {
+            return LogChute.WARN_ID;
+        }
+        else 
+        {
+            return defaultLevel;
+        }
+    }
+    
+    public void log(int level, String message, Throwable t)
+    {
+        if (level >= logLevel)
+        {
+            String levelName;
+            levelName = getLevelName(level);
+            log.append(" [").append(levelName).append("] ");
+            log.append(message);
+            log.append("\n");
+            log.append(t.toString());
+            log.append("\n");
+        }
+    }
+
+    public boolean isLevelEnabled(int level)
+    {
+        return level >= logLevel;
+    }
+
+}

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/misc/TestLogChute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/misc/TestLogChute.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision