1 43 44 package org.jfree.data.xy.junit; 45 46 import java.io.ByteArrayInputStream ; 47 import java.io.ByteArrayOutputStream ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectInputStream ; 50 import java.io.ObjectOutput ; 51 import java.io.ObjectOutputStream ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.data.xy.DefaultIntervalXYDataset; 58 59 62 public class DefaultIntervalXYDatasetTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(DefaultIntervalXYDatasetTests.class); 71 } 72 73 78 public DefaultIntervalXYDatasetTests(String name) { 79 super(name); 80 } 81 82 85 public void testGetSeriesCount() { 86 DefaultIntervalXYDataset d = new DefaultIntervalXYDataset(); 87 assertEquals(0, d.getSeriesCount()); 88 d = createSampleDataset1(); 89 assertEquals(2, d.getSeriesCount()); 90 } 91 92 95 public void testGetSeriesKey() { 96 DefaultIntervalXYDataset d = createSampleDataset1(); 97 assertEquals("S1", d.getSeriesKey(0)); 98 assertEquals("S2", d.getSeriesKey(1)); 99 100 boolean pass = false; 102 try { 103 d.getSeriesKey(-1); 104 } 105 catch (IllegalArgumentException e) { 106 pass = true; 107 } 108 assertTrue(pass); 109 110 pass = false; 111 try { 112 d.getSeriesKey(2); 113 } 114 catch (IllegalArgumentException e) { 115 pass = true; 116 } 117 assertTrue(pass); 118 } 119 120 123 public void testGetItemCount() { 124 DefaultIntervalXYDataset d = createSampleDataset1(); 125 assertEquals(3, d.getItemCount(0)); 126 assertEquals(3, d.getItemCount(1)); 127 128 boolean pass = false; 130 try { 131 d.getItemCount(2); 132 } 133 catch (IllegalArgumentException e) { 134 pass = true; 135 } 136 assertTrue(pass); 137 } 138 139 private static final double EPSILON = 0.0000000001; 140 141 144 public void testGetXValue() { 145 DefaultIntervalXYDataset d = createSampleDataset1(); 146 assertEquals(1.0, d.getXValue(0, 0), EPSILON); 147 assertEquals(2.0, d.getXValue(0, 1), EPSILON); 148 assertEquals(3.0, d.getXValue(0, 2), EPSILON); 149 assertEquals(11.0, d.getXValue(1, 0), EPSILON); 150 assertEquals(12.0, d.getXValue(1, 1), EPSILON); 151 assertEquals(13.0, d.getXValue(1, 2), EPSILON); 152 } 153 154 157 public void testGetYValue() { 158 DefaultIntervalXYDataset d = createSampleDataset1(); 159 assertEquals(4.0, d.getYValue(0, 0), EPSILON); 160 assertEquals(5.0, d.getYValue(0, 1), EPSILON); 161 assertEquals(6.0, d.getYValue(0, 2), EPSILON); 162 assertEquals(14.0, d.getYValue(1, 0), EPSILON); 163 assertEquals(15.0, d.getYValue(1, 1), EPSILON); 164 assertEquals(16.0, d.getYValue(1, 2), EPSILON); 165 } 166 167 170 public void testGetStartXValue() { 171 DefaultIntervalXYDataset d = createSampleDataset1(); 172 assertEquals(0.9, d.getStartXValue(0, 0), EPSILON); 173 assertEquals(1.9, d.getStartXValue(0, 1), EPSILON); 174 assertEquals(2.9, d.getStartXValue(0, 2), EPSILON); 175 assertEquals(10.9, d.getStartXValue(1, 0), EPSILON); 176 assertEquals(11.9, d.getStartXValue(1, 1), EPSILON); 177 assertEquals(12.9, d.getStartXValue(1, 2), EPSILON); 178 } 179 180 183 public void testGetEndXValue() { 184 DefaultIntervalXYDataset d = createSampleDataset1(); 185 assertEquals(1.1, d.getEndXValue(0, 0), EPSILON); 186 assertEquals(2.1, d.getEndXValue(0, 1), EPSILON); 187 assertEquals(3.1, d.getEndXValue(0, 2), EPSILON); 188 assertEquals(11.1, d.getEndXValue(1, 0), EPSILON); 189 assertEquals(12.1, d.getEndXValue(1, 1), EPSILON); 190 assertEquals(13.1, d.getEndXValue(1, 2), EPSILON); 191 } 192 193 196 public void testGetStartYValue() { 197 DefaultIntervalXYDataset d = createSampleDataset1(); 198 assertEquals(1.09, d.getStartYValue(0, 0), EPSILON); 199 assertEquals(2.09, d.getStartYValue(0, 1), EPSILON); 200 assertEquals(3.09, d.getStartYValue(0, 2), EPSILON); 201 assertEquals(11.09, d.getStartYValue(1, 0), EPSILON); 202 assertEquals(12.09, d.getStartYValue(1, 1), EPSILON); 203 assertEquals(13.09, d.getStartYValue(1, 2), EPSILON); 204 } 205 206 209 public void testGetEndYValue() { 210 DefaultIntervalXYDataset d = createSampleDataset1(); 211 assertEquals(1.11, d.getEndYValue(0, 0), EPSILON); 212 assertEquals(2.11, d.getEndYValue(0, 1), EPSILON); 213 assertEquals(3.11, d.getEndYValue(0, 2), EPSILON); 214 assertEquals(11.11, d.getEndYValue(1, 0), EPSILON); 215 assertEquals(12.11, d.getEndYValue(1, 1), EPSILON); 216 assertEquals(13.11, d.getEndYValue(1, 2), EPSILON); 217 } 218 219 222 public void testEquals() { 223 DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset(); 224 DefaultIntervalXYDataset d2 = new DefaultIntervalXYDataset(); 225 assertTrue(d1.equals(d2)); 226 assertTrue(d2.equals(d1)); 227 228 d1 = createSampleDataset1(); 229 assertFalse(d1.equals(d2)); 230 d2 = createSampleDataset1(); 231 assertTrue(d1.equals(d2)); 232 } 233 234 237 public void testCloning() { 238 DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset(); 239 DefaultIntervalXYDataset d2 = null; 240 try { 241 d2 = (DefaultIntervalXYDataset) d1.clone(); 242 } 243 catch (CloneNotSupportedException e) { 244 e.printStackTrace(); 245 } 246 assertTrue(d1 != d2); 247 assertTrue(d1.getClass() == d2.getClass()); 248 assertTrue(d1.equals(d2)); 249 250 d1 = createSampleDataset1(); 252 try { 253 d2 = (DefaultIntervalXYDataset) d1.clone(); 254 } 255 catch (CloneNotSupportedException e) { 256 e.printStackTrace(); 257 } 258 assertTrue(d1 != d2); 259 assertTrue(d1.getClass() == d2.getClass()); 260 assertTrue(d1.equals(d2)); 261 } 262 263 266 public void testSerialization() { 267 268 DefaultIntervalXYDataset d1 = new DefaultIntervalXYDataset(); 269 DefaultIntervalXYDataset d2 = null; 270 271 try { 272 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 273 ObjectOutput out = new ObjectOutputStream (buffer); 274 out.writeObject(d1); 275 out.close(); 276 277 ObjectInput in = new ObjectInputStream ( 278 new ByteArrayInputStream (buffer.toByteArray())); 279 d2 = (DefaultIntervalXYDataset) in.readObject(); 280 in.close(); 281 } 282 catch (Exception e) { 283 e.printStackTrace(); 284 } 285 assertEquals(d1, d2); 286 287 d1 = createSampleDataset1(); 289 try { 290 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 291 ObjectOutput out = new ObjectOutputStream (buffer); 292 out.writeObject(d1); 293 out.close(); 294 295 ObjectInput in = new ObjectInputStream ( 296 new ByteArrayInputStream (buffer.toByteArray())); 297 d2 = (DefaultIntervalXYDataset) in.readObject(); 298 in.close(); 299 } 300 catch (Exception e) { 301 e.printStackTrace(); 302 } 303 assertEquals(d1, d2); 304 305 } 306 307 310 public void testIndexOf() { 311 DefaultIntervalXYDataset d = createSampleDataset1(); 312 assertEquals(0, d.indexOf("S1")); 313 assertEquals(1, d.indexOf("S2")); 314 assertEquals(-1, d.indexOf("Green Eggs and Ham")); 315 assertEquals(-1, d.indexOf(null)); 316 } 317 318 321 public void testAddSeries() { 322 DefaultIntervalXYDataset d = new DefaultIntervalXYDataset(); 323 d.addSeries("S1", new double[][] {{1.0}, {0.5}, {1.5}, {2.0}, {2.5}, 324 {1.5}}); 325 assertEquals(1, d.getSeriesCount()); 326 assertEquals("S1", d.getSeriesKey(0)); 327 328 d.addSeries("S1", new double[][] {{1.1}, {0.6}, {1.6}, {2.1}, {2.6}, 330 {1.6}}); 331 assertEquals(1, d.getSeriesCount()); 332 assertEquals(2.1, d.getYValue(0, 0), EPSILON); 333 334 boolean pass = false; 336 try 337 { 338 d.addSeries(null, new double[][] {{1.1}, {0.6}, {1.6}, {2.1}, {2.6}, 339 {1.6}}); 340 } 341 catch (IllegalArgumentException e) 342 { 343 pass = true; 344 } 345 assertTrue(pass); 346 } 347 348 353 public DefaultIntervalXYDataset createSampleDataset1() { 354 DefaultIntervalXYDataset d = new DefaultIntervalXYDataset(); 355 double[] x1 = new double[] {1.0, 2.0, 3.0}; 356 double[] x1Start = new double[] {0.9, 1.9, 2.9}; 357 double[] x1End = new double[] {1.1, 2.1, 3.1}; 358 double[] y1 = new double[] {4.0, 5.0, 6.0}; 359 double[] y1Start = new double[] {1.09, 2.09, 3.09}; 360 double[] y1End = new double[] {1.11, 2.11, 3.11}; 361 double[][] data1 = new double[][] {x1, x1Start, x1End, y1, y1Start, 362 y1End}; 363 d.addSeries("S1", data1); 364 365 double[] x2 = new double[] {11.0, 12.0, 13.0}; 366 double[] x2Start = new double[] {10.9, 11.9, 12.9}; 367 double[] x2End = new double[] {11.1, 12.1, 13.1}; 368 double[] y2 = new double[] {14.0, 15.0, 16.0}; 369 double[] y2Start = new double[] {11.09, 12.09, 13.09}; 370 double[] y2End = new double[] {11.11, 12.11, 13.11}; 371 double[][] data2 = new double[][] {x2, x2Start, x2End, y2, y2Start, 372 y2End}; 373 d.addSeries("S2", data2); 374 return d; 375 } 376 377 } 378 | Popular Tags |