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ülcü
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 String s = String.valueOf(byteArrayOutputStream);
84 assertMismatchDetected(true);
85 }
86
87 /*
88 * Checks the whole error message to ensure all the names show up correctly.
89 */
90 @Test
91 public void testTriggerWholeMessage() {
92 setTrialEnabled(true);
93 LoggerFactory.getLogger(String.class);
94 boolean success = String.valueOf(byteArrayOutputStream).contains(
95 "Detected logger name mismatch. Given name: \"java.lang.String\"; " + "computed name: \"org.slf4j.DetectLoggerNameMismatchTest\".");
96 assertTrue("Actual value of byteArrayOutputStream: " + String.valueOf(byteArrayOutputStream), success);
97 }
98
99 /*
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.
131 class ShapeBase {
132 public Logger logger = LoggerFactory.getLogger(getClass());
133 }
134
135 class Square extends ShapeBase {
136 }