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.nop;
026
027import static org.junit.Assert.assertNull;
028
029import org.junit.Test;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.slf4j.MDC;
033import org.slf4j.Marker;
034import org.slf4j.MarkerFactory;
035
036/**
037 * Test whether invoking the SLF4J API causes problems or not.
038 * 
039 * @author Ceki Gulcu
040 *
041 */
042public class InvocationTest {
043
044    @Test
045    public void test1() {
046        Logger logger = LoggerFactory.getLogger("test1");
047        logger.debug("Hello world.");
048    }
049
050    @Test
051    public void test2() {
052        Integer i1 = Integer.valueOf(1);
053        Integer i2 = Integer.valueOf(2);
054        Integer i3 = Integer.valueOf(3);
055        Exception e = new Exception("This is a test exception.");
056        Logger logger = LoggerFactory.getLogger("test2");
057
058        logger.debug("Hello world 1.");
059        logger.debug("Hello world {}", i1);
060        logger.debug("val={} val={}", i1, i2);
061        logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
062
063        logger.debug("Hello world 2", e);
064        logger.info("Hello world 2.");
065
066        logger.warn("Hello world 3.");
067        logger.warn("Hello world 3", e);
068
069        logger.error("Hello world 4.");
070        logger.error("Hello world {}", Integer.valueOf(3));
071        logger.error("Hello world 4.", e);
072    }
073
074    @Test
075    public void testNull() {
076        Logger logger = LoggerFactory.getLogger("testNull");
077        logger.debug(null);
078        logger.info(null);
079        logger.warn(null);
080        logger.error(null);
081
082        Exception e = new Exception("This is a test exception.");
083        logger.debug(null, e);
084        logger.info(null, e);
085        logger.warn(null, e);
086        logger.error(null, e);
087    }
088
089    @Test
090    public void testMarker() {
091        Logger logger = LoggerFactory.getLogger("testMarker");
092        Marker blue = MarkerFactory.getMarker("BLUE");
093        logger.debug(blue, "hello");
094        logger.info(blue, "hello");
095        logger.warn(blue, "hello");
096        logger.error(blue, "hello");
097
098        logger.debug(blue, "hello {}", "world");
099        logger.info(blue, "hello {}", "world");
100        logger.warn(blue, "hello {}", "world");
101        logger.error(blue, "hello {}", "world");
102
103        logger.debug(blue, "hello {} and {} ", "world", "universe");
104        logger.info(blue, "hello {} and {} ", "world", "universe");
105        logger.warn(blue, "hello {} and {} ", "world", "universe");
106        logger.error(blue, "hello {} and {} ", "world", "universe");
107    }
108
109    @Test
110    public void testMDC() {
111        MDC.put("k", "v");
112        assertNull(MDC.get("k"));
113        MDC.remove("k");
114        assertNull(MDC.get("k"));
115        MDC.clear();
116    }
117
118    @Test
119    public void testMDCCloseable() {
120        MDC.MDCCloseable closeable = MDC.putCloseable("k", "v");
121        assertNull(MDC.get("k"));
122        closeable.close();
123        assertNull(MDC.get("k"));
124        MDC.clear();
125    }
126}