1 49 50 package org.jfree.data.xy.junit; 51 52 import java.io.ByteArrayInputStream ; 53 import java.io.ByteArrayOutputStream ; 54 import java.io.ObjectInput ; 55 import java.io.ObjectInputStream ; 56 import java.io.ObjectOutput ; 57 import java.io.ObjectOutputStream ; 58 59 import junit.framework.Test; 60 import junit.framework.TestCase; 61 import junit.framework.TestSuite; 62 63 import org.jfree.data.xy.DefaultTableXYDataset; 64 import org.jfree.data.xy.XYSeries; 65 66 71 public class TableXYDatasetTests extends TestCase { 72 73 78 public static Test suite() { 79 return new TestSuite(TableXYDatasetTests.class); 80 } 81 82 87 public TableXYDatasetTests(String name) { 88 super(name); 89 } 90 91 94 public void testEquals() { 95 96 DefaultTableXYDataset d1 = new DefaultTableXYDataset(); 97 DefaultTableXYDataset d2 = new DefaultTableXYDataset(); 98 assertTrue(d1.equals(d2)); 99 assertTrue(d2.equals(d1)); 100 101 d1.addSeries(createSeries1()); 102 assertFalse(d1.equals(d2)); 103 104 d2.addSeries(createSeries1()); 105 assertTrue(d1.equals(d2)); 106 107 } 108 109 112 public void testCloning() { 113 DefaultTableXYDataset d1 = new DefaultTableXYDataset(); 114 d1.addSeries(createSeries1()); 115 DefaultTableXYDataset d2 = null; 116 try { 117 d2 = (DefaultTableXYDataset) d1.clone(); 118 } 119 catch (CloneNotSupportedException e) { 120 System.err.println("Failed to clone."); 121 } 122 assertTrue(d1 != d2); 123 assertTrue(d1.getClass() == d2.getClass()); 124 assertTrue(d1.equals(d2)); 125 } 126 127 130 public void testSerialization() { 131 132 DefaultTableXYDataset d1 = new DefaultTableXYDataset(); 133 d1.addSeries(createSeries2()); 134 DefaultTableXYDataset d2 = null; 135 136 try { 137 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 138 ObjectOutput out = new ObjectOutputStream (buffer); 139 out.writeObject(d1); 140 out.close(); 141 142 ObjectInput in = new ObjectInputStream ( 143 new ByteArrayInputStream (buffer.toByteArray()) 144 ); 145 d2 = (DefaultTableXYDataset) in.readObject(); 146 in.close(); 147 } 148 catch (Exception e) { 149 System.out.println(e.toString()); 150 } 151 assertEquals(d1, d2); 152 153 } 154 155 158 public void testTableXYDataset() { 159 160 XYSeries series1 = createSeries1(); 161 XYSeries series2 = createSeries2(); 162 163 DefaultTableXYDataset dataset = new DefaultTableXYDataset(); 164 dataset.addSeries(series1); 165 dataset.addSeries(series2); 166 167 assertEquals(6, dataset.getItemCount()); 169 assertEquals(6, dataset.getX(0, 5).intValue()); 170 assertEquals(null, dataset.getY(0, 5)); 171 assertEquals(6, dataset.getX(1, 5).intValue()); 172 assertEquals(2, dataset.getY(1, 5).intValue()); 173 174 series2.add(7, 2); 177 assertEquals(7, dataset.getItemCount()); 178 assertEquals(null, dataset.getY(0, 6)); 179 assertEquals(2, dataset.getY(1, 6).intValue()); 180 181 dataset.removeSeries(series1); 183 assertEquals(7, dataset.getItemCount()); 185 186 dataset.removeSeries(series2); 188 series1 = createSeries1(); 189 dataset.addSeries(series1); 190 191 assertEquals(4, dataset.getItemCount()); 193 194 } 195 196 199 public void test788597() { 200 DefaultTableXYDataset dataset = new DefaultTableXYDataset(); 201 dataset.addSeries(createSeries1()); 202 assertEquals(4, dataset.getItemCount()); 203 dataset.removeAllSeries(); 204 assertEquals(0, dataset.getItemCount()); 205 } 206 207 210 public void testRemoveAllValuesForX() { 211 DefaultTableXYDataset dataset = new DefaultTableXYDataset(); 212 dataset.addSeries(createSeries1()); 213 dataset.addSeries(createSeries2()); 214 dataset.removeAllValuesForX(new Double (2.0)); 215 assertEquals(5, dataset.getItemCount()); 216 assertEquals(new Double (1.0), dataset.getX(0, 0)); 217 assertEquals(new Double (3.0), dataset.getX(0, 1)); 218 assertEquals(new Double (4.0), dataset.getX(0, 2)); 219 assertEquals(new Double (5.0), dataset.getX(0, 3)); 220 assertEquals(new Double (6.0), dataset.getX(0, 4)); 221 } 222 223 226 public void testPrune() { 227 DefaultTableXYDataset dataset = new DefaultTableXYDataset(); 228 dataset.addSeries(createSeries1()); 229 dataset.addSeries(createSeries2()); 230 dataset.removeSeries(1); 231 dataset.prune(); 232 assertEquals(4, dataset.getItemCount()); 233 } 234 235 238 public void testAutoPrune() { 239 240 DefaultTableXYDataset dataset = new DefaultTableXYDataset(true); 242 dataset.addSeries(createSeriesA()); 243 assertEquals(2, dataset.getItemCount()); dataset.addSeries(createSeriesB()); 245 assertEquals(2, dataset.getItemCount()); dataset.removeSeries(1); 247 assertEquals(1, dataset.getItemCount()); 249 DefaultTableXYDataset dataset2 = new DefaultTableXYDataset(true); 251 dataset2.addSeries(createSeriesA()); 252 assertEquals(2, dataset2.getItemCount()); dataset2.addSeries(createSeriesB()); 254 assertEquals(2, dataset2.getItemCount()); dataset2.removeSeries(1); 256 assertEquals(1, dataset2.getItemCount()); 258 } 259 260 265 private XYSeries createSeriesA() { 266 XYSeries s = new XYSeries("A", true, false); 267 s.add(1.0, 1.1); 268 s.add(2.0, null); 269 return s; 270 } 271 272 277 private XYSeries createSeriesB() { 278 XYSeries s = new XYSeries("B", true, false); 279 s.add(1.0, null); 280 s.add(2.0, 2.2); 281 return s; 282 } 283 284 289 private XYSeries createSeries1() { 290 XYSeries series1 = new XYSeries("Series 1", true, false); 291 series1.add(1.0, 1.0); 292 series1.add(2.0, 1.0); 293 series1.add(4.0, 1.0); 294 series1.add(5.0, 1.0); 295 return series1; 296 } 297 298 303 private XYSeries createSeries2() { 304 XYSeries series2 = new XYSeries("Series 2", true, false); 305 series2.add(2.0, 2.0); 306 series2.add(3.0, 2.0); 307 series2.add(4.0, 2.0); 308 series2.add(5.0, 2.0); 309 series2.add(6.0, 2.0); 310 return series2; 311 } 312 313 } 314 | Popular Tags |