001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.conditional;
015
016import static org.junit.Assert.*;
017
018import ch.qos.logback.core.joran.spi.InterpretationContext;
019
020import org.junit.After;
021import org.junit.Before;
022import org.junit.Test;
023
024import ch.qos.logback.core.Context;
025import ch.qos.logback.core.ContextBase;
026import ch.qos.logback.core.testUtil.RandomUtil;
027
028public class PropertyEvalScriptBuilderTest {
029
030    Context context = new ContextBase();
031    InterpretationContext localPropContainer = new InterpretationContext(context, null);
032    PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder(localPropContainer);
033    int diff = RandomUtil.getPositiveInt();
034
035    String k = "ka" + diff;
036    String v = "va";
037    String containsScript = "p(\"" + k + "\").contains(\"" + v + "\")";
038
039    String isNullScriptStr = "isNull(\"" + k + "\")";
040    String isDefiedScriptStr = "isDefined(\"" + k + "\")";
041
042    @Before
043    public void setUp() {
044        context.setName("c" + diff);
045        pesb.setContext(context);
046    }
047
048    @After
049    public void tearDown() {
050        System.clearProperty(k);
051    }
052
053    void buildAndAssertTrue(String scriptStr) throws Exception {
054        Condition condition = pesb.build(scriptStr);
055        assertNotNull(condition);
056        assertTrue(condition.evaluate());
057    }
058
059    void buildAndAssertFalse(String scriptStr) throws Exception {
060        Condition condition = pesb.build(scriptStr);
061        assertNotNull(condition);
062        assertFalse(condition.evaluate());
063    }
064
065    @Test
066    public void existingLocalPropertyShouldEvaluateToTrue() throws Exception {
067        localPropContainer.addSubstitutionProperty(k, v);
068        buildAndAssertTrue(containsScript);
069    }
070
071    @Test
072    public void existingContextPropertyShouldEvaluateToTrue() throws Exception {
073        context.putProperty(k, v);
074        buildAndAssertTrue(containsScript);
075    }
076
077    @Test
078    public void existingSystemPropertyShouldEvaluateToTrue() throws Exception {
079        System.setProperty(k, v);
080        buildAndAssertTrue(containsScript);
081    }
082
083    @Test
084    public void isNullForExistingLocalProperty() throws Exception {
085        localPropContainer.addSubstitutionProperty(k, v);
086        buildAndAssertFalse(isNullScriptStr);
087    }
088
089    @Test
090    public void isNullForExistingContextProperty() throws Exception {
091        context.putProperty(k, v);
092        buildAndAssertFalse(isNullScriptStr);
093    }
094
095    @Test
096    public void isNullForExistingSystemProperty() throws Exception {
097        System.setProperty(k, v);
098        buildAndAssertFalse(isNullScriptStr);
099    }
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}