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.reload4j;
026
027import static org.junit.Assert.assertEquals;
028import static org.junit.Assert.assertNotNull;
029import static org.junit.Assert.assertNull;
030import static org.junit.Assert.fail;
031
032import java.util.HashMap;
033import java.util.Map;
034
035import org.apache.log4j.spi.LoggingEvent;
036import org.junit.After;
037import org.junit.Before;
038import org.junit.Test;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041import org.slf4j.MDC;
042import org.slf4j.Marker;
043import org.slf4j.MarkerFactory;
044
045/**
046 * Test whether invoking the SLF4J API causes problems or not.
047 * 
048 * @author Ceki Gulcu
049 * 
050 */
051public class InvocationTest {
052
053    ListAppender listAppender = new ListAppender();
054    org.apache.log4j.Logger root;
055
056    @Before
057    public void setUp() throws Exception {
058        root = org.apache.log4j.Logger.getRootLogger();
059        root.addAppender(listAppender);
060    }
061
062    @After
063    public void tearDown() throws Exception {
064        root.getLoggerRepository().resetConfiguration();
065    }
066
067    @Test
068    public void test1() {
069
070        Logger logger = LoggerFactory.getLogger("test1");
071        logger.debug("Hello world.");
072        assertEquals(1, listAppender.list.size());
073    }
074
075    @Test
076    public void test2() {
077        Integer i1 = Integer.valueOf(1);
078        Integer i2 = Integer.valueOf(2);
079        Integer i3 = Integer.valueOf(3);
080        Exception e = new Exception("This is a test exception.");
081        Logger logger = LoggerFactory.getLogger("test2");
082
083        logger.trace("Hello trace.");
084
085        logger.debug("Hello world 1.");
086        logger.debug("Hello world {}", i1);
087        logger.debug("val={} val={}", i1, i2);
088        logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
089
090        logger.debug("Hello world 2", e);
091        logger.info("Hello world 2.");
092
093        logger.warn("Hello world 3.");
094        logger.warn("Hello world 3", e);
095
096        logger.error("Hello world 4.");
097        logger.error("Hello world {}", Integer.valueOf(3));
098        logger.error("Hello world 4.", e);
099        assertEquals(11, listAppender.list.size());
100    }
101
102    @Test
103    public void testNull() {
104        Logger logger = LoggerFactory.getLogger("testNull");
105        logger.trace(null);
106        logger.debug(null);
107        logger.info(null);
108        logger.warn(null);
109        logger.error(null);
110
111        Exception e = new Exception("This is a test exception.");
112        logger.debug(null, e);
113        logger.info(null, e);
114        logger.warn(null, e);
115        logger.error(null, e);
116        assertEquals(8, listAppender.list.size());
117    }
118
119    // http://jira.qos.ch/browse/SLF4J-69
120    // formerly http://bugzilla.slf4j.org/show_bug.cgi?id=78
121    @Test
122    public void testNullParameter_BUG78() {
123        Logger logger = LoggerFactory.getLogger("testNullParameter_BUG78");
124        String[] parameters = null;
125        String msg = "hello {}";
126
127        logger.debug(msg, (Object[]) parameters);
128
129        assertEquals(1, listAppender.list.size());
130        LoggingEvent e = (LoggingEvent) listAppender.list.get(0);
131        assertEquals(msg, e.getMessage());
132    }
133
134    @Test
135    public void testMarker() {
136        Logger logger = LoggerFactory.getLogger("testMarker");
137        Marker blue = MarkerFactory.getMarker("BLUE");
138        logger.trace(blue, "hello");
139        logger.debug(blue, "hello");
140        logger.info(blue, "hello");
141        logger.warn(blue, "hello");
142        logger.error(blue, "hello");
143
144        logger.debug(blue, "hello {}", "world");
145        logger.info(blue, "hello {}", "world");
146        logger.warn(blue, "hello {}", "world");
147        logger.error(blue, "hello {}", "world");
148
149        logger.debug(blue, "hello {} and {} ", "world", "universe");
150        logger.info(blue, "hello {} and {} ", "world", "universe");
151        logger.warn(blue, "hello {} and {} ", "world", "universe");
152        logger.error(blue, "hello {} and {} ", "world", "universe");
153        assertEquals(12, listAppender.list.size());
154    }
155
156    @Test
157    public void testMDC() {
158        MDC.put("k", "v");
159        assertNotNull(MDC.get("k"));
160        assertEquals("v", MDC.get("k"));
161
162        MDC.remove("k");
163        assertNull(MDC.get("k"));
164
165        MDC.put("k1", "v1");
166        assertEquals("v1", MDC.get("k1"));
167        MDC.clear();
168        assertNull(MDC.get("k1"));
169
170        try {
171            MDC.put(null, "x");
172            fail("null keys are invalid");
173        } catch (IllegalArgumentException e) {
174        }
175    }
176
177    @Test
178    public void testMDCContextMapValues() {
179        Map<String, String> map = new HashMap<>();
180        map.put("ka", "va");
181        map.put("kb", "vb");
182
183        MDC.put("k", "v");
184        assertEquals("v", MDC.get("k"));
185        MDC.setContextMap(map);
186        assertNull(MDC.get("k"));
187        assertEquals("va", MDC.get("ka"));
188        assertEquals("vb", MDC.get("kb"));
189    }
190
191    @Test
192    public void testCallerInfo() {
193        Logger logger = LoggerFactory.getLogger("testMarker");
194        listAppender.extractLocationInfo = true;
195        logger.debug("hello");
196        LoggingEvent event = listAppender.list.get(0);
197        assertEquals(this.getClass().getName(), event.getLocationInformation().getClassName());
198    }
199
200    @Test
201    public void testCallerInfoWithFluentAPI() {
202        Logger logger = LoggerFactory.getLogger("testMarker");
203        listAppender.extractLocationInfo = true;
204        logger.atDebug().log("hello");
205        LoggingEvent event = listAppender.list.get(0);
206        assertEquals(this.getClass().getName(), event.getLocationInformation().getClassName());
207    }
208
209}