View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.ByteArrayOutputStream;
19  
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
25  import ch.qos.logback.core.pattern.parser.SamplePatternLayout;
26  
27  public class OutputStreamAppenderTest {
28  
29      Context context = new ContextBase();
30  
31      @Before
32      public void setUp() throws Exception {
33      }
34  
35      @After
36      public void tearDown() throws Exception {
37      }
38  
39      @Test
40      public void smoke() {
41          String FILE_HEADER = "FILE_HEADER ";
42          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
43          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
44          String FILE_FOOTER = "FILE_FOOTER";
45          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
46      }
47  
48      @Test
49      public void nullFileHeader() {
50          String FILE_HEADER = null;
51          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
52          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
53          String FILE_FOOTER = "FILE_FOOTER";
54          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
55      }
56  
57      @Test
58      public void nullPresentationHeader() {
59          String FILE_HEADER = "FILE_HEADER ";
60          String PRESENTATION_HEADER = null;
61          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
62          String FILE_FOOTER = "FILE_FOOTER";
63          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
64      }
65  
66      @Test
67      public void nullPresentationFooter() {
68          String FILE_HEADER = "FILE_HEADER ";
69          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
70          String PRESENTATION_FOOTER = null;
71          String FILE_FOOTER = "FILE_FOOTER";
72          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
73      }
74  
75      @Test
76      public void nullFileFooter() {
77          String FILE_HEADER = "FILE_HEADER ";
78          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
79          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
80          String FILE_FOOTER = null;
81          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
82      }
83  
84      public void headerFooterCheck(String fileHeader, String presentationHeader, String presentationFooter, String fileFooter) {
85          OutputStreamAppender<Object> wa = new OutputStreamAppender<Object>();
86          wa.setContext(context);
87          ByteArrayOutputStream baos = new ByteArrayOutputStream();
88  
89          SamplePatternLayout<Object> spl = new SamplePatternLayout<Object>();
90          spl.setContext(context);
91  
92          spl.setFileHeader(fileHeader);
93          spl.setPresentationHeader(presentationHeader);
94          spl.setPresentationFooter(presentationFooter);
95          spl.setFileFooter(fileFooter);
96  
97          spl.start();
98          LayoutWrappingEncoder<Object> encoder = new LayoutWrappingEncoder<Object>();
99          encoder.setLayout(spl);
100         encoder.setContext(context);
101 
102         wa.setEncoder(encoder);
103         wa.setOutputStream(baos);
104         wa.start();
105 
106         wa.stop();
107         String result = baos.toString();
108 
109         String expectedHeader = emtptyIfNull(fileHeader) + emtptyIfNull(presentationHeader);
110 
111         System.out.println(result);
112         assertTrue(result, result.startsWith(expectedHeader));
113 
114         String expectedFooter = emtptyIfNull(presentationFooter) + emtptyIfNull(fileFooter);
115         assertTrue(result, result.endsWith(expectedFooter));
116     }
117 
118     String emtptyIfNull(String s) {
119         return s == null ? "" : s;
120     }
121 }