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.access.joran;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNotNull;
018import static org.junit.Assert.assertTrue;
019
020import ch.qos.logback.access.spi.IAccessEvent;
021import org.junit.After;
022import org.junit.Before;
023import org.junit.Test;
024
025import ch.qos.logback.access.AccessTestConstants;
026import ch.qos.logback.access.dummy.DummyAccessEventBuilder;
027import ch.qos.logback.access.spi.AccessContext;
028import ch.qos.logback.core.joran.spi.JoranException;
029import ch.qos.logback.core.read.ListAppender;
030import ch.qos.logback.core.testUtil.StringListAppender;
031
032public class JoranConfiguratorTest {
033
034    AccessContext context = new AccessContext();
035
036    @Before
037    public void setUp() throws Exception {
038    }
039
040    @After
041    public void tearDown() throws Exception {
042    }
043
044    void configure(String file) throws JoranException {
045        JoranConfigurator jc = new JoranConfigurator();
046        jc.setContext(context);
047        jc.doConfigure(file);
048    }
049
050    @Test
051    public void smoke() throws Exception {
052        configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/smoke.xml");
053
054        ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) context.getAppender("LIST");
055        IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent();
056        listAppender.doAppend(event);
057
058        assertEquals(1, listAppender.list.size());
059
060        assertEquals(1, listAppender.list.size());
061        IAccessEvent ae = listAppender.list.get(0);
062        assertNotNull(ae);
063    }
064
065    @Test
066    public void defaultLayout() throws Exception {
067        configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/defaultLayout.xml");
068        StringListAppender<IAccessEvent> listAppender = (StringListAppender<IAccessEvent>) context.getAppender("STR_LIST");
069        IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent();
070        listAppender.doAppend(event);
071        assertEquals(1, listAppender.strList.size());
072        // the result contains a line separator at the end
073        assertTrue(listAppender.strList.get(0).startsWith("testMethod"));
074    }
075}