aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/xml/XMLFormatterTest.java
blob: b96815e79794948015896c7c30964e4a28dd9f1f (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
/*******************************************************************************
 * Copyright (c) 2009, 2017 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.xml;

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.SessionInfo;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.MemoryOutput;
import org.jacoco.report.ReportStructureTestDriver;
import org.jacoco.report.internal.xml.XMLSupport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

/**
 * Unit tests for {@link XMLFormatter}.
 */
public class XMLFormatterTest {

	private ReportStructureTestDriver driver;

	private XMLFormatter formatter;

	private MemoryOutput output;

	private List<SessionInfo> infos;

	private Collection<ExecutionData> data;

	@Before
	public void setup() {
		driver = new ReportStructureTestDriver();
		formatter = new XMLFormatter();
		output = new MemoryOutput();
		infos = new ArrayList<SessionInfo>();
		data = new ArrayList<ExecutionData>();
	}

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

	@Test
	public void testSessionInfo() throws Exception {
		infos.add(new SessionInfo("session-1", 12345, 67890));
		infos.add(new SessionInfo("session-2", 1, 2));
		infos.add(new SessionInfo("session-3", 1, 2));
		final IReportVisitor visitor = formatter.createVisitor(output);
		visitor.visitInfo(infos, data);
		visitor.visitGroup("foo");
		visitor.visitEnd();
		assertPathMatches("session-1", "/report/sessioninfo[1]/@id");
		assertPathMatches("12345", "/report/sessioninfo[1]/@start");
		assertPathMatches("67890", "/report/sessioninfo[1]/@dump");
		assertPathMatches("session-2", "/report/sessioninfo[2]/@id");
		assertPathMatches("session-3", "/report/sessioninfo[3]/@id");
	}

	@Test
	public void testStructureWithNestedGroups() throws Exception {
		final IReportVisitor visitor = formatter.createVisitor(output);
		visitor.visitInfo(infos, data);
		driver.sendNestedGroups(visitor);
		assertPathMatches("report", "/report/@name");
		assertPathMatches("group1", "/report/group[1]/@name");
		assertPathMatches("group", "/report/group[1]/group[1]/@name");
		assertPathMatches("bundle", "/report/group[1]/group[1]/group[1]/@name");
		assertPathMatches("bundle", "/report/group[2]/@name");
	}

	@Test
	public void testStructureWithGroup() throws Exception {
		final IReportVisitor visitor = formatter.createVisitor(output);
		visitor.visitInfo(infos, data);
		driver.sendGroup(visitor);
		assertPathMatches("group", "/report/@name");
		assertPathMatches("bundle", "/report/group/@name");
		assertPathMatches("org/jacoco/example", "/report/group/package/@name");
		assertPathMatches("org/jacoco/example/FooClass",
				"/report/group/package/class/@name");
		assertPathMatches("fooMethod",
				"/report/group/package/class/method/@name");

		assertPathMatches("1", "count(/report/counter[@type='INSTRUCTION'])");
		assertPathMatches("10", "report/counter[@type='INSTRUCTION']/@missed");
		assertPathMatches("15", "report/counter[@type='INSTRUCTION']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='BRANCH'])");
		assertPathMatches("1", "report/counter[@type='BRANCH']/@missed");
		assertPathMatches("2", "report/counter[@type='BRANCH']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='COMPLEXITY'])");
		assertPathMatches("1", "report/counter[@type='COMPLEXITY']/@missed");
		assertPathMatches("2", "report/counter[@type='COMPLEXITY']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='LINE'])");
		assertPathMatches("0", "report/counter[@type='LINE']/@missed");
		assertPathMatches("3", "report/counter[@type='LINE']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='METHOD'])");
		assertPathMatches("0", "report/counter[@type='METHOD']/@missed");
		assertPathMatches("1", "report/counter[@type='METHOD']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='CLASS'])");
		assertPathMatches("0", "report/counter[@type='CLASS']/@missed");
		assertPathMatches("1", "report/counter[@type='CLASS']/@covered");
	}

	@Test
	public void testStructureWithBundleOnly() throws Exception {
		final IReportVisitor visitor = formatter.createVisitor(output);
		visitor.visitInfo(infos, data);
		driver.sendBundle(visitor);
		assertPathMatches("bundle", "/report/@name");
		assertPathMatches("org/jacoco/example", "/report/package/@name");
		assertPathMatches("org/jacoco/example/FooClass",
				"/report/package/class/@name");
		assertPathMatches("fooMethod", "/report/package/class/method/@name");

		assertPathMatches("1", "count(/report/counter[@type='INSTRUCTION'])");
		assertPathMatches("10", "report/counter[@type='INSTRUCTION']/@missed");
		assertPathMatches("15", "report/counter[@type='INSTRUCTION']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='BRANCH'])");
		assertPathMatches("1", "report/counter[@type='BRANCH']/@missed");
		assertPathMatches("2", "report/counter[@type='BRANCH']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='COMPLEXITY'])");
		assertPathMatches("1", "report/counter[@type='COMPLEXITY']/@missed");
		assertPathMatches("2", "report/counter[@type='COMPLEXITY']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='LINE'])");
		assertPathMatches("0", "report/counter[@type='LINE']/@missed");
		assertPathMatches("3", "report/counter[@type='LINE']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='METHOD'])");
		assertPathMatches("0", "report/counter[@type='METHOD']/@missed");
		assertPathMatches("1", "report/counter[@type='METHOD']/@covered");

		assertPathMatches("1", "count(/report/counter[@type='CLASS'])");
		assertPathMatches("0", "report/counter[@type='CLASS']/@missed");
		assertPathMatches("1", "report/counter[@type='CLASS']/@covered");
	}

	@Test
	public void testDefaultEncoding() throws Exception {
		final IReportVisitor visitor = formatter.createVisitor(output);
		visitor.visitInfo(infos, data);
		driver.sendBundle(visitor);
		final BufferedReader reader = new BufferedReader(new InputStreamReader(
				output.getContentsAsStream(), "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");
		final IReportVisitor visitor = formatter.createVisitor(output);
		visitor.visitInfo(infos, data);
		driver.sendBundle(visitor);
		final BufferedReader reader = new BufferedReader(new InputStreamReader(
				output.getContentsAsStream(), "UTF-16"));
		final String line = reader.readLine();
		assertTrue(line,
				line.startsWith("<?xml version=\"1.0\" encoding=\"UTF-16\""));
	}

	private void assertPathMatches(String expected, String path)
			throws Exception {
		XMLSupport support = new XMLSupport(XMLFormatter.class);
		Document document = support.parse(output.toByteArray());
		assertEquals(expected, support.findStr(document, path));
	}

}