001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core; 015 016import static org.junit.Assert.assertTrue; 017 018import java.io.ByteArrayOutputStream; 019 020import org.junit.After; 021import org.junit.Before; 022import org.junit.Test; 023 024import ch.qos.logback.core.encoder.LayoutWrappingEncoder; 025import ch.qos.logback.core.pattern.parser.SamplePatternLayout; 026 027public class OutputStreamAppenderTest { 028 029 Context context = new ContextBase(); 030 031 @Before 032 public void setUp() throws Exception { 033 } 034 035 @After 036 public void tearDown() throws Exception { 037 } 038 039 @Test 040 public void smoke() { 041 String FILE_HEADER = "FILE_HEADER "; 042 String PRESENTATION_HEADER = "PRESENTATION_HEADER"; 043 String PRESENTATION_FOOTER = "PRESENTATION_FOOTER "; 044 String FILE_FOOTER = "FILE_FOOTER"; 045 headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER); 046 } 047 048 @Test 049 public void nullFileHeader() { 050 String FILE_HEADER = null; 051 String PRESENTATION_HEADER = "PRESENTATION_HEADER"; 052 String PRESENTATION_FOOTER = "PRESENTATION_FOOTER "; 053 String FILE_FOOTER = "FILE_FOOTER"; 054 headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER); 055 } 056 057 @Test 058 public void nullPresentationHeader() { 059 String FILE_HEADER = "FILE_HEADER "; 060 String PRESENTATION_HEADER = null; 061 String PRESENTATION_FOOTER = "PRESENTATION_FOOTER "; 062 String FILE_FOOTER = "FILE_FOOTER"; 063 headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER); 064 } 065 066 @Test 067 public void nullPresentationFooter() { 068 String FILE_HEADER = "FILE_HEADER "; 069 String PRESENTATION_HEADER = "PRESENTATION_HEADER"; 070 String PRESENTATION_FOOTER = null; 071 String FILE_FOOTER = "FILE_FOOTER"; 072 headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER); 073 } 074 075 @Test 076 public void nullFileFooter() { 077 String FILE_HEADER = "FILE_HEADER "; 078 String PRESENTATION_HEADER = "PRESENTATION_HEADER"; 079 String PRESENTATION_FOOTER = "PRESENTATION_FOOTER "; 080 String FILE_FOOTER = null; 081 headerFooterCheck(FILE_HEADER, PRESENTATION_HEADER, PRESENTATION_FOOTER, FILE_FOOTER); 082 } 083 084 public void headerFooterCheck(String fileHeader, String presentationHeader, String presentationFooter, String fileFooter) { 085 OutputStreamAppender<Object> wa = new OutputStreamAppender<Object>(); 086 wa.setContext(context); 087 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 088 089 SamplePatternLayout<Object> spl = new SamplePatternLayout<Object>(); 090 spl.setContext(context); 091 092 spl.setFileHeader(fileHeader); 093 spl.setPresentationHeader(presentationHeader); 094 spl.setPresentationFooter(presentationFooter); 095 spl.setFileFooter(fileFooter); 096 097 spl.start(); 098 LayoutWrappingEncoder<Object> encoder = new LayoutWrappingEncoder<Object>(); 099 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}