aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/internal/html/page/SessionsPageTest.java
blob: 613f8bca76a8950ee5db72dfa0735d96910858f4 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.report.internal.html.page;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.SessionInfo;
import org.jacoco.report.internal.html.HTMLElement;
import org.jacoco.report.internal.html.index.ElementIndex;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

/**
 * Unit tests for {@link SessionsPage}.
 */
public class SessionsPageTest extends PageTestBase {

	private final List<SessionInfo> noSessions = Collections.emptyList();

	private final Collection<ExecutionData> noExecutionData = Collections
			.emptyList();

	private ElementIndex index;

	@Before
	@Override
	public void setup() throws Exception {
		super.setup();
		index = new ElementIndex(rootFolder);
	}

	@Test
	public void testGetLinkStyle() {
		final SessionsPage page = new SessionsPage(noSessions, noExecutionData,
				index, null, rootFolder, context);
		assertEquals("el_session", page.getLinkStyle());
	}

	@Test
	public void testGetFileName() {
		final SessionsPage page = new SessionsPage(noSessions, noExecutionData,
				index, null, rootFolder, context);
		assertEquals("jacoco-sessions.html", page.getFileName());
	}

	@Test
	public void testGetLinkLabel() {
		final SessionsPage page = new SessionsPage(noSessions, noExecutionData,
				index, null, rootFolder, context);
		assertEquals("Sessions", page.getLinkLabel());
	}

	@Test
	public void testEmptyContent() throws Exception {
		final SessionsPage page = new SessionsPage(noSessions, noExecutionData,
				index, null, rootFolder, context);
		page.render();
		final Document doc = support
				.parse(output.getFile("jacoco-sessions.html"));
		assertEquals("No session information available.",
				support.findStr(doc, "/html/body/p[1]"));
		assertEquals("No execution data available.",
				support.findStr(doc, "/html/body/p[2]"));
	}

	@Test
	public void testSessionListContent() throws Exception {
		final List<SessionInfo> sessions = new ArrayList<SessionInfo>();
		sessions.add(new SessionInfo("Session-A", 0, 0));
		sessions.add(new SessionInfo("Session-B", 0, 0));
		sessions.add(new SessionInfo("Session-C", 0, 0));
		final SessionsPage page = new SessionsPage(sessions, noExecutionData,
				index, null, rootFolder, context);
		page.render();
		final Document doc = support
				.parse(output.getFile("jacoco-sessions.html"));
		assertEquals("el_session", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[1]/td[1]/span/@class"));
		assertEquals("Session-A", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[1]/td[1]/span"));
		assertEquals("Session-B",
				support.findStr(doc, "/html/body/table[1]/tbody/tr[2]/td[1]"));
		assertEquals("Session-C",
				support.findStr(doc, "/html/body/table[1]/tbody/tr[3]/td[1]"));
	}

	@Test
	public void testExecutionDataContent() throws Exception {
		final Collection<ExecutionData> data = new ArrayList<ExecutionData>();
		data.add(new ExecutionData(0x1000, "ClassB", new boolean[0]));
		data.add(new ExecutionData(0x1001, "ClassC", new boolean[0]));
		data.add(new ExecutionData(0x1002, "ClassA", new boolean[0]));
		index.addClass(new ReportPage(null, rootFolder, context) {

			public String getLinkLabel() {
				return "Foo";
			}

			@Override
			protected String getFileName() {
				return "Foo.html";
			}

			public String getLinkStyle() {
				return "sample";
			}

			@Override
			protected void content(HTMLElement body) throws IOException {
			}
		}, 0x1002);

		final SessionsPage page = new SessionsPage(noSessions, data, index,
				null, rootFolder, context);
		page.render();
		final Document doc = support
				.parse(output.getFile("jacoco-sessions.html"));
		assertEquals("el_class", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[1]/td[1]/a/@class"));
		assertEquals("Foo.html", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[1]/td[1]/a/@href"));
		assertEquals("ClassA", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[1]/td[1]/a"));
		assertEquals("0000000000001002", support.findStr(doc,
				"/html/body/table[1]/tbody/tr[1]/td[2]/code"));
		assertEquals("ClassB",
				support.findStr(doc, "/html/body/table[1]/tbody/tr[2]/td[1]"));
		assertEquals("ClassC",
				support.findStr(doc, "/html/body/table[1]/tbody/tr[3]/td[1]"));
	}

}