Home | History | Annotate | Download | only in page
      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.assertNotNull;
     16 import static org.junit.Assert.assertNull;
     17 import static org.junit.Assert.fail;
     18 
     19 import java.io.IOException;
     20 import java.io.Reader;
     21 import java.io.StringReader;
     22 import java.util.Arrays;
     23 import java.util.Collections;
     24 
     25 import org.jacoco.core.analysis.IClassCoverage;
     26 import org.jacoco.core.analysis.ISourceFileCoverage;
     27 import org.jacoco.core.internal.analysis.PackageCoverageImpl;
     28 import org.jacoco.core.internal.analysis.SourceFileCoverageImpl;
     29 import org.jacoco.report.ISourceFileLocator;
     30 import org.jacoco.report.internal.ReportOutputFolder;
     31 import org.jacoco.report.internal.html.ILinkable;
     32 import org.junit.Before;
     33 import org.junit.Test;
     34 import org.w3c.dom.Document;
     35 
     36 /**
     37  * Unit tests for {@link PackageSourcePage}.
     38  */
     39 public class PackageSourcePageTest extends PageTestBase {
     40 
     41 	private PackageCoverageImpl node;
     42 	private ISourceFileLocator sourceLocator;
     43 	private ILinkable packagePageLink;
     44 
     45 	private PackageSourcePage page;
     46 
     47 	@Before
     48 	@Override
     49 	public void setup() throws Exception {
     50 		super.setup();
     51 		ISourceFileCoverage src1 = new SourceFileCoverageImpl("Src1.java",
     52 				"org/jacoco/example");
     53 		ISourceFileCoverage src2 = new SourceFileCoverageImpl("Src2.java",
     54 				"org/jacoco/example");
     55 		node = new PackageCoverageImpl("org/jacoco/example",
     56 				Collections.<IClassCoverage> emptyList(), Arrays.asList(src1,
     57 						src2));
     58 		sourceLocator = new ISourceFileLocator() {
     59 
     60 			public int getTabWidth() {
     61 				return 4;
     62 			}
     63 
     64 			public Reader getSourceFile(String packageName, String fileName)
     65 					throws IOException {
     66 				return fileName.equals("Src1.java") ? new StringReader("")
     67 						: null;
     68 			}
     69 		};
     70 		packagePageLink = new ILinkable() {
     71 
     72 			public String getLinkStyle() {
     73 				fail();
     74 				return null;
     75 			}
     76 
     77 			public String getLinkLabel() {
     78 				fail();
     79 				return null;
     80 			}
     81 
     82 			public String getLink(ReportOutputFolder base) {
     83 				return "index.html";
     84 			}
     85 		};
     86 	}
     87 
     88 	@Test
     89 	public void testContents() throws Exception {
     90 		page = new PackageSourcePage(node, null, sourceLocator, rootFolder,
     91 				context, packagePageLink);
     92 		page.render();
     93 
     94 		final Document doc = support.parse(output.getFile("index.source.html"));
     95 		assertEquals("index.html",
     96 				support.findStr(doc, "/html/body/div[1]/span[1]/a/@href"));
     97 		assertEquals("el_class",
     98 				support.findStr(doc, "/html/body/div[1]/span[1]/a/@class"));
     99 		assertEquals("Classes",
    100 				support.findStr(doc, "/html/body/div[1]/span[1]/a"));
    101 		assertEquals("el_source", support.findStr(doc,
    102 				"/html/body/table[1]/tbody/tr[1]/td[1]/a/@class"));
    103 		assertEquals("Src1.java",
    104 				support.findStr(doc, "/html/body/table[1]/tbody/tr[1]/td[1]/a"));
    105 		assertEquals("el_source", support.findStr(doc,
    106 				"/html/body/table[1]/tbody/tr[2]/td[1]/span/@class"));
    107 		assertEquals("Src2.java", support.findStr(doc,
    108 				"/html/body/table[1]/tbody/tr[2]/td[1]/span"));
    109 	}
    110 
    111 	@Test
    112 	public void testGetSourceFilePages() throws Exception {
    113 		page = new PackageSourcePage(node, null, sourceLocator, rootFolder,
    114 				context, packagePageLink);
    115 		page.render();
    116 
    117 		assertNotNull(page.getSourceFilePage("Src1.java"));
    118 		assertNull(page.getSourceFilePage("Src2.java"));
    119 	}
    120 
    121 }
    122