aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report/src/org/jacoco/report/internal/html/page/SessionsPage.java
blob: 56181ef01f9c3b90b1d503efed619e32cfd6df9a (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
 * 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 java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.jacoco.core.data.IExecutionData;
import org.jacoco.core.data.SessionInfo;
import org.jacoco.report.ILanguageNames;
import org.jacoco.report.internal.ReportOutputFolder;
import org.jacoco.report.internal.html.HTMLElement;
import org.jacoco.report.internal.html.IHTMLReportContext;
import org.jacoco.report.internal.html.index.ElementIndex;
import org.jacoco.report.internal.html.resources.Styles;

/**
 * Page to display information about sessions covered by this report.
 */
public class SessionsPage extends ReportPage {

	private static final String MSG_SESSIONS = "This coverage report is based "
			+ "on execution data from the following sessions:";

	private static final String MSG_NO_SESSIONS = "No session information available.";

	private static final String MSG_EXECDATA = "Execution data for the "
			+ "following classes is considered in this report:";

	private static final String MSG_NO_EXECDATA = "No execution data available.";

	private final List<SessionInfo> sessionInfos;

	private final DateFormat dateFormat;

	private final List<IExecutionData> executionData;

	private final ElementIndex index;

	/**
	 * Creates a new page page to display session information.
	 *
	 * @param sessionInfos
	 *            session info objects
	 * @param executionData
	 *            execution data objects
	 * @param index
	 *            index for cross-linking
	 * @param parent
	 *            optional hierarchical parent
	 * @param folder
	 *            base folder to create this page in
	 * @param context
	 *            settings context
	 */
	public SessionsPage(final List<SessionInfo> sessionInfos,
			// BEGIN android-change
			final Collection<IExecutionData> executionData,
			// END android-change
			final ElementIndex index, final ReportPage parent,
			final ReportOutputFolder folder, final IHTMLReportContext context) {
		super(parent, folder, context);
		this.sessionInfos = sessionInfos;
		// BEGIN android-change
		this.executionData = new ArrayList<IExecutionData>(executionData);
		// END android-change
		this.index = index;
		this.dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,
				DateFormat.DEFAULT, context.getLocale());
		final ILanguageNames names = context.getLanguageNames();
		// BEGIN android-change
		Collections.sort(this.executionData, new Comparator<IExecutionData>() {
			public int compare(final IExecutionData e1, final IExecutionData e2) {
			// END android-change
				return names.getQualifiedClassName(e1.getName()).compareTo(
						names.getQualifiedClassName(e2.getName()));
			}
		});
	}

	@Override
	protected void content(final HTMLElement body) throws IOException {
		if (sessionInfos.isEmpty()) {
			body.p().text(MSG_NO_SESSIONS);
		} else {
			body.p().text(MSG_SESSIONS);
			sessionTable(body);
		}
		if (executionData.isEmpty()) {
			body.p().text(MSG_NO_EXECDATA);
		} else {
			body.p().text(MSG_EXECDATA);
			executionDataTable(body);
		}
	}

	private void sessionTable(final HTMLElement body) throws IOException {
		final HTMLElement table = body.table(Styles.COVERAGETABLE);
		{
			final HTMLElement tr = table.thead().tr();
			tr.td().text("Session");
			tr.td().text("Start Time");
			tr.td().text("Dump Time");
		}
		final HTMLElement tbody = table.tbody();
		for (final SessionInfo i : sessionInfos) {
			final HTMLElement tr = tbody.tr();
			tr.td().span(Styles.EL_SESSION).text(i.getId());
			tr.td().text(dateFormat.format(new Date(i.getStartTimeStamp())));
			tr.td().text(dateFormat.format(new Date(i.getDumpTimeStamp())));
		}
	}

	private void executionDataTable(final HTMLElement body) throws IOException {
		final HTMLElement table = body.table(Styles.COVERAGETABLE);
		{
			final HTMLElement tr = table.thead().tr();
			tr.td().text("Class");
			tr.td().text("Id");
		}
		final HTMLElement tbody = table.tbody();
		final ILanguageNames names = context.getLanguageNames();
		// BEGIN android-change
		for (final IExecutionData e : executionData) {
		// END android-change
			final HTMLElement tr = tbody.tr();
			final String link = index.getLinkToClass(e.getId());
			final String qualifiedName = names.getQualifiedClassName(e
					.getName());
			if (link == null) {
				tr.td().span(Styles.EL_CLASS).text(qualifiedName);
			} else {
				tr.td().a(link, Styles.EL_CLASS).text(qualifiedName);
			}
			final String id = String.format("%016x", Long.valueOf(e.getId()));
			tr.td().code().text(id);
		}
	}

	@Override
	protected String getFileName() {
		return "jacoco-sessions.html";
	}

	public String getLinkStyle() {
		return Styles.EL_SESSION;
	}

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

}