View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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  
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 ch.qos.logback.core.status.testUtil.StatusChecker;
21  import ch.qos.logback.core.util.StatusPrinter;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  import static ch.qos.logback.core.boolex.ExpressionPropertyCondition.MALFORMED_EXPRESSION;
26  import static org.junit.jupiter.api.Assertions.assertFalse;
27  import static org.junit.jupiter.api.Assertions.assertTrue;
28  
29  public class ExpressionPropertyConditionTest {
30  
31  
32      Context context = new ContextBase();
33      ModelInterpretationContext mic = new ModelInterpretationContext(context);
34      ExpressionPropertyCondition epc = new ExpressionPropertyCondition();
35      StatusChecker statusChecker = new StatusChecker(context);
36  
37      static final String SMOKE_KEY = "SMOKE_KEY";
38      static final String UNDEFINED_KEY = "UNDEFINED_KEY";
39      static final String SMOKE_VALUE = "SMOKE_VALUE";
40  
41      @BeforeEach
42      public void setUp() throws Exception {
43          epc.setContext(context);
44          epc.setLocalPropertyContainer(mic);
45          context.putProperty(SMOKE_KEY, SMOKE_VALUE);
46      }
47  
48      @Test
49      public void smokeDefined() {
50          String expression = String.format("isDefined(\"%s\")", SMOKE_KEY);
51          check(expression, true);
52      }
53  
54  
55      @Test
56      public void notSmokeDefined() {
57          String expression = String.format("!isDefined(\"%s\")", UNDEFINED_KEY);
58          check(expression, true);
59      }
60  
61      @Test
62      public void notSmokeDefined_and_nullBoo() {
63          String expression = String.format("!isDefined(\"%s\") && isNull(\"%s\")", UNDEFINED_KEY, "BOO");
64          check(expression, true);
65      }
66  
67      @Test
68      public void paranthesisSmokeDefined() {
69          String expression = String.format("(isDefined(\"%s\"))", SMOKE_KEY);
70          check(expression, true);
71      }
72  
73      @Test
74      public void SmokeDefinedAndIsNull_AND_IsNull() {
75          String expression = String.format("(isDefined(\"%s\") || isNull(\"%s\")) && isNull(\"x\")", SMOKE_KEY, UNDEFINED_KEY);
76          check(expression, true);
77      }
78  
79      @Test
80      public void NOTSmokeDefinedAndIsNull_AND_IsNull() {
81          String expression = String.format("!(isDefined(\"%s\") || isNull(\"%s\")) && isNull(\"x\")", SMOKE_KEY, UNDEFINED_KEY);
82          check(expression, false);
83      }
84  
85      @Test
86      public void smokeBiFunction() {
87          String expression = String.format("propertyEquals(\"%s\", \"%s\")" , SMOKE_KEY, SMOKE_VALUE);
88          check(expression, true);
89      }
90  
91  
92      @Test
93      public void propertyContains() {
94          String expression = String.format("propertyContains(\"%s\", \"%s\")" , SMOKE_KEY, SMOKE_VALUE.substring(0, 2));
95          check(expression, true);
96      }
97  
98  
99      @Test
100     public void notPropertyContainsX() {
101         String expression = String.format("!propertyContains(\"%s\", \"%s\")" , SMOKE_KEY, SMOKE_VALUE+"x");
102         check(expression, true);
103     }
104 
105     @Test
106     public void propertyEqualsOrIsNull() {
107         String expression = String.format("!propertyEquals(\"%s\", \"%s\") || !isNull(\"%s\")" , SMOKE_KEY, SMOKE_VALUE, UNDEFINED_KEY);
108         check(expression, false);
109     }
110 
111     @Test
112     public void trueLiteral() {
113         check("true", true);
114     }
115 
116     @Test
117     public void falseLiteral() {
118         check("false", false);
119     }
120 
121     @Test
122     public void trueAndSmokeDefined() {
123         String expression = String.format("true && isDefined(\"%s\")", SMOKE_KEY);
124         check(expression, true);
125     }
126 
127     @Test
128     public void falseOrSmokeDefined() {
129         String expression = String.format("false || isDefined(\"%s\")", SMOKE_KEY);
130         check(expression, true);
131     }
132 
133     @Test
134     public void notFalse() {
135         check("!false", true);
136     }
137 
138     @Test
139     public void trueAndFalse() {
140         check("true && false", false);
141     }
142 
143     @Test
144     public void dotIsUnexpectedCharacter() {
145         epc.setExpression(".");
146         epc.start();
147 
148         assertFalse(epc.isStarted());
149         assertFalse(epc.evaluate());
150 
151         statusChecker.assertContainsMatch(MALFORMED_EXPRESSION);
152     }
153 
154     @Test
155     public void numerical() {
156         epc.setExpression("1 != 2");
157         epc.start();
158         assertFalse(epc.isStarted());
159         assertFalse(epc.evaluate());
160         statusChecker.assertContainsMatch(MALFORMED_EXPRESSION);
161     }
162 
163 
164 
165     void check(String expression, boolean expected) {
166         epc.setExpression(expression);
167         epc.start();
168 
169         if(expected) {
170             assertTrue(epc.evaluate());
171         } else {
172             assertFalse(epc.evaluate());
173         }
174 
175     }
176 }