KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > xml > XmlElementTest


1 /*
2  * Created on 2003-okt-30
3  */

4 package org.columba.core.xml;
5
6 import junit.framework.TestCase;
7
8
9 /**
10  * Tests for the <code>XmlElement</code> class.
11  *
12  * @author Erik Mattsson
13  */

14 public class XmlElementTest extends TestCase {
15     /*
16  * Test for boolean equals(Object)
17  */

18     public void testEquals() {
19         XmlElement xml1 = new XmlElement();
20         XmlElement xml2 = new XmlElement();
21         xml1.setName("ONE");
22         xml2.setName("ONE");
23         xml1.setData("DATA");
24         xml2.setData("DATA");
25         xml1.addAttribute("name", "value");
26         xml2.addAttribute("name", "value");
27
28         XmlElement child1 = new XmlElement("child1");
29         XmlElement sibling1 = new XmlElement("sibling1");
30         XmlElement child2 = new XmlElement("child2");
31         XmlElement sibling2 = new XmlElement("sibling2");
32
33         child1.addElement((XmlElement) child2.clone());
34         child1.addElement((XmlElement) sibling2.clone());
35
36         xml1.addElement((XmlElement) child1.clone());
37         xml1.addElement((XmlElement) sibling1.clone());
38         xml2.addElement((XmlElement) child1.clone());
39         xml2.addElement((XmlElement) sibling1.clone());
40
41         assertTrue("The XML elements are not equal", xml1.equals(xml2));
42         assertTrue("The XML elements are not equal", xml2.equals(xml1));
43         assertTrue("The XML elements are not equal", xml1.equals(xml1));
44         assertTrue("The XML elements are not equal", xml2.equals(xml2));
45
46         assertFalse("The XML elements are equal to null ", xml1.equals(null));
47         assertFalse("The XML elements are equal to null ", xml2.equals(null));
48     }
49
50     /*
51  * Test for boolean equals(Object)
52  */

53     public void testEquals2() {
54         XmlElement xml1 = new XmlElement();
55         XmlElement xml2 = new XmlElement();
56         xml1.setName("ONE");
57         xml2.setName("ONE");
58         xml1.setData("DATA");
59         xml2.setData("DATA");
60         xml1.addAttribute("name", "value");
61         assertTrue("The XML elements are equal", !xml1.equals(xml2));
62         assertTrue("The XML elements are equal", !xml2.equals(xml1));
63     }
64
65     /*
66  * Test for boolean not equals(Object)
67  */

68     public void testNotEqualsObject() {
69         XmlElement xml1 = new XmlElement();
70         XmlElement xml2 = new XmlElement();
71         xml1.setName("ONE");
72         xml2.setName("ONE");
73         xml1.addElement(new XmlElement("child1"));
74         assertTrue("The XML elements are equal", !xml1.equals(xml2));
75         assertTrue("The XML elements are equal", !xml2.equals(xml1));
76     }
77
78     /*
79  * Test for hashCode()
80  */

81     public void testHashcode() {
82         XmlElement xml1 = new XmlElement();
83         XmlElement xml2 = new XmlElement();
84         xml1.setName("ONE");
85         xml2.setName("ONE");
86         xml1.addElement(new XmlElement("child1"));
87         xml2.addElement(new XmlElement("child1"));
88         assertEquals("The hashcode are not equal", xml2.hashCode(),
89             xml1.hashCode());
90     }
91
92     /*
93  * Test for clone()
94  */

95     public void testClone() {
96         XmlElement xml1 = new XmlElement("a Name");
97         XmlElement xml2 = (XmlElement) xml1.clone();
98         assertEquals("The parent and the cloned object are not equal", xml1,
99             xml2);
100         assertNotSame("The parent and the cloned object are the same", xml1,
101             xml2);
102         assertNotSame("The parent and the cloned Attributes objects are the same object.",
103             xml1.getAttributes(), xml2.getAttributes());
104         assertNotSame("The parent and the cloned Sub Element objects are the same object.",
105             xml1.getElements(), xml2.getElements());
106
107         xml1 = new XmlElement("a Name", "data");
108         xml2 = (XmlElement) xml1.clone();
109         assertEquals("The parent and the cloned object are not equal", xml1,
110             xml2);
111         assertNotSame("The parent and the cloned object are the same", xml1,
112             xml2);
113         assertNotSame("The parent and the cloned Attributes objects are the same object.",
114             xml1.getAttributes(), xml2.getAttributes());
115         assertNotSame("The parent and the cloned Sub Element objects are the same object.",
116             xml1.getElements(), xml2.getElements());
117
118         xml1 = new XmlElement();
119         xml1.setName("a NAME");
120         xml1.addAttribute("key", "values");
121         xml1.addAttribute("key2", "other values");
122         xml1.addSubElement("child");
123         xml1.addSubElement(new XmlElement("child2"));
124         xml2 = (XmlElement) xml1.clone();
125         assertEquals("The parent and the cloned object are not equal", xml1,
126             xml2);
127         assertNotSame("The parent and the cloned object are the same", xml1,
128             xml2);
129         assertNotSame("The parent and the cloned Attributes objects are the same object.",
130             xml1.getAttributes(), xml2.getAttributes());
131         assertNotSame("The parent and the cloned Sub Element objects are the same object.",
132             xml1.getElements(), xml2.getElements());
133         assertEquals("The Name is not the same", "a NAME", xml2.getName());
134         assertEquals("The value for Attributes key='key' is not the expected",
135             "values", xml2.getAttribute("key"));
136         assertEquals("The value for Attributes key='key2' is not the expected",
137             "other values", xml2.getAttribute("key2"));
138         assertEquals("The first childs name is not the expected", "child",
139             xml2.getElement(0).getName());
140         assertEquals("The second childs name is not the expected", "child2",
141             xml2.getElement(1).getName());
142         assertEquals("The parent and cloned object hashCode() methods return different values.",
143             xml1.hashCode(), xml2.hashCode());
144     }
145
146     /*
147  * Test for XmlElement(String,String)
148  */

149     public void testConstructorStrStr() {
150         XmlElement xml = new XmlElement("a Name", "a Data");
151         xml.addAttribute("key", "values");
152         xml.addAttribute("key2", "other values");
153         xml.addSubElement("child");
154         xml.addSubElement(new XmlElement("child2"));
155
156         assertEquals("The Name isnt correct", "a Name", xml.getName());
157         assertEquals("The Data isnt correct", "a Data", xml.getData());
158         assertEquals("The attribute 'key' isnt correct", "values",
159             xml.getAttribute("key"));
160         assertEquals("The attribute 'key' isnt correct", "other values",
161             xml.getAttribute("key2"));
162         assertEquals("The child element isnt correct", "child",
163             xml.getElement(0).getName());
164     }
165 }
166
Popular Tags