1 36 package org.columba.ristretto.message; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.ByteArrayOutputStream ; 40 import java.io.IOException ; 41 import java.io.ObjectInputStream ; 42 import java.io.ObjectOutputStream ; 43 import java.util.HashMap ; 44 import java.util.Map ; 45 46 import junit.framework.TestCase; 47 48 49 54 public class AttributesTest extends TestCase { 55 56 59 public void testPutAndGet() { 60 Attributes attrs = new Attributes(); 61 Object value1 = new Integer (13); 62 attrs.put("key-1", value1); 63 Object 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 75 public void testLoad() throws IOException { 76 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream (); 77 ObjectOutputStream output = new ObjectOutputStream (byteOutputStream); 78 output.writeInt(3); 79 output.writeUTF("float-1"); 80 output.writeObject(new Float (3.5f)); 81 output.writeUTF("int-1"); 82 output.writeObject(new Integer (1)); 83 output.writeUTF("float-2"); 84 output.writeObject(new Float (999.9f)); 85 output.close(); 86 87 ObjectInputStream input = new ObjectInputStream (new ByteArrayInputStream (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 (3.5f), attrs.get("float-1")); 92 assertEquals("Float key 2's value is not correct", new Float (999.9f), attrs.get("float-2")); 93 assertEquals("Integer key 1's value is not correct", new Integer (1), attrs.get("int-1")); 94 } 95 96 101 public void testSave() throws IOException , ClassNotFoundException { 102 Map expected = new HashMap (); 103 expected.put("put-str-1", "hejhopp"); 104 expected.put("put-int-1", new Integer (-1)); 105 expected.put("put-str-2", "ristrettos"); 106 expected.put("put-float-1", new Float (3.5f)); 107 108 Attributes attr = new Attributes(); 109 attr.put("put-str-1", "hejhopp"); 110 attr.put("put-int-1", new Integer (-1)); 111 attr.put("put-str-2", "ristrettos"); 112 attr.put("put-float-1", new Float (3.5f)); 113 114 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream (); 115 attr.save(new ObjectOutputStream (byteOutputStream)); 116 117 ObjectInputStream input = new ObjectInputStream (new ByteArrayInputStream (byteOutputStream.toByteArray())); 118 assertEquals("Number of written objects wasnt correct", 4, input.readInt()); 119 String 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 133 public void testCount() { 134 Attributes attr = new Attributes(); 135 attr.put("put-str-1", "hejhopp"); 136 attr.put("put-int-1", new Integer (-1)); 137 attr.put("put-str-2", "ristrettos"); 138 attr.put("put-float-1", new Float (3.5f)); 139 assertEquals("The count() method didnt return the correct size", 4, attr.count()); 140 } 141 142 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 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 (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 (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 (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 (4))); 180 } 181 182 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 (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 (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 (3)); 200 assertTrue("The hashcode() returned same values for non equal objects", actual.hashCode() == expected.hashCode()); 201 } 202 } 203 | Popular Tags |