001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package org.dummy;
015
016import static org.junit.Assert.assertEquals;
017
018import org.apache.log4j.Logger;
019import org.junit.Before;
020import org.junit.Test;
021import org.slf4j.LoggerFactory;
022
023import ch.qos.logback.classic.Level;
024import ch.qos.logback.classic.LoggerContext;
025import ch.qos.logback.classic.PatternLayout;
026import ch.qos.logback.classic.spi.ILoggingEvent;
027
028/**
029 * Used to test log4j-over-slf4j
030 * 
031 * @author Ceki Gulcu
032 *
033 */
034public class Log4jInvocation {
035
036    static final String HELLO = "Hello";
037
038    DummyLBAppender listAppender;
039    LoggerContext lc;
040    ch.qos.logback.classic.Logger rootLogger;
041
042    @Before
043    public void fixture() {
044        lc = (LoggerContext) LoggerFactory.getILoggerFactory();
045        lc.reset();
046
047        listAppender = new DummyLBAppender();
048        listAppender.setContext(lc);
049        listAppender.start();
050        rootLogger = lc.getLogger("root");
051        rootLogger.addAppender(listAppender);
052    }
053
054    @Test
055    public void basic() {
056        assertEquals(0, listAppender.list.size());
057
058        Logger logger = Logger.getLogger("basic-test");
059        logger.debug(HELLO);
060
061        assertEquals(1, listAppender.list.size());
062        ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
063        assertEquals(HELLO, event.getMessage());
064    }
065
066    @Test
067    public void callerData() {
068        assertEquals(0, listAppender.list.size());
069
070        PatternLayout pl = new PatternLayout();
071        pl.setPattern("%-5level [%class] %logger - %msg");
072        pl.setContext(lc);
073        pl.start();
074        listAppender.layout = pl;
075
076        Logger logger = Logger.getLogger("basic-test");
077        logger.trace("none");
078        assertEquals(0, listAppender.list.size());
079
080        rootLogger.setLevel(Level.TRACE);
081        logger.trace(HELLO);
082        assertEquals(1, listAppender.list.size());
083
084        ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
085        assertEquals(HELLO, event.getMessage());
086
087        assertEquals(1, listAppender.stringList.size());
088        assertEquals("TRACE [" + Log4jInvocation.class.getName() + "] basic-test - Hello", listAppender.stringList.get(0));
089    }
090}