001: /*
002: * Created on 2003-okt-31
003: */
004: package org.columba.core.config;
005:
006: import junit.framework.TestCase;
007:
008: import org.columa.core.config.IDefaultItem;
009: import org.columba.core.xml.XmlElement;
010:
011: /**
012: * Test cases for the <code>DefaultItem</code> class.
013: *
014: * @author redsolo
015: */
016: public class DefaultItemTest extends TestCase {
017: /*
018: * Test for int hashCode().
019: */
020: public void testHashCode() {
021: IDefaultItem item = new DefaultItem(new XmlElement());
022: item.setBoolean("boolean", false);
023: item.setBoolean("badboolean", true);
024: item.setString("key", "value");
025:
026: IDefaultItem item2 = new DefaultItem(new XmlElement());
027: item2.setBoolean("boolean", false);
028: item2.setBoolean("badboolean", true);
029: item2.setString("key", "value");
030: assertTrue("The hashcodes are not the same",
031: item.hashCode() == item2.hashCode());
032: assertTrue("The hashcodes are the same for different items.",
033: item.hashCode() != new DefaultItem(new XmlElement())
034: .hashCode());
035: }
036:
037: /*
038: * Test for boolean equals(Object)
039: */
040: public void testEqualsObject() {
041: IDefaultItem item = new DefaultItem(new XmlElement());
042: item.setBoolean("boolean", false);
043: item.setBoolean("badboolean", true);
044: item.setString("key", "value");
045:
046: IDefaultItem item2 = new DefaultItem(new XmlElement());
047: item2.setBoolean("boolean", false);
048: item2.setBoolean("badboolean", true);
049: item2.setString("key", "value");
050: assertTrue("The items are not equal", item.equals(item2));
051: assertTrue("The items are not equal", item2.equals(item));
052: assertTrue("The items are not equal", item.equals(item));
053: assertTrue("The items are not equal", item2.equals(item2));
054: assertNotSame("The objects are the same", item, item2);
055: assertTrue("The items are equal", !item.equals(new DefaultItem(
056: new XmlElement())));
057:
058: assertFalse("The item is equal to an empty item", item
059: .equals(new DefaultItem(null)));
060: assertFalse("The item is equal to a null object", item
061: .equals(null));
062: assertTrue("The items are not equal", item
063: .equals(new DefaultItem((XmlElement) item.getRoot()
064: .clone())));
065: }
066:
067: /*
068: * Test for clone()
069: */
070: public void testClone() {
071: IDefaultItem item1 = new DefaultItem(new XmlElement("EL"));
072: IDefaultItem item2 = (IDefaultItem) item1.clone();
073: assertEquals("The parent and the cloned object are not equal",
074: item1, item2);
075: assertNotSame("The parent and the cloned object are the same",
076: item1, item2);
077: assertNotSame(
078: "The parent and the cloned Xml Elements objects are the same object.",
079: item1.getRoot(), item2.getRoot());
080: assertEquals(
081: "The parent and the cloned Xml Elements objects are not equal.",
082: item1.getRoot(), item2.getRoot());
083: assertEquals(
084: "The parent and the cloned object did not return the same hashcodes",
085: item1.hashCode(), item2.hashCode());
086:
087: XmlElement xml = new XmlElement();
088: xml.setName("a NAME");
089: xml.addAttribute("key", "values");
090: xml.addAttribute("key2", "other values");
091: xml.addSubElement("child");
092: xml.addSubElement(new XmlElement("child2"));
093:
094: item1 = new DefaultItem(xml);
095: item2 = (IDefaultItem) item1.clone();
096: assertEquals("The parent and the cloned object are not equal",
097: item1, item2);
098: assertNotSame("The parent and the cloned object are the same",
099: item1, item2);
100: assertSame(
101: "The getRoot() method did not return the same object put in",
102: xml, item1.getRoot());
103: assertNotSame(
104: "The parent and the cloned Xml Elements objects are the same object.",
105: item1.getRoot(), item2.getRoot());
106: assertEquals(
107: "The parent and the cloned Xml Elements objects are not equal.",
108: item1.getRoot(), item2.getRoot());
109: assertNotSame(
110: "The parent and the cloned Xml Elements objects are the same object.",
111: xml, item2.getRoot());
112: assertEquals(
113: "The parent and the cloned object did not return the same hashcodes",
114: item1.hashCode(), item2.hashCode());
115: }
116:
117: public void testSet() {
118: XmlElement root = new XmlElement("root");
119: IDefaultItem item = new DefaultItem(root);
120: item.setString("sub/path", "test", "value");
121:
122: assertTrue(root.getElement("sub/path") != null);
123: assertEquals(item.getString("sub/path", "test"), "value");
124: }
125: }
|