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