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 org.slf4j.implTest;
15  
16  import ch.qos.logback.classic.ClassicConstants;
17  import ch.qos.logback.classic.ClassicTestConstants;
18  import ch.qos.logback.core.CoreConstants;
19  import ch.qos.logback.core.status.NopStatusListener;
20  import ch.qos.logback.core.testUtil.RandomUtil;
21  import ch.qos.logback.core.testUtil.TeeOutputStream;
22  import org.junit.jupiter.api.AfterEach;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  import org.slf4j.LoggerFactoryFriend;
26  
27  import java.io.PrintStream;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  
31  /**
32   * @author Ceki Gülcü
33   */
34  public class InitializationOutputTest {
35  
36      int diff = RandomUtil.getPositiveInt();
37  
38      TeeOutputStream tee;
39      PrintStream original;
40  
41      @BeforeEach
42      public void setUp() {
43          original = System.out;
44          // tee will output bytes on System.out but it will also
45          // collect them so that the output can be compared against
46          // some expected output data
47  
48          // keep the console quiet
49          tee = new TeeOutputStream(null);
50  
51          // redirect System.out to tee
52          System.setOut(new PrintStream(tee));
53      }
54  
55      @AfterEach
56      public void tearDown() {
57          System.setOut(original);
58          System.clearProperty(ClassicConstants.CONFIG_FILE_PROPERTY);
59          System.clearProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY);
60      }
61  
62      @Test
63      public void noOutputIfContextHasAStatusListener() {
64          System.setProperty(ClassicConstants.CONFIG_FILE_PROPERTY,
65                  ClassicTestConstants.INPUT_PREFIX + "issue/logback292.xml");
66          System.setProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY, NopStatusListener.class.getName());
67  
68          LoggerFactoryFriend.reset();
69          assertEquals(0, tee.baos.size());
70      }
71  
72  }