View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, 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.blackbox.joran.conditional;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.blackbox.BlackboxCoreTestConstants;
19  import ch.qos.logback.core.blackbox.joran.BlackboxSimpleConfigurator;
20  import ch.qos.logback.core.blackbox.joran.action.BlackboxTopElementAction;
21  import ch.qos.logback.core.blackbox.joran.action.ext.BlackboxStackAction;
22  import ch.qos.logback.core.blackbox.model.BlackboxStackModel;
23  import ch.qos.logback.core.blackbox.model.BlackboxTopModel;
24  import ch.qos.logback.core.blackbox.model.processor.BlackboxStackModelHandler;
25  import ch.qos.logback.core.joran.action.Action;
26  import ch.qos.logback.core.joran.action.PropertyAction;
27  import ch.qos.logback.core.joran.conditional.ElseAction;
28  import ch.qos.logback.core.joran.conditional.IfAction;
29  import ch.qos.logback.core.joran.conditional.ThenAction;
30  import ch.qos.logback.core.joran.spi.ElementSelector;
31  import ch.qos.logback.core.joran.spi.JoranException;
32  import ch.qos.logback.core.joran.spi.RuleStore;
33  import ch.qos.logback.core.model.ImplicitModel;
34  import ch.qos.logback.core.model.PropertyModel;
35  import ch.qos.logback.core.model.conditional.ElseModel;
36  import ch.qos.logback.core.model.conditional.IfModel;
37  import ch.qos.logback.core.model.conditional.ThenModel;
38  import ch.qos.logback.core.model.processor.DefaultProcessor;
39  import ch.qos.logback.core.model.processor.ImplicitModelHandler;
40  import ch.qos.logback.core.model.processor.NOPModelHandler;
41  import ch.qos.logback.core.model.processor.PropertyModelHandler;
42  import ch.qos.logback.core.model.processor.conditional.ElseModelHandler;
43  import ch.qos.logback.core.model.processor.conditional.IfModelHandler;
44  import ch.qos.logback.core.model.processor.conditional.ThenModelHandler;
45  import ch.qos.logback.core.status.StatusUtil;
46  import ch.qos.logback.core.testUtil.CoreTestConstants;
47  import ch.qos.logback.core.testUtil.RandomUtil;
48  import ch.qos.logback.core.util.StatusPrinter;
49  import org.junit.jupiter.api.AfterEach;
50  import org.junit.jupiter.api.Assertions;
51  import org.junit.jupiter.api.BeforeEach;
52  import org.junit.jupiter.api.Test;
53  
54  import java.util.Arrays;
55  import java.util.HashMap;
56  import java.util.Stack;
57  import java.util.function.Supplier;
58  
59  public class IfThenElseTest {
60  
61      Context context = new ContextBase();
62      StatusUtil checker = new StatusUtil(context);
63      BlackboxSimpleConfigurator simpleConfigurator;
64      int diff = RandomUtil.getPositiveInt();
65      static final String CONDITIONAL_DIR_PREFIX = BlackboxCoreTestConstants.JORAN_INPUT_PREFIX + "conditional/";
66  
67      String ki1 = "ki1";
68      String val1 = "val1";
69      String sysKey = "sysKey";
70      String dynaKey = "dynaKey";
71  
72      @BeforeEach
73      public void setUp() throws Exception {
74          HashMap<ElementSelector, Supplier<Action>> rulesMap = new HashMap<>();
75          rulesMap.put(new ElementSelector("x"), BlackboxTopElementAction::new);
76          rulesMap.put(new ElementSelector("x/stack"), BlackboxStackAction::new);
77          rulesMap.put(new ElementSelector("x/property"), PropertyAction::new);
78          rulesMap.put(new ElementSelector("*/if"), IfAction::new);
79          rulesMap.put(new ElementSelector("*/if/then"), ThenAction::new);
80          rulesMap.put(new ElementSelector("*/if/else"), ElseAction::new);
81  
82          simpleConfigurator = new BlackboxSimpleConfigurator(rulesMap) {
83              
84              @Override
85              protected void addElementSelectorAndActionAssociations(RuleStore rs) {
86                  super.addElementSelectorAndActionAssociations(rs);
87                  
88                  rs.addTransparentPathPart("if");
89                  rs.addTransparentPathPart("then");
90                  rs.addTransparentPathPart("else");
91  
92              }
93              
94              @Override
95              protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
96                  defaultProcessor.addHandler(BlackboxTopModel.class, NOPModelHandler::makeInstance);
97                  
98                  defaultProcessor.addHandler(BlackboxStackModel.class, BlackboxStackModelHandler::makeInstance);
99                  defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
100                 defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
101                 defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance);
102                 defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance);
103                 defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance);
104             }
105         };
106         
107         simpleConfigurator.setContext(context);
108     }
109 
110     @AfterEach
111     public void tearDown() throws Exception {
112         StatusPrinter.printIfErrorsOccured(context);
113         System.clearProperty(sysKey);
114     }
115 
116     @Test
117     public void whenContextPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
118         context.putProperty(ki1, val1);
119         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
120         verifyConfig(new String[] { "BEGIN", "a", "END" });
121     }
122 
123     @Test
124     public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
125         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty.xml");
126         verifyConfig(new String[] { "BEGIN", "a", "END" });
127     }
128 
129     @Test
130     public void whenNoPropertyIsDefined_ElseBranchIsEvaluated() throws JoranException {
131         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
132         verifyConfig(new String[] { "BEGIN", "b", "END" });
133     }
134 
135     @Test
136     public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED() throws JoranException {
137         context.putProperty(ki1, val1);
138         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
139         verifyConfig(new String[] { "BEGIN", "a", "END" });
140     }
141 
142     @Test
143     public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED() throws JoranException {
144         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
145         verifyConfig(new String[] { "BEGIN", "END" });
146         Assertions.assertTrue(checker.isErrorFree(0));
147     }
148 
149     @Test
150     public void nestedIf() throws JoranException {
151         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "nestedIf.xml");
152         //StatusPrinter.print(context);
153         verifyConfig(new String[] { "BEGIN", "a", "c", "END" });
154         Assertions.assertTrue(checker.isErrorFree(0));
155     }
156 
157     @Test
158     public void useNonExistenceOfSystemPropertyToDefineAContextProperty() throws JoranException {
159         Assertions.assertNull(System.getProperty(sysKey));
160         Assertions.assertNull(context.getProperty(dynaKey));
161         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml");
162         System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
163         Assertions.assertNotNull(context.getProperty(dynaKey));
164     }
165 
166     @Test
167     public void noContextPropertyShouldBeDefinedIfSystemPropertyExists() throws JoranException {
168         System.setProperty(sysKey, "a");
169         Assertions.assertNull(context.getProperty(dynaKey));
170         System.out.println("before " + dynaKey + "=" + context.getProperty(dynaKey));
171         simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml");
172         System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
173         Assertions.assertNull(context.getProperty(dynaKey));
174     }
175 
176     private void verifyConfig(String[] expected) {
177         Stack<String> witness = new Stack<>();
178         witness.addAll(Arrays.asList(expected));
179         
180         @SuppressWarnings({ "unchecked", "rawtypes" })
181         Stack<String> aStack = (Stack) context.getObject(BlackboxStackModelHandler.STACK_TEST);
182         Assertions.assertEquals(witness, aStack);
183     }
184 
185 }