Home | History | Annotate | Download | only in xml
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Marc R. Hoffmann - initial API and implementation
     10  *
     11  *******************************************************************************/
     12 package org.jacoco.report.internal.xml;
     13 
     14 import static org.junit.Assert.assertEquals;
     15 
     16 import java.io.IOException;
     17 import java.io.StringWriter;
     18 
     19 import org.junit.Before;
     20 import org.junit.Test;
     21 
     22 /**
     23  * Unit tests for {@link XMLElement}.
     24  */
     25 public class XMLElementTest {
     26 
     27 	private StringWriter buffer;
     28 
     29 	private XMLElement root;
     30 
     31 	@Before
     32 	public void setUp() throws IOException {
     33 		buffer = new StringWriter();
     34 		root = new XMLElement(buffer, "root");
     35 		root.beginOpenTag();
     36 	}
     37 
     38 	@Test
     39 	public void testEmptyNode() throws IOException {
     40 		root.close();
     41 		// Second close has no effect:
     42 		root.close();
     43 		assertEquals("<root/>", buffer.toString());
     44 	}
     45 
     46 	@Test(expected = IOException.class)
     47 	public void testAddAttributeToClosedNode() throws IOException {
     48 		root.close();
     49 		root.attr("attr", "value");
     50 	}
     51 
     52 	@Test(expected = IOException.class)
     53 	public void testAddChildToClosedNode() throws IOException {
     54 		root.close();
     55 		root.element("child");
     56 	}
     57 
     58 	@Test(expected = IOException.class)
     59 	public void testAddTextToClosedNode() throws IOException {
     60 		root.close();
     61 		root.text("text");
     62 	}
     63 
     64 	@Test
     65 	public void testNestedElement() throws IOException {
     66 		root.element("world");
     67 		root.close();
     68 		assertEquals("<root><world/></root>", buffer.toString());
     69 	}
     70 
     71 	@Test
     72 	public void test2NestedElements() throws IOException {
     73 		root.element("world");
     74 		root.element("universe");
     75 		root.close();
     76 		assertEquals("<root><world/><universe/></root>", buffer.toString());
     77 	}
     78 
     79 	@Test
     80 	public void testText() throws IOException {
     81 		root.text("world");
     82 		root.close();
     83 		assertEquals("<root>world</root>", buffer.toString());
     84 	}
     85 
     86 	@Test
     87 	public void testMixedContent() throws IOException {
     88 		root.element("tag1");
     89 		root.text("world");
     90 		root.element("tag2");
     91 		root.close();
     92 		assertEquals("<root><tag1/>world<tag2/></root>", buffer.toString());
     93 	}
     94 
     95 	@Test
     96 	public void testQuotedText() throws IOException {
     97 		root.text("<black&white\">");
     98 		root.close();
     99 		assertEquals("<root>&lt;black&amp;white&quot;&gt;</root>",
    100 				buffer.toString());
    101 	}
    102 
    103 	@Test
    104 	public void testNullAttributes() throws IOException {
    105 		root.attr("id", null);
    106 		root.close();
    107 		assertEquals("<root/>", buffer.toString());
    108 	}
    109 
    110 	@Test
    111 	public void testStringAttributes() throws IOException {
    112 		root.attr("id", "12345").attr("quote", "<\">");
    113 		root.close();
    114 		assertEquals("<root id=\"12345\" quote=\"&lt;&quot;&gt;\"/>",
    115 				buffer.toString());
    116 	}
    117 
    118 	@Test
    119 	public void testIntAttributes() throws IOException {
    120 		root.attr("missed", 0).attr("total", 123);
    121 		root.close();
    122 		assertEquals("<root missed=\"0\" total=\"123\"/>", buffer.toString());
    123 	}
    124 
    125 	@Test
    126 	public void testLongAttributes() throws IOException {
    127 		root.attr("min", Long.MIN_VALUE).attr("max", Long.MAX_VALUE);
    128 		root.close();
    129 		assertEquals(
    130 				"<root min=\"-9223372036854775808\" max=\"9223372036854775807\"/>",
    131 				buffer.toString());
    132 	}
    133 
    134 	@Test(expected = IOException.class)
    135 	public void testInvalidAttributeOutput1() throws IOException {
    136 		root.text("text");
    137 		root.attr("id", "12345");
    138 	}
    139 
    140 	@Test(expected = IOException.class)
    141 	public void testInvalidAttributeOutput2() throws IOException {
    142 		root.element("child");
    143 		root.attr("id", "12345");
    144 	}
    145 
    146 }
    147