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