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 java.util.Arrays;
17  import java.util.HashMap;
18  import java.util.Stack;
19  
20  import ch.qos.logback.core.joran.action.PropertyAction;
21  import ch.qos.logback.core.joran.spi.ElementSelector;
22  import org.junit.After;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import ch.qos.logback.core.Context;
27  import ch.qos.logback.core.ContextBase;
28  import ch.qos.logback.core.joran.TrivialConfigurator;
29  import ch.qos.logback.core.joran.action.Action;
30  import ch.qos.logback.core.joran.action.NOPAction;
31  import ch.qos.logback.core.joran.action.ext.StackAction;
32  import ch.qos.logback.core.joran.spi.JoranException;
33  import ch.qos.logback.core.testUtil.CoreTestConstants;
34  import ch.qos.logback.core.testUtil.RandomUtil;
35  import ch.qos.logback.core.testUtil.StatusChecker;
36  import ch.qos.logback.core.util.StatusPrinter;
37  
38  import static org.junit.Assert.*;
39  
40  public class IfThenElseTest {
41  
42      Context context = new ContextBase();
43      StatusChecker checker = new StatusChecker(context);
44      TrivialConfigurator tc;
45      int diff = RandomUtil.getPositiveInt();
46      static final String CONDITIONAL_DIR_PREFIX = CoreTestConstants.JORAN_INPUT_PREFIX + "conditional/";
47  
48      String ki1 = "ki1";
49      String val1 = "val1";
50      String sysKey = "sysKey";
51      String dynaKey = "dynaKey";
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("x/property"), new PropertyAction());
61          rulesMap.put(new ElementSelector("*/if"), new IfAction());
62          rulesMap.put(new ElementSelector("*/if/then"), new ThenAction());
63          rulesMap.put(new ElementSelector("*/if/then/*"), new NOPAction());
64          rulesMap.put(new ElementSelector("*/if/else"), new ElseAction());
65          rulesMap.put(new ElementSelector("*/if/else/*"), new NOPAction());
66  
67          tc = new TrivialConfigurator(rulesMap);
68          tc.setContext(context);
69      }
70  
71      @After
72      public void tearDown() throws Exception {
73          StatusPrinter.printIfErrorsOccured(context);
74          System.clearProperty(sysKey);
75      }
76  
77      @Test
78      public void whenContextPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
79          context.putProperty(ki1, val1);
80          tc.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
81          verifyConfig(new String[] { "BEGIN", "a", "END" });
82      }
83  
84      @Test
85      public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
86          tc.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty.xml");
87          verifyConfig(new String[] { "BEGIN", "a", "END" });
88      }
89  
90      @Test
91      public void whenNoPropertyIsDefined_ElseBranchIsEvaluated() throws JoranException {
92          tc.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
93          verifyConfig(new String[] { "BEGIN", "b", "END" });
94      }
95  
96      @Test
97      public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED() throws JoranException {
98          context.putProperty(ki1, val1);
99          tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
100         verifyConfig(new String[] { "BEGIN", "a", "END" });
101     }
102 
103     @Test
104     public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED() throws JoranException {
105         tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
106         verifyConfig(new String[] { "BEGIN", "END" });
107         assertTrue(checker.isErrorFree(0));
108     }
109 
110     @Test
111     public void nestedIf() throws JoranException {
112         tc.doConfigure(CONDITIONAL_DIR_PREFIX + "nestedIf.xml");
113         verifyConfig(new String[] { "BEGIN", "a", "c", "END" });
114         assertTrue(checker.isErrorFree(0));
115     }
116 
117     @Test
118     public void useNonExistenceOfSystemPropertyToDefineAContextProperty() throws JoranException {
119         assertNull(System.getProperty(sysKey));
120         assertNull(context.getProperty(dynaKey));
121         tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml");
122         System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
123         assertNotNull(context.getProperty(dynaKey));
124     }
125 
126     @Test
127     public void noContextPropertyShouldBeDefinedIfSystemPropertyExists() throws JoranException {
128         System.setProperty(sysKey, "a");
129         assertNull(context.getProperty(dynaKey));
130         System.out.println("before " + dynaKey + "=" + context.getProperty(dynaKey));
131         tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml");
132         System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
133         assertNull(context.getProperty(dynaKey));
134     }
135 
136     private void verifyConfig(String[] expected) {
137         Stack<String> witness = new Stack<String>();
138         witness.addAll(Arrays.asList(expected));
139         assertEquals(witness, stackAction.getStack());
140     }
141 
142 }