View Javadoc

1   /**
2    * Copyright (c) 2004-2012 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.impl;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertNull;
30  import static org.junit.Assert.assertTrue;
31  
32  import java.io.ByteArrayOutputStream;
33  import java.io.PrintStream;
34  
35  import org.junit.After;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  public class SimpleLoggerTest {
40  
41      String A_KEY = SimpleLogger.LOG_KEY_PREFIX + "a";
42      PrintStream original = System.out;
43      ByteArrayOutputStream bout = new ByteArrayOutputStream();
44      PrintStream replacement = new PrintStream(bout);
45  
46      @Before
47      public void before() {
48          System.setProperty(A_KEY, "info");
49      }
50  
51      @After
52      public void after() {
53          System.clearProperty(A_KEY);
54          System.clearProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY);
55          System.setErr(original);
56      }
57  
58      @Test
59      public void emptyLoggerName() {
60          SimpleLogger simpleLogger = new SimpleLogger("a");
61          assertEquals("info", simpleLogger.recursivelyComputeLevelString());
62      }
63  
64      @Test
65      public void offLevel() {
66          System.setProperty(A_KEY, "off");
67          SimpleLogger.init();
68          SimpleLogger simpleLogger = new SimpleLogger("a");
69          assertEquals("off", simpleLogger.recursivelyComputeLevelString());
70          assertFalse(simpleLogger.isErrorEnabled());
71      }
72  
73      @Test
74      public void loggerNameWithNoDots_WithLevel() {
75          SimpleLogger.init();
76          SimpleLogger simpleLogger = new SimpleLogger("a");
77  
78          assertEquals("info", simpleLogger.recursivelyComputeLevelString());
79      }
80  
81      @Test
82      public void loggerNameWithOneDotShouldInheritFromParent() {
83          SimpleLogger simpleLogger = new SimpleLogger("a.b");
84          assertEquals("info", simpleLogger.recursivelyComputeLevelString());
85      }
86  
87      @Test
88      public void loggerNameWithNoDots_WithNoSetLevel() {
89          SimpleLogger simpleLogger = new SimpleLogger("x");
90          assertNull(simpleLogger.recursivelyComputeLevelString());
91      }
92  
93      @Test
94      public void loggerNameWithOneDot_NoSetLevel() {
95          SimpleLogger simpleLogger = new SimpleLogger("x.y");
96          assertNull(simpleLogger.recursivelyComputeLevelString());
97      }
98  
99      @Test
100     public void checkUseOfLastSystemStreamReference() {
101         SimpleLogger.init();
102         SimpleLogger simpleLogger = new SimpleLogger(this.getClass().getName());
103 
104         System.setErr(replacement);
105         simpleLogger.info("hello");
106         replacement.flush();
107         assertTrue(bout.toString().contains("INFO org.slf4j.impl.SimpleLoggerTest - hello"));
108     }
109 
110     @Test
111     public void checkUseOfCachedOutputStream() {
112         System.setErr(replacement);
113         System.setProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY, "true");
114         SimpleLogger.init();
115         SimpleLogger simpleLogger = new SimpleLogger(this.getClass().getName());
116         // change reference to original before logging
117         System.setErr(original);
118 
119         simpleLogger.info("hello");
120         replacement.flush();
121         assertTrue(bout.toString().contains("INFO org.slf4j.impl.SimpleLoggerTest - hello"));
122     }
123 }