KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > config > AccountItemTest


1 /*
2  * Created on 2003-11-02
3  */

4 package org.columba.mail.config;
5
6 import junit.framework.TestCase;
7
8 import org.columba.core.xml.XmlElement;
9
10
11 /**
12  * Test cases for the <code>AccountItem</code> class.
13  *
14  * @author karlpeder
15  */

16 public class AccountItemTest extends TestCase {
17     /*
18  * Test for int hashCode().
19  */

20     public void testHashCode() {
21         // first account item
22
XmlElement xml = new XmlElement("account");
23         xml.addAttribute("name", "my account");
24         xml.addAttribute("uid", "0");
25
26         XmlElement child = xml.addSubElement("identity");
27         child.addAttribute("name", "John Doe");
28         child.addAttribute("attach_signature", "false");
29         child = xml.addSubElement("popserver");
30         child.addAttribute("port", "25");
31         child.addAttribute("login_method", "USER");
32         child = xml.addSubElement("specialfolders");
33         child.addAttribute("inbox", "101");
34         child.addAttribute("sent", "104");
35
36         AccountItem item = new AccountItem(xml);
37
38         // second account item
39
XmlElement xml2 = new XmlElement("account");
40         xml2.addAttribute("uid", "0");
41         xml2.addAttribute("name", "my account");
42
43         XmlElement child2 = xml2.addSubElement("identity");
44         child2.addAttribute("attach_signature", "false");
45         child2.addAttribute("name", "John Doe");
46         child2 = xml2.addSubElement("popserver");
47         child2.addAttribute("login_method", "USER");
48         child2.addAttribute("port", "25");
49         child2 = xml2.addSubElement("specialfolders");
50         child2.addAttribute("sent", "104");
51         child2.addAttribute("inbox", "101");
52
53         AccountItem item2 = new AccountItem(xml2);
54
55         // third item, a bit different from the first
56
XmlElement xml3 = new XmlElement("account");
57         xml3.addAttribute("name", "my account");
58         xml3.addAttribute("uid", "0");
59
60         XmlElement child3 = xml3.addSubElement("identity");
61         child3.addAttribute("name", "Kalle Kamel");
62         child3.addAttribute("attach_signature", "false");
63         child3 = xml3.addSubElement("popserver");
64         child3.addAttribute("port", "25");
65         child3.addAttribute("login_method", "USER");
66         child3 = xml3.addSubElement("specialfolders");
67         child3.addAttribute("inbox", "101");
68         child3.addAttribute("sent", "104");
69
70         AccountItem item3 = new AccountItem(xml3);
71
72         // should have the same hashcodes...
73
assertTrue("The hashcodes of item and item2 are not the same",
74             item.hashCode() == item2.hashCode());
75
76         // expect a different hashcode from a newly created item...
77
assertFalse("The hashcodes of item and a new object are the same",
78             item.hashCode() == (new AccountItem(new XmlElement())).hashCode());
79
80         // expect a different hashcode for item and item3
81
assertFalse("The hashcodes of item and item3 are the same",
82             item.hashCode() == item3.hashCode());
83     }
84
85     /*
86  * Test for boolean equals(Object)
87  */

88     public void testEqualsObject() {
89         // first account item
90
XmlElement xml = new XmlElement("account");
91         xml.addAttribute("name", "my account");
92         xml.addAttribute("uid", "0");
93
94         XmlElement child = xml.addSubElement("identity");
95         child.addAttribute("name", "John Doe");
96         child.addAttribute("attach_signature", "false");
97         child = xml.addSubElement("popserver");
98         child.addAttribute("port", "25");
99         child.addAttribute("login_method", "USER");
100         child = xml.addSubElement("specialfolders");
101         child.addAttribute("inbox", "101");
102         child.addAttribute("sent", "104");
103
104         AccountItem item = new AccountItem(xml);
105
106         // second account item
107
XmlElement xml2 = new XmlElement("account");
108         xml2.addAttribute("uid", "0");
109         xml2.addAttribute("name", "my account");
110
111         XmlElement child2 = xml2.addSubElement("identity");
112         child2.addAttribute("attach_signature", "false");
113         child2.addAttribute("name", "John Doe");
114         child2 = xml2.addSubElement("popserver");
115         child2.addAttribute("login_method", "USER");
116         child2.addAttribute("port", "25");
117         child2 = xml2.addSubElement("specialfolders");
118         child2.addAttribute("sent", "104");
119         child2.addAttribute("inbox", "101");
120
121         AccountItem item2 = new AccountItem(xml2);
122
123         // third item, a bit different from the first
124
XmlElement xml3 = new XmlElement("account");
125         xml3.addAttribute("name", "my account");
126         xml3.addAttribute("uid", "0");
127
128         XmlElement child3 = xml3.addSubElement("identity");
129         child3.addAttribute("name", "Kalle Kamel");
130         child3.addAttribute("attach_signature", "false");
131         child3 = xml3.addSubElement("popserver");
132         child3.addAttribute("port", "25");
133         child3.addAttribute("login_method", "USER");
134         child3 = xml3.addSubElement("specialfolders");
135         child3.addAttribute("inbox", "101");
136         child3.addAttribute("sent", "104");
137
138         AccountItem item3 = new AccountItem(xml3);
139
140         // test self equality...
141
assertTrue("Self equality failed for item", item.equals(item));
142         assertTrue("Self equality failed for item2", item2.equals(item2));
143
144         // item and item2 should be equal...
145
assertTrue("item and item2 are not equal", item.equals(item2));
146         assertTrue("item2 and item are not equal", item2.equals(item));
147
148         // item and item2 should be two different objects
149
assertNotSame("item and item2 refers to the same object", item, item2);
150
151         // item should not be equal to a newly created item or null
152
assertFalse("item is equal to a newly created AccountItem",
153             item.equals(new AccountItem(new XmlElement())));
154         assertFalse("item is equal to null", item.equals(null));
155
156         // item and item3 should not be equal
157
assertFalse("item and item3 are equal", item.equals(item3));
158     }
159 }
160
Popular Tags