View Javadoc
1   /**
2    * Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland)
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.simple;
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  import java.util.regex.Pattern;
35  
36  import org.junit.After;
37  import org.junit.Before;
38  import org.junit.Test;
39  
40  public class SimpleLoggerTest {
41  
42      String A_KEY = SimpleLogger.LOG_KEY_PREFIX + "a";
43      PrintStream original = System.out;
44      ByteArrayOutputStream bout = new ByteArrayOutputStream();
45      PrintStream replacement = new PrintStream(bout);
46  
47      @Before
48      public void before() {
49          System.setProperty(A_KEY, "info");
50      }
51  
52      @After
53      public void after() {
54          System.clearProperty(A_KEY);
55          System.clearProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY);
56          System.clearProperty(SimpleLogger.SHOW_THREAD_ID_KEY);
57          System.clearProperty(SimpleLogger.SHOW_THREAD_NAME_KEY);
58          System.setErr(original);
59      }
60  
61      @Test
62      public void emptyLoggerName() {
63          SimpleLogger simpleLogger = new SimpleLogger("a");
64          assertEquals("info", simpleLogger.recursivelyComputeLevelString());
65      }
66  
67      @Test
68      public void offLevel() {
69          System.setProperty(A_KEY, "off");
70          SimpleLogger.init();
71          SimpleLogger simpleLogger = new SimpleLogger("a");
72          assertEquals("off", simpleLogger.recursivelyComputeLevelString());
73          assertFalse(simpleLogger.isErrorEnabled());
74      }
75  
76      @Test
77      public void loggerNameWithNoDots_WithLevel() {
78          SimpleLogger.init();
79          SimpleLogger simpleLogger = new SimpleLogger("a");
80  
81          assertEquals("info", simpleLogger.recursivelyComputeLevelString());
82      }
83  
84      @Test
85      public void loggerNameWithOneDotShouldInheritFromParent() {
86          SimpleLogger simpleLogger = new SimpleLogger("a.b");
87          assertEquals("info", simpleLogger.recursivelyComputeLevelString());
88      }
89  
90      @Test
91      public void loggerNameWithNoDots_WithNoSetLevel() {
92          SimpleLogger simpleLogger = new SimpleLogger("x");
93          assertNull(simpleLogger.recursivelyComputeLevelString());
94      }
95  
96      @Test
97      public void loggerNameWithOneDot_NoSetLevel() {
98          SimpleLogger simpleLogger = new SimpleLogger("x.y");
99          assertNull(simpleLogger.recursivelyComputeLevelString());
100     }
101 
102     @Test
103     public void checkUseOfLastSystemStreamReference() {
104         SimpleLogger.init();
105         SimpleLogger simpleLogger = new SimpleLogger(this.getClass().getName());
106 
107         System.setErr(replacement);
108         simpleLogger.info("hello");
109         replacement.flush();
110         assertTrue(bout.toString().contains("INFO " + this.getClass().getName() + " - hello"));
111     }
112 
113     @Test
114     public void checkUseOfCachedOutputStream() {
115         System.setErr(replacement);
116         System.setProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY, "true");
117         SimpleLogger.init();
118         SimpleLogger simpleLogger = new SimpleLogger(this.getClass().getName());
119         // change reference to original before logging
120         System.setErr(original);
121 
122         simpleLogger.info("hello");
123         replacement.flush();
124         assertTrue(bout.toString().contains("INFO " + this.getClass().getName() + " - hello"));
125     }
126 
127     @Test
128     public void testTheadIdWithoutThreadName() {
129         System.setProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, Boolean.FALSE.toString());
130         String patternStr = "^tid=\\d{1,12} INFO org.slf4j.simple.SimpleLoggerTest - hello";
131         commonTestThreadId(patternStr);
132     }
133 
134     @Test
135     public void testThreadId() {
136         String patternStr = "^\\[.*\\] tid=\\d{1,12} INFO org.slf4j.simple.SimpleLoggerTest - hello";
137         commonTestThreadId(patternStr);
138     }
139 
140     private void commonTestThreadId(String patternStr) {
141         System.setErr(replacement);
142         System.setProperty(SimpleLogger.SHOW_THREAD_ID_KEY, Boolean.TRUE.toString());
143         SimpleLogger.init();
144         SimpleLogger simpleLogger = new SimpleLogger(this.getClass().getName());
145         simpleLogger.info("hello");
146         replacement.flush();
147         String output = bout.toString();
148         System.out.println(patternStr);
149         System.out.println(output);
150         assertTrue(Pattern.compile(patternStr).matcher(output).lookingAt());
151     }
152 }