1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework for Java.
3    * 
4    * Copyright (C) 2000-2006, 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.event;
11  
12  import static org.junit.Assert.*;
13  
14  import java.io.FileInputStream;
15  import java.util.List;
16  
17  import javax.xml.parsers.SAXParser;
18  import javax.xml.parsers.SAXParserFactory;
19  
20  import org.junit.Test;
21  import org.xml.sax.Attributes;
22  
23  import ch.qos.logback.core.Context;
24  import ch.qos.logback.core.ContextBase;
25  import ch.qos.logback.core.status.Status;
26  import ch.qos.logback.core.status.StatusManager;
27  import ch.qos.logback.core.util.Constants;
28  
29  /**
30   * Test whether SaxEventRecorder does a good job.
31   * 
32   * @author Ceki Gulcu
33   */
34  public class EventRecorderTest {
35  
36    Context context =  new ContextBase();
37  
38  
39    SAXParser createParser() throws Exception {
40      SAXParserFactory spf = SAXParserFactory.newInstance();
41      return spf.newSAXParser();
42    }
43  
44    public List<SaxEvent> doTest(String filename) throws Exception {
45      SaxEventRecorder recorder = new SaxEventRecorder();
46      recorder.setContext(context);
47      FileInputStream fis = new FileInputStream(Constants.TEST_DIR_PREFIX
48          + "input/joran/"+ filename);
49      recorder.recordEvents(fis);
50      return  recorder.getSaxEventList();
51      
52   
53    }
54   
55    public void dump(List<SaxEvent> seList) {
56      for (SaxEvent se : seList) {
57        System.out.println(se);
58      }
59    }
60    /**
61     * Tests that whenever an action throws an exception, processing of child
62     * elements is skipped.
63     * 
64     * @throws Exception
65     */
66    @Test
67    public void test1() throws Exception {
68      List<SaxEvent> seList = doTest("event1.xml");
69      StatusManager sm = context.getStatusManager();
70      assertTrue(sm.getLevel() == Status.INFO);
71      //dump(seList);  
72      assertEquals(11, seList.size());
73      
74    }
75  
76    @Test
77    public void test2() throws Exception {
78      List<SaxEvent> seList = doTest("ampEvent.xml");
79      StatusManager sm = context.getStatusManager();
80      assertTrue(sm.getLevel() == Status.INFO);
81      //dump(seList);  
82      assertEquals(3, seList.size());
83      
84      BodyEvent be = (BodyEvent) seList.get(1);
85      assertEquals("xxx & yyy", be.getText());
86    }
87  
88    @Test
89    public void test3() throws Exception {
90      List<SaxEvent> seList = doTest("inc.xml");
91      StatusManager sm = context.getStatusManager();
92      assertTrue(sm.getLevel() == Status.INFO);
93      //dump(seList);  
94      assertEquals(4, seList.size());
95      
96      StartEvent se = (StartEvent) seList.get(1);
97      Attributes attr = se.getAttributes();
98      assertNotNull(attr);
99      assertEquals("1", attr.getValue("increment"));
100   }
101 
102 }