aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/internal/xml/XMLElementTest.java
blob: 18919c218a8b7e219bd14e953d3bb738b59dbd61 (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
/*******************************************************************************
 * Copyright (c) 2009, 2016 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.IOException;
import java.io.StringWriter;

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

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

	private StringWriter buffer;

	private XMLElement root;

	@Before
	public void setUp() throws IOException {
		buffer = new StringWriter();
		root = new XMLElement(buffer, "root");
		root.beginOpenTag();
	}

	@Test
	public void testEmptyNode() throws IOException {
		root.close();
		// Second close has no effect:
		root.close();
		assertEquals("<root/>", buffer.toString());
	}

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

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

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

	@Test
	public void testNestedElement() throws IOException {
		root.element("world");
		root.close();
		assertEquals("<root><world/></root>", buffer.toString());
	}

	@Test
	public void test2NestedElements() throws IOException {
		root.element("world");
		root.element("universe");
		root.close();
		assertEquals("<root><world/><universe/></root>", buffer.toString());
	}

	@Test
	public void testText() throws IOException {
		root.text("world");
		root.close();
		assertEquals("<root>world</root>", buffer.toString());
	}

	@Test
	public void testMixedContent() throws IOException {
		root.element("tag1");
		root.text("world");
		root.element("tag2");
		root.close();
		assertEquals("<root><tag1/>world<tag2/></root>", buffer.toString());
	}

	@Test
	public void testQuotedText() throws IOException {
		root.text("<black&white\">");
		root.close();
		assertEquals("<root>&lt;black&amp;white&quot;&gt;</root>",
				buffer.toString());
	}

	@Test
	public void testNullAttributes() throws IOException {
		root.attr("id", null);
		root.close();
		assertEquals("<root/>", buffer.toString());
	}

	@Test
	public void testStringAttributes() throws IOException {
		root.attr("id", "12345").attr("quote", "<\">");
		root.close();
		assertEquals("<root id=\"12345\" quote=\"&lt;&quot;&gt;\"/>",
				buffer.toString());
	}

	@Test
	public void testIntAttributes() throws IOException {
		root.attr("missed", 0).attr("total", 123);
		root.close();
		assertEquals("<root missed=\"0\" total=\"123\"/>", buffer.toString());
	}

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

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

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

}