1 42 43 package org.jfree.data.general.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.general.DefaultKeyedValueDataset; 57 58 61 public class DefaultKeyedValueDatasetTests extends TestCase { 62 63 68 public static Test suite() { 69 return new TestSuite(DefaultKeyedValueDatasetTests.class); 70 } 71 72 77 public DefaultKeyedValueDatasetTests(String name) { 78 super(name); 79 } 80 81 84 public void testEquals() { 85 86 DefaultKeyedValueDataset d1 87 = new DefaultKeyedValueDataset("Test", new Double (45.5)); 88 DefaultKeyedValueDataset d2 89 = new DefaultKeyedValueDataset("Test", new Double (45.5)); 90 assertTrue(d1.equals(d2)); 91 assertTrue(d2.equals(d1)); 92 93 d1 = new DefaultKeyedValueDataset("Test 1", new Double (45.5)); 94 d2 = new DefaultKeyedValueDataset("Test 2", new Double (45.5)); 95 assertFalse(d1.equals(d2)); 96 97 d1 = new DefaultKeyedValueDataset("Test", new Double (45.5)); 98 d2 = new DefaultKeyedValueDataset("Test", new Double (45.6)); 99 assertFalse(d1.equals(d2)); 100 101 } 102 103 106 public void testCloning() { 107 DefaultKeyedValueDataset d1 108 = new DefaultKeyedValueDataset("Test", new Double (45.5)); 109 DefaultKeyedValueDataset d2 = null; 110 try { 111 d2 = (DefaultKeyedValueDataset) d1.clone(); 112 } 113 catch (CloneNotSupportedException e) { 114 System.err.println("Failed to clone."); 115 } 116 assertTrue(d1 != d2); 117 assertTrue(d1.getClass() == d2.getClass()); 118 assertTrue(d1.equals(d2)); 119 } 120 121 124 public void testCloneIndependence() { 125 DefaultKeyedValueDataset d1 126 = new DefaultKeyedValueDataset("Key", new Double (10.0)); 127 DefaultKeyedValueDataset d2 = null; 128 try { 129 d2 = (DefaultKeyedValueDataset) d1.clone(); 130 } 131 catch (CloneNotSupportedException e) { 132 System.err.println("Failed to clone."); 133 } 134 assertTrue(d1.equals(d2)); 135 d2.updateValue(new Double (99.9)); 136 assertFalse(d1.equals(d2)); 137 d2.updateValue(new Double (10.0)); 138 assertTrue(d1.equals(d2)); 139 } 140 141 144 public void testSerialization() { 145 146 DefaultKeyedValueDataset d1 147 = new DefaultKeyedValueDataset("Test", new Double (25.3)); 148 DefaultKeyedValueDataset d2 = null; 149 150 try { 151 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 152 ObjectOutput out = new ObjectOutputStream (buffer); 153 out.writeObject(d1); 154 out.close(); 155 156 ObjectInput in = new ObjectInputStream ( 157 new ByteArrayInputStream (buffer.toByteArray()) 158 ); 159 d2 = (DefaultKeyedValueDataset) in.readObject(); 160 in.close(); 161 } 162 catch (Exception e) { 163 System.out.println(e.toString()); 164 } 165 assertEquals(d1, d2); 166 } 167 168 } 169 | Popular Tags |