1/*******************************************************************************
2 * Copyright (c) 2009, 2015 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.table;
13
14import static org.junit.Assert.assertEquals;
15import static org.junit.Assert.assertTrue;
16
17import java.io.IOException;
18import java.util.Comparator;
19import java.util.Locale;
20
21import org.jacoco.core.analysis.CoverageNodeImpl;
22import org.jacoco.core.analysis.ICoverageNode;
23import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
24import org.jacoco.core.analysis.ICoverageNode.ElementType;
25import org.jacoco.core.internal.analysis.CounterImpl;
26import org.jacoco.report.MemoryMultiReportOutput;
27import org.jacoco.report.internal.ReportOutputFolder;
28import org.jacoco.report.internal.html.HTMLDocument;
29import org.jacoco.report.internal.html.HTMLElement;
30import org.jacoco.report.internal.html.HTMLSupport;
31import org.jacoco.report.internal.html.resources.Resources;
32import org.junit.After;
33import org.junit.Before;
34import org.junit.Test;
35import org.w3c.dom.Document;
36
37/**
38 * Unit tests for {@link PercentageColumn}.
39 */
40public class PercentageColumnTest {
41
42	private MemoryMultiReportOutput output;
43
44	private ReportOutputFolder root;
45
46	private Resources resources;
47
48	private HTMLDocument doc;
49
50	private HTMLElement td;
51
52	private HTMLSupport support;
53
54	private IColumnRenderer column;
55
56	@Before
57	public void setup() throws Exception {
58		output = new MemoryMultiReportOutput();
59		root = new ReportOutputFolder(output);
60		resources = new Resources(root);
61		doc = new HTMLDocument(root.createFile("Test.html"), "UTF-8");
62		doc.head().title();
63		td = doc.body().table("somestyle").tr().td();
64		support = new HTMLSupport();
65		column = new PercentageColumn(CounterEntity.LINE, Locale.ENGLISH);
66	}
67
68	@After
69	public void teardown() throws IOException {
70		output.close();
71		output.assertAllClosed();
72	}
73
74	@Test
75	public void testInit() throws Exception {
76		assertTrue(column.init(null, null));
77		doc.close();
78	}
79
80	@Test
81	public void testItem1() throws Exception {
82		final ITableItem item = createItem(100, 50);
83		column.item(td, item, resources, root);
84		doc.close();
85		final Document doc = support.parse(output.getFile("Test.html"));
86		assertEquals("33%",
87				support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
88	}
89
90	@Test
91	public void testItem2() throws Exception {
92		final ITableItem item = createItem(0, 0);
93		column.item(td, item, resources, root);
94		doc.close();
95		final Document doc = support.parse(output.getFile("Test.html"));
96		assertEquals("n/a",
97				support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
98	}
99
100	@Test
101	public void testLocale() throws Exception {
102		IColumnRenderer column = new PercentageColumn(CounterEntity.LINE,
103				Locale.FRENCH);
104		final ITableItem item = createItem(0, 1000);
105		column.item(td, item, resources, root);
106		doc.close();
107		final Document doc = support.parse(output.getFile("Test.html"));
108		assertEquals("100 %",
109				support.findStr(doc, "/html/body/table/tr/td[1]/text()"));
110	}
111
112	@Test
113	public void testFooter1() throws Exception {
114		final ITableItem item = createItem(20, 60);
115		column.footer(td, item.getNode(), resources, root);
116		doc.close();
117		final Document doc = support.parse(output.getFile("Test.html"));
118		assertEquals("75%", support.findStr(doc, "/html/body/table/tr"));
119	}
120
121	@Test
122	public void testFooter2() throws Exception {
123		final ITableItem item = createItem(0, 0);
124		column.footer(td, item.getNode(), resources, root);
125		doc.close();
126		final Document doc = support.parse(output.getFile("Test.html"));
127		assertEquals("n/a", support.findStr(doc, "/html/body/table/tr"));
128	}
129
130	@Test
131	public void testComparator() throws Exception {
132		final Comparator<ITableItem> c = column.getComparator();
133		final ITableItem i1 = createItem(50, 50);
134		final ITableItem i2 = createItem(800, 200);
135		assertTrue(c.compare(i1, i2) < 0);
136		assertTrue(c.compare(i2, i1) > 0);
137		assertEquals(0, c.compare(i1, i1));
138		doc.close();
139	}
140
141	private ITableItem createItem(final int missed, final int covered) {
142		final ICoverageNode node = createNode(missed, covered);
143		return new ITableItem() {
144			public String getLinkLabel() {
145				return "Foo";
146			}
147
148			public String getLink(ReportOutputFolder base) {
149				return null;
150			}
151
152			public String getLinkStyle() {
153				return Resources.getElementStyle(node.getElementType());
154			}
155
156			public ICoverageNode getNode() {
157				return node;
158			}
159		};
160	}
161
162	private CoverageNodeImpl createNode(final int missed, final int covered) {
163		return new CoverageNodeImpl(ElementType.GROUP, "Foo") {
164			{
165				this.lineCounter = CounterImpl.getInstance(missed, covered);
166			}
167		};
168	}
169}
170