View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 v1.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  package ch.qos.logback.core.joran.conditional;
15  
16  import static org.junit.Assert.assertFalse;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import ch.qos.logback.core.Context;
25  import ch.qos.logback.core.ContextBase;
26  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
27  import ch.qos.logback.core.testUtil.RandomUtil;
28  
29  public class PropertyEvalScriptBuilderTest {
30  
31      Context context = new ContextBase();
32      ModelInterpretationContext localPropContainer = new ModelInterpretationContext(context);
33      PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder(localPropContainer);
34      int diff = RandomUtil.getPositiveInt();
35  
36      String k = "ka" + diff;
37      String v = "va";
38      String containsScript = "p(\"" + k + "\").contains(\"" + v + "\")";
39  
40      String isNullScriptStr = "isNull(\"" + k + "\")";
41      String isDefiedScriptStr = "isDefined(\"" + k + "\")";
42  
43      @Before
44      public void setUp() {
45          context.setName("c" + diff);
46          pesb.setContext(context);
47      }
48  
49      @After
50      public void tearDown() {
51          System.clearProperty(k);
52      }
53  
54      void buildAndAssertTrue(String scriptStr) throws Exception {
55          Condition condition = pesb.build(scriptStr);
56          assertNotNull(condition);
57          assertTrue(condition.evaluate());
58      }
59  
60      void buildAndAssertFalse(String scriptStr) throws Exception {
61          Condition condition = pesb.build(scriptStr);
62          assertNotNull(condition);
63          assertFalse(condition.evaluate());
64      }
65  
66      @Test
67      public void existingLocalPropertyShouldEvaluateToTrue() throws Exception {
68          localPropContainer.addSubstitutionProperty(k, v);
69          buildAndAssertTrue(containsScript);
70      }
71  
72      @Test
73      public void existingContextPropertyShouldEvaluateToTrue() throws Exception {
74          context.putProperty(k, v);
75          buildAndAssertTrue(containsScript);
76      }
77  
78      @Test
79      public void existingSystemPropertyShouldEvaluateToTrue() throws Exception {
80          System.setProperty(k, v);
81          buildAndAssertTrue(containsScript);
82      }
83  
84      @Test
85      public void isNullForExistingLocalProperty() throws Exception {
86          localPropContainer.addSubstitutionProperty(k, v);
87          buildAndAssertFalse(isNullScriptStr);
88      }
89  
90      @Test
91      public void isNullForExistingContextProperty() throws Exception {
92          context.putProperty(k, v);
93          buildAndAssertFalse(isNullScriptStr);
94      }
95  
96      @Test
97      public void isNullForExistingSystemProperty() throws Exception {
98          System.setProperty(k, v);
99          buildAndAssertFalse(isNullScriptStr);
100     }
101 
102     @Test
103     public void inexistentPropertyShouldEvaluateToFalse() throws Exception {
104         buildAndAssertFalse(containsScript);
105     }
106 
107     @Test
108     public void isNullForInexistentPropertyShouldEvaluateToTrue() throws Exception {
109         buildAndAssertTrue(isNullScriptStr);
110     }
111 
112     public void isDefinedForIExistimgtPropertyShouldEvaluateToTrue() throws Exception {
113         localPropContainer.addSubstitutionProperty(k, v);
114         buildAndAssertTrue(isDefiedScriptStr);
115     }
116 
117     @Test
118     public void isDefinedForInexistentPropertyShouldEvaluateToTrue() throws Exception {
119         buildAndAssertFalse(isDefiedScriptStr);
120     }
121 
122 }