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;
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
67     * make sure there are no errors.
68     */
69    @Test
70    public void testNoTriggerWithoutProperty() {
71      setTrialEnabled(false);
72      Logger logger = LoggerFactory.getLogger(String.class);
73      assertEquals("java.lang.String", logger.getName());
74      assertMismatchDetected(false);
75    }
76  
77    /*
78     * Pass in the wrong class to the Logger with the check enabled, and
79     * make sure there ARE errors.
80     */
81    @Test
82    public void testTriggerWithProperty() {
83      setTrialEnabled(true);
84      LoggerFactory.getLogger(String.class);
85      assertMismatchDetected(true);
86    }
87  
88    /*
89     * Checks the whole error message to ensure all the names show up correctly.
90     */
91    @Test
92    public void testTriggerWholeMessage() {
93      setTrialEnabled(true);
94      LoggerFactory.getLogger(String.class);
95      assertTrue(
96        "Actual value of byteArrayOutputStream: " + String.valueOf(byteArrayOutputStream),
97        String.valueOf(byteArrayOutputStream).contains(
98          "Detected logger name mismatch. Given name: \"java.lang.String\"; " +
99          "computed name: \"org.slf4j.DetectLoggerNameMismatchTest\"."));
100   }
101 
102   /*
103    * Checks that there are no errors with the check enabled if the
104    * class matches.
105    */
106   @Test
107   public void testPassIfMatch() {
108     setTrialEnabled(true);
109     Logger logger = LoggerFactory.getLogger(DetectLoggerNameMismatchTest.class);
110     assertEquals("org.slf4j.DetectLoggerNameMismatchTest", logger.getName());
111     assertMismatchDetected(false);
112   }
113 
114   private void assertMismatchDetected(boolean mismatchDetected) {
115     assertEquals(mismatchDetected,
116         String.valueOf(byteArrayOutputStream).contains(MISMATCH_STRING));
117   }
118 
119   @Test
120   public void verifyLoggerDefinedInBaseWithOverridenGetClassMethod() {
121     setTrialEnabled(true);
122     Square square = new Square();
123     assertEquals("org.slf4j.Square", square.logger.getName());
124     assertMismatchDetected(false);
125   }
126 
127   private static void setTrialEnabled(boolean enabled) {
128     // The system property is read into a static variable at initialization time
129     // so we cannot just reset the system property to test this feature.
130     // Therefore we set the variable directly.
131     LoggerFactory.DETECT_LOGGER_NAME_MISMATCH = enabled;
132   }
133 }
134 
135 // Used for testing that inheritance is ignored by the checker.
136 class ShapeBase {
137   public Logger logger = LoggerFactory.getLogger(getClass());
138 }
139 
140 class Square extends ShapeBase {}