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.stax;
15  
16  import java.io.FileInputStream;
17  import java.util.List;
18  
19  import javax.xml.stream.events.Attribute;
20  
21  import ch.qos.logback.core.Context;
22  import ch.qos.logback.core.ContextBase;
23  import ch.qos.logback.core.status.Status;
24  import ch.qos.logback.core.testUtil.CoreTestConstants;
25  import ch.qos.logback.core.status.testUtil.StatusChecker;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.Test;
28  
29  public class StaxEventRecorderTest {
30  
31      Context context = new ContextBase();
32      StatusChecker statusChecker = new StatusChecker(context);
33  
34      public List<StaxEvent> doTest(String filename) throws Exception {
35          StaxEventRecorder recorder = new StaxEventRecorder(context);
36          FileInputStream fis = new FileInputStream(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + filename);
37          recorder.recordEvents(fis);
38          return recorder.getEventList();
39      }
40  
41      public void dump(List<StaxEvent> seList) {
42          for (StaxEvent se : seList) {
43              System.out.println(se);
44          }
45      }
46  
47      @Test
48      public void testParsingOfXMLWithAttributesAndBodyText() throws Exception {
49          List<StaxEvent> seList = doTest("event1.xml");
50          Assertions.assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
51          // dump(seList);
52          Assertions.assertEquals(11, seList.size());
53          Assertions.assertEquals("test", seList.get(0).getName());
54          Assertions.assertEquals("badBegin", seList.get(1).getName());
55          StartEvent startEvent = (StartEvent) seList.get(7);
56          Assertions.assertEquals("John Doe", startEvent.getAttributeByName("name").getValue());
57          Assertions.assertEquals("XXX&", ((BodyEvent) seList.get(8)).getText());
58      }
59  
60      @Test
61      public void testProcessingOfTextWithEntityCharacters() throws Exception {
62          List<StaxEvent> seList = doTest("ampEvent.xml");
63          Assertions.assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
64          // dump(seList);
65          Assertions.assertEquals(3, seList.size());
66  
67          BodyEvent be = (BodyEvent) seList.get(1);
68          Assertions.assertEquals("xxx & yyy", be.getText());
69      }
70  
71      @Test
72      public void testAttributeProcessing() throws Exception {
73          List<StaxEvent> seList = doTest("inc.xml");
74          Assertions.assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
75          Assertions.assertEquals(4, seList.size());
76          StartEvent se = (StartEvent) seList.get(1);
77          Attribute attr = se.getAttributeByName("increment");
78          Assertions.assertNotNull(attr);
79          Assertions.assertEquals("1", attr.getValue());
80      }
81  
82      @Test
83      public void bodyWithSpacesAndQuotes() throws Exception {
84          List<StaxEvent> seList = doTest("spacesAndQuotes.xml");
85          Assertions.assertEquals(3, seList.size());
86          BodyEvent be = (BodyEvent) seList.get(1);
87          Assertions.assertEquals("[x][x] \"xyz\"%n", be.getText());
88      }
89  }