summaryrefslogtreecommitdiffstats
blob: 837719366a96942d68826115f5e80fd423fc2a25 (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
/*
 * Copyright (c) 2012 Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010 Remko Tronçon
 * All rights reserved.
 */
package com.isode.stroke.serializer.xml;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;

public class XMLElementTest {

    @Test
    public void testSerialize() {
        XMLElement testling = new XMLElement("foo", "http://example.com");
        testling.setAttribute("myatt", "myval");
        XMLElement barElement = new XMLElement("bar");
        barElement.addNode(new XMLTextNode("Blo"));
        testling.addNode(barElement);
        XMLElement bazElement = new XMLElement("baz");
        bazElement.addNode(new XMLTextNode("Bli&</stream>"));
        testling.addNode(bazElement);

        String result = testling.serialize();
        String expectedResult =
                "<foo myatt=\"myval\" xmlns=\"http://example.com\">"
                + "<bar>Blo</bar>"
                + "<baz>Bli&amp;&lt;/stream&gt;</baz>"
                + "</foo>";

        assertEquals(expectedResult, result);
    }

    @Test
    public void testSerialize_NoChildren() {
        XMLElement testling = new XMLElement("foo", "http://example.com");

        assertEquals("<foo xmlns=\"http://example.com\"/>", testling.serialize());
    }

    @Test
    public void testSerialize_SpecialAttributeCharacters() {
        XMLElement testling = new XMLElement("foo");
        testling.setAttribute("myatt", "<\"'&>");

        assertEquals("<foo myatt=\"&lt;&quot;&apos;&amp;&gt;\"/>", testling.serialize());
    }

    @Test
    public void testSerialize_EmptyAttributeValue() {
        XMLElement testling = new XMLElement("foo");
        testling.setAttribute("myatt", "");

        assertEquals("<foo myatt=\"\"/>", testling.serialize());
    }
}