[logback-dev] svn commit: r1842 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core test/java/ch/qos/logback/core test/java/ch/qos/logback/core/appender

noreply.ceki at qos.ch noreply.ceki at qos.ch
Fri Oct 17 23:42:28 CEST 2008


Author: ceki
Date: Fri Oct 17 23:42:28 2008
New Revision: 1842

Added:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/PackageTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/WriterAppenderTest.java
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllCoreTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/ConsoleAppenderTest.java

Log:
Fixing LBCORE-55

In WriterAppender, getPresentationFooter and getFileFooter are now printed in the correct order, 
presentation footer first, file footer last.

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java	Fri Oct 17 23:42:28 2008
@@ -17,12 +17,12 @@
 import ch.qos.logback.core.status.ErrorStatus;
 
 /**
- * WriterAppender appends events to a hava.io.Writer. 
- * This class provides basic services that other appenders build upon.
+ * WriterAppender appends events to a hava.io.Writer. This class provides basic
+ * services that other appenders build upon.
+ * 
+ * For more information about this appender, please refer to the online manual
+ * at http://logback.qos.ch/manual/appenders.html#WriterAppender
  * 
- * For more information about this appender, please refer to the online manual at
- * http://logback.qos.ch/manual/appenders.html#WriterAppender
- *
  * @author Ceki Gülcü
  */
 public class WriterAppender<E> extends AppenderBase<E> {
@@ -97,16 +97,14 @@
   public void start() {
     int errors = 0;
     if (this.layout == null) {
-      addStatus(
-          new ErrorStatus("No layout set for the appender named \"" + name
-              + "\".", this));
+      addStatus(new ErrorStatus("No layout set for the appender named \""
+          + name + "\".", this));
       errors++;
     }
 
     if (this.writer == null) {
-      addStatus(
-          new ErrorStatus("No writer set for the appender named \"" + name
-              + "\".", this));
+      addStatus(new ErrorStatus("No writer set for the appender named \""
+          + name + "\".", this));
       errors++;
     }
     // only error free appenders should be activated
@@ -147,9 +145,8 @@
         this.writer.close();
         this.writer = null;
       } catch (IOException e) {
-        addStatus(
-            new ErrorStatus("Could not close writer for WriterAppener.", this,
-                e));
+        addStatus(new ErrorStatus("Could not close writer for WriterAppener.",
+            this, e));
       }
     }
   }
@@ -205,48 +202,48 @@
   }
 
   void writeHeader() {
-    if (layout != null) {
-      String h = layout.getFileHeader();
+    if (layout != null && (this.writer != null)) {
+      try {
 
-      if ((h != null) && (this.writer != null)) {
-        try {
+        String h = layout.getFileHeader();
+        if (h != null) {
           this.writer.write(h);
-          String ph = layout.getPresentationHeader();
-          if (ph != null) {
-            this.writer.write(ph);
-          }
-          // append a line separator. This should be useful in most cases and should 
-          // not hurt. 
+        }
+        String ph = layout.getPresentationHeader();
+        if (ph != null) {
+          this.writer.write(ph);
+        }
+        // If at least one of file header or presentation header were not null, then append a line separator. 
+        // This should be useful in most cases and should not hurt.
+        if ((h != null) || (ph != null)) {
           this.writer.write(Layout.LINE_SEP);
           this.writer.flush();
-        } catch (IOException ioe) {
-          this.started = false;
-          addStatus(new ErrorStatus(
-              "Failed to write header for appender named [" + name + "].",
-              this, ioe));
         }
+      } catch (IOException ioe) {
+        this.started = false;
+        addStatus(new ErrorStatus("Failed to write header for appender named ["
+            + name + "].", this, ioe));
       }
     }
   }
 
   void writeFooter() {
-    if (layout != null) {
-      String h = layout.getFileFooter();
-      if ((h != null) && (this.writer != null)) {
-        try {
+    if (layout != null && this.writer != null) {
+      try {
+        String pf = layout.getPresentationFooter();
+        if (pf != null) {
+          this.writer.write(pf);
+        }
+        String h = layout.getFileFooter();
+        if (h != null) {
           this.writer.write(h);
-          String pf = layout.getPresentationFooter();
-          if (pf != null) {
-            this.writer.write(pf);
-          }
-          // flushing is mandatory if the writer is not later closed.
-          this.writer.flush();
-        } catch (IOException ioe) {
-          this.started = false;
-          addStatus(new ErrorStatus(
-              "Failed to write footer for appender named [" + name + "].",
-              this, ioe));
         }
+        // flushing is mandatory if the writer is not later closed.
+        this.writer.flush();
+      } catch (IOException ioe) {
+        this.started = false;
+        addStatus(new ErrorStatus("Failed to write footer for appender named ["
+            + name + "].", this, ioe));
       }
     }
   }
@@ -258,7 +255,7 @@
    * be closed when the appender instance is closed.
    * 
    * @param writer
-   *          An already opened Writer.
+   *                An already opened Writer.
    */
   public synchronized void setWriter(Writer writer) {
     // close any previously opened writer

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllCoreTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllCoreTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllCoreTest.java	Fri Oct 17 23:42:28 2008
@@ -20,6 +20,7 @@
     suite.addTest(ch.qos.logback.core.util.PackageTest.suite());
     suite.addTest(ch.qos.logback.core.helpers.PackageTest.suite());
     suite.addTest(ch.qos.logback.core.pattern.PackageTest.suite());
+    suite.addTest(ch.qos.logback.core.PackageTest.suite());
     suite.addTest(ch.qos.logback.core.joran.PackageTest.suite());
     suite.addTest(ch.qos.logback.core.appender.PackageTest.suite());
     suite.addTest(ch.qos.logback.core.rolling.PackageTest.suite());

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/PackageTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/PackageTest.java	Fri Oct 17 23:42:28 2008
@@ -0,0 +1,15 @@
+package ch.qos.logback.core;
+
+import junit.framework.JUnit4TestAdapter;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class PackageTest extends TestCase {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTest(new JUnit4TestAdapter(WriterAppenderTest.class));
+    return suite;
+  }
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/WriterAppenderTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/WriterAppenderTest.java	Fri Oct 17 23:42:28 2008
@@ -0,0 +1,114 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package ch.qos.logback.core;
+
+
+import static org.junit.Assert.*;
+
+import java.io.StringWriter;
+import java.io.Writer;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import ch.qos.logback.core.pattern.parser.SamplePatternLayout;
+
+public class WriterAppenderTest {
+
+  Context context = new ContextBase();
+  
+  @Before
+  public void setUp() throws Exception {
+  }
+
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  @Test
+  public void smoke() {
+    String FILE_HEADER = "FILE_HEADER ";
+    String PRESENTATION_HEADER = "PRESENTATION_HEADER";
+    String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
+    String FILE_FOOTER = "FILE_FOOTER";
+    headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
+  }
+
+  @Test
+  public void nullFileHeader() {
+    String FILE_HEADER = null;
+    String PRESENTATION_HEADER = "PRESENTATION_HEADER";
+    String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
+    String FILE_FOOTER = "FILE_FOOTER";
+    headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
+  }
+
+  @Test
+  public void nullPresentationHeader() {
+    String FILE_HEADER = "FILE_HEADER ";
+    String PRESENTATION_HEADER = null;
+    String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
+    String FILE_FOOTER = "FILE_FOOTER";
+    headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
+  }
+
+  @Test
+  public void nullPresentationFooter() {
+    String FILE_HEADER = "FILE_HEADER ";
+    String PRESENTATION_HEADER =  "PRESENTATION_HEADER";
+    String PRESENTATION_FOOTER = null;
+    String FILE_FOOTER = "FILE_FOOTER";
+    headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
+  }
+  
+  @Test
+  public void nullFileFooter() {
+    String FILE_HEADER = "FILE_HEADER ";
+    String PRESENTATION_HEADER = "PRESENTATION_HEADER";
+    String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
+    String FILE_FOOTER = null;
+    headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
+  }
+  
+  public void headerFooterCheck(String fileHeader, String presentationHeader, String presentationFooter, String fileFooter) {
+    WriterAppender<Object> wa = new WriterAppender<Object>();
+    wa.setContext(context);
+    Writer sw = new StringWriter();
+ 
+    SamplePatternLayout<Object> spl = new SamplePatternLayout<Object>();
+    spl.setContext(context);
+  
+    spl.setFileHeader(fileHeader);
+    spl.setPresentationHeader(presentationHeader);
+    spl.setPresentationFooter(presentationFooter);
+    spl.setFileFooter(fileFooter);
+  
+    spl.start();
+    
+    wa.setLayout(spl);
+    wa.setWriter(sw);
+    wa.start();
+    
+    wa.stop();
+    String result = sw.toString();
+
+    String expectedHeader = emtptyIfNull(fileHeader) + emtptyIfNull(presentationHeader);
+    assertTrue(result, result.startsWith(expectedHeader));
+
+    String expectedFooter = emtptyIfNull(presentationFooter) + emtptyIfNull(fileFooter);
+    assertTrue(result, result.endsWith(expectedFooter));
+  }
+  
+  String emtptyIfNull(String s) {
+    return s == null ? "" : s;
+  }
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/ConsoleAppenderTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/ConsoleAppenderTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/ConsoleAppenderTest.java	Fri Oct 17 23:42:28 2008
@@ -85,12 +85,12 @@
   public void testClose() {
     ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
     DummyLayout<Object> dummyLayout = new DummyLayout<Object>();
-    dummyLayout.setFileFooter("closed");
+    dummyLayout.setFileFooter("CLOSED");
     ca.setLayout(dummyLayout);
     ca.start();
     ca.doAppend(new Object());
     ca.stop();
-    assertEquals(DummyLayout.DUMMY + "closed", tee.toString());
+    assertEquals(DummyLayout.DUMMY + "CLOSED", tee.toString());
   }
 
 


More information about the logback-dev mailing list