aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/PackageSourcePageTest.java
blob: 026fde73225d246d54570ce2a304482153d96493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
 * Copyright (c) 2009, 2014 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.report.internal.html.page;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Collections;

import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.ISourceFileCoverage;
import org.jacoco.core.internal.analysis.PackageCoverageImpl;
import org.jacoco.core.internal.analysis.SourceFileCoverageImpl;
import org.jacoco.report.ISourceFileLocator;
import org.jacoco.report.internal.ReportOutputFolder;
import org.jacoco.report.internal.html.ILinkable;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

/**
 * Unit tests for {@link PackageSourcePage}.
 */
public class PackageSourcePageTest extends PageTestBase {

	private PackageCoverageImpl node;
	private ISourceFileLocator sourceLocator;
	private ILinkable packagePageLink;

	private PackageSourcePage page;

	@Before
	@Override
	public void setup() throws Exception {
		super.setup();
		ISourceFileCoverage src1 = new SourceFileCoverageImpl("Src1.java",
				"org/jacoco/example");
		ISourceFileCoverage src2 = new SourceFileCoverageImpl("Src2.java",
				"org/jacoco/example");
		node = new PackageCoverageImpl("org/jacoco/example",
				Collections.<IClassCoverage> emptyList(), Arrays.asList(src1,
						src2));
		sourceLocator = new ISourceFileLocator() {

			public int getTabWidth() {
				return 4;
			}

			public Reader getSourceFile(String packageName, String fileName)
					throws IOException {
				return fileName.equals("Src1.java") ? new StringReader("")
						: null;
			}
		};
		packagePageLink = new ILinkable() {

			public String getLinkStyle() {
				fail();
				return null;
			}

			public String getLinkLabel() {
				fail();
				return null;
			}

			public String getLink(ReportOutputFolder base) {
				return "index.html";
			}
		};
	}

	@Test
	public void testContents() throws Exception {
		page = new PackageSourcePage(node, null, sourceLocator, rootFolder,
				context, packagePageLink);
		page.render();

		final Document doc = support.parse(output.getFile("index.source.html"));
		assertEquals("index.html",
				support.findStr(doc, "/html/body/div[1]/span[1]/a/@href"));
		assertEquals("el_class",
				support.findStr(doc, "/html/body/div[1]/span[1]/a/@class"));
		assertEquals("Classes",
				support.findStr(doc, "/html/body/div[1]/span[1]/a"));
		assertEquals("el_source", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[1]/td[1]/a/@class"));
		assertEquals("Src1.java",
				support.findStr(doc, "/html/body/table[1]/tbody/tr[1]/td[1]/a"));
		assertEquals("el_source", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[2]/td[1]/span/@class"));
		assertEquals("Src2.java", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[2]/td[1]/span"));
	}

	@Test
	public void testGetSourceFilePages() throws Exception {
		page = new PackageSourcePage(node, null, sourceLocator, rootFolder,
				context, packagePageLink);
		page.render();

		assertNotNull(page.getSourceFilePage("Src1.java"));
		assertNull(page.getSourceFilePage("Src2.java"));
	}

}