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.assertEquals; 017 018import java.util.Arrays; 019import java.util.HashMap; 020import java.util.Stack; 021 022import ch.qos.logback.core.joran.spi.ElementSelector; 023import org.junit.After; 024import org.junit.Before; 025import org.junit.Test; 026 027import ch.qos.logback.core.Context; 028import ch.qos.logback.core.ContextBase; 029import ch.qos.logback.core.joran.TrivialConfigurator; 030import ch.qos.logback.core.joran.action.Action; 031import ch.qos.logback.core.joran.action.IncludeAction; 032import ch.qos.logback.core.joran.action.NOPAction; 033import ch.qos.logback.core.joran.action.ext.StackAction; 034import ch.qos.logback.core.joran.spi.JoranException; 035import ch.qos.logback.core.testUtil.CoreTestConstants; 036import ch.qos.logback.core.testUtil.RandomUtil; 037import ch.qos.logback.core.util.StatusPrinter; 038 039public class IfThenElseAndIncludeCompositionTest { 040 041 Context context = new ContextBase(); 042 TrivialConfigurator tc; 043 int diff = RandomUtil.getPositiveInt(); 044 static final String CONDITIONAL_DIR_PREFIX = CoreTestConstants.JORAN_INPUT_PREFIX + "conditional/"; 045 046 final static String THEN_FILE_TO_INCLUDE_KEY = "thenFileToInclude"; 047 final static String ELSE_FILE_TO_INCLUDE_KEY = "elseFileToInclude"; 048 049 static final String NESTED_INCLUDE_FILE = CONDITIONAL_DIR_PREFIX + "nestedInclude.xml"; 050 static final String THEN_FILE_TO_INCLUDE = CONDITIONAL_DIR_PREFIX + "includedA.xml"; 051 static final String ELSE_FILE_TO_INCLUDE = CONDITIONAL_DIR_PREFIX + "includedB.xml"; 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("*/if"), new IfAction()); 061 rulesMap.put(new ElementSelector("*/if/then"), new ThenAction()); 062 rulesMap.put(new ElementSelector("*/if/then/*"), new NOPAction()); 063 rulesMap.put(new ElementSelector("*/if/else"), new ElseAction()); 064 rulesMap.put(new ElementSelector("*/if/else/*"), new NOPAction()); 065 rulesMap.put(new ElementSelector("x/include"), new IncludeAction()); 066 067 tc = new TrivialConfigurator(rulesMap); 068 tc.setContext(context); 069 } 070 071 @After 072 public void tearDown() throws Exception { 073 StatusPrinter.printInCaseOfErrorsOrWarnings(context); 074 context = null; 075 // StackAction.reset(); 076 } 077 078 @Test 079 public void includeNestedWithinIf() throws JoranException { 080 context.putProperty(THEN_FILE_TO_INCLUDE_KEY, THEN_FILE_TO_INCLUDE); 081 context.putProperty(ELSE_FILE_TO_INCLUDE_KEY, ELSE_FILE_TO_INCLUDE); 082 tc.doConfigure(NESTED_INCLUDE_FILE); 083 verifyConfig(new String[] { "BEGIN", "e0", "IncludedB0", "e1", "END" }); 084 } 085 086 void verifyConfig(String[] expected) { 087 Stack<String> witness = new Stack<String>(); 088 witness.addAll(Arrays.asList(expected)); 089 assertEquals(witness, stackAction.getStack()); 090 } 091 092}