2424import ch .qos .logback .core .blackbox .model .processor .BlackboxStackModelHandler ;
2525import ch .qos .logback .core .joran .action .Action ;
2626import ch .qos .logback .core .joran .action .PropertyAction ;
27+ import ch .qos .logback .core .joran .conditional .ByPropertiesConditionAction ;
2728import ch .qos .logback .core .joran .conditional .ElseAction ;
2829import ch .qos .logback .core .joran .conditional .IfAction ;
2930import ch .qos .logback .core .joran .conditional .ThenAction ;
3233import ch .qos .logback .core .joran .spi .RuleStore ;
3334import ch .qos .logback .core .model .ImplicitModel ;
3435import ch .qos .logback .core .model .PropertyModel ;
36+ import ch .qos .logback .core .model .conditional .ByPropertiesConditionModel ;
3537import ch .qos .logback .core .model .conditional .ElseModel ;
3638import ch .qos .logback .core .model .conditional .IfModel ;
3739import ch .qos .logback .core .model .conditional .ThenModel ;
3840import ch .qos .logback .core .model .processor .DefaultProcessor ;
3941import ch .qos .logback .core .model .processor .ImplicitModelHandler ;
4042import ch .qos .logback .core .model .processor .NOPModelHandler ;
4143import ch .qos .logback .core .model .processor .PropertyModelHandler ;
44+ import ch .qos .logback .core .model .processor .conditional .ByPropertiesConditionModelHandler ;
4245import ch .qos .logback .core .model .processor .conditional .ElseModelHandler ;
4346import ch .qos .logback .core .model .processor .conditional .IfModelHandler ;
4447import ch .qos .logback .core .model .processor .conditional .ThenModelHandler ;
4548import ch .qos .logback .core .status .Status ;
4649import ch .qos .logback .core .status .StatusUtil ;
4750import ch .qos .logback .core .testUtil .RandomUtil ;
4851import ch .qos .logback .core .util .StatusPrinter ;
52+ import ch .qos .logback .core .util .StatusPrinter2 ;
4953import org .junit .jupiter .api .AfterEach ;
5054import org .junit .jupiter .api .Assertions ;
5155import org .junit .jupiter .api .BeforeEach ;
@@ -60,6 +64,7 @@ public class IfThenElseTest {
6064
6165 Context context = new ContextBase ();
6266 StatusUtil checker = new StatusUtil (context );
67+ StatusPrinter2 statusPrinter2 = new StatusPrinter2 ();
6368 BlackboxSimpleConfigurator simpleConfigurator ;
6469 int diff = RandomUtil .getPositiveInt ();
6570 static final String CONDITIONAL_DIR_PREFIX = BlackboxCoreTestConstants .JORAN_INPUT_PREFIX + "conditional/" ;
@@ -75,6 +80,7 @@ public void setUp() throws Exception {
7580 rulesMap .put (new ElementSelector ("x" ), BlackboxTopElementAction ::new );
7681 rulesMap .put (new ElementSelector ("x/stack" ), BlackboxStackAction ::new );
7782 rulesMap .put (new ElementSelector ("x/property" ), PropertyAction ::new );
83+ rulesMap .put (new ElementSelector ("*/condition" ), ByPropertiesConditionAction ::new );
7884 rulesMap .put (new ElementSelector ("*/if" ), IfAction ::new );
7985 rulesMap .put (new ElementSelector ("*/if/then" ), ThenAction ::new );
8086 rulesMap .put (new ElementSelector ("*/if/else" ), ElseAction ::new );
@@ -98,6 +104,7 @@ protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
98104 defaultProcessor .addHandler (BlackboxStackModel .class , BlackboxStackModelHandler ::makeInstance );
99105 defaultProcessor .addHandler (PropertyModel .class , PropertyModelHandler ::makeInstance );
100106 defaultProcessor .addHandler (ImplicitModel .class , ImplicitModelHandler ::makeInstance );
107+ defaultProcessor .addHandler (ByPropertiesConditionModel .class , ByPropertiesConditionModelHandler ::makeInstance );
101108 defaultProcessor .addHandler (IfModel .class , IfModelHandler ::makeInstance );
102109 defaultProcessor .addHandler (ThenModel .class , ThenModelHandler ::makeInstance );
103110 defaultProcessor .addHandler (ElseModel .class , ElseModelHandler ::makeInstance );
@@ -109,7 +116,7 @@ protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
109116
110117 @ AfterEach
111118 public void tearDown () throws Exception {
112- StatusPrinter .printIfErrorsOccured (context );
119+ statusPrinter2 .printIfErrorsOccured (context );
113120 System .clearProperty (sysKey );
114121 }
115122
@@ -120,14 +127,22 @@ public void ifWithExec() throws JoranException {
120127 checker .containsException (org .codehaus .commons .compiler .CompileException .class );
121128 checker .containsMatch (Status .ERROR , "Failed to parse condition" );
122129 }
123-
130+ // ----------------------------------------------------------------------------------------------------
124131 @ Test
125132 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated () throws JoranException {
126133 context .putProperty (ki1 , val1 );
127134 simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "if0.xml" );
128135 verifyConfig (new String [] { "BEGIN" , "a" , "END" });
129136 }
130137
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+ // ----------------------------------------------------------------------------------------------------
131146 @ Test
132147 public void ifWithNew () throws JoranException {
133148 context .putProperty (ki1 , val1 );
@@ -137,47 +152,83 @@ public void ifWithNew() throws JoranException {
137152 verifyConfig (new String [] { "BEGIN" , "END" });
138153 }
139154
140-
155+ // ----------------------------------------------------------------------------------------------------
141156 @ Test
142157 public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated () throws JoranException {
143158 simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "if_localProperty.xml" );
144159 verifyConfig (new String [] { "BEGIN" , "a" , "END" });
145160 }
146161
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+ // ----------------------------------------------------------------------------------------------------
147168 @ Test
148169 public void whenNoPropertyIsDefined_ElseBranchIsEvaluated () throws JoranException {
149170 simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "if0.xml" );
150171 verifyConfig (new String [] { "BEGIN" , "b" , "END" });
151172 }
152173
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+
153181 @ Test
154182 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED () throws JoranException {
155183 context .putProperty (ki1 , val1 );
156184 simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml" );
157185 verifyConfig (new String [] { "BEGIN" , "a" , "END" });
158186 }
159187
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+ // ----------------------------------------------------------------------------------------------------
160195 @ Test
161196 public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED () throws JoranException {
162197 simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml" );
163198 verifyConfig (new String [] { "BEGIN" , "END" });
164199 Assertions .assertTrue (checker .isErrorFree (0 ));
165200 }
166201
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+ // ----------------------------------------------------------------------------------------------------
167209 @ Test
168210 public void nestedIf () throws JoranException {
169211 simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "nestedIf.xml" );
170212 //StatusPrinter.print(context);
171213 verifyConfig (new String [] { "BEGIN" , "a" , "c" , "END" });
172214 Assertions .assertTrue (checker .isErrorFree (0 ));
173215 }
216+ @ Test
217+ public void nestedIf_NoJoran () throws JoranException {
218+ simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "nestedIf_NoJoran.xml" );
219+ //StatusPrinter.print(context);
220+ verifyConfig (new String [] { "BEGIN" , "a" , "c" , "END" });
221+ Assertions .assertTrue (checker .isErrorFree (0 ));
222+ }
223+
224+ // ----------------------------------------------------------------------------------------------------
174225
175226 @ Test
176227 public void useNonExistenceOfSystemPropertyToDefineAContextProperty () throws JoranException {
177228 Assertions .assertNull (System .getProperty (sysKey ));
178229 Assertions .assertNull (context .getProperty (dynaKey ));
179230 simpleConfigurator .doConfigure (CONDITIONAL_DIR_PREFIX + "ifSystem.xml" );
180- System .out .println (dynaKey + "=" + context .getProperty (dynaKey ));
231+ // System.out.println(dynaKey + "=" + context.getProperty(dynaKey));
181232 Assertions .assertNotNull (context .getProperty (dynaKey ));
182233 }
183234
0 commit comments