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 ch.qos.logback.classic.pattern;
015
016import static org.junit.Assert.assertEquals;
017
018import org.junit.After;
019import org.junit.Before;
020import org.junit.Test;
021import org.slf4j.IMarkerFactory;
022import org.slf4j.Marker;
023import org.slf4j.helpers.BasicMarkerFactory;
024
025import ch.qos.logback.classic.Level;
026import ch.qos.logback.classic.Logger;
027import ch.qos.logback.classic.LoggerContext;
028import ch.qos.logback.classic.spi.ILoggingEvent;
029import ch.qos.logback.classic.spi.LoggingEvent;
030
031public class MarkerConverterTest {
032
033    LoggerContext lc;
034    MarkerConverter converter;
035    // use a different facotry for each test so that they are independent
036    IMarkerFactory markerFactory = new BasicMarkerFactory();
037
038    @Before
039    public void setUp() throws Exception {
040        lc = new LoggerContext();
041        converter = new MarkerConverter();
042        converter.start();
043    }
044
045    @After
046    public void tearDown() throws Exception {
047        lc = null;
048        converter.stop();
049        converter = null;
050    }
051
052    @Test
053    public void testWithNullMarker() {
054        String result = converter.convert(createLoggingEvent(null));
055        assertEquals("", result);
056    }
057
058    @Test
059    public void testWithMarker() {
060        String name = "test";
061        Marker marker = markerFactory.getMarker(name);
062        String result = converter.convert(createLoggingEvent(marker));
063        assertEquals(name, result);
064    }
065
066    @Test
067    public void testWithOneChildMarker() {
068        Marker marker = markerFactory.getMarker("test");
069        marker.add(markerFactory.getMarker("child"));
070
071        String result = converter.convert(createLoggingEvent(marker));
072
073        assertEquals("test [ child ]", result);
074    }
075
076    @Test
077    public void testWithSeveralChildMarker() {
078        Marker marker = markerFactory.getMarker("testParent");
079        marker.add(markerFactory.getMarker("child1"));
080        marker.add(markerFactory.getMarker("child2"));
081        marker.add(markerFactory.getMarker("child3"));
082
083        String result = converter.convert(createLoggingEvent(marker));
084
085        assertEquals("testParent [ child1, child2, child3 ]", result);
086    }
087
088    private ILoggingEvent createLoggingEvent(Marker marker) {
089        LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), Level.DEBUG, "test message", null, null);
090        le.setMarker(marker);
091        return le;
092    }
093}