1 47 48 package org.jfree.data.time.junit; 49 50 import java.io.ByteArrayInputStream ; 51 import java.io.ByteArrayOutputStream ; 52 import java.io.ObjectInput ; 53 import java.io.ObjectInputStream ; 54 import java.io.ObjectOutput ; 55 import java.io.ObjectOutputStream ; 56 57 import junit.framework.Test; 58 import junit.framework.TestCase; 59 import junit.framework.TestSuite; 60 61 import org.jfree.data.general.SeriesChangeEvent; 62 import org.jfree.data.general.SeriesChangeListener; 63 import org.jfree.data.general.SeriesException; 64 import org.jfree.data.time.Day; 65 import org.jfree.data.time.FixedMillisecond; 66 import org.jfree.data.time.Month; 67 import org.jfree.data.time.RegularTimePeriod; 68 import org.jfree.data.time.TimeSeries; 69 import org.jfree.data.time.TimeSeriesDataItem; 70 import org.jfree.data.time.Year; 71 import org.jfree.date.MonthConstants; 72 73 76 public class TimeSeriesTests extends TestCase implements SeriesChangeListener { 77 78 79 private TimeSeries seriesA; 80 81 82 private TimeSeries seriesB; 83 84 85 private TimeSeries seriesC; 86 87 88 private boolean gotSeriesChangeEvent = false; 89 90 95 public static Test suite() { 96 return new TestSuite(TimeSeriesTests.class); 97 } 98 99 104 public TimeSeriesTests(String name) { 105 super(name); 106 } 107 108 111 protected void setUp() { 112 113 this.seriesA = new TimeSeries("Series A", Year.class); 114 try { 115 this.seriesA.add(new Year(2000), new Integer (102000)); 116 this.seriesA.add(new Year(2001), new Integer (102001)); 117 this.seriesA.add(new Year(2002), new Integer (102002)); 118 this.seriesA.add(new Year(2003), new Integer (102003)); 119 this.seriesA.add(new Year(2004), new Integer (102004)); 120 this.seriesA.add(new Year(2005), new Integer (102005)); 121 } 122 catch (SeriesException e) { 123 System.err.println("Problem creating series."); 124 } 125 126 this.seriesB = new TimeSeries("Series B", Year.class); 127 try { 128 this.seriesB.add(new Year(2006), new Integer (202006)); 129 this.seriesB.add(new Year(2007), new Integer (202007)); 130 this.seriesB.add(new Year(2008), new Integer (202008)); 131 } 132 catch (SeriesException e) { 133 System.err.println("Problem creating series."); 134 } 135 136 this.seriesC = new TimeSeries("Series C", Year.class); 137 try { 138 this.seriesC.add(new Year(1999), new Integer (301999)); 139 this.seriesC.add(new Year(2000), new Integer (302000)); 140 this.seriesC.add(new Year(2002), new Integer (302002)); 141 } 142 catch (SeriesException e) { 143 System.err.println("Problem creating series."); 144 } 145 146 } 147 148 154 public void seriesChanged(SeriesChangeEvent event) { 155 this.gotSeriesChangeEvent = true; 156 } 157 158 161 public void testClone() { 162 163 TimeSeries series = new TimeSeries("Test Series"); 164 165 RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002); 166 try { 167 series.add(jan1st2002, new Integer (42)); 168 } 169 catch (SeriesException e) { 170 System.err.println("Problem adding to series."); 171 } 172 173 TimeSeries clone = null; 174 try { 175 clone = (TimeSeries) series.clone(); 176 clone.setKey("Clone Series"); 177 try { 178 clone.update(jan1st2002, new Integer (10)); 179 } 180 catch (SeriesException e) { 181 System.err.println("Problem updating series."); 182 } 183 } 184 catch (CloneNotSupportedException e) { 185 assertTrue(false); 186 } 187 188 int seriesValue = series.getValue(jan1st2002).intValue(); 189 int cloneValue = clone.getValue(jan1st2002).intValue(); 190 191 assertEquals(42, seriesValue); 192 assertEquals(10, cloneValue); 193 assertEquals("Test Series", series.getKey()); 194 assertEquals("Clone Series", clone.getKey()); 195 196 } 197 198 201 public void testAddValue() { 202 203 try { 204 this.seriesA.add(new Year(1999), new Integer (1)); 205 } 206 catch (SeriesException e) { 207 System.err.println("Problem adding to series."); 208 } 209 210 int value = this.seriesA.getValue(0).intValue(); 211 assertEquals(1, value); 212 213 } 214 215 218 public void testGetValue() { 219 220 Number value1 = this.seriesA.getValue(new Year(1999)); 221 assertNull(value1); 222 int value2 = this.seriesA.getValue(new Year(2000)).intValue(); 223 assertEquals(102000, value2); 224 225 } 226 227 230 public void testDelete() { 231 this.seriesA.delete(0, 0); 232 assertEquals(5, this.seriesA.getItemCount()); 233 Number value = this.seriesA.getValue(new Year(2000)); 234 assertNull(value); 235 } 236 237 240 public void testDelete2() { 241 TimeSeries s1 = new TimeSeries("Series", Year.class); 242 s1.add(new Year(2000), 13.75); 243 s1.add(new Year(2001), 11.90); 244 s1.add(new Year(2002), null); 245 s1.addChangeListener(this); 246 this.gotSeriesChangeEvent = false; 247 s1.delete(new Year(2001)); 248 assertTrue(this.gotSeriesChangeEvent); 249 assertEquals(2, s1.getItemCount()); 250 assertEquals(null, s1.getValue(new Year(2001))); 251 } 252 253 256 public void testSerialization() { 257 258 TimeSeries s1 = new TimeSeries("A test", Year.class); 259 s1.add(new Year(2000), 13.75); 260 s1.add(new Year(2001), 11.90); 261 s1.add(new Year(2002), null); 262 s1.add(new Year(2005), 19.32); 263 s1.add(new Year(2007), 16.89); 264 TimeSeries s2 = null; 265 266 try { 267 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 268 ObjectOutput out = new ObjectOutputStream (buffer); 269 out.writeObject(s1); 270 out.close(); 271 272 ObjectInput in = new ObjectInputStream ( 273 new ByteArrayInputStream (buffer.toByteArray()) 274 ); 275 s2 = (TimeSeries) in.readObject(); 276 in.close(); 277 } 278 catch (Exception e) { 279 System.out.println(e.toString()); 280 } 281 assertTrue(s1.equals(s2)); 282 283 } 284 285 288 public void testEquals() { 289 TimeSeries s1 = new TimeSeries("Time Series 1"); 290 TimeSeries s2 = new TimeSeries("Time Series 2"); 291 boolean b1 = s1.equals(s2); 292 assertFalse("b1", b1); 293 294 s2.setKey("Time Series 1"); 295 boolean b2 = s1.equals(s2); 296 assertTrue("b2", b2); 297 298 RegularTimePeriod p1 = new Day(); 299 RegularTimePeriod p2 = p1.next(); 300 s1.add(p1, 100.0); 301 s1.add(p2, 200.0); 302 boolean b3 = s1.equals(s2); 303 assertFalse("b3", b3); 304 305 s2.add(p1, 100.0); 306 s2.add(p2, 200.0); 307 boolean b4 = s1.equals(s2); 308 assertTrue("b4", b4); 309 310 s1.setMaximumItemCount(100); 311 boolean b5 = s1.equals(s2); 312 assertFalse("b5", b5); 313 314 s2.setMaximumItemCount(100); 315 boolean b6 = s1.equals(s2); 316 assertTrue("b6", b6); 317 318 s1.setHistoryCount(100); 319 boolean b7 = s1.equals(s2); 320 assertFalse("b7", b7); 321 322 s2.setHistoryCount(100); 323 boolean b8 = s1.equals(s2); 324 assertTrue("b8", b8); 325 } 326 327 331 public void testEquals2() { 332 TimeSeries s1 = new TimeSeries("Series", null, null, Day.class); 333 TimeSeries s2 = new TimeSeries("Series", null, null, Day.class); 334 assertTrue(s1.equals(s2)); 335 } 336 337 341 public void testCreateCopy1() { 342 343 TimeSeries series = new TimeSeries("Series", Month.class); 344 series.add(new Month(MonthConstants.JANUARY, 2003), 45.0); 345 series.add(new Month(MonthConstants.FEBRUARY, 2003), 55.0); 346 series.add(new Month(MonthConstants.JUNE, 2003), 35.0); 347 series.add(new Month(MonthConstants.NOVEMBER, 2003), 85.0); 348 series.add(new Month(MonthConstants.DECEMBER, 2003), 75.0); 349 350 try { 351 TimeSeries result1 = series.createCopy( 353 new Month(MonthConstants.NOVEMBER, 2002), 354 new Month(MonthConstants.DECEMBER, 2002) 355 ); 356 assertEquals(0, result1.getItemCount()); 357 358 TimeSeries result2 = series.createCopy( 360 new Month(MonthConstants.NOVEMBER, 2002), 361 new Month(MonthConstants.JANUARY, 2003) 362 ); 363 assertEquals(1, result2.getItemCount()); 364 365 TimeSeries result3 = series.createCopy( 368 new Month(MonthConstants.NOVEMBER, 2002), 369 new Month(MonthConstants.APRIL, 2003) 370 ); 371 assertEquals(2, result3.getItemCount()); 372 373 TimeSeries result4 = series.createCopy( 374 new Month(MonthConstants.NOVEMBER, 2002), 375 new Month(MonthConstants.DECEMBER, 2003) 376 ); 377 assertEquals(5, result4.getItemCount()); 378 379 TimeSeries result5 = series.createCopy( 380 new Month(MonthConstants.NOVEMBER, 2002), 381 new Month(MonthConstants.MARCH, 2004) 382 ); 383 assertEquals(5, result5.getItemCount()); 384 385 TimeSeries result6 = series.createCopy( 386 new Month(MonthConstants.JANUARY, 2003), 387 new Month(MonthConstants.JANUARY, 2003) 388 ); 389 assertEquals(1, result6.getItemCount()); 390 391 TimeSeries result7 = series.createCopy( 392 new Month(MonthConstants.JANUARY, 2003), 393 new Month(MonthConstants.APRIL, 2003) 394 ); 395 assertEquals(2, result7.getItemCount()); 396 397 TimeSeries result8 = series.createCopy( 398 new Month(MonthConstants.JANUARY, 2003), 399 new Month(MonthConstants.DECEMBER, 2003) 400 ); 401 assertEquals(5, result8.getItemCount()); 402 403 TimeSeries result9 = series.createCopy( 404 new Month(MonthConstants.JANUARY, 2003), 405 new Month(MonthConstants.MARCH, 2004) 406 ); 407 assertEquals(5, result9.getItemCount()); 408 409 TimeSeries result10 = series.createCopy( 410 new Month(MonthConstants.MAY, 2003), 411 new Month(MonthConstants.DECEMBER, 2003) 412 ); 413 assertEquals(3, result10.getItemCount()); 414 415 TimeSeries result11 = series.createCopy( 416 new Month(MonthConstants.MAY, 2003), 417 new Month(MonthConstants.MARCH, 2004) 418 ); 419 assertEquals(3, result11.getItemCount()); 420 421 TimeSeries result12 = series.createCopy( 422 new Month(MonthConstants.DECEMBER, 2003), 423 new Month(MonthConstants.DECEMBER, 2003) 424 ); 425 assertEquals(1, result12.getItemCount()); 426 427 TimeSeries result13 = series.createCopy( 428 new Month(MonthConstants.DECEMBER, 2003), 429 new Month(MonthConstants.MARCH, 2004) 430 ); 431 assertEquals(1, result13.getItemCount()); 432 433 TimeSeries result14 = series.createCopy( 434 new Month(MonthConstants.JANUARY, 2004), 435 new Month(MonthConstants.MARCH, 2004) 436 ); 437 assertEquals(0, result14.getItemCount()); 438 } 439 catch (CloneNotSupportedException e) { 440 assertTrue(false); 441 } 442 443 } 444 445 449 public void testSetMaximumItemCount() { 450 451 TimeSeries s1 = new TimeSeries("S1", Year.class); 452 s1.add(new Year(2000), 13.75); 453 s1.add(new Year(2001), 11.90); 454 s1.add(new Year(2002), null); 455 s1.add(new Year(2005), 19.32); 456 s1.add(new Year(2007), 16.89); 457 458 assertTrue(s1.getItemCount() == 5); 459 s1.setMaximumItemCount(3); 460 assertTrue(s1.getItemCount() == 3); 461 TimeSeriesDataItem item = s1.getDataItem(0); 462 assertTrue(item.getPeriod().equals(new Year(2002))); 463 464 } 465 466 469 public void testAddOrUpdate() { 470 TimeSeries s1 = new TimeSeries("S1", Year.class); 471 s1.setMaximumItemCount(2); 472 s1.addOrUpdate(new Year(2000), 100.0); 473 assertEquals(1, s1.getItemCount()); 474 s1.addOrUpdate(new Year(2001), 101.0); 475 assertEquals(2, s1.getItemCount()); 476 s1.addOrUpdate(new Year(2001), 102.0); 477 assertEquals(2, s1.getItemCount()); 478 s1.addOrUpdate(new Year(2002), 103.0); 479 assertEquals(2, s1.getItemCount()); 480 } 481 482 485 public void testBug1075255() { 486 TimeSeries ts = new TimeSeries("dummy", FixedMillisecond.class); 487 ts.add(new FixedMillisecond(0L), 0.0); 488 TimeSeries ts2 = new TimeSeries("dummy2", FixedMillisecond.class); 489 ts2.add(new FixedMillisecond(0L), 1.0); 490 try { 491 ts.addAndOrUpdate(ts2); 492 } 493 catch (Exception e) { 494 e.printStackTrace(); 495 assertTrue(false); 496 } 497 assertEquals(1, ts.getItemCount()); 498 } 499 } 500 | Popular Tags |