aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLElementTest.java
blob: 5ed161c2199e8dfe3d6c9fae2b045f77cf4eacf2 (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
/*******************************************************************************
 * 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.internal.xml;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link XMLElement}.
 */
public class XMLElementTest {

	private static final String DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

	private ByteArrayOutputStream buffer;

	private XMLElement root;

	@Before
	public void setup() throws IOException {
		buffer = new ByteArrayOutputStream();
		root = new XMLElement("root", null, null, false, "UTF-8", buffer);
	}

	@Test
	public void init_should_write_doctype_when_given() throws IOException {
		root = new XMLElement("root", "-//JACOCO//TEST", "test.dtd", false,
				"UTF-8", buffer);
		assertEquals(DECL
				+ "<!DOCTYPE root PUBLIC \"-//JACOCO//TEST\" \"test.dtd\"><root/>",
				actual());
	}

	@Test
	public void init_should_write_standalone_when_given() throws IOException {
		root = new XMLElement("root", null, null, true, "UTF-8", buffer);
		assertEquals(
				"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><root/>",
				actual());
	}

	@Test
	public void close_should_emit_empty_element_when_no_children_exist()
			throws IOException {
		assertContent("<root/>");
	}

	@Test
	public void close_should_be_allowed_multiple_times() throws IOException {
		root.close();
		root.close();
		assertContent("<root/>");
	}

	@Test(expected = IOException.class)
	public void attr_should_throw_exception_when_closed() throws IOException {
		root.close();
		root.attr("attr", "value");
	}

	@Test(expected = IOException.class)
	public void element_should_throw_exception_when_closed()
			throws IOException {
		root.close();
		root.element("child");
	}

	@Test(expected = IOException.class)
	public void text_should_throw_exception_when_closed() throws IOException {
		root.close();
		root.text("text");
	}

	@Test
	public void element_should_emit_nested_element() throws IOException {
		root.element("world");
		assertContent("<root><world/></root>");
	}

	@Test
	public void element_should_allow_multiple_nested_elements()
			throws IOException {
		root.element("world");
		root.element("universe");
		assertContent("<root><world/><universe/></root>");
	}

	@Test
	public void text_should_emit_text() throws IOException {
		root.text("world");
		assertContent("<root>world</root>");
	}

	@Test
	public void text_should_allow_mixing_with_elements() throws IOException {
		root.element("tag1");
		root.text("world");
		root.element("tag2");
		assertContent("<root><tag1/>world<tag2/></root>");
	}

	@Test
	public void test_should_be_quoted() throws IOException {
		root.text("<black&white\">");
		assertContent("<root>&lt;black&amp;white&quot;&gt;</root>");
	}

	@Test
	public void attr_should_ignore_call_when_value_is_null()
			throws IOException {
		root.attr("id", null);
		assertContent("<root/>");
	}

	@Test
	public void attr_should_emit_string_value() throws IOException {
		root.attr("id", "12345");
		assertContent("<root id=\"12345\"/>");
	}

	@Test
	public void attr_should_quote_string_value() throws IOException {
		root.attr("quote", "<\">");
		assertContent("<root quote=\"&lt;&quot;&gt;\"/>");
	}

	@Test
	public void attr_should_emit_int_value() throws IOException {
		root.attr("missed", 0);
		root.attr("total", 123);
		assertContent("<root missed=\"0\" total=\"123\"/>");
	}

	@Test
	public void attr_should_emit_long_value() throws IOException {
		root.attr("min", Long.MIN_VALUE);
		root.attr("max", Long.MAX_VALUE);
		assertContent(
				"<root min=\"-9223372036854775808\" max=\"9223372036854775807\"/>");
	}

	@Test(expected = IOException.class)
	public void attr_should_throw_exception_when_text_was_added()
			throws IOException {
		root.text("text");
		root.attr("id", "12345");
	}

	@Test(expected = IOException.class)
	public void attr_should_throw_exception_when_child_was_added()
			throws IOException {
		root.element("child");
		root.attr("id", "12345");
	}

	private void assertContent(String expected) throws IOException {
		assertEquals(DECL + expected, actual());
	}

	private String actual() throws IOException {
		root.close();
		return buffer.toString("UTF-8");
	}

}