aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/html/HTMLFormatterTest.java
blob: a26c9095f68feabe3b41a81082d5df2a0cc4ac16 (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
/*******************************************************************************
 * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.report.html;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import org.jacoco.report.ILanguageNames;
import org.jacoco.report.MemoryMultiReportOutput;
import org.jacoco.report.ReportStructureTestDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link HTMLFormatter}.
 */
public class HTMLFormatterTest {

	private ReportStructureTestDriver driver;

	private HTMLFormatter formatter;

	private MemoryMultiReportOutput output;

	@Before
	public void setup() {
		driver = new ReportStructureTestDriver();
		formatter = new HTMLFormatter();
		output = new MemoryMultiReportOutput();
	}

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

	@Test
	public void testStructureWithNestedGroups() throws IOException {
		driver.sendNestedGroups(formatter.createVisitor(output));
		output.assertFile("index.html");
		output.assertFile("group1/index.html");
		output.assertFile("group1/group/index.html");
		output.assertFile("group1/group/bundle/index.html");
		output.assertFile("bundle/index.html");
	}

	@Test
	public void testStructureWithGroup() throws IOException {
		driver.sendGroup(formatter.createVisitor(output));
		output.assertFile("index.html");
		output.assertFile("bundle/index.html");

		output.assertFile("bundle/org.jacoco.example/index.html");
		output.assertFile("bundle/org.jacoco.example/index.source.html");
		output.assertFile("bundle/org.jacoco.example/FooClass.html");
		output.assertFile("bundle/org.jacoco.example/FooClass.java.html");
		output.assertNoFile("bundle/org.jacoco.example/Empty.html");
		output.assertNoFile("bundle/org.jacoco.example/Empty.java.html");

		output.assertNoFile("bundle/empty/index.html");
		output.assertNoFile("bundle/empty/index.source.html");
		output.assertNoFile("bundle/empty/Empty.html");
		output.assertNoFile("bundle/empty/Empty.java.html");
	}

	@Test
	public void testStructureWithBundleOnly() throws IOException {
		driver.sendBundle(formatter.createVisitor(output));
		output.assertFile("index.html");

		output.assertFile("org.jacoco.example/index.html");
		output.assertFile("org.jacoco.example/index.source.html");
		output.assertFile("org.jacoco.example/FooClass.html");
		output.assertFile("org.jacoco.example/FooClass.java.html");
		output.assertNoFile("org.jacoco.example/Empty.html");
		output.assertNoFile("org.jacoco.example/Empty.java.html");

		output.assertNoFile("empty/index.html");
		output.assertNoFile("empty/index.source.html");
		output.assertNoFile("empty/Empty.html");
		output.assertNoFile("empty/Empty.java.html");
	}

	@Test
	public void testDefaultEncoding() throws Exception {
		driver.sendBundle(formatter.createVisitor(output));
		final BufferedReader reader = new BufferedReader(new InputStreamReader(
				output.getFileAsStream("index.html"), "UTF-8"));
		final String line = reader.readLine();
		assertTrue(line,
				line.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\""));
	}

	@Test
	public void testSetEncoding() throws Exception {
		formatter.setOutputEncoding("UTF-16");
		driver.sendBundle(formatter.createVisitor(output));
		final BufferedReader reader = new BufferedReader(new InputStreamReader(
				output.getFileAsStream("index.html"), "UTF-16"));
		final String line = reader.readLine();
		assertTrue(line,
				line.startsWith("<?xml version=\"1.0\" encoding=\"UTF-16\""));
	}

	@Test
	public void testGetLanguageNames() throws Exception {
		ILanguageNames names = new ILanguageNames() {
			public String getPackageName(String vmname) {
				return null;
			}

			public String getQualifiedClassName(String vmname) {
				return null;
			}

			public String getClassName(String vmname, String vmsignature,
					String vmsuperclass, String[] vminterfaces) {
				return null;
			}

			public String getMethodName(String vmclassname,
					String vmmethodname, String vmdesc, String vmsignature) {
				return null;
			}

			public String getQualifiedMethodName(String vmclassname,
					String vmmethodname, String vmdesc, String vmsignature) {
				return null;
			}
		};
		formatter.setLanguageNames(names);
		assertSame(names, formatter.getLanguageNames());
		output.close();
	}

	@Test
	public void testGetFooterText() throws Exception {
		formatter.setFooterText("Custom Footer");
		assertEquals("Custom Footer", formatter.getFooterText());
		output.close();
	}

	@Test
	public void testGetLocale() throws Exception {
		formatter.setLocale(Locale.KOREAN);
		assertEquals(Locale.KOREAN, formatter.getLocale());
		output.close();
	}

}