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; 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.parsers.SAXParser; 024import javax.xml.parsers.SAXParserFactory; 025 026import org.junit.Test; 027import org.xml.sax.Attributes; 028 029import ch.qos.logback.core.Context; 030import ch.qos.logback.core.ContextBase; 031import ch.qos.logback.core.status.Status; 032import ch.qos.logback.core.testUtil.CoreTestConstants; 033import ch.qos.logback.core.testUtil.StatusChecker; 034 035/** 036 * Test whether SaxEventRecorder does a good job. 037 * 038 * @author Ceki Gulcu 039 */ 040public class SaxEventRecorderTest { 041 042 Context context = new ContextBase(); 043 StatusChecker statusChecker = new StatusChecker(context); 044 045 SAXParser createParser() throws Exception { 046 SAXParserFactory spf = SAXParserFactory.newInstance(); 047 return spf.newSAXParser(); 048 } 049 050 public List<SaxEvent> doTest(String filename) throws Exception { 051 SaxEventRecorder recorder = new SaxEventRecorder(context); 052 FileInputStream fis = new FileInputStream(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/" + filename); 053 recorder.recordEvents(fis); 054 return recorder.getSaxEventList(); 055 056 } 057 058 public void dump(List<SaxEvent> seList) { 059 for (SaxEvent se : seList) { 060 System.out.println(se); 061 } 062 } 063 064 @Test 065 public void test1() throws Exception { 066 List<SaxEvent> seList = doTest("event1.xml"); 067 assertTrue(statusChecker.getHighestLevel(0) == Status.INFO); 068 // dump(seList); 069 assertEquals(11, seList.size()); 070 } 071 072 @Test 073 public void test2() throws Exception { 074 List<SaxEvent> seList = doTest("ampEvent.xml"); 075 assertTrue(statusChecker.getHighestLevel(0) == Status.INFO); 076 // dump(seList); 077 assertEquals(3, seList.size()); 078 079 BodyEvent be = (BodyEvent) seList.get(1); 080 assertEquals("xxx & yyy", be.getText()); 081 } 082 083 @Test 084 public void test3() throws Exception { 085 List<SaxEvent> seList = doTest("inc.xml"); 086 assertTrue(statusChecker.getHighestLevel(0) == Status.INFO); 087 // dump(seList); 088 assertEquals(4, seList.size()); 089 090 StartEvent se = (StartEvent) seList.get(1); 091 Attributes attr = se.getAttributes(); 092 assertNotNull(attr); 093 assertEquals("1", attr.getValue("increment")); 094 } 095 096 @Test 097 public void bodyWithSpacesAndQuotes() throws Exception { 098 List<SaxEvent> seList = doTest("spacesAndQuotes.xml"); 099 assertEquals(3, seList.size()); 100 BodyEvent be = (BodyEvent) seList.get(1); 101 assertEquals("[x][x] \"xyz\"%n", be.getText()); 102 } 103 104}