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.event;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.io.FileInputStream;
21  import java.util.List;
22  
23  import javax.xml.parsers.SAXParser;
24  import javax.xml.parsers.SAXParserFactory;
25  
26  import org.junit.Test;
27  import org.xml.sax.Attributes;
28  
29  import ch.qos.logback.core.Context;
30  import ch.qos.logback.core.ContextBase;
31  import ch.qos.logback.core.status.Status;
32  import ch.qos.logback.core.testUtil.CoreTestConstants;
33  import ch.qos.logback.core.testUtil.StatusChecker;
34  
35  /**
36   * Test whether SaxEventRecorder does a good job.
37   * 
38   * @author Ceki Gulcu
39   */
40  public class SaxEventRecorderTest {
41  
42      Context context = new ContextBase();
43      StatusChecker statusChecker = new StatusChecker(context);
44  
45      SAXParser createParser() throws Exception {
46          SAXParserFactory spf = SAXParserFactory.newInstance();
47          return spf.newSAXParser();
48      }
49  
50      public List<SaxEvent> doTest(String filename) throws Exception {
51          SaxEventRecorder recorder = new SaxEventRecorder(context);
52          FileInputStream fis = new FileInputStream(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + filename);
53          recorder.recordEvents(fis);
54          return recorder.getSaxEventList();
55  
56      }
57  
58      public void dump(List<SaxEvent> seList) {
59          for (SaxEvent se : seList) {
60              System.out.println(se);
61          }
62      }
63  
64      @Test
65      public void test1() throws Exception {
66          List<SaxEvent> seList = doTest("event1.xml");
67          assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
68          // dump(seList);
69          assertEquals(11, seList.size());
70      }
71  
72      @Test
73      public void test2() throws Exception {
74          List<SaxEvent> seList = doTest("ampEvent.xml");
75          assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
76          // dump(seList);
77          assertEquals(3, seList.size());
78  
79          BodyEvent be = (BodyEvent) seList.get(1);
80          assertEquals("xxx & yyy", be.getText());
81      }
82  
83      @Test
84      public void test3() throws Exception {
85          List<SaxEvent> seList = doTest("inc.xml");
86          assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
87          // dump(seList);
88          assertEquals(4, seList.size());
89  
90          StartEvent se = (StartEvent) seList.get(1);
91          Attributes attr = se.getAttributes();
92          assertNotNull(attr);
93          assertEquals("1", attr.getValue("increment"));
94      }
95  
96      @Test
97      public void bodyWithSpacesAndQuotes() throws Exception {
98          List<SaxEvent> seList = doTest("spacesAndQuotes.xml");
99          assertEquals(3, seList.size());
100         BodyEvent be = (BodyEvent) seList.get(1);
101         assertEquals("[x][x] \"xyz\"%n", be.getText());
102     }
103 
104 }