Home | History | Annotate | Download | only in slf4j
      1 /**
      2  * Copyright (c) 2004-2011 QOS.ch
      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;
     26 
     27 import static org.junit.Assert.assertEquals;
     28 import static org.junit.Assert.assertTrue;
     29 
     30 import java.io.ByteArrayOutputStream;
     31 import java.io.PrintStream;
     32 
     33 import org.junit.After;
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 
     37 /**
     38  * Tests that detecting logger name mismatches works and doesn't cause problems
     39  * or trigger if disabled.
     40  * <p>
     41  * This test can't live inside slf4j-api because the NOP Logger doesn't
     42  * remember its name.
     43  *
     44  * @author Alexander Dorokhine
     45  * @author Ceki G&uuml;lc&uuml;
     46  */
     47 public class DetectLoggerNameMismatchTest {
     48 
     49     private static final String MISMATCH_STRING = "Detected logger name mismatch";
     50 
     51     private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     52     private final PrintStream oldErr = System.err;
     53 
     54     @Before
     55     public void setUp() {
     56         System.setErr(new PrintStream(byteArrayOutputStream));
     57     }
     58 
     59     @After
     60     public void tearDown() {
     61         setTrialEnabled(false);
     62         System.setErr(oldErr);
     63     }
     64 
     65     /*
     66      * Pass in the wrong class to the Logger with the check disabled, and make sure there are no errors.
     67      */
     68     @Test
     69     public void testNoTriggerWithoutProperty() {
     70         setTrialEnabled(false);
     71         Logger logger = LoggerFactory.getLogger(String.class);
     72         assertEquals("java.lang.String", logger.getName());
     73         assertMismatchDetected(false);
     74     }
     75 
     76     /*
     77      * Pass in the wrong class to the Logger with the check enabled, and make sure there ARE errors.
     78      */
     79     @Test
     80     public void testTriggerWithProperty() {
     81         setTrialEnabled(true);
     82         LoggerFactory.getLogger(String.class);
     83         assertMismatchDetected(true);
     84     }
     85 
     86     /*
     87      * Checks the whole error message to ensure all the names show up correctly.
     88      */
     89     @Test
     90     public void testTriggerWholeMessage() {
     91         setTrialEnabled(true);
     92         LoggerFactory.getLogger(String.class);
     93         assertTrue("Actual value of byteArrayOutputStream: " + String.valueOf(byteArrayOutputStream), String.valueOf(byteArrayOutputStream).contains(
     94                         "Detected logger name mismatch. Given name: \"java.lang.String\"; " + "computed name: \"org.slf4j.DetectLoggerNameMismatchTest\"."));
     95     }
     96 
     97     /*
     98      * Checks that there are no errors with the check enabled if the class matches.
     99      */
    100     @Test
    101     public void testPassIfMatch() {
    102         setTrialEnabled(true);
    103         Logger logger = LoggerFactory.getLogger(DetectLoggerNameMismatchTest.class);
    104         assertEquals("org.slf4j.DetectLoggerNameMismatchTest", logger.getName());
    105         assertMismatchDetected(false);
    106     }
    107 
    108     private void assertMismatchDetected(boolean mismatchDetected) {
    109         assertEquals(mismatchDetected, String.valueOf(byteArrayOutputStream).contains(MISMATCH_STRING));
    110     }
    111 
    112     @Test
    113     public void verifyLoggerDefinedInBaseWithOverridenGetClassMethod() {
    114         setTrialEnabled(true);
    115         Square square = new Square();
    116         assertEquals("org.slf4j.Square", square.logger.getName());
    117         assertMismatchDetected(false);
    118     }
    119 
    120     private static void setTrialEnabled(boolean enabled) {
    121         // The system property is read into a static variable at initialization time
    122         // so we cannot just reset the system property to test this feature.
    123         // Therefore we set the variable directly.
    124         LoggerFactory.DETECT_LOGGER_NAME_MISMATCH = enabled;
    125     }
    126 }
    127 
    128 // Used for testing that inheritance is ignored by the checker.
    129 class ShapeBase {
    130     public Logger logger = LoggerFactory.getLogger(getClass());
    131 }
    132 
    133 class Square extends ShapeBase {
    134 }
    135