1/******************************************************************************* 2 * Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12package org.jacoco.report.internal.xml; 13 14import static org.junit.Assert.assertEquals; 15 16import java.io.IOException; 17import java.io.StringWriter; 18 19import javax.xml.parsers.ParserConfigurationException; 20 21import org.jacoco.report.ReportStructureTestDriver; 22import org.jacoco.report.xml.XMLFormatter; 23import org.junit.Before; 24import org.junit.Test; 25import org.w3c.dom.Document; 26import org.xml.sax.SAXException; 27 28/** 29 * Unit tests for {@link XMLGroupVisitor}. 30 */ 31public class XMLGroupVisitorTest { 32 33 private XMLElement root; 34 35 private StringWriter buffer; 36 37 private XMLSupport support; 38 39 private XMLGroupVisitor handler; 40 41 private ReportStructureTestDriver driver; 42 43 @Before 44 public void setup() throws Exception { 45 buffer = new StringWriter(); 46 support = new XMLSupport(XMLFormatter.class); 47 root = new XMLDocument("report", "-//JACOCO//DTD Report 1.0//EN", 48 "report.dtd", "UTF-8", true, buffer); 49 root.attr("name", "Report"); 50 handler = new XMLGroupVisitor(root, null); 51 driver = new ReportStructureTestDriver(); 52 } 53 54 @Test 55 public void testVisitBundle() throws Exception { 56 driver.sendBundle(handler); 57 root.close(); 58 final Document doc = getDocument(); 59 assertEquals("bundle", support.findStr(doc, "//report/group/@name")); 60 } 61 62 @Test 63 public void testVisitGroup() throws Exception { 64 driver.sendGroup(handler); 65 root.close(); 66 final Document doc = getDocument(); 67 assertEquals("group", support.findStr(doc, "//report/group/@name")); 68 } 69 70 @Test 71 public void testVisitEnd() throws Exception { 72 driver.sendBundle(handler); 73 handler.visitEnd(); 74 root.close(); 75 final Document doc = getDocument(); 76 assertEquals("2", support.findStr(doc, 77 "//report/counter[@type='BRANCH']/@covered")); 78 } 79 80 private Document getDocument() throws SAXException, IOException, 81 ParserConfigurationException { 82 return support.parse(buffer.toString()); 83 } 84 85} 86