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.jul;
026
027import org.junit.After;
028import org.junit.Assert;
029import org.junit.Before;
030import org.junit.Test;
031import org.slf4j.*;
032
033import java.util.logging.Handler;
034import java.util.logging.Level;
035import java.util.logging.LogRecord;
036
037import static org.junit.Assert.assertNotNull;
038import static org.junit.Assert.assertNull;
039import static org.junit.Assert.assertEquals;
040import static org.junit.Assert.fail;
041
042/**
043 * Test whether invoking the SLF4J API causes problems or not.
044 *
045 * @author Ceki Gulcu
046 */
047public class InvocationTest {
048
049    Level oldLevel;
050    java.util.logging.Logger root = java.util.logging.Logger.getLogger("");
051
052    ListHandler listHandler = new ListHandler();
053
054    @Before
055    public void setUp() throws Exception {
056        oldLevel = root.getLevel();
057        root.setLevel(Level.FINE);
058        // removeAllHandlers(root);
059        root.addHandler(listHandler);
060    }
061
062    @After
063    public void tearDown() throws Exception {
064        root.setLevel(oldLevel);
065        removeListHandlers(root);
066    }
067
068    void removeListHandlers(java.util.logging.Logger logger) {
069        Handler[] handlers = logger.getHandlers();
070        for (Handler h : handlers) {
071            if (h instanceof ListHandler)
072                logger.removeHandler(h);
073        }
074    }
075
076    @Test
077    public void smoke() {
078        Logger logger = LoggerFactory.getLogger("test1");
079        logger.debug("Hello world.");
080        assertLogMessage("Hello world.", 0);
081    }
082
083    @Test
084    public void verifyMessageFormatting() {
085        Integer i1 = Integer.valueOf(1);
086        Integer i2 = Integer.valueOf(2);
087        Integer i3 = Integer.valueOf(3);
088        Exception e = new Exception("This is a test exception.");
089        Logger logger = LoggerFactory.getLogger("test2");
090
091        int index = 0;
092        logger.debug("Hello world");
093        assertLogMessage("Hello world", index++);
094
095        logger.debug("Hello world {}", i1);
096        assertLogMessage("Hello world " + i1, index++);
097
098        logger.debug("val={} val={}", i1, i2);
099        assertLogMessage("val=1 val=2", index++);
100
101        logger.debug("val={} val={} val={}", new Object[] { i1, i2, i3 });
102        assertLogMessage("val=1 val=2 val=3", index++);
103
104        logger.debug("Hello world 2", e);
105        assertLogMessage("Hello world 2", index);
106        assertException(e.getClass(), index++);
107        logger.info("Hello world 2.");
108
109        logger.warn("Hello world 3.");
110        logger.warn("Hello world 3", e);
111
112        logger.error("Hello world 4.");
113        logger.error("Hello world {}", Integer.valueOf(3));
114        logger.error("Hello world 4.", e);
115    }
116
117    @Test
118    public void testNull() {
119        Logger logger = LoggerFactory.getLogger("testNull");
120        logger.debug(null);
121        logger.info(null);
122        logger.warn(null);
123        logger.error(null);
124
125        Exception e = new Exception("This is a test exception.");
126        logger.debug(null, e);
127        logger.info(null, e);
128        logger.warn(null, e);
129        logger.error(null, e);
130    }
131
132    @Test
133    public void testMarker() {
134        Logger logger = LoggerFactory.getLogger("testMarker");
135        Marker blue = MarkerFactory.getMarker("BLUE");
136        logger.debug(blue, "hello");
137        logger.info(blue, "hello");
138        logger.warn(blue, "hello");
139        logger.error(blue, "hello");
140
141        logger.debug(blue, "hello {}", "world");
142        logger.info(blue, "hello {}", "world");
143        logger.warn(blue, "hello {}", "world");
144        logger.error(blue, "hello {}", "world");
145
146        logger.debug(blue, "hello {} and {} ", "world", "universe");
147        logger.info(blue, "hello {} and {} ", "world", "universe");
148        logger.warn(blue, "hello {} and {} ", "world", "universe");
149        logger.error(blue, "hello {} and {} ", "world", "universe");
150    }
151
152    @Test
153    public void testMDC() {
154        MDC.put("k", "v");
155        assertNotNull(MDC.get("k"));
156        assertEquals("v", MDC.get("k"));
157
158        MDC.remove("k");
159        assertNull(MDC.get("k"));
160
161        MDC.put("k1", "v1");
162        assertEquals("v1", MDC.get("k1"));
163        MDC.clear();
164        assertNull(MDC.get("k1"));
165
166        try {
167            MDC.put(null, "x");
168            fail("null keys are invalid");
169        } catch (IllegalArgumentException e) {
170        }
171    }
172
173    private void assertLogMessage(String expected, int index) {
174        LogRecord logRecord = listHandler.recordList.get(index);
175        Assert.assertNotNull(logRecord);
176        assertEquals(expected, logRecord.getMessage());
177    }
178
179    private void assertException(Class<? extends Throwable> exceptionType, int index) {
180        LogRecord logRecord = listHandler.recordList.get(index);
181        Assert.assertNotNull(logRecord);
182        assertEquals(exceptionType, logRecord.getThrown().getClass());
183    }
184
185}