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 java.util.HashMap; 28 import java.util.Map; 29 30 import org.apache.log4j.spi.LoggingEvent; 31 32 import junit.framework.TestCase; 33 34 /** 35 * Test whether invoking the SLF4J API causes problems or not. 36 * 37 * @author Ceki Gulcu 38 * 39 */ 40 public class InvocationTest extends TestCase { 41 42 ListAppender listAppender = new ListAppender(); 43 org.apache.log4j.Logger root; 44 InvocationTest(String arg0)45 public InvocationTest(String arg0) { 46 super(arg0); 47 } 48 setUp()49 protected void setUp() throws Exception { 50 super.setUp(); 51 root = org.apache.log4j.Logger.getRootLogger(); 52 root.addAppender(listAppender); 53 54 } 55 tearDown()56 protected void tearDown() throws Exception { 57 super.tearDown(); 58 root.getLoggerRepository().resetConfiguration(); 59 } 60 test1()61 public void test1() { 62 Logger logger = LoggerFactory.getLogger("test1"); 63 logger.debug("Hello world."); 64 assertEquals(1, listAppender.list.size()); 65 } 66 test2()67 public void test2() { 68 Integer i1 = new Integer(1); 69 Integer i2 = new Integer(2); 70 Integer i3 = new Integer(3); 71 Exception e = new Exception("This is a test exception."); 72 Logger logger = LoggerFactory.getLogger("test2"); 73 74 logger.trace("Hello trace."); 75 76 logger.debug("Hello world 1."); 77 logger.debug("Hello world {}", i1); 78 logger.debug("val={} val={}", i1, i2); 79 logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 }); 80 81 logger.debug("Hello world 2", e); 82 logger.info("Hello world 2."); 83 84 logger.warn("Hello world 3."); 85 logger.warn("Hello world 3", e); 86 87 logger.error("Hello world 4."); 88 logger.error("Hello world {}", new Integer(3)); 89 logger.error("Hello world 4.", e); 90 assertEquals(11, listAppender.list.size()); 91 } 92 testNull()93 public void testNull() { 94 Logger logger = LoggerFactory.getLogger("testNull"); 95 logger.trace(null); 96 logger.debug(null); 97 logger.info(null); 98 logger.warn(null); 99 logger.error(null); 100 101 Exception e = new Exception("This is a test exception."); 102 logger.debug(null, e); 103 logger.info(null, e); 104 logger.warn(null, e); 105 logger.error(null, e); 106 assertEquals(8, listAppender.list.size()); 107 } 108 109 // http://bugzilla.slf4j.org/show_bug.cgi?id=78 testNullParameter_BUG78()110 public void testNullParameter_BUG78() { 111 Logger logger = LoggerFactory.getLogger("testNullParameter_BUG78"); 112 String[] parameters = null; 113 String msg = "hello {}"; 114 115 logger.debug(msg, parameters); 116 assertEquals(1, listAppender.list.size()); 117 LoggingEvent e = (LoggingEvent) listAppender.list.get(0); 118 assertEquals(msg, e.getMessage()); 119 } 120 testMarker()121 public void testMarker() { 122 Logger logger = LoggerFactory.getLogger("testMarker"); 123 Marker blue = MarkerFactory.getMarker("BLUE"); 124 logger.trace(blue, "hello"); 125 logger.debug(blue, "hello"); 126 logger.info(blue, "hello"); 127 logger.warn(blue, "hello"); 128 logger.error(blue, "hello"); 129 130 logger.debug(blue, "hello {}", "world"); 131 logger.info(blue, "hello {}", "world"); 132 logger.warn(blue, "hello {}", "world"); 133 logger.error(blue, "hello {}", "world"); 134 135 logger.debug(blue, "hello {} and {} ", "world", "universe"); 136 logger.info(blue, "hello {} and {} ", "world", "universe"); 137 logger.warn(blue, "hello {} and {} ", "world", "universe"); 138 logger.error(blue, "hello {} and {} ", "world", "universe"); 139 assertEquals(12, listAppender.list.size()); 140 } 141 testMDC()142 public void testMDC() { 143 MDC.put("k", "v"); 144 assertNotNull(MDC.get("k")); 145 assertEquals("v", MDC.get("k")); 146 147 MDC.remove("k"); 148 assertNull(MDC.get("k")); 149 150 MDC.put("k1", "v1"); 151 assertEquals("v1", MDC.get("k1")); 152 MDC.clear(); 153 assertNull(MDC.get("k1")); 154 155 try { 156 MDC.put(null, "x"); 157 fail("null keys are invalid"); 158 } catch (IllegalArgumentException e) { 159 } 160 } 161 testMDCContextMapValues()162 public void testMDCContextMapValues() { 163 Map<String, String> map = new HashMap<String, String>(); 164 map.put("ka", "va"); 165 map.put("kb", "vb"); 166 167 MDC.put("k", "v"); 168 assertEquals("v", MDC.get("k")); 169 MDC.setContextMap(map); 170 assertNull(MDC.get("k")); 171 assertEquals("va", MDC.get("ka")); 172 assertEquals("vb", MDC.get("kb")); 173 } 174 175 } 176