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.access.joran;
15  
16  import ch.qos.logback.access.AccessTestConstants;
17  import ch.qos.logback.access.spi.AccessContext;
18  import ch.qos.logback.access.spi.IAccessEvent;
19  import ch.qos.logback.core.ConsoleAppender;
20  import ch.qos.logback.core.joran.spi.JoranException;
21  import ch.qos.logback.core.read.ListAppender;
22  import ch.qos.logback.core.testUtil.CoreTestConstants;
23  import ch.qos.logback.core.testUtil.RandomUtil;
24  import ch.qos.logback.core.testUtil.StatusChecker;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import java.io.IOException;
30  import java.net.InetAddress;
31  import java.net.UnknownHostException;
32  
33  import static org.junit.Assert.assertNotNull;
34  import static org.junit.Assert.assertNull;
35  import static org.junit.Assert.assertTrue;
36  
37  /**
38   * @author Ceki Gülcü
39   */
40  public class ConditionalTest {
41  
42      AccessContext context = new AccessContext();
43      StatusChecker checker = new StatusChecker(context);
44  
45      int diff = RandomUtil.getPositiveInt();
46      String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
47  
48      @Before
49      public void setUp() {
50          InetAddress localhost = null;
51          try {
52              localhost = InetAddress.getLocalHost();
53              context.putProperty("aHost", localhost.getHostName());
54          } catch (UnknownHostException e) {
55              e.printStackTrace();
56          }
57      }
58  
59      void configure(String file) throws JoranException {
60          JoranConfigurator jc = new JoranConfigurator();
61          jc.setContext(context);
62          jc.doConfigure(file);
63      }
64  
65      @Test
66      public void conditionalConsoleApp_IF_THEN_True() throws JoranException, UnknownHostException {
67          configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/conditional/conditionalConsole.xml");
68          ConsoleAppender<IAccessEvent> consoleAppender = (ConsoleAppender<IAccessEvent>) context.getAppender("CON");
69          assertNotNull(consoleAppender);
70          assertTrue(checker.isErrorFree(0));
71      }
72  
73      @Test
74      public void conditionalConsoleApp_IF_THEN_False() throws JoranException, IOException, InterruptedException {
75          context.putProperty("aHost", null);
76          configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/conditional/conditionalConsole.xml");
77  
78          ConsoleAppender<IAccessEvent> consoleAppender = (ConsoleAppender<IAccessEvent>) context.getAppender("CON");
79          assertNull(consoleAppender);
80  
81          StatusChecker checker = new StatusChecker(context);
82          assertTrue(checker.isErrorFree(0));
83      }
84  
85      @Test
86      public void conditionalConsoleApp_ELSE() throws JoranException, IOException, InterruptedException {
87          configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/conditional/conditionalConsole_ELSE.xml");
88          ConsoleAppender<IAccessEvent> consoleAppender = (ConsoleAppender<IAccessEvent>) context.getAppender("CON");
89          assertNull(consoleAppender);
90  
91          ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) context.getAppender("LIST");
92          assertNotNull(listAppender);
93          assertTrue(checker.isErrorFree(0));
94      }
95  }