SessionsPage.java revision 8bc34b18bc5a0b53a94859089c876740c82cec5c
1/*******************************************************************************
2 * Copyright (c) 2009, 2016 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 *******************************************************************************/
12package org.jacoco.report.internal.html.page;
13
14import java.io.IOException;
15import java.text.DateFormat;
16import java.util.ArrayList;
17import java.util.Collection;
18import java.util.Collections;
19import java.util.Comparator;
20import java.util.Date;
21import java.util.List;
22
23import org.jacoco.core.data.ExecutionData;
24import org.jacoco.core.data.SessionInfo;
25import org.jacoco.report.ILanguageNames;
26import org.jacoco.report.internal.ReportOutputFolder;
27import org.jacoco.report.internal.html.HTMLElement;
28import org.jacoco.report.internal.html.IHTMLReportContext;
29import org.jacoco.report.internal.html.index.ElementIndex;
30import org.jacoco.report.internal.html.resources.Styles;
31
32/**
33 * Page to display information about sessions covered by this report.
34 */
35public class SessionsPage extends ReportPage {
36
37	private static final String MSG_SESSIONS = "This coverage report is based "
38			+ "on execution data from the following sessions:";
39
40	private static final String MSG_NO_SESSIONS = "No session information available.";
41
42	private static final String MSG_EXECDATA = "Execution data for the "
43			+ "following classes is considered in this report:";
44
45	private static final String MSG_NO_EXECDATA = "No execution data available.";
46
47	private final List<SessionInfo> sessionInfos;
48
49	private final DateFormat dateFormat;
50
51	private final List<ExecutionData> executionData;
52
53	private final ElementIndex index;
54
55	/**
56	 * Creates a new page page to display session information.
57	 *
58	 * @param sessionInfos
59	 *            session info objects
60	 * @param executionData
61	 *            execution data objects
62	 * @param index
63	 *            index for cross-linking
64	 * @param parent
65	 *            optional hierarchical parent
66	 * @param folder
67	 *            base folder to create this page in
68	 * @param context
69	 *            settings context
70	 */
71	public SessionsPage(final List<SessionInfo> sessionInfos,
72			final Collection<ExecutionData> executionData,
73			final ElementIndex index, final ReportPage parent,
74			final ReportOutputFolder folder, final IHTMLReportContext context) {
75		super(parent, folder, context);
76		this.sessionInfos = sessionInfos;
77		this.executionData = new ArrayList<ExecutionData>(executionData);
78		this.index = index;
79		this.dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,
80				DateFormat.DEFAULT, context.getLocale());
81		final ILanguageNames names = context.getLanguageNames();
82		Collections.sort(this.executionData, new Comparator<ExecutionData>() {
83			public int compare(final ExecutionData e1, final ExecutionData e2) {
84				return names.getQualifiedClassName(e1.getName()).compareTo(
85						names.getQualifiedClassName(e2.getName()));
86			}
87		});
88	}
89
90	@Override
91	protected void content(final HTMLElement body) throws IOException {
92		if (sessionInfos.isEmpty()) {
93			body.p().text(MSG_NO_SESSIONS);
94		} else {
95			body.p().text(MSG_SESSIONS);
96			sessionTable(body);
97		}
98		if (executionData.isEmpty()) {
99			body.p().text(MSG_NO_EXECDATA);
100		} else {
101			body.p().text(MSG_EXECDATA);
102			executionDataTable(body);
103		}
104	}
105
106	private void sessionTable(final HTMLElement body) throws IOException {
107		final HTMLElement table = body.table(Styles.COVERAGETABLE);
108		{
109			final HTMLElement tr = table.thead().tr();
110			tr.td().text("Session");
111			tr.td().text("Start Time");
112			tr.td().text("Dump Time");
113		}
114		final HTMLElement tbody = table.tbody();
115		for (final SessionInfo i : sessionInfos) {
116			final HTMLElement tr = tbody.tr();
117			tr.td().span(Styles.EL_SESSION).text(i.getId());
118			tr.td().text(dateFormat.format(new Date(i.getStartTimeStamp())));
119			tr.td().text(dateFormat.format(new Date(i.getDumpTimeStamp())));
120		}
121	}
122
123	private void executionDataTable(final HTMLElement body) throws IOException {
124		final HTMLElement table = body.table(Styles.COVERAGETABLE);
125		{
126			final HTMLElement tr = table.thead().tr();
127			tr.td().text("Class");
128			tr.td().text("Id");
129		}
130		final HTMLElement tbody = table.tbody();
131		final ILanguageNames names = context.getLanguageNames();
132		for (final ExecutionData e : executionData) {
133			final HTMLElement tr = tbody.tr();
134			final String link = index.getLinkToClass(e.getId());
135			final String qualifiedName = names.getQualifiedClassName(e
136					.getName());
137			if (link == null) {
138				tr.td().span(Styles.EL_CLASS).text(qualifiedName);
139			} else {
140				tr.td().a(link, Styles.EL_CLASS).text(qualifiedName);
141			}
142			final String id = String.format("%016x", Long.valueOf(e.getId()));
143			tr.td().code().text(id);
144		}
145	}
146
147	@Override
148	protected String getFileName() {
149		return "jacoco-sessions.html";
150	}
151
152	public String getLinkStyle() {
153		return Styles.EL_SESSION;
154	}
155
156	public String getLinkLabel() {
157		return "Sessions";
158	}
159
160}
161