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