1
2
3
4
5
6
7
8
9
10
11
12
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.ByPropertiesConditionAction;
28 import ch.qos.logback.core.joran.conditional.ElseAction;
29 import ch.qos.logback.core.joran.conditional.IfAction;
30 import ch.qos.logback.core.joran.conditional.ThenAction;
31 import ch.qos.logback.core.joran.spi.ElementSelector;
32 import ch.qos.logback.core.joran.spi.JoranException;
33 import ch.qos.logback.core.joran.spi.RuleStore;
34 import ch.qos.logback.core.model.ImplicitModel;
35 import ch.qos.logback.core.model.PropertyModel;
36 import ch.qos.logback.core.model.conditional.ByPropertiesConditionModel;
37 import ch.qos.logback.core.model.conditional.ElseModel;
38 import ch.qos.logback.core.model.conditional.IfModel;
39 import ch.qos.logback.core.model.conditional.ThenModel;
40 import ch.qos.logback.core.model.processor.DefaultProcessor;
41 import ch.qos.logback.core.model.processor.ImplicitModelHandler;
42 import ch.qos.logback.core.model.processor.NOPModelHandler;
43 import ch.qos.logback.core.model.processor.PropertyModelHandler;
44 import ch.qos.logback.core.model.processor.conditional.ByPropertiesConditionModelHandler;
45 import ch.qos.logback.core.model.processor.conditional.ElseModelHandler;
46 import ch.qos.logback.core.model.processor.conditional.IfModelHandler;
47 import ch.qos.logback.core.model.processor.conditional.ThenModelHandler;
48 import ch.qos.logback.core.status.Status;
49 import ch.qos.logback.core.status.StatusUtil;
50 import ch.qos.logback.core.testUtil.RandomUtil;
51 import ch.qos.logback.core.util.StatusPrinter;
52 import ch.qos.logback.core.util.StatusPrinter2;
53 import org.junit.jupiter.api.AfterEach;
54 import org.junit.jupiter.api.Assertions;
55 import org.junit.jupiter.api.BeforeEach;
56 import org.junit.jupiter.api.Test;
57
58 import java.util.Arrays;
59 import java.util.HashMap;
60 import java.util.Stack;
61 import java.util.function.Supplier;
62
63 public class IfThenElseTest {
64
65 Context context = new ContextBase();
66 StatusUtil checker = new StatusUtil(context);
67 StatusPrinter2 statusPrinter2 = new StatusPrinter2();
68 BlackboxSimpleConfigurator simpleConfigurator;
69 int diff = RandomUtil.getPositiveInt();
70 static final String CONDITIONAL_DIR_PREFIX = BlackboxCoreTestConstants.JORAN_INPUT_PREFIX + "conditional/";
71
72 String ki1 = "ki1";
73 String val1 = "val1";
74 String sysKey = "sysKey";
75 String dynaKey = "dynaKey";
76
77 @BeforeEach
78 public void setUp() throws Exception {
79 HashMap<ElementSelector, Supplier<Action>> rulesMap = new HashMap<>();
80 rulesMap.put(new ElementSelector("x"), BlackboxTopElementAction::new);
81 rulesMap.put(new ElementSelector("x/stack"), BlackboxStackAction::new);
82 rulesMap.put(new ElementSelector("x/property"), PropertyAction::new);
83 rulesMap.put(new ElementSelector("*/condition"), ByPropertiesConditionAction::new);
84 rulesMap.put(new ElementSelector("*/if"), IfAction::new);
85 rulesMap.put(new ElementSelector("*/if/then"), ThenAction::new);
86 rulesMap.put(new ElementSelector("*/if/else"), ElseAction::new);
87
88 simpleConfigurator = new BlackboxSimpleConfigurator(rulesMap) {
89
90 @Override
91 protected void addElementSelectorAndActionAssociations(RuleStore rs) {
92 super.addElementSelectorAndActionAssociations(rs);
93
94 rs.addTransparentPathPart("if");
95 rs.addTransparentPathPart("then");
96 rs.addTransparentPathPart("else");
97
98 }
99
100 @Override
101 protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
102 defaultProcessor.addHandler(BlackboxTopModel.class, NOPModelHandler::makeInstance);
103
104 defaultProcessor.addHandler(BlackboxStackModel.class, BlackboxStackModelHandler::makeInstance);
105 defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
106 defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
107 defaultProcessor.addHandler(ByPropertiesConditionModel.class, ByPropertiesConditionModelHandler::makeInstance);
108 defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance);
109 defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance);
110 defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance);
111 }
112 };
113
114 simpleConfigurator.setContext(context);
115 }
116
117 @AfterEach
118 public void tearDown() throws Exception {
119 statusPrinter2.printIfErrorsOccured(context);
120 System.clearProperty(sysKey);
121 }
122
123 @Test
124 public void ifWithExec() throws JoranException {
125 context.putProperty(ki1, val1);
126 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithExec.xml");
127 checker.containsException(org.codehaus.commons.compiler.CompileException.class);
128 checker.containsMatch(Status.ERROR, "Failed to parse condition");
129 }
130
131 @Test
132 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
133 context.putProperty(ki1, val1);
134 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
135 verifyConfig(new String[] { "BEGIN", "a", "END" });
136 }
137
138 @Test
139 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_WithoutJoran() throws JoranException {
140 context.putProperty(ki1, val1);
141
142 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0_NoJoran.xml");
143 verifyConfig(new String[] { "BEGIN", "a", "END" });
144 }
145
146 @Test
147 public void ifWithNew() throws JoranException {
148 context.putProperty(ki1, val1);
149 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifNew.xml");
150 checker.containsMatch(Status.ERROR, IfModelHandler.NEW_OPERATOR_DISALLOWED_MSG);
151 checker.containsMatch(Status.ERROR, IfModelHandler.NEW_OPERATOR_DISALLOWED_SEE);
152 verifyConfig(new String[] { "BEGIN", "END" });
153 }
154
155
156 @Test
157 public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException {
158 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty.xml");
159 verifyConfig(new String[] { "BEGIN", "a", "END" });
160 }
161
162 @Test
163 public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated_NoJoran() throws JoranException {
164 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty_NoJoran.xml");
165 verifyConfig(new String[] { "BEGIN", "a", "END" });
166 }
167
168 @Test
169 public void whenNoPropertyIsDefined_ElseBranchIsEvaluated() throws JoranException {
170 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml");
171 verifyConfig(new String[] { "BEGIN", "b", "END" });
172 }
173
174 @Test
175 public void whenNoPropertyIsDefined_ElseBranchIsEvaluated_NoJoran() throws JoranException {
176 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "if0_NoJoran.xml");
177 verifyConfig(new String[] { "BEGIN", "b", "END" });
178 }
179
180
181 @Test
182 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED() throws JoranException {
183 context.putProperty(ki1, val1);
184 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
185 verifyConfig(new String[] { "BEGIN", "a", "END" });
186 }
187
188 @Test
189 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED_NoJoran() throws JoranException {
190 context.putProperty(ki1, val1);
191 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse_NoJoran.xml");
192 verifyConfig(new String[] { "BEGIN", "a", "END" });
193 }
194
195 @Test
196 public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED() throws JoranException {
197 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml");
198 verifyConfig(new String[] { "BEGIN", "END" });
199 Assertions.assertTrue(checker.isErrorFree(0));
200 }
201
202 @Test
203 public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED_NoJoran() throws JoranException {
204 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse_NoJoran.xml");
205 verifyConfig(new String[] { "BEGIN", "END" });
206 Assertions.assertTrue(checker.isErrorFree(0));
207 }
208
209 @Test
210 public void nestedIf() throws JoranException {
211 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "nestedIf.xml");
212
213 verifyConfig(new String[] { "BEGIN", "a", "c", "END" });
214 Assertions.assertTrue(checker.isErrorFree(0));
215 }
216 @Test
217 public void nestedIf_NoJoran() throws JoranException {
218 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "nestedIf_NoJoran.xml");
219
220 verifyConfig(new String[] { "BEGIN", "a", "c", "END" });
221 Assertions.assertTrue(checker.isErrorFree(0));
222 }
223
224 @Test
225 public void useNonExistenceOfSystemPropertyToDefineAContextProperty() throws JoranException {
226 Assertions.assertNull(System.getProperty(sysKey));
227 Assertions.assertNull(context.getProperty(dynaKey));
228 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml");
229
230 Assertions.assertNotNull(context.getProperty(dynaKey));
231 }
232 @Test
233 public void useNonExistenceOfSystemPropertyToDefineAContextProperty_NoJoran() throws JoranException {
234 Assertions.assertNull(System.getProperty(sysKey));
235 Assertions.assertNull(context.getProperty(dynaKey));
236 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem_NoJoran.xml");
237
238 Assertions.assertNotNull(context.getProperty(dynaKey));
239 }
240
241 @Test
242 public void noContextPropertyShouldBeDefinedIfSystemPropertyExists() throws JoranException {
243 System.setProperty(sysKey, "a");
244 Assertions.assertNull(context.getProperty(dynaKey));
245 System.out.println("before " + dynaKey + "=" + context.getProperty(dynaKey));
246 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml");
247 System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
248 Assertions.assertNull(context.getProperty(dynaKey));
249 }
250
251 @Test
252 public void noContextPropertyShouldBeDefinedIfSystemPropertyExists_NoJoran() throws JoranException {
253 System.setProperty(sysKey, "a");
254 Assertions.assertNull(context.getProperty(dynaKey));
255 System.out.println("before " + dynaKey + "=" + context.getProperty(dynaKey));
256 simpleConfigurator.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem_NoJoran.xml");
257 System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
258 Assertions.assertNull(context.getProperty(dynaKey));
259 }
260
261
262 private void verifyConfig(String[] expected) {
263 Stack<String> witness = new Stack<>();
264 witness.addAll(Arrays.asList(expected));
265
266 @SuppressWarnings({ "unchecked", "rawtypes" })
267 Stack<String> aStack = (Stack) context.getObject(BlackboxStackModelHandler.STACK_TEST);
268 Assertions.assertEquals(witness, aStack);
269 }
270
271 }