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.KeyedObjects; 57 58 61 public class KeyedObjectsTests extends TestCase { 62 63 68 public static Test suite() { 69 return new TestSuite(KeyedObjectsTests.class); 70 } 71 72 77 public KeyedObjectsTests(String name) { 78 super(name); 79 } 80 81 84 protected void setUp() { 85 } 87 88 91 public void testCloning() { 92 KeyedObjects ko1 = new KeyedObjects(); 93 ko1.addObject("V1", new Integer (1)); 94 ko1.addObject("V2", null); 95 ko1.addObject("V3", new Integer (3)); 96 KeyedObjects ko2 = null; 97 try { 98 ko2 = (KeyedObjects) ko1.clone(); 99 } 100 catch (CloneNotSupportedException e) { 101 System.err.println("Failed to clone."); 102 } 103 assertTrue(ko1 != ko2); 104 assertTrue(ko1.getClass() == ko2.getClass()); 105 assertTrue(ko1.equals(ko2)); 106 } 107 108 111 public void testInsertAndRetrieve() { 112 113 KeyedObjects data = new KeyedObjects(); 114 data.addObject("A", new Double (1.0)); 115 data.addObject("B", new Double (2.0)); 116 data.addObject("C", new Double (3.0)); 117 data.addObject("D", null); 118 119 assertEquals(data.getKey(0), "A"); 121 assertEquals(data.getKey(1), "B"); 122 assertEquals(data.getKey(2), "C"); 123 assertEquals(data.getKey(3), "D"); 124 125 assertEquals(data.getObject("A"), new Double (1.0)); 127 assertEquals(data.getObject("B"), new Double (2.0)); 128 assertEquals(data.getObject("C"), new Double (3.0)); 129 assertEquals(data.getObject("D"), null); 130 assertEquals(data.getObject("Not a key"), null); 131 132 assertEquals(data.getObject(0), new Double (1.0)); 134 assertEquals(data.getObject(1), new Double (2.0)); 135 assertEquals(data.getObject(2), new Double (3.0)); 136 assertEquals(data.getObject(3), null); 137 138 } 139 140 143 public void testSerialization() { 144 145 KeyedObjects ko1 = new KeyedObjects(); 146 ko1.addObject("Key 1", "Object 1"); 147 ko1.addObject("Key 2", null); 148 ko1.addObject("Key 3", "Object 2"); 149 150 KeyedObjects ko2 = null; 151 152 try { 153 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 154 ObjectOutput out = new ObjectOutputStream (buffer); 155 out.writeObject(ko1); 156 out.close(); 157 158 ObjectInput in = new ObjectInputStream ( 159 new ByteArrayInputStream (buffer.toByteArray()) 160 ); 161 ko2 = (KeyedObjects) in.readObject(); 162 in.close(); 163 } 164 catch (Exception e) { 165 System.out.println(e.toString()); 166 } 167 assertEquals(ko1, ko2); 168 169 } 170 171 } 172 | Popular Tags |