aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/internal/html/table/TableTest.java
blob: b17f6d22e78cd4232d8403c06bb37b90814cf706 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*******************************************************************************
 * 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.table;

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

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.jacoco.core.analysis.CounterComparator;
import org.jacoco.core.analysis.CoverageNodeImpl;
import org.jacoco.core.analysis.ICoverageNode;
import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
import org.jacoco.core.analysis.ICoverageNode.ElementType;
import org.jacoco.core.internal.analysis.CounterImpl;
import org.jacoco.report.MemoryMultiReportOutput;
import org.jacoco.report.internal.ReportOutputFolder;
import org.jacoco.report.internal.html.HTMLElement;
import org.jacoco.report.internal.html.HTMLSupport;
import org.jacoco.report.internal.html.resources.Resources;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

/**
 * Unit tests for {@link Table}.
 */
public class TableTest {

	private MemoryMultiReportOutput output;

	private ReportOutputFolder root;

	private Resources resources;

	private HTMLElement html;

	private HTMLElement body;

	private Table table;

	@Before
	public void setup() throws IOException {
		output = new MemoryMultiReportOutput();
		root = new ReportOutputFolder(output);
		resources = new Resources(root);
		html = new HTMLElement(root.createFile("Test.html"), "UTF-8");
		html.head().title();
		body = html.body();
		table = new Table();
	}

	@After
	public void teardown() throws IOException {
		output.close();
		output.assertAllClosed();
	}

	@Test
	public void testCallbackSequence() throws IOException {
		final IColumnRenderer recorder = new StubRenderer(
				CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)) {

			private final StringBuilder store = new StringBuilder();

			@Override
			public boolean init(List<? extends ITableItem> items,
					ICoverageNode total) {
				store.append("init-");
				return true;
			}

			@Override
			public void footer(HTMLElement td, ICoverageNode total,
					Resources resources, ReportOutputFolder base) {
				store.append("footer-");
			}

			@Override
			public void item(HTMLElement td, ITableItem item,
					Resources resources, ReportOutputFolder base) {
				store.append("item").append(item.getLinkLabel()).append("-");
			}

			@Override
			public String toString() {
				return store.toString();
			}

		};
		final List<ITableItem> items = Arrays.asList(createItem("A", 1),
				createItem("B", 2), createItem("C", 3));
		table.add("Header", null, recorder, false);
		table.render(body, items, createTotal("Sum", 6), resources, root);
		html.close();
		assertEquals("init-footer-itemA-itemB-itemC-", recorder.toString());
	}

	@Test
	public void testInvisible() throws IOException {
		final IColumnRenderer column = new StubRenderer(
				CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)) {

			@Override
			public boolean init(List<? extends ITableItem> items,
					ICoverageNode total) {
				return false;
			}

			@Override
			public void footer(HTMLElement td, ICoverageNode total,
					Resources resources, ReportOutputFolder base) {
				fail();
			}

			@Override
			public void item(HTMLElement td, ITableItem item,
					Resources resources, ReportOutputFolder base) {
				fail();
			}
		};
		final List<ITableItem> items = Arrays.asList(createItem("A", 1));
		table.add("Header", null, column, false);
		table.render(body, items, createTotal("Sum", 1), resources, root);
		html.close();
	}

	@Test(expected = IllegalStateException.class)
	public void testTwoDefaultSorts() throws IOException {
		html.close();
		table.add("Header1", null,
				new StubRenderer(
						CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)),
				true);
		table.add("Header2", null,
				new StubRenderer(
						CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)),
				true);
	}

	@Test
	public void testSortIds() throws Exception {
		final List<ITableItem> items = Arrays.asList(createItem("C", 3),
				createItem("E", 4), createItem("A", 1), createItem("D", 2));
		table.add("Forward", null,
				new StubRenderer(
						CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)),
				false);
		table.add("Reverse", null, new StubRenderer(
				CounterComparator.TOTALITEMS.reverse().on(CounterEntity.CLASS)),
				false);
		table.render(body, items, createTotal("Sum", 6), resources, root);
		html.close();

		final HTMLSupport support = new HTMLSupport();
		final Document doc = support.parse(output.getFile("Test.html"));

		// The elements in Column 1 are sorted in forward order:
		assertEquals("sortable",
				support.findStr(doc, "/html/body/table/thead/tr/td[1]/@class"));
		assertEquals("a",
				support.findStr(doc, "/html/body/table/thead/tr/td[1]/@id"));
		assertEquals("a2",
				support.findStr(doc, "/html/body/table/tbody/tr[1]/td[1]/@id"));
		assertEquals("a3",
				support.findStr(doc, "/html/body/table/tbody/tr[2]/td[1]/@id"));
		assertEquals("a0",
				support.findStr(doc, "/html/body/table/tbody/tr[3]/td[1]/@id"));
		assertEquals("a1",
				support.findStr(doc, "/html/body/table/tbody/tr[4]/td[1]/@id"));

		// The elements in Column 2 are sorted in reverse order:
		assertEquals("sortable",
				support.findStr(doc, "/html/body/table/thead/tr/td[2]/@class"));
		assertEquals("b",
				support.findStr(doc, "/html/body/table/thead/tr/td[2]/@id"));
		assertEquals("b1",
				support.findStr(doc, "/html/body/table/tbody/tr[1]/td[2]/@id"));
		assertEquals("b0",
				support.findStr(doc, "/html/body/table/tbody/tr[2]/td[2]/@id"));
		assertEquals("b3",
				support.findStr(doc, "/html/body/table/tbody/tr[3]/td[2]/@id"));
		assertEquals("b2",
				support.findStr(doc, "/html/body/table/tbody/tr[4]/td[2]/@id"));
	}

	@Test
	public void testDefaultSorting() throws Exception {
		final List<ITableItem> items = Arrays.asList(createItem("C", 3),
				createItem("E", 5), createItem("A", 1), createItem("D", 4),
				createItem("B", 2));
		table.add("Forward", null,
				new StubRenderer(
						CounterComparator.TOTALITEMS.on(CounterEntity.CLASS)),
				true);
		table.render(body, items, createTotal("Sum", 1), resources, root);
		html.close();

		final HTMLSupport support = new HTMLSupport();
		final Document doc = support.parse(output.getFile("Test.html"));

		assertEquals("down sortable",
				support.findStr(doc, "/html/body/table/thead/tr/td[1]/@class"));
		assertEquals("A",
				support.findStr(doc, "/html/body/table/tbody/tr[1]/td[1]"));
		assertEquals("B",
				support.findStr(doc, "/html/body/table/tbody/tr[2]/td[1]"));
		assertEquals("C",
				support.findStr(doc, "/html/body/table/tbody/tr[3]/td[1]"));
		assertEquals("D",
				support.findStr(doc, "/html/body/table/tbody/tr[4]/td[1]"));
		assertEquals("E",
				support.findStr(doc, "/html/body/table/tbody/tr[5]/td[1]"));
	}

	private ITableItem createItem(final String name, final int count) {
		final ICoverageNode node = new CoverageNodeImpl(ElementType.GROUP,
				name) {
			{
				this.classCounter = CounterImpl.getInstance(count, 0);
			}
		};
		return new ITableItem() {
			public String getLinkLabel() {
				return name;
			}

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

			public String getLinkStyle() {
				return Resources.getElementStyle(node.getElementType());
			}

			public ICoverageNode getNode() {
				return node;
			}
		};
	}

	private ICoverageNode createTotal(final String name, final int count) {
		return new CoverageNodeImpl(ElementType.GROUP, name) {
			{
				this.classCounter = CounterImpl.getInstance(count, 0);
			}
		};
	}

	private static class StubRenderer implements IColumnRenderer {

		private final Comparator<ITableItem> comparator;

		StubRenderer(Comparator<ICoverageNode> comparator) {
			this.comparator = new TableItemComparator(comparator);
		}

		public boolean init(List<? extends ITableItem> items,
				ICoverageNode total) {
			return true;
		}

		public void footer(HTMLElement td, ICoverageNode total,
				Resources resources, ReportOutputFolder base) {
		}

		public void item(HTMLElement td, ITableItem item, Resources resources,
				ReportOutputFolder base) throws IOException {
			td.text(item.getLinkLabel());
		}

		public Comparator<ITableItem> getComparator() {
			return comparator;
		}

	}

}