001/** 002 * Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland) 003 * All rights reserved. 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining 006 * a copy of this software and associated documentation files (the 007 * "Software"), to deal in the Software without restriction, including 008 * without limitation the rights to use, copy, modify, merge, publish, 009 * distribute, sublicense, and/or sell copies of the Software, and to 010 * permit persons to whom the Software is furnished to do so, subject to 011 * the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be 014 * included in all copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 021 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 023 * 024 */ 025package org.slf4j.simple; 026 027import static org.junit.Assert.assertEquals; 028import static org.junit.Assert.assertFalse; 029import static org.junit.Assert.assertNull; 030import static org.junit.Assert.assertTrue; 031 032import java.io.ByteArrayOutputStream; 033import java.io.PrintStream; 034import java.util.regex.Pattern; 035 036import org.junit.After; 037import org.junit.Before; 038import org.junit.Test; 039 040public class SimpleLoggerTest { 041 042 String A_KEY = SimpleLogger.LOG_KEY_PREFIX + "a"; 043 PrintStream original = System.out; 044 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 045 PrintStream replacement = new PrintStream(bout); 046 047 @Before 048 public void before() { 049 System.setProperty(A_KEY, "info"); 050 } 051 052 @After 053 public void after() { 054 System.clearProperty(A_KEY); 055 System.clearProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY); 056 System.clearProperty(SimpleLogger.SHOW_THREAD_ID_KEY); 057 System.clearProperty(SimpleLogger.SHOW_THREAD_NAME_KEY); 058 System.setErr(original); 059 } 060 061 @Test 062 public void emptyLoggerName() { 063 SimpleLogger simpleLogger = new SimpleLogger("a"); 064 assertEquals("info", simpleLogger.recursivelyComputeLevelString()); 065 } 066 067 @Test 068 public void offLevel() { 069 System.setProperty(A_KEY, "off"); 070 SimpleLogger.init(); 071 SimpleLogger simpleLogger = new SimpleLogger("a"); 072 assertEquals("off", simpleLogger.recursivelyComputeLevelString()); 073 assertFalse(simpleLogger.isErrorEnabled()); 074 } 075 076 @Test 077 public void loggerNameWithNoDots_WithLevel() { 078 SimpleLogger.init(); 079 SimpleLogger simpleLogger = new SimpleLogger("a"); 080 081 assertEquals("info", simpleLogger.recursivelyComputeLevelString()); 082 } 083 084 @Test 085 public void loggerNameWithOneDotShouldInheritFromParent() { 086 SimpleLogger simpleLogger = new SimpleLogger("a.b"); 087 assertEquals("info", simpleLogger.recursivelyComputeLevelString()); 088 } 089 090 @Test 091 public void loggerNameWithNoDots_WithNoSetLevel() { 092 SimpleLogger simpleLogger = new SimpleLogger("x"); 093 assertNull(simpleLogger.recursivelyComputeLevelString()); 094 } 095 096 @Test 097 public void loggerNameWithOneDot_NoSetLevel() { 098 SimpleLogger simpleLogger = new SimpleLogger("x.y"); 099 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}