001/** 002 * Copyright (c) 2004-2011 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.simple; 026 027import static org.junit.Assert.assertEquals; 028import static org.junit.Assert.assertTrue; 029 030import java.io.ByteArrayOutputStream; 031import java.io.PrintStream; 032 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035import org.slf4j.LoggerFactoryFriend; 036import org.junit.After; 037import org.junit.Before; 038import org.junit.Test; 039 040/** 041 * Tests that detecting logger name mismatches works and doesn't cause problems 042 * or trigger if disabled. 043 * <p> 044 * This test can't live inside slf4j-api because the NOP Logger doesn't 045 * remember its name. 046 * 047 * @author Alexander Dorokhine 048 * @author Ceki Gülcü 049 */ 050public class DetectLoggerNameMismatchTest { 051 052 private static final String MISMATCH_STRING = "Detected logger name mismatch"; 053 054 static String NAME_OF_THIS_CLASS = DetectLoggerNameMismatchTest.class.getName(); 055 056 private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 057 private final PrintStream oldErr = System.err; 058 059 @Before 060 public void setUp() { 061 System.setErr(new PrintStream(byteArrayOutputStream)); 062 } 063 064 @After 065 public void tearDown() { 066 setTrialEnabled(false); 067 System.setErr(oldErr); 068 } 069 070 /* 071 * Pass in the wrong class to the Logger with the check disabled, and make sure there are no errors. 072 */ 073 @Test 074 public void testNoTriggerWithoutProperty() { 075 setTrialEnabled(false); 076 Logger logger = LoggerFactory.getLogger(String.class); 077 assertEquals("java.lang.String", logger.getName()); 078 assertMismatchDetected(false); 079 } 080 081 /* 082 * Pass in the wrong class to the Logger with the check enabled, and make sure there ARE errors. 083 */ 084 @Test 085 public void testTriggerWithProperty() { 086 setTrialEnabled(true); 087 LoggerFactory.getLogger(String.class); 088 assertMismatchDetected(true); 089 } 090 091 /* 092 * Checks the whole error message to ensure all the names show up correctly. 093 */ 094 @Test 095 public void testTriggerWholeMessage() { 096 setTrialEnabled(true); 097 LoggerFactory.getLogger(String.class); 098 boolean success = String.valueOf(byteArrayOutputStream) 099 .contains("Detected logger name mismatch. Given name: \"java.lang.String\"; " + "computed name: \"" + NAME_OF_THIS_CLASS + "\"."); 100 assertTrue("Actual value of byteArrayOutputStream: " + String.valueOf(byteArrayOutputStream), success); 101 } 102 103 /* 104 * Checks that there are no errors with the check enabled if the class matches. 105 */ 106 @Test 107 public void testPassIfMatch() { 108 setTrialEnabled(true); 109 Logger logger = LoggerFactory.getLogger(DetectLoggerNameMismatchTest.class); 110 assertEquals(DetectLoggerNameMismatchTest.class.getName(), logger.getName()); 111 assertMismatchDetected(false); 112 } 113 114 private void assertMismatchDetected(boolean mismatchDetected) { 115 assertEquals(mismatchDetected, String.valueOf(byteArrayOutputStream).contains(MISMATCH_STRING)); 116 } 117 118 @Test 119 public void verifyLoggerDefinedInBaseWithOverridenGetClassMethod() { 120 setTrialEnabled(true); 121 Square square = new Square(); 122 assertEquals(Square.class.getName(), square.logger.getName()); 123 assertMismatchDetected(false); 124 } 125 126 private static void setTrialEnabled(boolean enabled) { 127 // The system property is read into a static variable at initialization time 128 // so we cannot just reset the system property to test this feature. 129 // Therefore we set the variable directly. 130 LoggerFactoryFriend.setDetectLoggerNameMismatch(enabled); 131 } 132} 133 134// Used for testing that inheritance is ignored by the checker. 135class ShapeBase { 136 public Logger logger = LoggerFactory.getLogger(getClass()); 137} 138 139class Square extends ShapeBase { 140}