/******************************************************************************* * 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.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("", 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("", buffer.toString()); } @Test public void test2NestedElements() throws IOException { root.element("world"); root.element("universe"); root.close(); assertEquals("", buffer.toString()); } @Test public void testText() throws IOException { root.text("world"); root.close(); assertEquals("world", buffer.toString()); } @Test public void testMixedContent() throws IOException { root.element("tag1"); root.text("world"); root.element("tag2"); root.close(); assertEquals("world", buffer.toString()); } @Test public void testQuotedText() throws IOException { root.text(""); root.close(); assertEquals("<black&white">", buffer.toString()); } @Test public void testNullAttributes() throws IOException { root.attr("id", null); root.close(); assertEquals("", buffer.toString()); } @Test public void testStringAttributes() throws IOException { root.attr("id", "12345").attr("quote", "<\">"); root.close(); assertEquals("", buffer.toString()); } @Test public void testIntAttributes() throws IOException { root.attr("missed", 0).attr("total", 123); root.close(); assertEquals("", buffer.toString()); } @Test public void testLongAttributes() throws IOException { root.attr("min", Long.MIN_VALUE).attr("max", Long.MAX_VALUE); root.close(); assertEquals( "", 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"); } }