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 java.util.Arrays; 017import java.util.HashMap; 018import java.util.Stack; 019 020import ch.qos.logback.core.joran.action.PropertyAction; 021import ch.qos.logback.core.joran.spi.ElementSelector; 022import org.junit.After; 023import org.junit.Before; 024import org.junit.Test; 025 026import ch.qos.logback.core.Context; 027import ch.qos.logback.core.ContextBase; 028import ch.qos.logback.core.joran.TrivialConfigurator; 029import ch.qos.logback.core.joran.action.Action; 030import ch.qos.logback.core.joran.action.NOPAction; 031import ch.qos.logback.core.joran.action.ext.StackAction; 032import ch.qos.logback.core.joran.spi.JoranException; 033import ch.qos.logback.core.testUtil.CoreTestConstants; 034import ch.qos.logback.core.testUtil.RandomUtil; 035import ch.qos.logback.core.testUtil.StatusChecker; 036import ch.qos.logback.core.util.StatusPrinter; 037 038import static org.junit.Assert.*; 039 040public class IfThenElseTest { 041 042 Context context = new ContextBase(); 043 StatusChecker checker = new StatusChecker(context); 044 TrivialConfigurator tc; 045 int diff = RandomUtil.getPositiveInt(); 046 static final String CONDITIONAL_DIR_PREFIX = CoreTestConstants.JORAN_INPUT_PREFIX + "conditional/"; 047 048 String ki1 = "ki1"; 049 String val1 = "val1"; 050 String sysKey = "sysKey"; 051 String dynaKey = "dynaKey"; 052 053 StackAction stackAction = new StackAction(); 054 055 @Before 056 public void setUp() throws Exception { 057 HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>(); 058 rulesMap.put(new ElementSelector("x"), new NOPAction()); 059 rulesMap.put(new ElementSelector("x/stack"), stackAction); 060 rulesMap.put(new ElementSelector("x/property"), new PropertyAction()); 061 rulesMap.put(new ElementSelector("*/if"), new IfAction()); 062 rulesMap.put(new ElementSelector("*/if/then"), new ThenAction()); 063 rulesMap.put(new ElementSelector("*/if/then/*"), new NOPAction()); 064 rulesMap.put(new ElementSelector("*/if/else"), new ElseAction()); 065 rulesMap.put(new ElementSelector("*/if/else/*"), new NOPAction()); 066 067 tc = new TrivialConfigurator(rulesMap); 068 tc.setContext(context); 069 } 070 071 @After 072 public void tearDown() throws Exception { 073 StatusPrinter.printIfErrorsOccured(context); 074 System.clearProperty(sysKey); 075 } 076 077 @Test 078 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException { 079 context.putProperty(ki1, val1); 080 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml"); 081 verifyConfig(new String[] { "BEGIN", "a", "END" }); 082 } 083 084 @Test 085 public void whenLocalPropertyIsSet_IfThenBranchIsEvaluated() throws JoranException { 086 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "if_localProperty.xml"); 087 verifyConfig(new String[] { "BEGIN", "a", "END" }); 088 } 089 090 @Test 091 public void whenNoPropertyIsDefined_ElseBranchIsEvaluated() throws JoranException { 092 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "if0.xml"); 093 verifyConfig(new String[] { "BEGIN", "b", "END" }); 094 } 095 096 @Test 097 public void whenContextPropertyIsSet_IfThenBranchIsEvaluated_NO_ELSE_DEFINED() throws JoranException { 098 context.putProperty(ki1, val1); 099 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml"); 100 verifyConfig(new String[] { "BEGIN", "a", "END" }); 101 } 102 103 @Test 104 public void whenNoPropertyIsDefined_IfThenBranchIsNotEvaluated_NO_ELSE_DEFINED() throws JoranException { 105 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifWithoutElse.xml"); 106 verifyConfig(new String[] { "BEGIN", "END" }); 107 assertTrue(checker.isErrorFree(0)); 108 } 109 110 @Test 111 public void nestedIf() throws JoranException { 112 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "nestedIf.xml"); 113 verifyConfig(new String[] { "BEGIN", "a", "c", "END" }); 114 assertTrue(checker.isErrorFree(0)); 115 } 116 117 @Test 118 public void useNonExistenceOfSystemPropertyToDefineAContextProperty() throws JoranException { 119 assertNull(System.getProperty(sysKey)); 120 assertNull(context.getProperty(dynaKey)); 121 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml"); 122 System.out.println(dynaKey + "=" + context.getProperty(dynaKey)); 123 assertNotNull(context.getProperty(dynaKey)); 124 } 125 126 @Test 127 public void noContextPropertyShouldBeDefinedIfSystemPropertyExists() throws JoranException { 128 System.setProperty(sysKey, "a"); 129 assertNull(context.getProperty(dynaKey)); 130 System.out.println("before " + dynaKey + "=" + context.getProperty(dynaKey)); 131 tc.doConfigure(CONDITIONAL_DIR_PREFIX + "ifSystem.xml"); 132 System.out.println(dynaKey + "=" + context.getProperty(dynaKey)); 133 assertNull(context.getProperty(dynaKey)); 134 } 135 136 private void verifyConfig(String[] expected) { 137 Stack<String> witness = new Stack<String>(); 138 witness.addAll(Arrays.asList(expected)); 139 assertEquals(witness, stackAction.getStack()); 140 } 141 142}