summaryrefslogtreecommitdiff
path: root/qpid/java/broker-plugins/experimental/info/src/test/java/org/apache/qpid/info/test/XMLWriterTest.java
blob: f352226361fc69eee501cd767a04c67f87c52fb1 (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package org.apache.qpid.info.test;

import junit.framework.TestCase;
import org.apache.qpid.info.util.XMLWriter;

import java.util.HashMap;

/*
 * This test verifies the XML writer custom class operations
 */

public class XMLWriterTest extends TestCase
{

    private XMLWriter xw = null;

    /** Test constructor arg is returned via getXML() */
    public void testXMLWriter()
    {
        StringBuffer input = new StringBuffer("Test");
        xw = new XMLWriter(input);
        assertNotNull("XMLWriter could not instantiate", xw);
        assertEquals("XMLWriter.getXML() failed", input, xw.getXML());
    }

    /** Test header generation */
    public void testWriteXMLHeader()
    {
        xw = new XMLWriter(new StringBuffer());
        assertNotNull(xw);
        xw.writeXMLHeader();
        assertEquals("XMLWriter.writeXMLHeader(...) failed", "<?xml version=\"1.0\"?>\n", xw.getXML().toString());
    }

    /** Test tag created and written correctly */
    public void testWriteTag()
    {
        String INDENT = "    ";
        xw = new XMLWriter(new StringBuffer());
        assertNotNull("XMLWriter could not instantiate", xw);
        xw.writeTag("test", new HashMap<String, String>(), "TEST");
        assertEquals("XMLWriter.writeTag(...) failed", "<test>\n" + INDENT + "TEST\n" + "</test>\n", xw.getXML()
                .toString());
    }

    /** Test tag created and written correctly */
    public void testWriteTagWithNullAttribute()
    {
        String INDENT = "    ";
        xw = new XMLWriter(new StringBuffer());
        assertNotNull("XMLWriter could not instantiate", xw);
        xw.writeTag("test", null, "TEST");
        assertEquals("XMLWriter.writeTag(...) failed", "<test>\n" + INDENT + "TEST\n" + "</test>\n", xw.getXML()
                .toString());
    }

    /** Test tag created and written correctly with attribute */
    public void testWriteTagWithAttribute()
    {
        String INDENT = "    ";
        xw = new XMLWriter(new StringBuffer());
        assertNotNull("XMLWriter could not instantiate", xw);
        HashMap<String, String> attr = new HashMap<String, String>();
        attr.put("id", "1");

        xw.writeTag("test", attr, "TEST");
        assertEquals("XMLWriter.writeTag(...) failed", "<test id=\"1\">\n" + INDENT + "TEST\n" + "</test>\n", xw.getXML()
                .toString());
    }

    /** Test open tag with an empty attribute map. Just creates an open tag */
    public void testWriteOpenTag()
    {
        xw = new XMLWriter(new StringBuffer());
        assertNotNull(xw);
        HashMap<String, String> attr = new HashMap<String, String>();
        xw.writeOpenTag("test", attr);
        assertEquals("XMLWriter.writeOpenTag(...) failed", "<test>\n", xw.getXML().toString());
    }

    /** Test open tag with a null attribute map. Just creates an open tag */
    public void testNullAtrributeOnTag()
    {
        xw = new XMLWriter(new StringBuffer());
        assertNotNull(xw);
        xw.writeOpenTag("test", null);
        assertEquals("XMLWriter.writeOpenTag(...) failed", "<test>\n", xw.getXML().toString());
    }

    /** Test that setting an attribute value on the tag is correctly outputted. */
    public void testAtrributeOnTag()
    {
        xw = new XMLWriter(new StringBuffer());
        assertNotNull(xw);
        HashMap<String, String> attr = new HashMap<String, String>();

        attr.put("id", "1");
        xw.writeOpenTag("test1", attr);
        assertEquals("XMLWriter.writeOpenTag(...) failed", "<test1 id=\"1\">\n", xw.getXML().toString());
    }

    /** Test Close Tag is correctly written */
    public void testWriteCloseTag()
    {
        xw = new XMLWriter(new StringBuffer());
        assertNotNull(xw);
        xw.writeCloseTag("test");
        assertEquals("</test>\n", xw.getXML().toString());
    }

}