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; 015 016import static org.junit.Assert.assertEquals; 017import static org.junit.Assert.assertNull; 018import static org.junit.Assert.assertTrue; 019 020import java.util.HashMap; 021import java.util.List; 022 023import javax.xml.parsers.SAXParser; 024import javax.xml.parsers.SAXParserFactory; 025 026import ch.qos.logback.core.joran.spi.ElementSelector; 027import org.junit.Test; 028 029import ch.qos.logback.core.Context; 030import ch.qos.logback.core.ContextBase; 031import ch.qos.logback.core.joran.action.Action; 032import ch.qos.logback.core.joran.action.NOPAction; 033import ch.qos.logback.core.joran.action.ext.BadBeginAction; 034import ch.qos.logback.core.joran.action.ext.BadEndAction; 035import ch.qos.logback.core.joran.action.ext.HelloAction; 036import ch.qos.logback.core.joran.action.ext.TouchAction; 037import ch.qos.logback.core.joran.spi.ActionException; 038import ch.qos.logback.core.status.Status; 039import ch.qos.logback.core.status.StatusManager; 040import ch.qos.logback.core.testUtil.CoreTestConstants; 041 042/** 043 * Test the way Interpreter skips child elements in case of exceptions thrown by 044 * Actions. It also tests addition of status messages in case of exceptions. 045 * 046 * @author Ceki Gulcu 047 */ 048public class SkippingInInterpreterTest { 049 050 HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>(); 051 Context context = new ContextBase(); 052 StatusManager sm = context.getStatusManager(); 053 054 SAXParser createParser() throws Exception { 055 SAXParserFactory spf = SAXParserFactory.newInstance(); 056 return spf.newSAXParser(); 057 } 058 059 void doTest(String filename, Integer expectedInt, Class<?> exceptionClass) throws Exception { 060 061 rulesMap.put(new ElementSelector("test"), new NOPAction()); 062 rulesMap.put(new ElementSelector("test/badBegin"), new BadBeginAction()); 063 rulesMap.put(new ElementSelector("test/badBegin/touch"), new TouchAction()); 064 rulesMap.put(new ElementSelector("test/badEnd"), new BadEndAction()); 065 rulesMap.put(new ElementSelector("test/badEnd/touch"), new TouchAction()); 066 rulesMap.put(new ElementSelector("test/hello"), new HelloAction()); 067 068 rulesMap.put(new ElementSelector("test/isolate"), new NOPAction()); 069 rulesMap.put(new ElementSelector("test/isolate/badEnd"), new BadEndAction()); 070 rulesMap.put(new ElementSelector("test/isolate/badEnd/touch"), new TouchAction()); 071 rulesMap.put(new ElementSelector("test/isolate/touch"), new TouchAction()); 072 rulesMap.put(new ElementSelector("test/hello"), new HelloAction()); 073 074 TrivialConfigurator tc = new TrivialConfigurator(rulesMap); 075 tc.setContext(context); 076 tc.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/skip/" + filename); 077 078 String str = context.getProperty(HelloAction.PROPERTY_KEY); 079 assertEquals("Hello John Doe.", str); 080 081 Integer i = (Integer) context.getObject(TouchAction.KEY); 082 if (expectedInt == null) { 083 assertNull(i); 084 } else { 085 assertEquals(expectedInt, i); 086 } 087 088 // check the existence of an ERROR status 089 List<Status> statusList = sm.getCopyOfStatusList(); 090 Status s0 = statusList.get(0); 091 assertEquals(Status.ERROR, s0.getLevel()); 092 assertTrue(s0.getThrowable().getClass() == exceptionClass); 093 } 094 095 @Test 096 public void testSkippingRuntimeExInBadBegin() throws Exception { 097 doTest("badBegin1.xml", null, IllegalStateException.class); 098 } 099 100 @Test 101 public void testSkippingActionExInBadBegin() throws Exception { 102 doTest("badBegin2.xml", null, ActionException.class); 103 } 104 105 @Test 106 public void testSkippingRuntimeExInBadEnd() throws Exception { 107 doTest("badEnd1.xml", Integer.valueOf(2), IllegalStateException.class); 108 } 109 110 @Test 111 public void testSkippingActionExInBadEnd() throws Exception { 112 doTest("badEnd2.xml", Integer.valueOf(2), ActionException.class); 113 } 114}