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 */ 25package org.slf4j.bridge; 26 27import java.text.MessageFormat; 28import java.util.ResourceBundle; 29import java.util.logging.Level; 30 31import junit.framework.TestCase; 32 33import org.apache.log4j.spi.LocationInfo; 34import org.apache.log4j.spi.LoggingEvent; 35 36public 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.getLogger("yay"); 43 44 public SLF4JBridgeHandlerTest(String arg0) { 45 super(arg0); 46 } 47 48 protected void setUp() throws Exception { 49 super.setUp(); 50 listAppender.extractLocationInfo = true; 51 log4jRoot = org.apache.log4j.Logger.getRootLogger(); 52 log4jRoot.addAppender(listAppender); 53 log4jRoot.setLevel(org.apache.log4j.Level.TRACE); 54 } 55 56 protected void tearDown() throws Exception { 57 super.tearDown(); 58 SLF4JBridgeHandler.uninstall(); 59 log4jRoot.getLoggerRepository().resetConfiguration(); 60 } 61 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 public void testLevels() { 82 SLF4JBridgeHandler.install(); 83 String msg = "msg"; 84 julLogger.setLevel(Level.ALL); 85 86 julLogger.finest(msg); 87 julLogger.finer(msg); 88 julLogger.fine(msg); 89 julLogger.info(msg); 90 julLogger.warning(msg); 91 julLogger.severe(msg); 92 93 assertEquals(6, listAppender.list.size()); 94 int i = 0; 95 assertLevel(i++, org.apache.log4j.Level.TRACE); 96 assertLevel(i++, org.apache.log4j.Level.DEBUG); 97 assertLevel(i++, org.apache.log4j.Level.DEBUG); 98 assertLevel(i++, org.apache.log4j.Level.INFO); 99 assertLevel(i++, org.apache.log4j.Level.WARN); 100 assertLevel(i++, org.apache.log4j.Level.ERROR); 101 } 102 103 public void testLogWithResourceBundle() { 104 SLF4JBridgeHandler.install(); 105 106 String resourceBundleName = "org.slf4j.bridge.testLogStrings"; 107 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName); 108 String resourceKey = "resource_key"; 109 String expectedMsg = bundle.getString(resourceKey); 110 String msg = resourceKey; 111 112 java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("yay", resourceBundleName); 113 114 julResourceBundleLogger.info(msg); 115 assertEquals(1, listAppender.list.size()); 116 LoggingEvent le = (LoggingEvent) listAppender.list.get(0); 117 assertEquals(LOGGER_NAME, le.getLoggerName()); 118 assertEquals(expectedMsg, le.getMessage()); 119 } 120 121 public void testLogWithResourceBundleWithParameters() { 122 SLF4JBridgeHandler.install(); 123 124 String resourceBundleName = "org.slf4j.bridge.testLogStrings"; 125 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName); 126 127 java.util.logging.Logger julResourceBundleLogger = java.util.logging.Logger.getLogger("foo", resourceBundleName); 128 129 String resourceKey1 = "resource_key_1"; 130 String expectedMsg1 = bundle.getString(resourceKey1); 131 julResourceBundleLogger.info(resourceKey1); // 1st log 132 133 String resourceKey2 = "resource_key_2"; 134 Object[] params2 = new Object[] { "foo", "bar" }; 135 String expectedMsg2 = MessageFormat.format(bundle.getString(resourceKey2), params2); 136 julResourceBundleLogger.log(Level.INFO, resourceKey2, params2); // 2nd log 137 138 String resourceKey3 = "invalidKey {0}"; 139 Object[] params3 = new Object[] { "John" }; 140 String expectedMsg3 = MessageFormat.format(resourceKey3, params3); 141 julResourceBundleLogger.log(Level.INFO, resourceKey3, params3); // 3rd log 142 143 julLogger.log(Level.INFO, resourceKey3, params3); // 4th log 144 145 assertEquals(4, listAppender.list.size()); 146 147 LoggingEvent le = null; 148 149 le = (LoggingEvent) listAppender.list.get(0); 150 assertEquals("foo", le.getLoggerName()); 151 assertEquals(expectedMsg1, le.getMessage()); 152 153 le = (LoggingEvent) listAppender.list.get(1); 154 assertEquals("foo", le.getLoggerName()); 155 assertEquals(expectedMsg2, le.getMessage()); 156 157 le = (LoggingEvent) listAppender.list.get(2); 158 assertEquals("foo", le.getLoggerName()); 159 assertEquals(expectedMsg3, le.getMessage()); 160 161 le = (LoggingEvent) listAppender.list.get(3); 162 assertEquals("yay", le.getLoggerName()); 163 assertEquals(expectedMsg3, le.getMessage()); 164 } 165 166 public void testLogWithPlaceholderNoParameters() { 167 SLF4JBridgeHandler.install(); 168 String msg = "msg {non-number-string}"; 169 julLogger.logp(Level.INFO, "SLF4JBridgeHandlerTest", "testLogWithPlaceholderNoParameters", msg, new Object[0]); 170 171 assertEquals(1, listAppender.list.size()); 172 LoggingEvent le = (LoggingEvent) listAppender.list.get(0); 173 assertEquals(LOGGER_NAME, le.getLoggerName()); 174 assertEquals(msg, le.getMessage()); 175 } 176 177 void assertLevel(int index, org.apache.log4j.Level expectedLevel) { 178 LoggingEvent le = (LoggingEvent) listAppender.list.get(index); 179 assertEquals(expectedLevel, le.getLevel()); 180 } 181} 182