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 44 import junit.framework.TestCase; 45 46 47 50 public class AddressTest extends TestCase { 51 52 55 public void testGet() { 56 Address addr = new Address("Emil", "email@lonneberga.se"); 57 assertEquals("The display name isnt correct", "Emil", addr.getDisplayName()); 58 assertEquals("The address isnt correct", "email@lonneberga.se", addr.getMailAddress()); 59 assertEquals("The short name isnt correct", "Emil", addr.getShortAddress()); 60 assertEquals("The cannonical mail isnt correct", "<email@lonneberga.se>", addr.getCanonicalMailAddress()); 61 } 62 63 66 public void testSet() { 67 Address addr = new Address("not@this.on"); 68 assertNotNull("The display wasnt null", addr.getDisplayName()); 69 addr.setDisplayName("a NAME"); 70 assertEquals("The set display name failed", "a NAME", addr.getDisplayName()); 71 } 72 73 78 public void testSerializing() throws IOException , ClassNotFoundException { 79 Address expected = new Address("Emil", "email@lonneberga.se"); 80 81 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream (); 82 ObjectOutputStream output = new ObjectOutputStream (byteOutput); 83 84 output.writeObject(expected); 85 assertTrue("The stream is empty after writing", byteOutput.size() > 0); 86 87 ObjectInputStream input = new ObjectInputStream (new ByteArrayInputStream (byteOutput.toByteArray())); 88 Address actual = (Address) input.readObject(); 89 assertEquals("The serializing didnt work", expected, actual); 90 } 91 92 97 public void testSerializing2() throws IOException , ClassNotFoundException { 98 Address expected = new Address("email@lonneberga.se"); 99 expected.setDisplayName(null); 100 101 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream (); 102 ObjectOutputStream output = new ObjectOutputStream (byteOutput); 103 104 output.writeObject(expected); 105 assertTrue("The stream is empty after writing", byteOutput.size() > 0); 106 107 ObjectInputStream input = new ObjectInputStream (new ByteArrayInputStream (byteOutput.toByteArray())); 108 Address actual = (Address) input.readObject(); 109 assertEquals("The serializing didnt work", expected, actual); 110 } 111 112 115 public void testEquals() { 116 Address addr1 = new Address("Emil", "email@lonneberga.se"); 117 Address addr2 = new Address("Emil", "email@lonneberga.se"); 118 Address addr3 = new Address("Emil", "email@lonneberga.de"); 119 assertTrue("The objects arent equal", addr1.equals(addr2)); 120 assertTrue("The objects arent equal", addr2.equals(addr1)); 121 assertFalse("The objects are equal though they are different types", addr2.equals(new Integer (3))); 122 assertFalse("The objects are equal though one is null", addr2.equals(null)); 123 assertFalse("The objects are equal", addr1.equals(addr3)); 124 assertFalse("The objects are equal", addr3.equals(addr1)); 125 assertEquals("Equal objects has different hashcodes", addr1.hashCode(), addr2.hashCode()); 126 assertFalse("Unequal objects has same hashcodes", addr1.hashCode() == addr3.hashCode()); 127 } 128 129 132 public void testCompareTo() { 133 Address addr1 = new Address("Emil", "email@lonneberga.se"); 134 Address addr2 = new Address("Emil", "email@lonneberga.se"); 135 assertTrue("The compareTo method failed", addr1.compareTo(addr2) == 0); 136 assertTrue("The compareTo method failed", addr1.compareTo(new Address("bah@bahcity.net")) > 0); 137 } 138 } 139 | Popular Tags |