[logback-dev] svn commit: r1317 - in logback/trunk/log4j-bridge/src: main/java/org/apache/log4j test/java/org/apache/log4j

noreply.seb at qos.ch noreply.seb at qos.ch
Thu Feb 15 15:44:03 CET 2007


Author: seb
Date: Thu Feb 15 15:44:03 2007
New Revision: 1317

Added:
   logback/trunk/log4j-bridge/src/test/java/org/apache/log4j/DummyObject.java
   logback/trunk/log4j-bridge/src/test/java/org/apache/log4j/LoggerTest.java
Removed:
   logback/trunk/log4j-bridge/src/test/java/org/apache/log4j/InvokeLog4jTest.java
Modified:
   logback/trunk/log4j-bridge/src/main/java/org/apache/log4j/Category.java

Log:
Changes related to bug #45
A prepareMessage(Object message): String method has been added to the Category class to check 
whether the message is null or not and to take care of the casting.
The InvokeLog4jTest class has been removed and replaced by a LoggerTest class that tests the same things, plus the
changes related to bug #45. Compared to the old test class, the new class tests all this silently.


Modified: logback/trunk/log4j-bridge/src/main/java/org/apache/log4j/Category.java
==============================================================================
--- logback/trunk/log4j-bridge/src/main/java/org/apache/log4j/Category.java	(original)
+++ logback/trunk/log4j-bridge/src/main/java/org/apache/log4j/Category.java	Thu Feb 15 15:44:03 2007
@@ -139,7 +139,7 @@
    * in addition, the call is marked with a marker named "TRACE".
    */
   public void trace(Object message) {
-    lbLogger.debug(TRACE_MARKER, (String) message);
+    lbLogger.debug(TRACE_MARKER, prepareMessage(message));
   }
 
   /**
@@ -147,7 +147,7 @@
    * method of logback in addition, the call is marked with a marker named "TRACE".
    */
   public void trace(Object message, Throwable t) {
-    lbLogger.debug(TRACE_MARKER, (String) message, t);
+    lbLogger.debug(TRACE_MARKER, prepareMessage(message), t);
   }
   
   /**
@@ -155,7 +155,7 @@
    * method of logback in addition, the call is marked with a marker named "TRACE".
    */
   public void trace(Object message, Object o) {
-    lbLogger.debug(TRACE_MARKER, (String)message, o);
+    lbLogger.debug(TRACE_MARKER, prepareMessage(message), o);
   }
 
   /**
@@ -173,7 +173,7 @@
   public void debug(Object message) {
     // casting to String as SLF4J only accepts String instances, not Object
     // instances.
-    lbLogger.debug((String) message);
+    lbLogger.debug(prepareMessage(message));
   }
 
   /**
@@ -181,7 +181,7 @@
    * method of logback.
    */
   public void debug(Object message, Throwable t) {
-    lbLogger.debug((String) message, t);
+    lbLogger.debug(prepareMessage(message), t);
   }
 
   /**
@@ -189,7 +189,7 @@
    * method of logback.
    */
   public void debug(Object message, Object o) {
-    lbLogger.debug((String)message, o);
+    lbLogger.debug(prepareMessage(message), o);
   }
   
   /**
@@ -205,7 +205,7 @@
    * method of logback.
    */
   public void info(Object message) {
-    lbLogger.info((String) message);
+    lbLogger.info(prepareMessage(message));
   }
 
   /**
@@ -213,7 +213,7 @@
    * method of logback.
    */
   public void info(Object message, Throwable t) {
-    lbLogger.info((String) message, t);
+    lbLogger.info(prepareMessage(message), t);
   }
 
   /**
@@ -221,7 +221,7 @@
    * method of logback.
    */
   public void info(Object message, Object o) {
-    lbLogger.info((String) message, o);
+    lbLogger.info(prepareMessage(message), o);
   }  
   
   /**
@@ -237,7 +237,7 @@
    * method of logback.
    */
   public void warn(Object message) {
-    lbLogger.warn((String) message);
+    lbLogger.warn(prepareMessage(message));
   }
 
   /**
@@ -245,7 +245,7 @@
    * method of logback.
    */
   public void warn(Object message, Throwable t) {
-    lbLogger.warn((String) message, t);
+    lbLogger.warn(prepareMessage(message), t);
   }
 
   /**
@@ -253,7 +253,7 @@
    * method of logback.
    */
   public void warn(Object message, Object o) {
-    lbLogger.warn((String)message, o);
+    lbLogger.warn(prepareMessage(message), o);
   }
   
   /**
@@ -269,7 +269,7 @@
    * method of logback.
    */
   public void error(Object message) {
-    lbLogger.error((String) message);
+    lbLogger.error(prepareMessage(message));
   }
 
   /**
@@ -277,7 +277,7 @@
    * method of logback.
    */
   public void error(Object message, Throwable t) {
-    lbLogger.error((String) message, t);
+    lbLogger.error(prepareMessage(message), t);
   }
 
   /**
@@ -285,7 +285,7 @@
    * method of logback.
    */
   public void error(Object message, Object o) {
-    lbLogger.error((String)message, o);
+    lbLogger.error(prepareMessage(message), o);
   }
   
   /**
@@ -301,7 +301,7 @@
    * method of logback.
    */
   public void fatal(Object message) {
-    lbLogger.error(FATAL_MARKER, (String) message);
+    lbLogger.error(FATAL_MARKER, prepareMessage(message));
   }
 
   /**
@@ -309,7 +309,7 @@
    * method of logback in addition, the call is marked with a marker named "FATAL".
    */
   public void fatal(Object message, Throwable t) {
-    lbLogger.error(FATAL_MARKER, (String) message, t);
+    lbLogger.error(FATAL_MARKER, prepareMessage(message), t);
   }
 
   /**
@@ -317,7 +317,7 @@
    * method of logback in addition, the call is marked with a marker named "FATAL".
    */
   public void fatal(Object message, Object o) {
-    lbLogger.error(FATAL_MARKER, (String)message, o);
+    lbLogger.error(FATAL_MARKER, prepareMessage(message), o);
   } 
   
   /**
@@ -349,5 +349,13 @@
       throw new IllegalStateException("Unknown Priority " + p);
     }
   }
+  
+  private String prepareMessage(Object message) {
+    if (message == null) {
+      return (String)message;
+    } else {
+      return message.toString();
+    }
+  }
 
 }

Added: logback/trunk/log4j-bridge/src/test/java/org/apache/log4j/DummyObject.java
==============================================================================
--- (empty file)
+++ logback/trunk/log4j-bridge/src/test/java/org/apache/log4j/DummyObject.java	Thu Feb 15 15:44:03 2007
@@ -0,0 +1,8 @@
+package org.apache.log4j;
+
+public class DummyObject {
+
+  public String toString() {
+    return "dummy";
+  }
+}

Added: logback/trunk/log4j-bridge/src/test/java/org/apache/log4j/LoggerTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/log4j-bridge/src/test/java/org/apache/log4j/LoggerTest.java	Thu Feb 15 15:44:03 2007
@@ -0,0 +1,147 @@
+package org.apache.log4j;
+
+import junit.framework.TestCase;
+
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+
+/**
+ * A class that tests the invocation of the org.apache.log4j.Logger class that
+ * belongs to the log4j-bridge package
+ * 
+ * @author Sébastien Pennec
+ * @author Ceki Gülcü
+ */
+
+public class LoggerTest extends TestCase {
+
+  LoggerContext context;
+  ListAppender<LoggingEvent> appender;
+  ch.qos.logback.classic.Logger logbackLogger;
+  org.apache.log4j.Logger log4jLogger;
+
+  public void setUp() {
+    context = (LoggerContext) LoggerFactory.getILoggerFactory();
+    context.shutdownAndReset();
+    appender = new ListAppender<LoggingEvent>();
+    appender.setContext(context);
+    appender.setName("listAppender");
+    appender.start();
+    ch.qos.logback.classic.Logger lbLogger = context
+        .getLogger(LoggerContext.ROOT_NAME);
+    lbLogger.addAppender(appender);
+
+    log4jLogger = org.apache.log4j.Logger.getLogger(LoggerTest.class);
+    logbackLogger = context.getLogger(LoggerTest.class);
+  }
+
+  public void tearDown() {
+    appender.stop();
+    context.stop();
+    appender = null;
+    context = null;
+    logbackLogger = null;
+    log4jLogger = null;
+  }
+
+  public void testLogWithObjectMessages() {
+    LoggingEvent event;
+
+    log4jLogger.debug("test");
+    event = appender.list.get(0);
+    assertEquals("test", event.getMessage());
+    appender.list.clear();
+
+    log4jLogger.debug(null);
+    event = appender.list.get(0);
+    assertEquals(null, event.getMessage());
+    appender.list.clear();
+
+    DummyObject dummy = new DummyObject();
+    log4jLogger.debug(dummy);
+    event = appender.list.get(0);
+    assertEquals(dummy.toString(), event.getMessage());
+    appender.list.clear();
+  }
+
+  public void testIsEnabledAPI() {
+    assertTrue(log4jLogger.isTraceEnabled());
+    assertTrue(log4jLogger.isDebugEnabled());
+    assertTrue(log4jLogger.isInfoEnabled());
+    assertTrue(log4jLogger.isWarnEnabled());
+    assertTrue(log4jLogger.isErrorEnabled());
+  }
+
+  public void testPrintAPI() {
+    Exception e = new Exception("just testing");
+
+    log4jLogger.debug(null);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.debug("debug message");
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.info(null);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.info("info  message");
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.warn(null);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.warn("warn message");
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.error(null);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.error("error message");
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.debug(null, e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.debug("debug message", e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.info(null, e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.info("info  message", e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.warn(null, e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.warn("warn message", e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.error(null, e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+    log4jLogger.error("error message", e);
+    assertEquals(1, appender.list.size());
+    appender.list.clear();
+
+  }
+
+}



More information about the logback-dev mailing list