001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j;
026
027import static org.junit.Assert.assertNull;
028
029import java.io.PrintStream;
030
031import org.junit.After;
032import org.junit.Before;
033import org.junit.Test;
034
035/**
036 * Test whether invoking the SLF4J API causes problems or not.
037 * 
038 * @author Ceki Gulcu
039 *
040 */
041public class InvocationTest {
042
043    PrintStream old = System.err;
044
045    @Before
046    public void setUp() throws Exception {
047        System.setErr(new SilentPrintStream(old));
048    }
049
050    @After
051    public void tearDown() throws Exception {
052
053        System.setErr(old);
054    }
055
056    @Test
057    public void test1() {
058        Logger logger = LoggerFactory.getLogger("test1");
059        logger.debug("Hello world.");
060    }
061
062    @Test
063    public void test2() {
064        Integer i1 = new Integer(1);
065        Integer i2 = new Integer(2);
066        Integer i3 = new Integer(3);
067        Exception e = new Exception("This is a test exception.");
068        Logger logger = LoggerFactory.getLogger("test2");
069
070        logger.debug("Hello world 1.");
071        logger.debug("Hello world {}", i1);
072        logger.debug("val={} val={}", i1, i2);
073        logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
074
075        logger.debug("Hello world 2", e);
076        logger.info("Hello world 2.");
077
078        logger.warn("Hello world 3.");
079        logger.warn("Hello world 3", e);
080
081        logger.error("Hello world 4.");
082        logger.error("Hello world {}", new Integer(3));
083        logger.error("Hello world 4.", e);
084    }
085
086    // http://jira.qos.ch/browse/SLF4J-69
087    // formerly http://bugzilla.slf4j.org/show_bug.cgi?id=78
088    @Test
089    public void testNullParameter_BUG78() {
090        Logger logger = LoggerFactory.getLogger("testNullParameter_BUG78");
091        String[] parameters = null;
092        String msg = "hello {}";
093        logger.info(msg, (Object[]) parameters);
094    }
095
096    @Test
097    public void testNull() {
098        Logger logger = LoggerFactory.getLogger("testNull");
099        logger.debug(null);
100        logger.info(null);
101        logger.warn(null);
102        logger.error(null);
103
104        Exception e = new Exception("This is a test exception.");
105        logger.debug(null, e);
106        logger.info(null, e);
107        logger.warn(null, e);
108        logger.error(null, e);
109    }
110
111    @Test
112    public void testMarker() {
113        Logger logger = LoggerFactory.getLogger("testMarker");
114        Marker blue = MarkerFactory.getMarker("BLUE");
115        logger.debug(blue, "hello");
116        logger.info(blue, "hello");
117        logger.warn(blue, "hello");
118        logger.error(blue, "hello");
119
120        logger.debug(blue, "hello {}", "world");
121        logger.info(blue, "hello {}", "world");
122        logger.warn(blue, "hello {}", "world");
123        logger.error(blue, "hello {}", "world");
124
125        logger.debug(blue, "hello {} and {} ", "world", "universe");
126        logger.info(blue, "hello {} and {} ", "world", "universe");
127        logger.warn(blue, "hello {} and {} ", "world", "universe");
128        logger.error(blue, "hello {} and {} ", "world", "universe");
129    }
130
131    @Test
132    public void testMDC() {
133        MDC.put("k", "v");
134        assertNull(MDC.get("k"));
135        MDC.remove("k");
136        assertNull(MDC.get("k"));
137        MDC.clear();
138    }
139}