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.core.joran.conditional;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.util.Arrays;
19  import java.util.HashMap;
20  import java.util.Stack;
21  
22  import ch.qos.logback.core.joran.spi.ElementSelector;
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import ch.qos.logback.core.Context;
28  import ch.qos.logback.core.ContextBase;
29  import ch.qos.logback.core.joran.TrivialConfigurator;
30  import ch.qos.logback.core.joran.action.Action;
31  import ch.qos.logback.core.joran.action.IncludeAction;
32  import ch.qos.logback.core.joran.action.NOPAction;
33  import ch.qos.logback.core.joran.action.ext.StackAction;
34  import ch.qos.logback.core.joran.spi.JoranException;
35  import ch.qos.logback.core.testUtil.CoreTestConstants;
36  import ch.qos.logback.core.testUtil.RandomUtil;
37  import ch.qos.logback.core.util.StatusPrinter;
38  
39  public class IfThenElseAndIncludeCompositionTest {
40  
41      Context context = new ContextBase();
42      TrivialConfigurator tc;
43      int diff = RandomUtil.getPositiveInt();
44      static final String CONDITIONAL_DIR_PREFIX = CoreTestConstants.JORAN_INPUT_PREFIX + "conditional/";
45  
46      final static String THEN_FILE_TO_INCLUDE_KEY = "thenFileToInclude";
47      final static String ELSE_FILE_TO_INCLUDE_KEY = "elseFileToInclude";
48  
49      static final String NESTED_INCLUDE_FILE = CONDITIONAL_DIR_PREFIX + "nestedInclude.xml";
50      static final String THEN_FILE_TO_INCLUDE = CONDITIONAL_DIR_PREFIX + "includedA.xml";
51      static final String ELSE_FILE_TO_INCLUDE = CONDITIONAL_DIR_PREFIX + "includedB.xml";
52  
53      StackAction stackAction = new StackAction();
54  
55      @Before
56      public void setUp() throws Exception {
57          HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
58          rulesMap.put(new ElementSelector("x"), new NOPAction());
59          rulesMap.put(new ElementSelector("x/stack"), stackAction);
60          rulesMap.put(new ElementSelector("*/if"), new IfAction());
61          rulesMap.put(new ElementSelector("*/if/then"), new ThenAction());
62          rulesMap.put(new ElementSelector("*/if/then/*"), new NOPAction());
63          rulesMap.put(new ElementSelector("*/if/else"), new ElseAction());
64          rulesMap.put(new ElementSelector("*/if/else/*"), new NOPAction());
65          rulesMap.put(new ElementSelector("x/include"), new IncludeAction());
66  
67          tc = new TrivialConfigurator(rulesMap);
68          tc.setContext(context);
69      }
70  
71      @After
72      public void tearDown() throws Exception {
73          StatusPrinter.printInCaseOfErrorsOrWarnings(context);
74          context = null;
75          // StackAction.reset();
76      }
77  
78      @Test
79      public void includeNestedWithinIf() throws JoranException {
80          context.putProperty(THEN_FILE_TO_INCLUDE_KEY, THEN_FILE_TO_INCLUDE);
81          context.putProperty(ELSE_FILE_TO_INCLUDE_KEY, ELSE_FILE_TO_INCLUDE);
82          tc.doConfigure(NESTED_INCLUDE_FILE);
83          verifyConfig(new String[] { "BEGIN", "e0", "IncludedB0", "e1", "END" });
84      }
85  
86      void verifyConfig(String[] expected) {
87          Stack<String> witness = new Stack<String>();
88          witness.addAll(Arrays.asList(expected));
89          assertEquals(witness, stackAction.getStack());
90      }
91  
92  }