View Javadoc
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.simple;
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.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.slf4j.LoggerFactoryFriend;
36  import org.junit.After;
37  import org.junit.Before;
38  import org.junit.Test;
39  
40  /**
41   * Tests that detecting logger name mismatches works and doesn't cause problems
42   * or trigger if disabled.
43   * <p>
44   * This test can't live inside slf4j-api because the NOP Logger doesn't
45   * remember its name.
46   *
47   * @author Alexander Dorokhine
48   * @author Ceki G&uuml;lc&uuml;
49   */
50  public class DetectLoggerNameMismatchTest {
51  
52      private static final String MISMATCH_STRING = "Detected logger name mismatch";
53  
54      static String NAME_OF_THIS_CLASS = DetectLoggerNameMismatchTest.class.getName();
55  
56      private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
57      private final PrintStream oldErr = System.err;
58  
59      @Before
60      public void setUp() {
61          System.setErr(new PrintStream(byteArrayOutputStream));
62      }
63  
64      @After
65      public void tearDown() {
66          setTrialEnabled(false);
67          System.setErr(oldErr);
68      }
69  
70      /*
71       * Pass in the wrong class to the Logger with the check disabled, and make sure there are no errors.
72       */
73      @Test
74      public void testNoTriggerWithoutProperty() {
75          setTrialEnabled(false);
76          Logger logger = LoggerFactory.getLogger(String.class);
77          assertEquals("java.lang.String", logger.getName());
78          assertMismatchDetected(false);
79      }
80  
81      /*
82       * Pass in the wrong class to the Logger with the check enabled, and make sure there ARE errors.
83       */
84      @Test
85      public void testTriggerWithProperty() {
86          setTrialEnabled(true);
87          LoggerFactory.getLogger(String.class);
88          assertMismatchDetected(true);
89      }
90  
91      /*
92       * Checks the whole error message to ensure all the names show up correctly.
93       */
94      @Test
95      public void testTriggerWholeMessage() {
96          setTrialEnabled(true);
97          LoggerFactory.getLogger(String.class);
98          boolean success = String.valueOf(byteArrayOutputStream)
99                          .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.
135 class ShapeBase {
136     public Logger logger = LoggerFactory.getLogger(getClass());
137 }
138 
139 class Square extends ShapeBase {
140 }