[slf4j-dev] svn commit: r1032 - slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge

ceki at slf4j.org ceki at slf4j.org
Sat May 31 00:10:15 CEST 2008


Author: ceki
Date: Sat May 31 00:10:15 2008
New Revision: 1032

Added:
   slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/ListAppender.java
   slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java

Log:
Damn: I hope it goes right this time!

- restoring missing test files
- added test for level mapping

Added: slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/ListAppender.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/ListAppender.java	Sat May 31 00:10:15 2008
@@ -0,0 +1,29 @@
+package org.slf4j.bridge;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.spi.LoggingEvent;
+
+public class ListAppender extends AppenderSkeleton {
+
+  public List list = new ArrayList();
+  
+  public boolean extractLocationInfo = false;
+  
+  protected void append(LoggingEvent event) {
+    list.add(event);
+    if(extractLocationInfo) {
+      event.getLocationInformation();
+    }
+  }
+
+  public void close() {
+  }
+
+  public boolean requiresLayout() {
+    return false;
+  }
+
+}

Added: slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/jul-to-slf4j/src/test/java/org/slf4j/bridge/SLF4JBridgeHandlerTest.java	Sat May 31 00:10:15 2008
@@ -0,0 +1,82 @@
+package org.slf4j.bridge;
+
+import java.util.logging.Level;
+
+import org.apache.log4j.spi.LocationInfo;
+import org.apache.log4j.spi.LoggingEvent;
+import org.slf4j.bridge.SLF4JBridgeHandler;
+
+import junit.framework.TestCase;
+
+public class SLF4JBridgeHandlerTest extends TestCase {
+
+  static String LOGGER_NAME = "yay";
+
+  ListAppender listAppender = new ListAppender();
+  org.apache.log4j.Logger log4jRoot;
+  java.util.logging.Logger julLogger = java.util.logging.Logger
+      .getLogger("yay");
+
+  public SLF4JBridgeHandlerTest(String arg0) {
+    super(arg0);
+  }
+
+  protected void setUp() throws Exception {
+    super.setUp();
+    listAppender.extractLocationInfo = true;
+    log4jRoot = org.apache.log4j.Logger.getRootLogger();
+    log4jRoot.addAppender(listAppender);
+    log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+    log4jRoot.getLoggerRepository().resetConfiguration();
+  }
+
+  public void testSmoke() {
+    SLF4JBridgeHandler.install();
+    String msg = "msg";
+    julLogger.info(msg);
+    assertEquals(1, listAppender.list.size());
+    LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
+    assertEquals(LOGGER_NAME, le.getLoggerName());
+    assertEquals(msg, le.getMessage());
+
+    // get the location info in the event.
+    // Note that this must have been computed previously
+    // within an appender for the following assertion to
+    // work properly
+    LocationInfo li = le.getLocationInformation();
+    System.out.println(li.fullInfo);
+    assertEquals("SLF4JBridgeHandlerTest.java", li.getFileName());
+    assertEquals("testSmoke", li.getMethodName());
+  }
+
+  public void testLevels() {
+    SLF4JBridgeHandler.install();
+    String msg = "msg";
+    julLogger.setLevel(Level.ALL);
+
+    julLogger.finest(msg);
+    julLogger.finer(msg);
+    julLogger.fine(msg);
+    julLogger.info(msg);
+    julLogger.warning(msg);
+    julLogger.severe(msg);
+
+    assertEquals(6, listAppender.list.size());
+    int i = 0;
+    assertLevel(i++, org.apache.log4j.Level.TRACE);
+    assertLevel(i++, org.apache.log4j.Level.DEBUG);
+    assertLevel(i++, org.apache.log4j.Level.DEBUG);
+    assertLevel(i++, org.apache.log4j.Level.INFO);
+    assertLevel(i++, org.apache.log4j.Level.WARN);
+    assertLevel(i++, org.apache.log4j.Level.ERROR);
+  }
+
+  void assertLevel(int index, org.apache.log4j.Level expectedLevel) {
+    LoggingEvent le = (LoggingEvent) listAppender.list.get(index);
+    assertEquals(expectedLevel, le.getLevel());
+  }
+}



More information about the slf4j-dev mailing list