1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 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 *******************************************************************************/ 12 package org.jacoco.report.internal.html.page; 13 14 import static org.junit.Assert.assertEquals; 15 import static org.junit.Assert.assertFalse; 16 import static org.junit.Assert.assertTrue; 17 18 import java.io.IOException; 19 20 import org.jacoco.report.internal.ReportOutputFolder; 21 import org.jacoco.report.internal.html.HTMLElement; 22 import org.jacoco.report.internal.html.HTMLSupport; 23 import org.junit.Before; 24 import org.junit.Test; 25 import org.w3c.dom.Document; 26 27 /** 28 * Unit tests for {@link ReportPage}. 29 */ 30 public class ReportPageTest extends PageTestBase { 31 32 private ReportPage rootpage; 33 34 private ReportPage page; 35 36 private class TestReportPage extends ReportPage { 37 38 private final String label; 39 private final String style; 40 41 protected TestReportPage(String label, String style, ReportPage parent) { 42 super(parent, rootFolder, ReportPageTest.this.context); 43 this.label = label; 44 this.style = style; 45 } 46 47 @Override 48 protected void content(HTMLElement body) throws IOException { 49 body.div("testcontent").text("Hello Test"); 50 } 51 52 @Override 53 protected String getFileName() { 54 return label + ".html"; 55 } 56 57 public String getLinkLabel() { 58 return label; 59 } 60 61 public String getLinkStyle() { 62 return style; 63 } 64 65 } 66 67 @Before 68 @Override 69 public void setup() throws Exception { 70 super.setup(); 71 rootpage = new TestReportPage("Report", "el_report", null); 72 page = new TestReportPage("Test", "el_group", rootpage); 73 } 74 75 @Test 76 public void testIsRootPage1() { 77 assertFalse(page.isRootPage()); 78 } 79 80 @Test 81 public void testIsRootPage2() { 82 assertTrue(rootpage.isRootPage()); 83 } 84 85 @Test 86 public void testGetLink() throws IOException { 87 ReportOutputFolder base = rootFolder.subFolder("here"); 88 assertEquals("../Test.html", page.getLink(base)); 89 } 90 91 @Test 92 public void testPageContent() throws Exception { 93 page.render(); 94 final HTMLSupport support = new HTMLSupport(); 95 final Document doc = support.parse(output.getFile("Test.html")); 96 97 // language 98 assertEquals("en", support.findStr(doc, "/html/@lang")); 99 100 // style sheet 101 assertEquals("jacoco-resources/report.css", support.findStr(doc, 102 "/html/head/link[@rel='stylesheet']/@href")); 103 104 // bread crumb 105 assertEquals("Report", support.findStr(doc, 106 "/html/body/div[@class='breadcrumb']/a[1]/text()")); 107 assertEquals("Report.html", support.findStr(doc, 108 "/html/body/div[@class='breadcrumb']/a[1]/@href")); 109 assertEquals("el_report", support.findStr(doc, 110 "/html/body/div[@class='breadcrumb']/a[1]/@class")); 111 assertEquals("Test", support.findStr(doc, 112 "/html/body/div[@class='breadcrumb']/span[2]/text()")); 113 assertEquals("el_group", support.findStr(doc, 114 "/html/body/div[@class='breadcrumb']/span[2]/@class")); 115 116 // Header 117 assertEquals("Test", support.findStr(doc, "/html/body/h1/text()")); 118 119 // Content 120 assertEquals("Hello Test", support.findStr(doc, 121 "/html/body/div[@class='testcontent']/text()")); 122 123 // Footer 124 assertEquals("CustomFooter", 125 support.findStr(doc, "/html/body/div[@class='footer']/text()")); 126 } 127 128 } 129