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.io.PrintStream; 28 29 import junit.framework.TestCase; 30 31 /** 32 * Test whether invoking the SLF4J API causes problems or not. 33 * 34 * @author Ceki Gulcu 35 * 36 */ 37 public class InvocationTest extends TestCase { 38 39 PrintStream old = System.err; 40 41 public InvocationTest(String arg0) { 42 super(arg0); 43 } 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 System.setErr(new SilentPrintStream(old)); 48 } 49 50 protected void tearDown() throws Exception { 51 super.tearDown(); 52 System.setErr(old); 53 } 54 55 public void test1() { 56 Logger logger = LoggerFactory.getLogger("test1"); 57 logger.debug("Hello world."); 58 } 59 60 public void test2() { 61 Integer i1 = new Integer(1); 62 Integer i2 = new Integer(2); 63 Integer i3 = new Integer(3); 64 Exception e = new Exception("This is a test exception."); 65 Logger logger = LoggerFactory.getLogger("test2"); 66 67 logger.debug("Hello world 1."); 68 logger.debug("Hello world {}", i1); 69 logger.debug("val={} val={}", i1, i2); 70 logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 }); 71 72 logger.debug("Hello world 2", e); 73 logger.info("Hello world 2."); 74 75 logger.warn("Hello world 3."); 76 logger.warn("Hello world 3", e); 77 78 logger.error("Hello world 4."); 79 logger.error("Hello world {}", new Integer(3)); 80 logger.error("Hello world 4.", e); 81 } 82 83 // http://bugzilla.slf4j.org/show_bug.cgi?id=78 84 public void testNullParameter_BUG78() { 85 Logger logger = LoggerFactory.getLogger("testNullParameter_BUG78"); 86 String[] parameters = null; 87 String msg = "hello {}"; 88 logger.info(msg, parameters); 89 } 90 91 public void testNull() { 92 Logger logger = LoggerFactory.getLogger("testNull"); 93 logger.debug(null); 94 logger.info(null); 95 logger.warn(null); 96 logger.error(null); 97 98 Exception e = new Exception("This is a test exception."); 99 logger.debug(null, e); 100 logger.info(null, e); 101 logger.warn(null, e); 102 logger.error(null, e); 103 } 104 105 public void testMarker() { 106 Logger logger = LoggerFactory.getLogger("testMarker"); 107 Marker blue = MarkerFactory.getMarker("BLUE"); 108 logger.debug(blue, "hello"); 109 logger.info(blue, "hello"); 110 logger.warn(blue, "hello"); 111 logger.error(blue, "hello"); 112 113 logger.debug(blue, "hello {}", "world"); 114 logger.info(blue, "hello {}", "world"); 115 logger.warn(blue, "hello {}", "world"); 116 logger.error(blue, "hello {}", "world"); 117 118 logger.debug(blue, "hello {} and {} ", "world", "universe"); 119 logger.info(blue, "hello {} and {} ", "world", "universe"); 120 logger.warn(blue, "hello {} and {} ", "world", "universe"); 121 logger.error(blue, "hello {} and {} ", "world", "universe"); 122 } 123 124 public void testMDC() { 125 MDC.put("k", "v"); 126 assertNull(MDC.get("k")); 127 MDC.remove("k"); 128 assertNull(MDC.get("k")); 129 MDC.clear(); 130 } 131 } 132