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.util; 015 016import static org.junit.Assert.*; 017 018import java.io.ByteArrayOutputStream; 019import java.io.PrintStream; 020 021import org.junit.After; 022import org.junit.Before; 023import org.junit.Test; 024 025import ch.qos.logback.core.Context; 026import ch.qos.logback.core.ContextBase; 027import ch.qos.logback.core.status.ErrorStatus; 028import ch.qos.logback.core.status.InfoStatus; 029import ch.qos.logback.core.status.Status; 030import ch.qos.logback.core.status.WarnStatus; 031 032public class StatusPrinterTest { 033 034 ByteArrayOutputStream outputStream; 035 PrintStream ps; 036 037 @Before 038 public void setUp() throws Exception { 039 outputStream = new ByteArrayOutputStream(); 040 ps = new PrintStream(outputStream); 041 StatusPrinter.setPrintStream(ps); 042 } 043 044 @After 045 public void tearDown() throws Exception { 046 StatusPrinter.setPrintStream(System.out); 047 ps = null; 048 outputStream = null; 049 } 050 051 @Test 052 public void testBasic() { 053 Context context = new ContextBase(); 054 context.getStatusManager().add(new InfoStatus("test", this)); 055 StatusPrinter.print(context); 056 String result = outputStream.toString(); 057 assertTrue(result.contains("|-INFO in " + this.getClass().getName())); 058 } 059 060 @Test 061 public void testNested() { 062 Status s0 = new ErrorStatus("test0", this); 063 Status s1 = new InfoStatus("test1", this); 064 Status s11 = new InfoStatus("test11", this); 065 Status s12 = new InfoStatus("test12", this); 066 s1.add(s11); 067 s1.add(s12); 068 069 Status s2 = new InfoStatus("test2", this); 070 Status s21 = new InfoStatus("test21", this); 071 Status s211 = new WarnStatus("test211", this); 072 073 Status s22 = new InfoStatus("test22", this); 074 s2.add(s21); 075 s2.add(s22); 076 s21.add(s211); 077 078 Context context = new ContextBase(); 079 context.getStatusManager().add(s0); 080 context.getStatusManager().add(s1); 081 context.getStatusManager().add(s2); 082 083 StatusPrinter.print(context); 084 String result = outputStream.toString(); 085 assertTrue(result.contains("+ INFO in " + this.getClass().getName())); 086 assertTrue(result.contains("+ WARN in " + this.getClass().getName())); 087 assertTrue(result.contains(" |-WARN in " + this.getClass().getName())); 088 } 089 090 @Test 091 public void testWithException() { 092 Status s0 = new ErrorStatus("test0", this); 093 Status s1 = new InfoStatus("test1", this, new Exception("testEx")); 094 Status s11 = new InfoStatus("test11", this); 095 Status s12 = new InfoStatus("test12", this); 096 s1.add(s11); 097 s1.add(s12); 098 099 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}