001/** 002 * Copyright (c) 2004-2012 QOS.ch 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.impl; 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; 034 035import org.junit.After; 036import org.junit.Before; 037import org.junit.Test; 038 039public class SimpleLoggerTest { 040 041 String A_KEY = SimpleLogger.LOG_KEY_PREFIX + "a"; 042 PrintStream original = System.out; 043 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 044 PrintStream replacement = new PrintStream(bout); 045 046 @Before 047 public void before() { 048 System.setProperty(A_KEY, "info"); 049 } 050 051 @After 052 public void after() { 053 System.clearProperty(A_KEY); 054 System.clearProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY); 055 System.setErr(original); 056 } 057 058 @Test 059 public void emptyLoggerName() { 060 SimpleLogger simpleLogger = new SimpleLogger("a"); 061 assertEquals("info", simpleLogger.recursivelyComputeLevelString()); 062 } 063 064 @Test 065 public void offLevel() { 066 System.setProperty(A_KEY, "off"); 067 SimpleLogger.init(); 068 SimpleLogger simpleLogger = new SimpleLogger("a"); 069 assertEquals("off", simpleLogger.recursivelyComputeLevelString()); 070 assertFalse(simpleLogger.isErrorEnabled()); 071 } 072 073 @Test 074 public void loggerNameWithNoDots_WithLevel() { 075 SimpleLogger.init(); 076 SimpleLogger simpleLogger = new SimpleLogger("a"); 077 078 assertEquals("info", simpleLogger.recursivelyComputeLevelString()); 079 } 080 081 @Test 082 public void loggerNameWithOneDotShouldInheritFromParent() { 083 SimpleLogger simpleLogger = new SimpleLogger("a.b"); 084 assertEquals("info", simpleLogger.recursivelyComputeLevelString()); 085 } 086 087 @Test 088 public void loggerNameWithNoDots_WithNoSetLevel() { 089 SimpleLogger simpleLogger = new SimpleLogger("x"); 090 assertNull(simpleLogger.recursivelyComputeLevelString()); 091 } 092 093 @Test 094 public void loggerNameWithOneDot_NoSetLevel() { 095 SimpleLogger simpleLogger = new SimpleLogger("x.y"); 096 assertNull(simpleLogger.recursivelyComputeLevelString()); 097 } 098 099 @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}