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.appender;
15  
16  import ch.qos.logback.core.Appender;
17  import ch.qos.logback.core.ConsoleAppender;
18  import ch.qos.logback.core.CoreConstants;
19  import ch.qos.logback.core.encoder.DummyEncoder;
20  import ch.qos.logback.core.encoder.EchoEncoder;
21  import ch.qos.logback.core.encoder.NopEncoder;
22  import ch.qos.logback.core.layout.DummyLayout;
23  import ch.qos.logback.core.status.Status;
24  import ch.qos.logback.core.testUtil.StatusChecker;
25  
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import java.io.PrintStream;
31  import java.io.UnsupportedEncodingException;
32  import java.nio.charset.Charset;
33  
34  import static org.junit.Assert.assertEquals;
35  import static org.junit.Assert.assertFalse;
36  
37  /**
38   * Redirecting System.out is quite messy. Disable this test in Maven bu not in Package.class
39   */
40  public class ConsoleAppenderTest extends AbstractAppenderTest<Object> {
41  
42      XTeeOutputStream tee;
43      PrintStream original;
44  
45      @Before
46      public void setUp() {
47          original = System.out;
48          // tee will output bytes on System out but it will also
49          // collect them so that the output can be compared against
50          // some expected output data
51          // tee = new TeeOutputStream(original);
52  
53          // keep the console quiet
54          tee = new XTeeOutputStream(null);
55  
56          // redirect System.out to tee
57          System.setOut(new PrintStream(tee));
58      }
59  
60      @After
61      public void tearDown() {
62          System.setOut(original);
63      }
64  
65      @Override
66      public Appender<Object> getAppender() {
67          return new ConsoleAppender<Object>();
68      }
69  
70      protected Appender<Object> getConfiguredAppender() {
71          ConsoleAppender<Object> ca = new ConsoleAppender<Object>();
72          ca.setEncoder(new NopEncoder<Object>());
73          ca.start();
74          return ca;
75      }
76  
77      @Test
78      public void smoke() {
79          ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
80          ca.setEncoder(new DummyEncoder<Object>());
81          ca.start();
82          ca.doAppend(new Object());
83          assertEquals(DummyLayout.DUMMY, tee.toString());
84      }
85  
86      @Test
87      public void open() {
88          ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
89          DummyEncoder<Object> dummyEncoder = new DummyEncoder<Object>();
90          dummyEncoder.setFileHeader("open");
91          ca.setEncoder(dummyEncoder);
92          ca.start();
93          ca.doAppend(new Object());
94          ca.stop();
95          assertEquals("open" + CoreConstants.LINE_SEPARATOR + DummyLayout.DUMMY, tee.toString());
96      }
97  
98      @Test
99      public void testClose() {
100         ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
101         DummyEncoder<Object> dummyEncoder = new DummyEncoder<Object>();
102         dummyEncoder.setFileFooter("CLOSED");
103         ca.setEncoder(dummyEncoder);
104         ca.start();
105         ca.doAppend(new Object());
106         ca.stop();
107         // ConsoleAppender must keep the underlying stream open.
108         // The console is not ours to close.
109         assertFalse(tee.isClosed());
110         assertEquals(DummyLayout.DUMMY + "CLOSED", tee.toString());
111     }
112 
113     // See http://jira.qos.ch/browse/LBCORE-143
114     @Test
115     public void changeInConsole() {
116         ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
117         EchoEncoder<Object> encoder = new EchoEncoder<Object>();
118         ca.setEncoder(encoder);
119         ca.start();
120         ca.doAppend("a");
121         assertEquals("a" + CoreConstants.LINE_SEPARATOR, tee.toString());
122 
123         XTeeOutputStream newTee = new XTeeOutputStream(null);
124         System.setOut(new PrintStream(newTee));
125         ca.doAppend("b");
126         assertEquals("b" + CoreConstants.LINE_SEPARATOR, newTee.toString());
127     }
128 
129     @Test
130     public void testUTF16BE() throws UnsupportedEncodingException {
131         ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
132         DummyEncoder<Object> dummyEncoder = new DummyEncoder<Object>();
133         Charset utf16BE = Charset.forName("UTF-16BE");
134         dummyEncoder.setCharset(utf16BE);
135         ca.setEncoder(dummyEncoder);
136         ca.start();
137         ca.doAppend(new Object());
138         assertEquals(DummyLayout.DUMMY, new String(tee.toByteArray(), utf16BE));
139     }
140 
141     @Test
142     public void wrongTarget() {
143         ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
144         EchoEncoder<Object> encoder = new EchoEncoder<Object>();
145         encoder.setContext(context);
146         ca.setContext(context);
147         ca.setTarget("foo");
148         ca.setEncoder(encoder);
149         ca.start();
150         ca.doAppend("a");
151         StatusChecker checker = new StatusChecker(context);
152         // 21:28:01,246 + WARN in ch.qos.logback.core.ConsoleAppender[null] - [foo] should be one of [System.out,
153         // System.err]
154         // 21:28:01,246 |-WARN in ch.qos.logback.core.ConsoleAppender[null] - Using previously set target, System.out by
155         // default.
156         // StatusPrinter.print(context);
157 
158         checker.assertContainsMatch(Status.WARN, "\\[foo\\] should be one of \\[System.out, System.err\\]");
159 
160     }
161 
162 }