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.util;
15  
16  import java.io.ByteArrayOutputStream;
17  import java.io.PrintStream;
18  
19  import org.junit.jupiter.api.AfterEach;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  
23  import ch.qos.logback.core.Context;
24  import ch.qos.logback.core.ContextBase;
25  import ch.qos.logback.core.status.ErrorStatus;
26  import ch.qos.logback.core.status.InfoStatus;
27  import ch.qos.logback.core.status.Status;
28  import ch.qos.logback.core.status.WarnStatus;
29  
30  import static org.junit.jupiter.api.Assertions.assertTrue;
31  
32  public class StatusPrinterTest {
33  
34      ByteArrayOutputStream outputStream;
35      PrintStream ps;
36  
37      @BeforeEach
38      public void setUp() throws Exception {
39          outputStream = new ByteArrayOutputStream();
40          ps = new PrintStream(outputStream);
41          StatusPrinter.setPrintStream(ps);
42      }
43  
44      @AfterEach
45      public void tearDown() throws Exception {
46          StatusPrinter.setPrintStream(System.out);
47          ps = null;
48          outputStream = null;
49      }
50  
51      @Test
52      public void testBasic() {
53          Context context = new ContextBase();
54          context.getStatusManager().add(new InfoStatus("test", this));
55          StatusPrinter.print(context);
56          String result = outputStream.toString();
57          assertTrue(result.contains("|-INFO in " + this.getClass().getName()));
58      }
59  
60      @Test
61      public void testNested() {
62          Status s0 = new ErrorStatus("test0", this);
63          Status s1 = new InfoStatus("test1", this);
64          Status s11 = new InfoStatus("test11", this);
65          Status s12 = new InfoStatus("test12", this);
66          s1.add(s11);
67          s1.add(s12);
68  
69          Status s2 = new InfoStatus("test2", this);
70          Status s21 = new InfoStatus("test21", this);
71          Status s211 = new WarnStatus("test211", this);
72  
73          Status s22 = new InfoStatus("test22", this);
74          s2.add(s21);
75          s2.add(s22);
76          s21.add(s211);
77  
78          Context context = new ContextBase();
79          context.getStatusManager().add(s0);
80          context.getStatusManager().add(s1);
81          context.getStatusManager().add(s2);
82  
83          StatusPrinter.print(context);
84          String result = outputStream.toString();
85          assertTrue(result.contains("+ INFO in " + this.getClass().getName()));
86          assertTrue(result.contains("+ WARN in " + this.getClass().getName()));
87          assertTrue(result.contains("    |-WARN in " + this.getClass().getName()));
88      }
89  
90      @Test
91      public void testWithException() {
92          Status s0 = new ErrorStatus("test0", this);
93          Status s1 = new InfoStatus("test1", this, new Exception("testEx"));
94          Status s11 = new InfoStatus("test11", this);
95          Status s12 = new InfoStatus("test12", this);
96          s1.add(s11);
97          s1.add(s12);
98  
99          Status s2 = new InfoStatus("test2", this);
100         Status s21 = new InfoStatus("test21", this);
101         Status s211 = new WarnStatus("test211", this);
102 
103         Status s22 = new InfoStatus("test22", this);
104         s2.add(s21);
105         s2.add(s22);
106         s21.add(s211);
107 
108         Context context = new ContextBase();
109         context.getStatusManager().add(s0);
110         context.getStatusManager().add(s1);
111         context.getStatusManager().add(s2);
112         StatusPrinter.print(context);
113         String result = outputStream.toString();
114         assertTrue(result.contains("|-ERROR in " + this.getClass().getName()));
115         assertTrue(result.contains("+ INFO in " + this.getClass().getName()));
116         assertTrue(result.contains("ch.qos.logback.core.util.StatusPrinterTest.testWithException"));
117     }
118 
119 }