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