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