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