1 44 45 package org.jfree.data.general.junit; 46 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectOutputStream ; 53 54 import junit.framework.Test; 55 import junit.framework.TestCase; 56 import junit.framework.TestSuite; 57 58 import org.jfree.data.general.DatasetChangeEvent; 59 import org.jfree.data.general.DatasetChangeListener; 60 import org.jfree.data.general.DefaultPieDataset; 61 62 65 public class DefaultPieDatasetTests extends TestCase 66 implements DatasetChangeListener { 67 68 private DatasetChangeEvent lastEvent; 69 70 public void datasetChanged(DatasetChangeEvent event) { 71 this.lastEvent = event; 72 } 73 74 79 public static Test suite() { 80 return new TestSuite(DefaultPieDatasetTests.class); 81 } 82 83 88 public DefaultPieDatasetTests(String name) { 89 super(name); 90 } 91 92 95 public void testClear() { 96 DefaultPieDataset d = new DefaultPieDataset(); 97 d.addChangeListener(this); 98 d.clear(); 100 assertNull(this.lastEvent); 101 d.setValue("A", 1.0); 102 assertEquals(1, d.getItemCount()); 103 this.lastEvent = null; 104 d.clear(); 105 assertNotNull(this.lastEvent); 106 assertEquals(0, d.getItemCount()); 107 } 108 109 112 public void testGetKey() { 113 DefaultPieDataset d = new DefaultPieDataset(); 114 d.setValue("A", 1.0); 115 d.setValue("B", 2.0); 116 assertEquals("A", d.getKey(0)); 117 assertEquals("B", d.getKey(1)); 118 119 boolean pass = false; 120 try { 121 d.getKey(-1); 122 } 123 catch (IndexOutOfBoundsException e) { 124 pass = true; 125 } 126 assertTrue(pass); 127 128 pass = false; 129 try { 130 d.getKey(2); 131 } 132 catch (IndexOutOfBoundsException e) { 133 pass = true; 134 } 135 assertTrue(pass); 136 } 137 138 141 public void testGetIndex() { 142 DefaultPieDataset d = new DefaultPieDataset(); 143 d.setValue("A", 1.0); 144 d.setValue("B", 2.0); 145 assertEquals(0, d.getIndex("A")); 146 assertEquals(1, d.getIndex("B")); 147 assertEquals(-1, d.getIndex("XX")); 148 149 boolean pass = false; 150 try { 151 d.getIndex(null); 152 } 153 catch (IllegalArgumentException e) { 154 pass = true; 155 } 156 assertTrue(pass); 157 } 158 159 162 public void testCloning() { 163 DefaultPieDataset d1 = new DefaultPieDataset(); 164 d1.setValue("V1", new Integer (1)); 165 d1.setValue("V2", null); 166 d1.setValue("V3", new Integer (3)); 167 DefaultPieDataset d2 = null; 168 try { 169 d2 = (DefaultPieDataset) d1.clone(); 170 } 171 catch (CloneNotSupportedException e) { 172 System.err.println("Failed to clone."); 173 } 174 assertTrue(d1 != d2); 175 assertTrue(d1.getClass() == d2.getClass()); 176 assertTrue(d1.equals(d2)); 177 } 178 179 182 public void testSerialization() { 183 184 DefaultPieDataset d1 = new DefaultPieDataset(); 185 d1.setValue("C1", new Double (234.2)); 186 d1.setValue("C2", null); 187 d1.setValue("C3", new Double (345.9)); 188 d1.setValue("C4", new Double (452.7)); 189 190 DefaultPieDataset d2 = null; 191 192 try { 193 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 194 ObjectOutput out = new ObjectOutputStream (buffer); 195 out.writeObject(d1); 196 out.close(); 197 198 ObjectInput in = new ObjectInputStream ( 199 new ByteArrayInputStream (buffer.toByteArray()) 200 ); 201 d2 = (DefaultPieDataset) in.readObject(); 202 in.close(); 203 } 204 catch (Exception e) { 205 System.out.println(e.toString()); 206 } 207 assertEquals(d1, d2); 208 209 } 210 211 } 212 | Popular Tags |