1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.joran;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNull;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.HashMap;
17  import java.util.List;
18  
19  import javax.xml.parsers.SAXParser;
20  import javax.xml.parsers.SAXParserFactory;
21  
22  import org.junit.Test;
23  
24  import ch.qos.logback.core.Context;
25  import ch.qos.logback.core.ContextBase;
26  import ch.qos.logback.core.joran.action.Action;
27  import ch.qos.logback.core.joran.action.NOPAction;
28  import ch.qos.logback.core.joran.action.ext.BadBeginAction;
29  import ch.qos.logback.core.joran.action.ext.BadEndAction;
30  import ch.qos.logback.core.joran.action.ext.HelloAction;
31  import ch.qos.logback.core.joran.action.ext.TouchAction;
32  import ch.qos.logback.core.joran.spi.ActionException;
33  import ch.qos.logback.core.joran.spi.Pattern;
34  import ch.qos.logback.core.status.Status;
35  import ch.qos.logback.core.status.StatusManager;
36  import ch.qos.logback.core.util.Constants;
37  
38  /**
39   * Test the way Interpreter skips child elements in case of exceptions thrown by
40   * Actions. It also tests addition of status messages in case of exceptions.
41   * 
42   * @author Ceki Gulcu
43   */
44  public class SkippingInInterpreterTest {
45  
46    HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
47    Context context = new ContextBase();
48    StatusManager sm = context.getStatusManager();
49  
50    SAXParser createParser() throws Exception {
51      SAXParserFactory spf = SAXParserFactory.newInstance();
52      return spf.newSAXParser();
53    }
54  
55    void doTest(String filename, Integer expectedInt, Class exceptionClass)
56        throws Exception {
57  
58      rulesMap.put(new Pattern("test"), new NOPAction());
59      rulesMap.put(new Pattern("test/badBegin"), new BadBeginAction());
60      rulesMap.put(new Pattern("test/badBegin/touch"), new TouchAction());
61      rulesMap.put(new Pattern("test/badEnd"), new BadEndAction());
62      rulesMap.put(new Pattern("test/badEnd/touch"), new TouchAction());
63      rulesMap.put(new Pattern("test/hello"), new HelloAction());
64  
65      rulesMap.put(new Pattern("test/isolate"), new NOPAction());
66      rulesMap.put(new Pattern("test/isolate/badEnd"), new BadEndAction());
67      rulesMap.put(new Pattern("test/isolate/badEnd/touch"), new TouchAction());
68      rulesMap.put(new Pattern("test/isolate/touch"), new TouchAction());
69      rulesMap.put(new Pattern("test/hello"), new HelloAction());
70  
71      TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
72      tc.setContext(context);
73      tc.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/skip/" + filename);
74  
75      String str = context.getProperty(HelloAction.PROPERTY_KEY);
76      assertEquals("Hello John Doe.", str);
77  
78      Integer i = (Integer) context.getObject(TouchAction.KEY);
79      if (expectedInt == null) {
80        assertNull(i);
81      } else {
82        assertEquals(expectedInt, i);
83      }
84  
85      // check the existence of an ERROR status
86      List<Status> statusList = sm.getCopyOfStatusList();
87      Status s0 = statusList.get(0);
88      assertEquals(Status.ERROR, s0.getLevel());
89      assertTrue(s0.getThrowable().getClass() == exceptionClass);
90    }
91  
92    @Test
93    public void testSkippingRuntimeExInBadBegin() throws Exception {
94      doTest("badBegin1.xml", null, IllegalStateException.class);
95    }
96  
97    @Test
98    public void testSkippingActionExInBadBegin() throws Exception {
99      doTest("badBegin2.xml", null, ActionException.class);
100   }
101 
102   @Test
103   public void testSkippingRuntimeExInBadEnd() throws Exception {
104     doTest("badEnd1.xml", new Integer(2), IllegalStateException.class);
105   }
106 
107   @Test
108   public void testSkippingActionExInBadEnd() throws Exception {
109     doTest("badEnd2.xml", new Integer(2), ActionException.class);
110   }
111 }