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 ch.qos.logback.access.AccessTestConstants;
017import ch.qos.logback.access.spi.AccessContext;
018import ch.qos.logback.access.spi.IAccessEvent;
019import ch.qos.logback.core.ConsoleAppender;
020import ch.qos.logback.core.joran.spi.JoranException;
021import ch.qos.logback.core.read.ListAppender;
022import ch.qos.logback.core.testUtil.CoreTestConstants;
023import ch.qos.logback.core.testUtil.RandomUtil;
024import ch.qos.logback.core.testUtil.StatusChecker;
025
026import org.junit.Before;
027import org.junit.Test;
028
029import java.io.IOException;
030import java.net.InetAddress;
031import java.net.UnknownHostException;
032
033import static org.junit.Assert.assertNotNull;
034import static org.junit.Assert.assertNull;
035import static org.junit.Assert.assertTrue;
036
037/**
038 * @author Ceki Gülcü
039 */
040public class ConditionalTest {
041
042    AccessContext context = new AccessContext();
043    StatusChecker checker = new StatusChecker(context);
044
045    int diff = RandomUtil.getPositiveInt();
046    String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
047
048    @Before
049    public void setUp() {
050        InetAddress localhost = null;
051        try {
052            localhost = InetAddress.getLocalHost();
053            context.putProperty("aHost", localhost.getHostName());
054        } catch (UnknownHostException e) {
055            e.printStackTrace();
056        }
057    }
058
059    void configure(String file) throws JoranException {
060        JoranConfigurator jc = new JoranConfigurator();
061        jc.setContext(context);
062        jc.doConfigure(file);
063    }
064
065    @Test
066    public void conditionalConsoleApp_IF_THEN_True() throws JoranException, UnknownHostException {
067        configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/conditional/conditionalConsole.xml");
068        ConsoleAppender<IAccessEvent> consoleAppender = (ConsoleAppender<IAccessEvent>) context.getAppender("CON");
069        assertNotNull(consoleAppender);
070        assertTrue(checker.isErrorFree(0));
071    }
072
073    @Test
074    public void conditionalConsoleApp_IF_THEN_False() throws JoranException, IOException, InterruptedException {
075        context.putProperty("aHost", null);
076        configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/conditional/conditionalConsole.xml");
077
078        ConsoleAppender<IAccessEvent> consoleAppender = (ConsoleAppender<IAccessEvent>) context.getAppender("CON");
079        assertNull(consoleAppender);
080
081        StatusChecker checker = new StatusChecker(context);
082        assertTrue(checker.isErrorFree(0));
083    }
084
085    @Test
086    public void conditionalConsoleApp_ELSE() throws JoranException, IOException, InterruptedException {
087        configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/conditional/conditionalConsole_ELSE.xml");
088        ConsoleAppender<IAccessEvent> consoleAppender = (ConsoleAppender<IAccessEvent>) context.getAppender("CON");
089        assertNull(consoleAppender);
090
091        ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) context.getAppender("LIST");
092        assertNotNull(listAppender);
093        assertTrue(checker.isErrorFree(0));
094    }
095}