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;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.util.HashMap;
21  import java.util.List;
22  
23  import javax.xml.parsers.SAXParser;
24  import javax.xml.parsers.SAXParserFactory;
25  
26  import ch.qos.logback.core.joran.spi.ElementSelector;
27  import org.junit.Test;
28  
29  import ch.qos.logback.core.Context;
30  import ch.qos.logback.core.ContextBase;
31  import ch.qos.logback.core.joran.action.Action;
32  import ch.qos.logback.core.joran.action.NOPAction;
33  import ch.qos.logback.core.joran.action.ext.BadBeginAction;
34  import ch.qos.logback.core.joran.action.ext.BadEndAction;
35  import ch.qos.logback.core.joran.action.ext.HelloAction;
36  import ch.qos.logback.core.joran.action.ext.TouchAction;
37  import ch.qos.logback.core.joran.spi.ActionException;
38  import ch.qos.logback.core.status.Status;
39  import ch.qos.logback.core.status.StatusManager;
40  import ch.qos.logback.core.testUtil.CoreTestConstants;
41  
42  /**
43   * Test the way Interpreter skips child elements in case of exceptions thrown by
44   * Actions. It also tests addition of status messages in case of exceptions.
45   * 
46   * @author Ceki Gulcu
47   */
48  public class SkippingInInterpreterTest {
49  
50      HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
51      Context context = new ContextBase();
52      StatusManager sm = context.getStatusManager();
53  
54      SAXParser createParser() throws Exception {
55          SAXParserFactory spf = SAXParserFactory.newInstance();
56          return spf.newSAXParser();
57      }
58  
59      void doTest(String filename, Integer expectedInt, Class<?> exceptionClass) throws Exception {
60  
61          rulesMap.put(new ElementSelector("test"), new NOPAction());
62          rulesMap.put(new ElementSelector("test/badBegin"), new BadBeginAction());
63          rulesMap.put(new ElementSelector("test/badBegin/touch"), new TouchAction());
64          rulesMap.put(new ElementSelector("test/badEnd"), new BadEndAction());
65          rulesMap.put(new ElementSelector("test/badEnd/touch"), new TouchAction());
66          rulesMap.put(new ElementSelector("test/hello"), new HelloAction());
67  
68          rulesMap.put(new ElementSelector("test/isolate"), new NOPAction());
69          rulesMap.put(new ElementSelector("test/isolate/badEnd"), new BadEndAction());
70          rulesMap.put(new ElementSelector("test/isolate/badEnd/touch"), new TouchAction());
71          rulesMap.put(new ElementSelector("test/isolate/touch"), new TouchAction());
72          rulesMap.put(new ElementSelector("test/hello"), new HelloAction());
73  
74          TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
75          tc.setContext(context);
76          tc.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/skip/" + filename);
77  
78          String str = context.getProperty(HelloAction.PROPERTY_KEY);
79          assertEquals("Hello John Doe.", str);
80  
81          Integer i = (Integer) context.getObject(TouchAction.KEY);
82          if (expectedInt == null) {
83              assertNull(i);
84          } else {
85              assertEquals(expectedInt, i);
86          }
87  
88          // check the existence of an ERROR status
89          List<Status> statusList = sm.getCopyOfStatusList();
90          Status s0 = statusList.get(0);
91          assertEquals(Status.ERROR, s0.getLevel());
92          assertTrue(s0.getThrowable().getClass() == exceptionClass);
93      }
94  
95      @Test
96      public void testSkippingRuntimeExInBadBegin() throws Exception {
97          doTest("badBegin1.xml", null, IllegalStateException.class);
98      }
99  
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 }