1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.core.boolex;
16
17 import ch.qos.logback.core.Context;
18 import ch.qos.logback.core.ContextBase;
19 import ch.qos.logback.core.model.processor.ModelInterpretationContext;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 public class ExpressionPropertyConditionTest {
27
28
29 Context context = new ContextBase();
30 ModelInterpretationContext mic = new ModelInterpretationContext(context);
31 ExpressionPropertyCondition epc = new ExpressionPropertyCondition();
32
33
34 static final String SMOKE_KEY = "SMOKE_KEY";
35 static final String UNDEFINED_KEY = "UNDEFINED_KEY";
36 static final String SMOKE_VALUE = "SMOKE_VALUE";
37
38 @BeforeEach
39 public void setUp() throws Exception {
40 epc.setContext(context);
41 epc.setLocalPropertyContainer(mic);
42 context.putProperty(SMOKE_KEY, SMOKE_VALUE);
43 }
44
45 @Test
46 public void smokeDefined() {
47 String expression = String.format("isDefined(\"%s\")", SMOKE_KEY);
48 check(expression, true);
49 }
50
51
52 @Test
53 public void notSmokeDefined() {
54 String expression = String.format("!isDefined(\"%s\")", UNDEFINED_KEY);
55 check(expression, true);
56 }
57
58 @Test
59 public void notSmokeDefined_and_nullBoo() {
60 String expression = String.format("!isDefined(\"%s\") && isNull(\"%s\")", UNDEFINED_KEY, "BOO");
61 check(expression, true);
62 }
63
64 @Test
65 public void paranthesisSmokeDefined() {
66 String expression = String.format("(isDefined(\"%s\"))", SMOKE_KEY);
67 check(expression, true);
68 }
69
70 @Test
71 public void SmokeDefinedAndIsNull_AND_IsNull() {
72 String expression = String.format("(isDefined(\"%s\") || isNull(\"%s\")) && isNull(\"x\")", SMOKE_KEY, UNDEFINED_KEY);
73 check(expression, true);
74 }
75
76 @Test
77 public void NOTSmokeDefinedAndIsNull_AND_IsNull() {
78 String expression = String.format("!(isDefined(\"%s\") || isNull(\"%s\")) && isNull(\"x\")", SMOKE_KEY, UNDEFINED_KEY);
79 check(expression, false);
80 }
81
82 @Test
83 public void smokeBiFunction() {
84 String expression = String.format("propertyEquals(\"%s\", \"%s\")" , SMOKE_KEY, SMOKE_VALUE);
85 check(expression, true);
86 }
87
88
89 @Test
90 public void propertyContains() {
91 String expression = String.format("propertyContains(\"%s\", \"%s\")" , SMOKE_KEY, SMOKE_VALUE.substring(0, 2));
92 check(expression, true);
93 }
94
95
96 @Test
97 public void notPropertyContainsX() {
98 String expression = String.format("!propertyContains(\"%s\", \"%s\")" , SMOKE_KEY, SMOKE_VALUE+"x");
99 check(expression, true);
100 }
101
102 @Test
103 public void propertyEqualsOrIsNull() {
104 String expression = String.format("!propertyEquals(\"%s\", \"%s\") || !isNull(\"%s\")" , SMOKE_KEY, SMOKE_VALUE, UNDEFINED_KEY);
105 check(expression, false);
106 }
107
108
109 void check(String expression, boolean expected) {
110 epc.setExpression(expression);
111 epc.start();
112
113 if(expected) {
114 assertTrue(epc.evaluate());
115 } else {
116 assertFalse(epc.evaluate());
117 }
118
119 }
120 }