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.dummyExt;
026
027import static org.junit.Assert.assertEquals;
028
029import org.apache.log4j.spi.LocationInfo;
030import org.apache.log4j.spi.LoggingEvent;
031import org.junit.Before;
032import org.junit.Test;
033import org.slf4j.ext.XLogger;
034import org.slf4j.ext.XLoggerFactory;
035
036public class XLoggerTest {
037
038    ListAppender listAppender;
039    org.apache.log4j.Logger log4jRoot;
040
041    final static String EXPECTED_FILE_NAME = "XLoggerTest.java";
042
043    @Before
044    public void setUp() throws Exception {
045
046        // start from a clean slate for each test
047
048        listAppender = new ListAppender();
049        listAppender.extractLocationInfo = true;
050        log4jRoot = org.apache.log4j.Logger.getRootLogger();
051        log4jRoot.addAppender(listAppender);
052        log4jRoot.setLevel(org.apache.log4j.Level.TRACE);
053    }
054
055    void verify(LoggingEvent le, String expectedMsg) {
056        assertEquals(expectedMsg, le.getMessage());
057        assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
058    }
059
060    void verifyWithException(LoggingEvent le, String expectedMsg, Throwable t) {
061        verify(le, expectedMsg);
062        assertEquals(t.toString(), le.getThrowableStrRep()[0]);
063    }
064
065    void verifyWithLevelAndException(LoggingEvent le, XLogger.Level level, String expectedMsg, Throwable t) {
066        verify(le, expectedMsg);
067        assertEquals(t.toString(), le.getThrowableStrRep()[0]);
068        assertEquals(le.getLevel().toString(), level.toString());
069    }
070
071    @Test
072    public void testEntering() {
073        XLogger logger = XLoggerFactory.getXLogger("UnitTest");
074        logger.entry();
075        logger.entry(1);
076        logger.entry("test");
077        logger.entry("a", "b", "c", "d");
078        logger.entry("a", "b", "c", "d", "e");
079        logger.entry("a", "b", "c", "d", "e", "f");
080
081        assertEquals(6, listAppender.list.size());
082        verify(listAppender.list.get(0), "entry");
083        verify(listAppender.list.get(1), "entry with (1)");
084        verify(listAppender.list.get(2), "entry with (test)");
085    }
086
087    @Test
088    public void testExiting() {
089        XLogger logger = XLoggerFactory.getXLogger("UnitTest");
090        logger.exit();
091        assertEquals(Integer.valueOf(0), logger.exit(0));
092        assertEquals(Boolean.FALSE, logger.exit(false));
093
094        assertEquals(3, listAppender.list.size());
095        verify(listAppender.list.get(0), "exit");
096        verify(listAppender.list.get(1), "exit with (0)");
097        verify(listAppender.list.get(2), "exit with (false)");
098    }
099
100    @Test
101    public void testThrowing() {
102        XLogger logger = XLoggerFactory.getXLogger("UnitTest");
103        Throwable t = new UnsupportedOperationException("Test");
104        assertEquals(t, logger.throwing(t));
105        assertEquals(t, logger.throwing(XLogger.Level.DEBUG, t));
106        assertEquals(2, listAppender.list.size());
107        verifyWithException(listAppender.list.get(0), "throwing", t);
108        LoggingEvent event = listAppender.list.get(1);
109        verifyWithLevelAndException(event, XLogger.Level.DEBUG, "throwing", t);
110    }
111
112    @Test
113    public void testCaught() {
114        XLogger logger = XLoggerFactory.getXLogger("UnitTest");
115        long x = 5;
116        Throwable t = null;
117        try {
118            @SuppressWarnings("unused")
119            long y = x / 0;
120        } catch (Exception ex) {
121            t = ex;
122            logger.catching(ex);
123            logger.catching(XLogger.Level.DEBUG, ex);
124        }
125        verifyWithException(listAppender.list.get(0), "catching", t);
126        verifyWithLevelAndException(listAppender.list.get(1), XLogger.Level.DEBUG, "catching", t);
127    }
128
129    // See http://jira.qos.ch/browse/SLF4J-105
130    // formerly http://bugzilla.slf4j.org/show_bug.cgi?id=114
131    @Test
132    public void testLocationExtraction_Bug114() {
133        XLogger logger = XLoggerFactory.getXLogger("UnitTest");
134        int line = 135; // requires update if line numbers change
135        logger.exit();
136        logger.debug("hello");
137
138        assertEquals(2, listAppender.list.size());
139
140        {
141            LoggingEvent e = listAppender.list.get(0);
142            LocationInfo li = e.getLocationInformation();
143            assertEquals(this.getClass().getName(), li.getClassName());
144            assertEquals("" + line, li.getLineNumber());
145        }
146
147        {
148            LoggingEvent e = listAppender.list.get(1);
149            LocationInfo li = e.getLocationInformation();
150            assertEquals(this.getClass().getName(), li.getClassName());
151            assertEquals("" + (line + 1), li.getLineNumber());
152        }
153    }
154    
155    @Test
156    public void testNoDoubleSubstitution_Bug421() {
157        XLogger logger = XLoggerFactory.getXLogger("UnitTest");
158        logger.error("{},{}", "foo", "[{}]");
159        verify(listAppender.list.get(0), "foo,[{}]");
160        
161        logger.error("{},{}", "[{}]", "foo");
162        verify(listAppender.list.get(1), "[{}],foo");
163    }
164    
165    
166}