001: /*
002: * Created on 2003-okt-30
003: */
004: package org.columba.core.xml;
005:
006: import junit.framework.TestCase;
007:
008: /**
009: * Tests for the <code>XmlElement</code> class.
010: *
011: * @author Erik Mattsson
012: */
013: public class XmlElementTest extends TestCase {
014: /*
015: * Test for boolean equals(Object)
016: */
017: public void testEquals() {
018: XmlElement xml1 = new XmlElement();
019: XmlElement xml2 = new XmlElement();
020: xml1.setName("ONE");
021: xml2.setName("ONE");
022: xml1.setData("DATA");
023: xml2.setData("DATA");
024: xml1.addAttribute("name", "value");
025: xml2.addAttribute("name", "value");
026:
027: XmlElement child1 = new XmlElement("child1");
028: XmlElement sibling1 = new XmlElement("sibling1");
029: XmlElement child2 = new XmlElement("child2");
030: XmlElement sibling2 = new XmlElement("sibling2");
031:
032: child1.addElement((XmlElement) child2.clone());
033: child1.addElement((XmlElement) sibling2.clone());
034:
035: xml1.addElement((XmlElement) child1.clone());
036: xml1.addElement((XmlElement) sibling1.clone());
037: xml2.addElement((XmlElement) child1.clone());
038: xml2.addElement((XmlElement) sibling1.clone());
039:
040: assertTrue("The XML elements are not equal", xml1.equals(xml2));
041: assertTrue("The XML elements are not equal", xml2.equals(xml1));
042: assertTrue("The XML elements are not equal", xml1.equals(xml1));
043: assertTrue("The XML elements are not equal", xml2.equals(xml2));
044:
045: assertFalse("The XML elements are equal to null ", xml1
046: .equals(null));
047: assertFalse("The XML elements are equal to null ", xml2
048: .equals(null));
049: }
050:
051: /*
052: * Test for boolean equals(Object)
053: */
054: public void testEquals2() {
055: XmlElement xml1 = new XmlElement();
056: XmlElement xml2 = new XmlElement();
057: xml1.setName("ONE");
058: xml2.setName("ONE");
059: xml1.setData("DATA");
060: xml2.setData("DATA");
061: xml1.addAttribute("name", "value");
062: assertTrue("The XML elements are equal", !xml1.equals(xml2));
063: assertTrue("The XML elements are equal", !xml2.equals(xml1));
064: }
065:
066: /*
067: * Test for boolean not equals(Object)
068: */
069: public void testNotEqualsObject() {
070: XmlElement xml1 = new XmlElement();
071: XmlElement xml2 = new XmlElement();
072: xml1.setName("ONE");
073: xml2.setName("ONE");
074: xml1.addElement(new XmlElement("child1"));
075: assertTrue("The XML elements are equal", !xml1.equals(xml2));
076: assertTrue("The XML elements are equal", !xml2.equals(xml1));
077: }
078:
079: /*
080: * Test for hashCode()
081: */
082: public void testHashcode() {
083: XmlElement xml1 = new XmlElement();
084: XmlElement xml2 = new XmlElement();
085: xml1.setName("ONE");
086: xml2.setName("ONE");
087: xml1.addElement(new XmlElement("child1"));
088: xml2.addElement(new XmlElement("child1"));
089: assertEquals("The hashcode are not equal", xml2.hashCode(),
090: xml1.hashCode());
091: }
092:
093: /*
094: * Test for clone()
095: */
096: public void testClone() {
097: XmlElement xml1 = new XmlElement("a Name");
098: XmlElement xml2 = (XmlElement) xml1.clone();
099: assertEquals("The parent and the cloned object are not equal",
100: xml1, xml2);
101: assertNotSame("The parent and the cloned object are the same",
102: xml1, xml2);
103: assertNotSame(
104: "The parent and the cloned Attributes objects are the same object.",
105: xml1.getAttributes(), xml2.getAttributes());
106: assertNotSame(
107: "The parent and the cloned Sub Element objects are the same object.",
108: xml1.getElements(), xml2.getElements());
109:
110: xml1 = new XmlElement("a Name", "data");
111: xml2 = (XmlElement) xml1.clone();
112: assertEquals("The parent and the cloned object are not equal",
113: xml1, xml2);
114: assertNotSame("The parent and the cloned object are the same",
115: xml1, xml2);
116: assertNotSame(
117: "The parent and the cloned Attributes objects are the same object.",
118: xml1.getAttributes(), xml2.getAttributes());
119: assertNotSame(
120: "The parent and the cloned Sub Element objects are the same object.",
121: xml1.getElements(), xml2.getElements());
122:
123: xml1 = new XmlElement();
124: xml1.setName("a NAME");
125: xml1.addAttribute("key", "values");
126: xml1.addAttribute("key2", "other values");
127: xml1.addSubElement("child");
128: xml1.addSubElement(new XmlElement("child2"));
129: xml2 = (XmlElement) xml1.clone();
130: assertEquals("The parent and the cloned object are not equal",
131: xml1, xml2);
132: assertNotSame("The parent and the cloned object are the same",
133: xml1, xml2);
134: assertNotSame(
135: "The parent and the cloned Attributes objects are the same object.",
136: xml1.getAttributes(), xml2.getAttributes());
137: assertNotSame(
138: "The parent and the cloned Sub Element objects are the same object.",
139: xml1.getElements(), xml2.getElements());
140: assertEquals("The Name is not the same", "a NAME", xml2
141: .getName());
142: assertEquals(
143: "The value for Attributes key='key' is not the expected",
144: "values", xml2.getAttribute("key"));
145: assertEquals(
146: "The value for Attributes key='key2' is not the expected",
147: "other values", xml2.getAttribute("key2"));
148: assertEquals("The first childs name is not the expected",
149: "child", xml2.getElement(0).getName());
150: assertEquals("The second childs name is not the expected",
151: "child2", xml2.getElement(1).getName());
152: assertEquals(
153: "The parent and cloned object hashCode() methods return different values.",
154: xml1.hashCode(), xml2.hashCode());
155: }
156:
157: /*
158: * Test for XmlElement(String,String)
159: */
160: public void testConstructorStrStr() {
161: XmlElement xml = new XmlElement("a Name", "a Data");
162: xml.addAttribute("key", "values");
163: xml.addAttribute("key2", "other values");
164: xml.addSubElement("child");
165: xml.addSubElement(new XmlElement("child2"));
166:
167: assertEquals("The Name isnt correct", "a Name", xml.getName());
168: assertEquals("The Data isnt correct", "a Data", xml.getData());
169: assertEquals("The attribute 'key' isnt correct", "values", xml
170: .getAttribute("key"));
171: assertEquals("The attribute 'key' isnt correct",
172: "other values", xml.getAttribute("key2"));
173: assertEquals("The child element isnt correct", "child", xml
174: .getElement(0).getName());
175: }
176: }
|