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.bridge;
26  
27  import static org.junit.Assert.assertEquals;
28  
29  import java.text.MessageFormat;
30  import java.util.ResourceBundle;
31  import java.util.logging.Level;
32  
33  import org.apache.log4j.spi.LocationInfo;
34  import org.apache.log4j.spi.LoggingEvent;
35  import org.junit.After;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  public class SLF4JBridgeHandlerTest {
40  
41      static String LOGGER_NAME = "yay";
42  
43      ListAppender listAppender = new ListAppender();
44      org.apache.log4j.Logger log4jRoot;
45      java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("yay");
46  
47      @Before
48      public void setUp() throws Exception {
49          listAppender.extractLocationInfo = true;
50          log4jRoot = org.apache.log4j.Logger.getRootLogger();
51          log4jRoot.addAppender(listAppender);
52          log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
53      }
54  
55      @After
56      public void tearDown() throws Exception {
57          SLF4JBridgeHandler.uninstall();
58          log4jRoot.getLoggerRepository().resetConfiguration();
59      }
60  
61      @Test
62      public void testSmoke() {
63          SLF4JBridgeHandler.install();
64          String msg = "msg";
65          julLogger.info(msg);
66          assertEquals(1, listAppender.list.size());
67          LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
68          assertEquals(LOGGER_NAME, le.getLoggerName());
69          assertEquals(msg, le.getMessage());
70  
71          // get the location info in the event.
72          // Note that this must have been computed previously
73          // within an appender for the following assertion to
74          // work properly
75          LocationInfo li = le.getLocationInformation();
76          System.out.println(li.fullInfo);
77          assertEquals("SLF4JBridgeHandlerTest.java", li.getFileName());
78          assertEquals("testSmoke", li.getMethodName());
79      }
80  
81      @Test
82      public void testLevels() {
83          SLF4JBridgeHandler.install();
84          String msg = "msg";
85          julLogger.setLevel(Level.ALL);
86  
87          julLogger.finest(msg);
88          julLogger.finer(msg);
89          julLogger.fine(msg);
90          julLogger.info(msg);
91          julLogger.warning(msg);
92          julLogger.severe(msg);
93  
94          assertEquals(6, listAppender.list.size());
95          int i = 0;
96          assertLevel(i++, org.apache.log4j.Level.TRACE);
97          assertLevel(i++, org.apache.log4j.Level.DEBUG);
98          assertLevel(i++, org.apache.log4j.Level.DEBUG);
99          assertLevel(i++, org.apache.log4j.Level.INFO);
100         assertLevel(i++, org.apache.log4j.Level.WARN);
101         assertLevel(i++, org.apache.log4j.Level.ERROR);
102     }
103 
104     @Test
105     public void testLogWithResourceBundle() {
106         SLF4JBridgeHandler.install();
107 
108         String resourceBundleName = "org.slf4j.bridge.testLogStrings";
109         ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
110         String resourceKey = "resource_key";
111         String expectedMsg = bundle.getString(resourceKey);
112         String msg = resourceKey;
113 
114         java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("yay", resourceBundleName);
115 
116         julResourceBundleLogger.info(msg);
117         assertEquals(1, listAppender.list.size());
118         LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
119         assertEquals(LOGGER_NAME, le.getLoggerName());
120         assertEquals(expectedMsg, le.getMessage());
121     }
122 
123     @Test
124     public void testLogWithResourceBundleWithParameters() {
125         SLF4JBridgeHandler.install();
126 
127         String resourceBundleName = "org.slf4j.bridge.testLogStrings";
128         ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
129 
130         java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("foo", resourceBundleName);
131 
132         String resourceKey1 = "resource_key_1";
133         String expectedMsg1 = bundle.getString(resourceKey1);
134         julResourceBundleLogger.info(resourceKey1); // 1st log
135 
136         String resourceKey2 = "resource_key_2";
137         Object[] params2 = new Object[] { "foo", "bar" };
138         String expectedMsg2 = MessageFormat.format(bundle.getString(resourceKey2), params2);
139         julResourceBundleLogger.log(Level.INFO, resourceKey2, params2); // 2nd log
140 
141         String resourceKey3 = "invalidKey {0}";
142         Object[] params3 = new Object[] { "John" };
143         String expectedMsg3 = MessageFormat.format(resourceKey3, params3);
144         julResourceBundleLogger.log(Level.INFO, resourceKey3, params3); // 3rd log
145 
146         julLogger.log(Level.INFO, resourceKey3, params3); // 4th log
147 
148         assertEquals(4, listAppender.list.size());
149 
150         LoggingEvent le = null;
151 
152         le = (LoggingEvent) listAppender.list.get(0);
153         assertEquals("foo", le.getLoggerName());
154         assertEquals(expectedMsg1, le.getMessage());
155 
156         le = (LoggingEvent) listAppender.list.get(1);
157         assertEquals("foo", le.getLoggerName());
158         assertEquals(expectedMsg2, le.getMessage());
159 
160         le = (LoggingEvent) listAppender.list.get(2);
161         assertEquals("foo", le.getLoggerName());
162         assertEquals(expectedMsg3, le.getMessage());
163 
164         le = (LoggingEvent) listAppender.list.get(3);
165         assertEquals("yay", le.getLoggerName());
166         assertEquals(expectedMsg3, le.getMessage());
167     }
168 
169     @Test
170     public void testLogWithPlaceholderNoParameters() {
171         SLF4JBridgeHandler.install();
172         String msg = "msg {non-number-string}";
173         julLogger.logp(Level.INFO, "SLF4JBridgeHandlerTest", "testLogWithPlaceholderNoParameters", msg, new Object[0]);
174 
175         assertEquals(1, listAppender.list.size());
176         LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
177         assertEquals(LOGGER_NAME, le.getLoggerName());
178         assertEquals(msg, le.getMessage());
179     }
180 
181     // See http://jira.qos.ch/browse/SLF4J-337
182 
183     @Test
184     public void illFormattedInputShouldBeReturnedAsIs() {
185         SLF4JBridgeHandler.install();
186         String msg = "foo {18=bad} {0}";
187 
188         julLogger.log(Level.INFO, msg, "ignored parameter due to IllegalArgumentException");
189         assertEquals(1, listAppender.list.size());
190         LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
191         assertEquals(msg, le.getMessage());
192     }
193 
194     void assertLevel(int index, org.apache.log4j.Level expectedLevel) {
195         LoggingEvent le = (LoggingEvent) listAppender.list.get(index);
196         assertEquals(expectedLevel, le.getLevel());
197     }
198 }