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.joran.conditional; 015 016import static org.junit.Assert.assertNotNull; 017import static org.junit.Assert.assertNull; 018 019import java.io.IOException; 020import java.net.InetAddress; 021import java.net.UnknownHostException; 022 023import org.junit.After; 024import org.junit.Before; 025import org.junit.Test; 026 027import ch.qos.logback.classic.ClassicTestConstants; 028import ch.qos.logback.classic.Logger; 029import ch.qos.logback.classic.LoggerContext; 030import ch.qos.logback.classic.joran.JoranConfigurator; 031import ch.qos.logback.classic.spi.ILoggingEvent; 032import ch.qos.logback.core.ConsoleAppender; 033import ch.qos.logback.core.FileAppender; 034import ch.qos.logback.core.joran.spi.JoranException; 035import ch.qos.logback.core.read.ListAppender; 036import ch.qos.logback.core.testUtil.CoreTestConstants; 037import ch.qos.logback.core.testUtil.RandomUtil; 038import ch.qos.logback.core.testUtil.StatusChecker; 039import ch.qos.logback.core.util.StatusPrinter; 040 041public class ConditionalTest { 042 043 LoggerContext context = new LoggerContext(); 044 Logger root = context.getLogger(Logger.ROOT_LOGGER_NAME); 045 046 int diff = RandomUtil.getPositiveInt(); 047 String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/"; 048 049 @Before 050 public void setUp() throws UnknownHostException { 051 context.setName("c" + diff); 052 context.putProperty("randomOutputDir", randomOutputDir); 053 } 054 055 @After 056 public void tearDown() { 057 StatusPrinter.printIfErrorsOccured(context); 058 } 059 060 void configure(String file) throws JoranException { 061 JoranConfigurator jc = new JoranConfigurator(); 062 jc.setContext(context); 063 jc.doConfigure(file); 064 } 065 066 @SuppressWarnings("rawtypes") 067 @Test 068 public void conditionalConsoleApp_IF_THEN_True() throws JoranException, IOException, InterruptedException { 069 InetAddress localhost = InetAddress.getLocalHost(); 070 System.out.println("In conditionalConsoleApp_IF_THEN_True, canonicalHostName=\"" + localhost.getCanonicalHostName() + "] and hostNmae=\"" 071 + localhost.getHostName() + "\""); 072 context.putProperty("aHost", localhost.getHostName()); 073 074 String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp.xml"; 075 configure(configFileAsStr); 076 FileAppender fileAppender = (FileAppender) root.getAppender("FILE"); 077 assertNotNull(fileAppender); 078 079 ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON"); 080 assertNotNull(consoleAppender); 081 StatusChecker checker = new StatusChecker(context); 082 checker.assertIsErrorFree(); 083 } 084 085 @SuppressWarnings("rawtypes") 086 @Test 087 public void conditionalConsoleApp_IF_THEN_False() throws JoranException, IOException, InterruptedException { 088 089 String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp.xml"; 090 configure(configFileAsStr); 091 FileAppender fileAppender = (FileAppender) root.getAppender("FILE"); 092 assertNotNull(fileAppender); 093 094 ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON"); 095 assertNull(consoleAppender); 096 StatusChecker checker = new StatusChecker(context); 097 checker.assertIsErrorFree(); 098 } 099 100 @SuppressWarnings("rawtypes") 101 @Test 102 public void conditionalConsoleApp_IF_THEN_ELSE() throws JoranException, IOException, InterruptedException { 103 104 String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp_ELSE.xml"; 105 configure(configFileAsStr); 106 107 FileAppender fileAppender = (FileAppender) root.getAppender("FILE"); 108 assertNotNull(fileAppender); 109 110 ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON"); 111 assertNull(consoleAppender); 112 113 ListAppender listAppender = (ListAppender) root.getAppender("LIST"); 114 assertNotNull(listAppender); 115 116 // StatusPrinter.printIfErrorsOccured(context); 117 StatusChecker checker = new StatusChecker(context); 118 checker.assertIsErrorFree(); 119 } 120 121 @Test 122 public void conditionalInclusionWithExistingFile() throws JoranException, IOException, InterruptedException { 123 124 String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalIncludeExistingFile.xml"; 125 configure(configFileAsStr); 126 127 ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CON"); 128 assertNotNull(consoleAppender); 129 StatusChecker checker = new StatusChecker(context); 130 checker.assertIsErrorFree(); 131 } 132 133 @Test 134 public void conditionalInclusionWithInexistentFile() throws JoranException, IOException, InterruptedException { 135 136 String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalIncludeInexistentFile.xml"; 137 configure(configFileAsStr); 138 139 ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CON"); 140 assertNull(consoleAppender); 141 StatusChecker checker = new StatusChecker(context); 142 checker.assertIsErrorFree(); 143 } 144 145}