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 java.text.MessageFormat;
28  import java.util.ResourceBundle;
29  import java.util.logging.Level;
30  
31  import junit.framework.TestCase;
32  
33  import org.apache.log4j.spi.LocationInfo;
34  import org.apache.log4j.spi.LoggingEvent;
35  
36  public class SLF4JBridgeHandlerTest extends TestCase {
37  
38    static String LOGGER_NAME = "yay";
39  
40    ListAppender listAppender = new ListAppender();
41    org.apache.log4j.Logger log4jRoot;
42    java.util.logging.Logger julLogger = java.util.logging.Logger
43        .getLogger("yay");
44  
45    public SLF4JBridgeHandlerTest(String arg0) {
46      super(arg0);
47    }
48  
49    protected void setUp() throws Exception {
50      super.setUp();
51      listAppender.extractLocationInfo = true;
52      log4jRoot = org.apache.log4j.Logger.getRootLogger();
53      log4jRoot.addAppender(listAppender);
54      log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
55    }
56  
57    protected void tearDown() throws Exception {
58      super.tearDown();
59      SLF4JBridgeHandler.uninstall();
60      log4jRoot.getLoggerRepository().resetConfiguration();
61    }
62  
63    public void testSmoke() {
64      SLF4JBridgeHandler.install();
65      String msg = "msg";
66      julLogger.info(msg);
67      assertEquals(1, listAppender.list.size());
68      LoggingEvent le = (LoggingEvent) listAppender.list.get(0);
69      assertEquals(LOGGER_NAME, le.getLoggerName());
70      assertEquals(msg, le.getMessage());
71  
72      // get the location info in the event.
73      // Note that this must have been computed previously
74      // within an appender for the following assertion to
75      // work properly
76      LocationInfo li = le.getLocationInformation();
77      System.out.println(li.fullInfo);
78      assertEquals("SLF4JBridgeHandlerTest.java", li.getFileName());
79      assertEquals("testSmoke", li.getMethodName());
80    }
81  
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   public void testLogWithResourceBundle() {
105     SLF4JBridgeHandler.install();
106 
107     String resourceBundleName = "org.slf4j.bridge.testLogStrings";
108     ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
109     String resourceKey = "resource_key";
110     String expectedMsg = bundle.getString(resourceKey);
111     String msg = resourceKey;
112 
113     java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger
114         .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   public void testLogWithResourceBundleWithParameters() {
124     SLF4JBridgeHandler.install();
125 
126     String resourceBundleName = "org.slf4j.bridge.testLogStrings";
127     ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName);
128 
129     java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger
130         .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),
139         params2);
140     julResourceBundleLogger.log(Level.INFO, resourceKey2, params2); // 2nd log
141 
142     
143     String resourceKey3 = "invalidKey {0}";
144     Object[] params3 = new Object[] { "John" };
145     String expectedMsg3 = MessageFormat.format(resourceKey3, params3);
146     julResourceBundleLogger.log(Level.INFO, resourceKey3, params3); // 3rd log
147 
148     julLogger.log(Level.INFO, resourceKey3, params3);  // 4th log
149 
150     assertEquals(4, listAppender.list.size());
151 
152     LoggingEvent le = null;
153 
154     le = (LoggingEvent) listAppender.list.get(0);
155     assertEquals("foo", le.getLoggerName());
156     assertEquals(expectedMsg1, le.getMessage());
157 
158     le = (LoggingEvent) listAppender.list.get(1);
159     assertEquals("foo", le.getLoggerName());
160     assertEquals(expectedMsg2, le.getMessage());
161 
162     le = (LoggingEvent) listAppender.list.get(2);
163     assertEquals("foo", le.getLoggerName());
164     assertEquals(expectedMsg3, le.getMessage());
165 
166     le = (LoggingEvent) listAppender.list.get(3);
167     assertEquals("yay", le.getLoggerName());
168     assertEquals(expectedMsg3, le.getMessage());
169   }
170 
171   void assertLevel(int index, org.apache.log4j.Level expectedLevel) {
172     LoggingEvent le = (LoggingEvent) listAppender.list.get(index);
173     assertEquals(expectedLevel, le.getLevel());
174   }
175 }