View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.joran.conditional;
15  
16  import static org.junit.Assert.assertNotNull;
17  import static org.junit.Assert.assertNull;
18  
19  import java.io.IOException;
20  import java.net.InetAddress;
21  import java.net.UnknownHostException;
22  
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import ch.qos.logback.classic.ClassicTestConstants;
28  import ch.qos.logback.classic.Logger;
29  import ch.qos.logback.classic.LoggerContext;
30  import ch.qos.logback.classic.joran.JoranConfigurator;
31  import ch.qos.logback.classic.spi.ILoggingEvent;
32  import ch.qos.logback.core.ConsoleAppender;
33  import ch.qos.logback.core.FileAppender;
34  import ch.qos.logback.core.joran.spi.JoranException;
35  import ch.qos.logback.core.read.ListAppender;
36  import ch.qos.logback.core.testUtil.CoreTestConstants;
37  import ch.qos.logback.core.testUtil.RandomUtil;
38  import ch.qos.logback.core.testUtil.StatusChecker;
39  import ch.qos.logback.core.util.StatusPrinter;
40  
41  public class ConditionalTest {
42  
43      LoggerContext context = new LoggerContext();
44      Logger root = context.getLogger(Logger.ROOT_LOGGER_NAME);
45  
46      int diff = RandomUtil.getPositiveInt();
47      String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
48  
49      @Before
50      public void setUp() throws UnknownHostException {
51          context.setName("c" + diff);
52          context.putProperty("randomOutputDir", randomOutputDir);
53      }
54  
55      @After
56      public void tearDown() {
57          StatusPrinter.printIfErrorsOccured(context);
58      }
59  
60      void configure(String file) throws JoranException {
61          JoranConfigurator jc = new JoranConfigurator();
62          jc.setContext(context);
63          jc.doConfigure(file);
64      }
65  
66      @SuppressWarnings("rawtypes")
67      @Test
68      public void conditionalConsoleApp_IF_THEN_True() throws JoranException, IOException, InterruptedException {
69          InetAddress localhost = InetAddress.getLocalHost();
70          System.out.println("In conditionalConsoleApp_IF_THEN_True, canonicalHostName=\"" + localhost.getCanonicalHostName() + "] and hostNmae=\""
71                          + localhost.getHostName() + "\"");
72          context.putProperty("aHost", localhost.getHostName());
73  
74          String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp.xml";
75          configure(configFileAsStr);
76          FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
77          assertNotNull(fileAppender);
78  
79          ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
80          assertNotNull(consoleAppender);
81          StatusChecker checker = new StatusChecker(context);
82          checker.assertIsErrorFree();
83      }
84  
85      @SuppressWarnings("rawtypes")
86      @Test
87      public void conditionalConsoleApp_IF_THEN_False() throws JoranException, IOException, InterruptedException {
88  
89          String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "conditional/conditionalConsoleApp.xml";
90          configure(configFileAsStr);
91          FileAppender fileAppender = (FileAppender) root.getAppender("FILE");
92          assertNotNull(fileAppender);
93  
94          ConsoleAppender consoleAppender = (ConsoleAppender) root.getAppender("CON");
95          assertNull(consoleAppender);
96          StatusChecker checker = new StatusChecker(context);
97          checker.assertIsErrorFree();
98      }
99  
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 }