[LOGBack-dev] svn commit: r567 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/pattern test/java/ch/qos/logback/classic/pattern

noreply.seb at qos.ch noreply.seb at qos.ch
Tue Sep 12 17:33:31 CEST 2006


Author: seb
Date: Tue Sep 12 17:33:31 2006
New Revision: 567

Added:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MDCConverterTest.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java

Log:
- corrected MDCConverter output format
- added test class

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/MDCConverter.java	Tue Sep 12 17:33:31 2006
@@ -38,7 +38,7 @@
     if (key == null) {
       // if no key is specified, return all the
       // values present in the MDC, separated with a single space.
-      StringBuffer buf = new StringBuffer("{");
+      StringBuffer buf = new StringBuffer();
       Set<String> keys = mdcPropertyMap.keySet();
       Iterator it = keys.iterator();
       String tmpKey;
@@ -47,7 +47,10 @@
         tmpKey = (String)it.next();
         tmpValue = (String)mdcPropertyMap.get(tmpKey);
         //format: {testeKey=testValue, testKey2=testValue2}
-        buf.append(tmpKey).append('=').append(tmpValue).append(", ");
+        buf.append(tmpKey).append('=').append(tmpValue);
+        if (it.hasNext()) {
+          buf.append(", ");
+        }
       }
       return buf.toString();
     }

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MDCConverterTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/pattern/MDCConverterTest.java	Tue Sep 12 17:33:31 2006
@@ -0,0 +1,45 @@
+package ch.qos.logback.classic.pattern;
+
+import junit.framework.TestCase;
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.MDC;
+import ch.qos.logback.classic.spi.LoggingEvent;
+
+public class MDCConverterTest extends TestCase {
+  
+  LoggerContext lc;
+  MDCConverter converter;
+  
+  public void setUp() throws Exception {
+    lc = new LoggerContext();
+    converter = new MDCConverter();
+    converter.start();
+  }
+  
+  public void tearDown() throws Exception {
+    lc = null;
+    converter.stop();
+    converter = null;
+  }
+  
+  public void testConverWithOneEntry() {
+    MDC.put("testKey", "testValue");
+    LoggingEvent le = createLoggingEvent();
+    String result = converter.convert(le);
+    assertEquals("testKey=testValue", result);
+  }
+
+  public void testConverWithMultipleEntries() {
+    MDC.put("testKey2", "testValue2");
+    LoggingEvent le = createLoggingEvent();
+    String result = converter.convert(le);
+    assertEquals("testKey=testValue, testKey2=testValue2", result);
+  }
+  
+  private LoggingEvent createLoggingEvent() {
+    LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(LoggerContext.ROOT_NAME),
+        Level.DEBUG, "test message", null, null);
+    return le;
+  }
+}



More information about the logback-dev mailing list