1 42 43 package org.jfree.data.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.data.KeyedObject; 57 58 61 public class KeyedObjectTests extends TestCase { 62 63 68 public static Test suite() { 69 return new TestSuite(KeyedObjectTests.class); 70 } 71 72 77 public KeyedObjectTests(String name) { 78 super(name); 79 } 80 81 84 public void testEquals() { 85 86 KeyedObject ko1 = new KeyedObject("Test", "Object"); 87 KeyedObject ko2 = new KeyedObject("Test", "Object"); 88 assertTrue(ko1.equals(ko2)); 89 assertTrue(ko2.equals(ko1)); 90 91 ko1 = new KeyedObject("Test 1", "Object"); 92 ko2 = new KeyedObject("Test 2", "Object"); 93 assertFalse(ko1.equals(ko2)); 94 95 ko1 = new KeyedObject("Test", "Object 1"); 96 ko2 = new KeyedObject("Test", "Object 2"); 97 assertFalse(ko1.equals(ko2)); 98 99 } 100 101 104 public void testCloning() { 105 KeyedObject ko1 = new KeyedObject("Test", "Object"); 106 KeyedObject ko2 = null; 107 try { 108 ko2 = (KeyedObject) ko1.clone(); 109 } 110 catch (CloneNotSupportedException e) { 111 System.err.println("Failed to clone."); 112 } 113 assertTrue(ko1 != ko2); 114 assertTrue(ko1.getClass() == ko2.getClass()); 115 assertTrue(ko1.equals(ko2)); 116 } 117 118 121 public void testSerialization() { 122 123 KeyedObject ko1 = new KeyedObject("Test", "Object"); 124 KeyedObject ko2 = null; 125 126 try { 127 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 128 ObjectOutput out = new ObjectOutputStream (buffer); 129 out.writeObject(ko1); 130 out.close(); 131 132 ObjectInput in = new ObjectInputStream ( 133 new ByteArrayInputStream (buffer.toByteArray()) 134 ); 135 ko2 = (KeyedObject) in.readObject(); 136 in.close(); 137 } 138 catch (Exception e) { 139 System.out.println(e.toString()); 140 } 141 assertEquals(ko1, ko2); 142 143 } 144 145 } 146 | Popular Tags |