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.encoder;
015
016import static ch.qos.logback.core.CoreConstants.CODES_URL;
017import static org.junit.Assert.assertTrue;
018import static org.junit.Assert.assertEquals;
019
020import org.junit.Before;
021import org.junit.Test;
022import org.slf4j.Logger;
023
024import ch.qos.logback.classic.ClassicTestConstants;
025import ch.qos.logback.classic.LoggerContext;
026import ch.qos.logback.classic.joran.JoranConfigurator;
027import ch.qos.logback.classic.spi.ILoggingEvent;
028import ch.qos.logback.core.FileAppender;
029import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
030import ch.qos.logback.core.joran.spi.JoranException;
031import ch.qos.logback.core.status.Status;
032import ch.qos.logback.core.testUtil.StatusChecker;
033import ch.qos.logback.core.util.StatusPrinter;
034
035public class LayoutInsteadOfEncoderTest {
036
037    // TeztConstants.TEST_SRC_PREFIX + "input/joran/ignore.xml"
038    JoranConfigurator jc = new JoranConfigurator();
039    LoggerContext loggerContext = new LoggerContext();
040
041    @Before
042    public void setUp() {
043        jc.setContext(loggerContext);
044
045    }
046
047    // jc.doConfigure(TeztConstants.TEST_SRC_PREFIX + "input/joran/ignore.xml");
048
049    @Test
050    public void layoutInsteadOfEncoer() throws JoranException {
051        jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "compatibility/layoutInsteadOfEncoder.xml");
052        StatusPrinter.print(loggerContext);
053        StatusChecker checker = new StatusChecker(loggerContext);
054        checker.assertContainsMatch(Status.WARN, "This appender no longer admits a layout as a sub-component");
055        checker.assertContainsMatch(Status.WARN, "See also " + CODES_URL + "#layoutInsteadOfEncoder for details");
056
057        ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
058        FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) root.getAppender("LIOE");
059        assertTrue(fileAppender.isStarted());
060        assertTrue(fileAppender.getEncoder() instanceof LayoutWrappingEncoder);
061    }
062    
063    @Test
064    public void immediateFlushInEncoder_TRUE() throws JoranException {
065        immediateFlushInEncoder(true);
066    }
067    
068    @Test
069    public void immediateFlushInEncoder_FALSE() throws JoranException {
070        immediateFlushInEncoder(false);
071    }
072    
073    public void immediateFlushInEncoder(Boolean immediateFlush) throws JoranException {
074        loggerContext.putProperty("immediateFlush", immediateFlush.toString());
075        jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "compatibility/immediateFlushInEncoder.xml");
076        StatusPrinter.print(loggerContext);
077        StatusChecker checker = new StatusChecker(loggerContext);
078
079        checker.assertContainsMatch(Status.WARN, "As of version 1.2.0 \"immediateFlush\" property should be set within the enclosing Appender.");
080        checker.assertContainsMatch(Status.WARN, "Please move \"immediateFlush\" property into the enclosing appender.");
081        checker.assertContainsMatch(Status.WARN, "Setting the \"immediateFlush\" property of the enclosing appender to "+immediateFlush);
082
083        ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
084        FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) root.getAppender("LIOE");
085        assertTrue(fileAppender.isStarted());
086        assertEquals(immediateFlush, Boolean.valueOf(fileAppender.isImmediateFlush())); 
087    }
088
089  
090}