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 java.io.ByteArrayOutputStream;
17  
18  import org.junit.jupiter.api.AfterEach;
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  
23  import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
24  import ch.qos.logback.core.pattern.parser.SamplePatternLayout;
25  
26  public class OutputStreamAppenderTest {
27  
28      Context context = new ContextBase();
29  
30      @BeforeEach
31      public void setUp() throws Exception {
32      }
33  
34      @AfterEach
35      public void tearDown() throws Exception {
36      }
37  
38      @Test
39      public void smoke() {
40          String FILE_HEADER = "FILE_HEADER ";
41          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
42          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
43          String FILE_FOOTER = "FILE_FOOTER";
44          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
45      }
46  
47      @Test
48      public void nullFileHeader() {
49          String FILE_HEADER = null;
50          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
51          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
52          String FILE_FOOTER = "FILE_FOOTER";
53          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
54      }
55  
56      @Test
57      public void nullPresentationHeader() {
58          String FILE_HEADER = "FILE_HEADER ";
59          String PRESENTATION_HEADER = null;
60          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
61          String FILE_FOOTER = "FILE_FOOTER";
62          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
63      }
64  
65      @Test
66      public void nullPresentationFooter() {
67          String FILE_HEADER = "FILE_HEADER ";
68          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
69          String PRESENTATION_FOOTER = null;
70          String FILE_FOOTER = "FILE_FOOTER";
71          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
72      }
73  
74      @Test
75      public void nullFileFooter() {
76          String FILE_HEADER = "FILE_HEADER ";
77          String PRESENTATION_HEADER = "PRESENTATION_HEADER";
78          String PRESENTATION_FOOTER = "PRESENTATION_FOOTER ";
79          String FILE_FOOTER = null;
80          headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER);
81      }
82  
83      public void headerFooterCheck(String fileHeader, String presentationHeader, String presentationFooter,
84              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         Assertions.assertTrue(result.startsWith(expectedHeader), result);
113 
114         String expectedFooter = emtptyIfNull(presentationFooter) + emtptyIfNull(fileFooter);
115         Assertions.assertTrue(result.endsWith(expectedFooter), result);
116     }
117 
118     String emtptyIfNull(String s) {
119         return s == null ? "" : s;
120     }
121 }