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