KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > message > AttributesTest


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.message;
37
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.ByteArrayOutputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.ObjectInputStream JavaDoc;
42 import java.io.ObjectOutputStream JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import junit.framework.TestCase;
47
48
49 /**
50  * Test cases for the Attributes class.
51  *
52  * @author redsolo
53  */

54 public class AttributesTest extends TestCase {
55
56     /**
57      * Tests the get() and put methods.
58      */

59     public void testPutAndGet() {
60         Attributes attrs = new Attributes();
61         Object JavaDoc value1 = new Integer JavaDoc(13);
62         attrs.put("key-1", value1);
63         Object JavaDoc value2 = Boolean.FALSE;
64         attrs.put("key-2", value2);
65         attrs.put("key-4", value1);
66         assertSame("Value for the first key was not correct", value1, attrs.get("key-1"));
67         assertSame("Value for the second key was not correct", value2, attrs.get("key-2"));
68         assertSame("Value for the third key was not correct", value1, attrs.get("key-4"));
69     }
70
71     /**
72      * Tests the load() method, ie. actually tests the constructor.
73      * @throws IOException thrown by the write() method in the object output stream.
74      */

75     public void testLoad() throws IOException JavaDoc {
76         ByteArrayOutputStream JavaDoc byteOutputStream = new ByteArrayOutputStream JavaDoc();
77         ObjectOutputStream JavaDoc output = new ObjectOutputStream JavaDoc(byteOutputStream);
78         output.writeInt(3);
79         output.writeUTF("float-1");
80         output.writeObject(new Float JavaDoc(3.5f));
81         output.writeUTF("int-1");
82         output.writeObject(new Integer JavaDoc(1));
83         output.writeUTF("float-2");
84         output.writeObject(new Float JavaDoc(999.9f));
85         output.close();
86
87         ObjectInputStream JavaDoc input = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(byteOutputStream.toByteArray()));
88         Attributes attrs = new Attributes(input);
89
90         assertEquals("The size was not correct after loading", 3, attrs.count());
91         assertEquals("Float key 1's value is not correct", new Float JavaDoc(3.5f), attrs.get("float-1"));
92         assertEquals("Float key 2's value is not correct", new Float JavaDoc(999.9f), attrs.get("float-2"));
93         assertEquals("Integer key 1's value is not correct", new Integer JavaDoc(1), attrs.get("int-1"));
94     }
95
96     /**
97      * Tests the save() method.
98      * @throws IOException thrown by the object input stream.
99      * @throws ClassNotFoundException thrown by the object input stream.
100      */

101     public void testSave() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
102         Map JavaDoc expected = new HashMap JavaDoc();
103         expected.put("put-str-1", "hejhopp");
104         expected.put("put-int-1", new Integer JavaDoc(-1));
105         expected.put("put-str-2", "ristrettos");
106         expected.put("put-float-1", new Float JavaDoc(3.5f));
107
108         Attributes attr = new Attributes();
109         attr.put("put-str-1", "hejhopp");
110         attr.put("put-int-1", new Integer JavaDoc(-1));
111         attr.put("put-str-2", "ristrettos");
112         attr.put("put-float-1", new Float JavaDoc(3.5f));
113
114         ByteArrayOutputStream JavaDoc byteOutputStream = new ByteArrayOutputStream JavaDoc();
115         attr.save(new ObjectOutputStream JavaDoc(byteOutputStream));
116
117         ObjectInputStream JavaDoc input = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(byteOutputStream.toByteArray()));
118         assertEquals("Number of written objects wasnt correct", 4, input.readInt());
119         String JavaDoc key = input.readUTF();
120         assertEquals("Value for key '" + key + "' wasnt correct", expected.get(key), input.readObject());
121         key = input.readUTF();
122         assertEquals("Value for key '" + key + "' wasnt correct", expected.get(key), input.readObject());
123         key = input.readUTF();
124         assertEquals("Value for key '" + key + "' wasnt correct", expected.get(key), input.readObject());
125         key = input.readUTF();
126         assertEquals("Value for key '" + key + "' wasnt correct", expected.get(key), input.readObject());
127         assertEquals("The stream was not empty after reading all four objects", 0, input.available());
128     }
129
130     /**
131      * Tests the count() method.
132      */

133     public void testCount() {
134         Attributes attr = new Attributes();
135         attr.put("put-str-1", "hejhopp");
136         attr.put("put-int-1", new Integer JavaDoc(-1));
137         attr.put("put-str-2", "ristrettos");
138         attr.put("put-float-1", new Float JavaDoc(3.5f));
139         assertEquals("The count() method didnt return the correct size", 4, attr.count());
140     }
141
142     /**
143      * Tests the clone() method.
144      */

145     public void testClone() {
146         Attributes attr = new Attributes();
147         attr.put("key", "value");
148         attr.put("key2", "value2");
149         attr.put("key3", "value5");
150         Attributes clone = (Attributes) attr.clone();
151         assertNotSame("The object is the same object after a clone", attr, clone);
152         assertEquals("Attributes sizes are not the same", attr.count(), clone.count());
153         assertSame("The key pairs are not the same", attr.get("key"), clone.get("key"));
154         assertSame("The key pairs are not the same", attr.get("key2"), clone.get("key2"));
155         assertSame("The key pairs are not the same", attr.get("key3"), clone.get("key3"));
156     }
157
158     /**
159      * Test the equals() method.
160      */

161     public void testEquals() {
162         Attributes expected = new Attributes();
163         expected.put("key-e-1", "value-e-1");
164         expected.put("key-e-4", "value-e-4");
165         expected.put("key-e-int", new Integer JavaDoc(3));
166
167         Attributes actual = new Attributes();
168         actual.put("key-e-1", "value-e-1");
169         actual.put("key-e-4", "value-e-4");
170         actual.put("key-e-int", new Integer JavaDoc(3));
171
172         assertTrue("The equals() method returned false for equal objects", actual.equals(expected));
173         assertTrue("The equals() method returned false for equal objects", expected.equals(actual));
174         assertFalse("The objects are equal though one is null", expected.equals(null));
175
176         actual.put("key-e-int", new Integer JavaDoc(4));
177         assertFalse("The equals() method returned true for non equal objects", actual.equals(expected));
178         assertFalse("The equals() method returned true for non equal objects", expected.equals(actual));
179         assertFalse("The equals() method returned true, when the objects were of different types", expected.equals(new Integer JavaDoc(4)));
180     }
181
182     /**
183      * Test the hashcode() method.
184      */

185     public void testHashCode() {
186         Attributes expected = new Attributes();
187         expected.put("key-e-1", "value-e-1");
188         expected.put("key-e-4", "value-e-4");
189         expected.put("key-e-int", new Integer JavaDoc(3));
190
191         Attributes actual = new Attributes();
192         actual.put("key-e-1", "value-e-1");
193         actual.put("key-e-4", "value-e-4");
194         actual.put("key-e-int", new Integer JavaDoc(3));
195
196         assertEquals("The hashcode() returned different values for equal objects", actual.hashCode(), expected.hashCode());
197         assertEquals("The hashcode() returned different values for equal objects", expected.hashCode(), actual.hashCode());
198
199         actual.put("key-e-int", new Integer JavaDoc(3));
200         assertTrue("The hashcode() returned same values for non equal objects", actual.hashCode() == expected.hashCode());
201     }
202 }
203
Popular Tags