001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.event.stax;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNotNull;
018import static org.junit.Assert.assertTrue;
019
020import java.io.FileInputStream;
021import java.util.List;
022
023import javax.xml.stream.events.Attribute;
024
025import org.junit.Test;
026
027import ch.qos.logback.core.Context;
028import ch.qos.logback.core.ContextBase;
029import ch.qos.logback.core.status.Status;
030import ch.qos.logback.core.testUtil.CoreTestConstants;
031import ch.qos.logback.core.testUtil.StatusChecker;
032
033public class StaxEventRecorderTest {
034
035    Context context = new ContextBase();
036    StatusChecker statusChecker = new StatusChecker(context);
037
038    public List<StaxEvent> doTest(String filename) throws Exception {
039        StaxEventRecorder recorder = new StaxEventRecorder(context);
040        FileInputStream fis = new FileInputStream(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + filename);
041        recorder.recordEvents(fis);
042        return recorder.getEventList();
043    }
044
045    public void dump(List<StaxEvent> seList) {
046        for (StaxEvent se : seList) {
047            System.out.println(se);
048        }
049    }
050
051    @Test
052    public void testParsingOfXMLWithAttributesAndBodyText() throws Exception {
053        List<StaxEvent> seList = doTest("event1.xml");
054        assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
055        // dump(seList);
056        assertEquals(11, seList.size());
057        assertEquals("test", seList.get(0).getName());
058        assertEquals("badBegin", seList.get(1).getName());
059        StartEvent startEvent = (StartEvent) seList.get(7);
060        assertEquals("John Doe", startEvent.getAttributeByName("name").getValue());
061        assertEquals("XXX&", ((BodyEvent) seList.get(8)).getText());
062    }
063
064    @Test
065    public void testProcessingOfTextWithEntityCharacters() throws Exception {
066        List<StaxEvent> seList = doTest("ampEvent.xml");
067        assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
068        // dump(seList);
069        assertEquals(3, seList.size());
070
071        BodyEvent be = (BodyEvent) seList.get(1);
072        assertEquals("xxx & yyy", be.getText());
073    }
074
075    @Test
076    public void testAttributeProcessing() throws Exception {
077        List<StaxEvent> seList = doTest("inc.xml");
078        assertTrue(statusChecker.getHighestLevel(0) == Status.INFO);
079        assertEquals(4, seList.size());
080        StartEvent se = (StartEvent) seList.get(1);
081        Attribute attr = se.getAttributeByName("increment");
082        assertNotNull(attr);
083        assertEquals("1", attr.getValue());
084    }
085
086    @Test
087    public void bodyWithSpacesAndQuotes() throws Exception {
088        List<StaxEvent> seList = doTest("spacesAndQuotes.xml");
089        assertEquals(3, seList.size());
090        BodyEvent be = (BodyEvent) seList.get(1);
091        assertEquals("[x][x] \"xyz\"%n", be.getText());
092    }
093}