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