1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    *
4    * Copyright (C) 1999-2006, QOS.ch
5    *
6    * This library is free software, you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation.
9    */
10  package ch.qos.logback.core.appender;
11  
12  import static org.junit.Assert.assertEquals;
13  
14  import java.io.PrintStream;
15  import java.io.UnsupportedEncodingException;
16  
17  import org.junit.After;
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  import ch.qos.logback.core.AppenderBase;
22  import ch.qos.logback.core.ConsoleAppender;
23  import ch.qos.logback.core.CoreConstants;
24  import ch.qos.logback.core.layout.DummyLayout;
25  import ch.qos.logback.core.layout.NopLayout;
26  import ch.qos.logback.core.util.TeeOutputStream;
27  
28  
29  public class ConsoleAppenderTest extends AbstractAppenderTest<Object> {
30  
31    TeeOutputStream tee;
32    PrintStream original;
33  
34  
35    @Before
36    public void setUp() throws Exception {
37      original = System.out;
38      // tee will output bytes on System out but it will also
39      // collect them so that the output can be compared against
40      // some expected output data
41      // tee = new TeeOutputStream(original);
42      
43      // keep the console quiet
44      tee = new TeeOutputStream(null);
45      
46      // redirect System.out to tee
47      System.setOut(new PrintStream(tee));
48    }
49  
50    @After
51    public void tearDown() throws Exception {
52      System.setOut(original);
53    }
54  
55    @Override
56    public AppenderBase<Object> getAppender() {
57      return new ConsoleAppender<Object>();
58    } 
59  
60    protected AppenderBase<Object> getConfiguredAppender() {
61      ConsoleAppender<Object> ca = new ConsoleAppender<Object>();
62      ca.setLayout(new NopLayout<Object>());
63      ca.start();
64      return ca;
65    }
66  
67    @org.junit.Test
68    public void testBasic() {
69      ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
70      ca.setLayout(new DummyLayout<Object>());
71      ca.start();
72      ca.doAppend(new Object());
73      assertEquals(DummyLayout.DUMMY, tee.toString());
74    }
75    
76    @org.junit.Test
77    public void testOpen() {
78      ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
79      DummyLayout<Object> dummyLayout = new DummyLayout<Object>();
80      dummyLayout.setFileHeader("open");
81      ca.setLayout(dummyLayout);
82      ca.start();
83      ca.doAppend(new Object());
84      ca.stop();
85      assertEquals("open"+CoreConstants.LINE_SEPARATOR+DummyLayout.DUMMY, tee.toString());
86    }
87    
88    @Test
89    public void testClose() {
90      ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
91      DummyLayout<Object> dummyLayout = new DummyLayout<Object>();
92      dummyLayout.setFileFooter("CLOSED");
93      ca.setLayout(dummyLayout);
94      ca.start();
95      ca.doAppend(new Object());
96      ca.stop();
97      assertEquals(DummyLayout.DUMMY + "CLOSED", tee.toString());
98    }
99  
100 
101 
102   @Test  
103   public void testUTF16BE() throws UnsupportedEncodingException {
104     ConsoleAppender<Object> ca = (ConsoleAppender<Object>) getAppender();
105     ca.setLayout(new DummyLayout<Object>());
106     String encodingName = "UTF-16BE";
107     ca.setEncoding(encodingName);
108     ca.start();
109     ca.doAppend(new Object());
110 
111     assertEquals(DummyLayout.DUMMY, new String(tee.toByteArray(), encodingName));
112   }
113 
114 
115 }