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.simple;
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;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036import org.slf4j.MDC;
037import org.slf4j.Marker;
038import org.slf4j.MarkerFactory;
039
040/**
041 * Test whether invoking the SLF4J API causes problems or not.
042 * 
043 * @author Ceki Gulcu
044 *
045 */
046public class InvocationTest {
047
048    PrintStream old = System.err;
049
050    @Before
051    public void setUp() throws Exception {
052        System.setErr(new SilentPrintStream(old));
053    }
054
055    @After
056    public void tearDown() throws Exception {
057        System.setErr(old);
058    }
059
060    @Test
061    public void test1() {
062        Logger logger = LoggerFactory.getLogger("test1");
063        logger.debug("Hello world.");
064    }
065
066    @Test
067    public void test2() {
068        Integer i1 = Integer.valueOf(1);
069        Integer i2 = Integer.valueOf(2);
070        Integer i3 = Integer.valueOf(3);
071        Exception e = new Exception("This is a test exception.");
072        Logger logger = LoggerFactory.getLogger("test2");
073
074        logger.debug("Hello world 1.");
075        logger.debug("Hello world {}", i1);
076        logger.debug("val={} val={}", i1, i2);
077        logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
078
079        logger.debug("Hello world 2", e);
080        logger.info("Hello world 2.");
081
082        logger.warn("Hello world 3.");
083        logger.warn("Hello world 3", e);
084
085        logger.error("Hello world 4.");
086        logger.error("Hello world {}", Integer.valueOf(3));
087        logger.error("Hello world 4.", e);
088    }
089
090    // http://jira.qos.ch/browse/SLF4J-69
091    // formerly http://bugzilla.slf4j.org/show_bug.cgi?id=78
092    @Test
093    public void testNullParameter_BUG78() {
094        Logger logger = LoggerFactory.getLogger("testNullParameter_BUG78");
095        String[] parameters = null;
096        String msg = "hello {}";
097        logger.info(msg, (Object[]) parameters);
098    }
099
100    @Test
101    public void testNull() {
102        Logger logger = LoggerFactory.getLogger("testNull");
103        logger.debug(null);
104        logger.info(null);
105        logger.warn(null);
106        logger.error(null);
107
108        Exception e = new Exception("This is a test exception.");
109        logger.debug(null, e);
110        logger.info(null, e);
111        logger.warn(null, e);
112        logger.error(null, e);
113    }
114
115    @Test
116    public void testMarker() {
117        Logger logger = LoggerFactory.getLogger("testMarker");
118        Marker blue = MarkerFactory.getMarker("BLUE");
119        logger.debug(blue, "hello");
120        logger.info(blue, "hello");
121        logger.warn(blue, "hello");
122        logger.error(blue, "hello");
123
124        logger.debug(blue, "hello {}", "world");
125        logger.info(blue, "hello {}", "world");
126        logger.warn(blue, "hello {}", "world");
127        logger.error(blue, "hello {}", "world");
128
129        logger.debug(blue, "hello {} and {} ", "world", "universe");
130        logger.info(blue, "hello {} and {} ", "world", "universe");
131        logger.warn(blue, "hello {} and {} ", "world", "universe");
132        logger.error(blue, "hello {} and {} ", "world", "universe");
133    }
134
135    @Test
136    public void testMDC() {
137        MDC.put("k", "v");
138        assertNull(MDC.get("k"));
139        MDC.remove("k");
140        assertNull(MDC.get("k"));
141        MDC.clear();
142    }
143}