1 43 44 package org.jfree.data.gantt.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 import java.util.Date ; 53 54 import junit.framework.Test; 55 import junit.framework.TestCase; 56 import junit.framework.TestSuite; 57 58 import org.jfree.data.gantt.Task; 59 import org.jfree.data.gantt.TaskSeries; 60 import org.jfree.data.gantt.TaskSeriesCollection; 61 import org.jfree.data.time.SimpleTimePeriod; 62 63 66 public class TaskSeriesCollectionTests extends TestCase { 67 68 73 public static Test suite() { 74 return new TestSuite(TaskSeriesCollectionTests.class); 75 } 76 77 82 public TaskSeriesCollectionTests(String name) { 83 super(name); 84 } 85 86 91 private TaskSeriesCollection createCollection1() { 92 TaskSeriesCollection result = new TaskSeriesCollection(); 93 TaskSeries s1 = new TaskSeries("S1"); 94 s1.add(new Task("Task 1", new Date (1), new Date (2))); 95 s1.add(new Task("Task 2", new Date (3), new Date (4))); 96 result.add(s1); 97 TaskSeries s2 = new TaskSeries("S2"); 98 s2.add(new Task("Task 3", new Date (5), new Date (6))); 99 result.add(s2); 100 return result; 101 } 102 103 108 private TaskSeriesCollection createCollection2() { 109 TaskSeriesCollection result = new TaskSeriesCollection(); 110 TaskSeries s1 = new TaskSeries("S1"); 111 Task t1 = new Task("Task 1", new Date (10), new Date (20)); 112 t1.addSubtask(new Task("Task 1A", new Date (10), new Date (15))); 113 t1.addSubtask(new Task("Task 1B", new Date (16), new Date (20))); 114 t1.setPercentComplete(0.10); 115 s1.add(t1); 116 Task t2 = new Task("Task 2", new Date (30), new Date (40)); 117 t2.addSubtask(new Task("Task 2A", new Date (30), new Date (35))); 118 t2.addSubtask(new Task("Task 2B", new Date (36), new Date (40))); 119 t2.setPercentComplete(0.20); 120 s1.add(t2); 121 result.add(s1); 122 TaskSeries s2 = new TaskSeries("S2"); 123 Task t3 = new Task("Task 3", new Date (50), new Date (60)); 124 t3.addSubtask(new Task("Task 3A", new Date (50), new Date (55))); 125 t3.addSubtask(new Task("Task 3B", new Date (56), new Date (60))); 126 t3.setPercentComplete(0.30); 127 s2.add(t3); 128 result.add(s2); 129 return result; 130 } 131 132 137 private TaskSeriesCollection createCollection3() { 138 139 Task sub1 = new Task("Sub1", new Date (11), new Date (111)); 141 Task sub2 = new Task("Sub2", new Date (22), new Date (222)); 142 Task sub3 = new Task("Sub3", new Date (33), new Date (333)); 143 Task sub4 = new Task("Sub4", new Date (44), new Date (444)); 144 Task sub5 = new Task("Sub5", new Date (55), new Date (555)); 145 Task sub6 = new Task("Sub6", new Date (66), new Date (666)); 146 sub1.setPercentComplete(0.111); 147 sub2.setPercentComplete(0.222); 148 sub3.setPercentComplete(0.333); 149 sub4.setPercentComplete(0.444); 150 sub5.setPercentComplete(0.555); 151 sub6.setPercentComplete(0.666); 152 153 TaskSeries seriesA = new TaskSeries("Series A"); 154 Task taskA1 = new Task("Task 1", new SimpleTimePeriod(new Date (100), 155 new Date (200))); 156 taskA1.setPercentComplete(0.1); 157 taskA1.addSubtask(sub1); 158 Task taskA2 = new Task("Task 2", new SimpleTimePeriod(new Date (220), 159 new Date (350))); 160 taskA2.setPercentComplete(0.2); 161 taskA2.addSubtask(sub2); 162 taskA2.addSubtask(sub3); 163 seriesA.add(taskA1); 164 seriesA.add(taskA2); 165 166 TaskSeries seriesB = new TaskSeries("Series B"); 167 Task taskB2 = new Task("Task 2", new SimpleTimePeriod(new Date (2220), 169 new Date (3350))); 170 taskB2.setPercentComplete(0.3); 171 taskB2.addSubtask(sub4); 172 taskB2.addSubtask(sub5); 173 taskB2.addSubtask(sub6); 174 seriesB.add(taskB2); 175 176 TaskSeriesCollection tsc = new TaskSeriesCollection(); 177 tsc.add(seriesA); 178 tsc.add(seriesB); 179 180 return tsc; 181 } 182 183 186 public void testGetSeriesCount() { 187 TaskSeriesCollection c = createCollection1(); 188 assertEquals(2, c.getSeriesCount()); 189 } 190 191 194 public void testGetSeriesKey() { 195 TaskSeriesCollection c = createCollection1(); 196 assertEquals("S1", c.getSeriesKey(0)); 197 assertEquals("S2", c.getSeriesKey(1)); 198 } 199 200 203 public void testGetRowCount() { 204 TaskSeriesCollection c = createCollection1(); 205 assertEquals(2, c.getRowCount()); 206 } 207 208 211 public void testGetRowKey() { 212 TaskSeriesCollection c = createCollection1(); 213 assertEquals("S1", c.getRowKey(0)); 214 assertEquals("S2", c.getRowKey(1)); 215 } 216 217 220 public void testGetRowIndex() { 221 TaskSeriesCollection c = createCollection1(); 222 assertEquals(0, c.getRowIndex("S1")); 223 assertEquals(1, c.getRowIndex("S2")); 224 } 225 226 229 public void testGetValue() { 230 TaskSeriesCollection c = createCollection1(); 231 assertEquals(new Long (1L), c.getValue("S1", "Task 1")); 232 assertEquals(new Long (3L), c.getValue("S1", "Task 2")); 233 assertEquals(new Long (5L), c.getValue("S2", "Task 3")); 234 235 assertEquals(new Long (1L), c.getValue(0, 0)); 236 assertEquals(new Long (3L), c.getValue(0, 1)); 237 assertEquals(null, c.getValue(0, 2)); 238 assertEquals(null, c.getValue(1, 0)); 239 assertEquals(null, c.getValue(1, 1)); 240 assertEquals(new Long (5L), c.getValue(1, 2)); 241 } 242 243 246 public void testGetStartValue() { 247 TaskSeriesCollection c = createCollection1(); 248 assertEquals(new Long (1L), c.getStartValue("S1", "Task 1")); 249 assertEquals(new Long (3L), c.getStartValue("S1", "Task 2")); 250 assertEquals(new Long (5L), c.getStartValue("S2", "Task 3")); 251 252 assertEquals(new Long (1L), c.getStartValue(0, 0)); 253 assertEquals(new Long (3L), c.getStartValue(0, 1)); 254 assertEquals(null, c.getStartValue(0, 2)); 255 assertEquals(null, c.getStartValue(1, 0)); 256 assertEquals(null, c.getStartValue(1, 1)); 257 assertEquals(new Long (5L), c.getStartValue(1, 2)); 258 259 TaskSeriesCollection c3 = createCollection3(); 261 assertEquals(new Long (100), c3.getStartValue(0, 0)); 262 assertEquals(new Long (220), c3.getStartValue(0, 1)); 263 assertTrue(c3.getStartValue(1, 0) == null); 264 assertEquals(new Long (2220), c3.getStartValue(1, 1)); 265 } 266 267 270 public void testGetStartValue2() { 271 TaskSeriesCollection c = createCollection2(); 272 assertEquals(new Long (10L), c.getStartValue("S1", "Task 1", 0)); 273 assertEquals(new Long (16L), c.getStartValue("S1", "Task 1", 1)); 274 assertEquals(new Long (30L), c.getStartValue("S1", "Task 2", 0)); 275 assertEquals(new Long (36L), c.getStartValue("S1", "Task 2", 1)); 276 assertEquals(new Long (50L), c.getStartValue("S2", "Task 3", 0)); 277 assertEquals(new Long (56L), c.getStartValue("S2", "Task 3", 1)); 278 279 assertEquals(new Long (10L), c.getStartValue(0, 0, 0)); 280 assertEquals(new Long (16L), c.getStartValue(0, 0, 1)); 281 assertEquals(new Long (30L), c.getStartValue(0, 1, 0)); 282 assertEquals(new Long (36L), c.getStartValue(0, 1, 1)); 283 assertEquals(new Long (50L), c.getStartValue(1, 2, 0)); 284 assertEquals(new Long (56L), c.getStartValue(1, 2, 1)); 285 286 TaskSeriesCollection c3 = createCollection3(); 287 assertEquals(new Long (11), c3.getStartValue(0, 0, 0)); 288 assertEquals(new Long (22), c3.getStartValue(0, 1, 0)); 289 assertEquals(new Long (33), c3.getStartValue(0, 1, 1)); 290 assertTrue(c3.getStartValue(1, 0, 0) == null); 291 assertEquals(new Long (44), c3.getStartValue(1, 1, 0)); 292 assertEquals(new Long (55), c3.getStartValue(1, 1, 1)); 293 assertEquals(new Long (66), c3.getStartValue(1, 1, 2)); 294 } 295 296 299 public void testGetStartValue3() { 300 TaskSeriesCollection c = new TaskSeriesCollection(); 301 TaskSeries s = new TaskSeries("Series 1"); 302 s.add(new Task("Task with null duration", null)); 303 c.add(s); 304 Number millis = c.getStartValue("Series 1", "Task with null duration"); 305 assertTrue(millis == null); 306 } 307 308 311 public void testGetEndValue() { 312 TaskSeriesCollection c = createCollection1(); 313 assertEquals(new Long (2L), c.getEndValue("S1", "Task 1")); 314 assertEquals(new Long (4L), c.getEndValue("S1", "Task 2")); 315 assertEquals(new Long (6L), c.getEndValue("S2", "Task 3")); 316 317 assertEquals(new Long (2L), c.getEndValue(0, 0)); 318 assertEquals(new Long (4L), c.getEndValue(0, 1)); 319 assertEquals(null, c.getEndValue(0, 2)); 320 assertEquals(null, c.getEndValue(1, 0)); 321 assertEquals(null, c.getEndValue(1, 1)); 322 assertEquals(new Long (6L), c.getEndValue(1, 2)); 323 324 TaskSeriesCollection c3 = createCollection3(); 326 assertEquals(new Long (200), c3.getEndValue(0, 0)); 327 assertEquals(new Long (350), c3.getEndValue(0, 1)); 328 assertTrue(c3.getEndValue(1, 0) == null); 329 assertEquals(new Long (3350), c3.getEndValue(1, 1)); 330 } 331 332 335 public void testGetEndValue2() { 336 TaskSeriesCollection c = createCollection2(); 337 assertEquals(new Long (15L), c.getEndValue("S1", "Task 1", 0)); 338 assertEquals(new Long (20L), c.getEndValue("S1", "Task 1", 1)); 339 assertEquals(new Long (35L), c.getEndValue("S1", "Task 2", 0)); 340 assertEquals(new Long (40L), c.getEndValue("S1", "Task 2", 1)); 341 assertEquals(new Long (55L), c.getEndValue("S2", "Task 3", 0)); 342 assertEquals(new Long (60L), c.getEndValue("S2", "Task 3", 1)); 343 344 assertEquals(new Long (15L), c.getEndValue(0, 0, 0)); 345 assertEquals(new Long (20L), c.getEndValue(0, 0, 1)); 346 assertEquals(new Long (35L), c.getEndValue(0, 1, 0)); 347 assertEquals(new Long (40L), c.getEndValue(0, 1, 1)); 348 assertEquals(new Long (55L), c.getEndValue(1, 2, 0)); 349 assertEquals(new Long (60L), c.getEndValue(1, 2, 1)); 350 351 TaskSeriesCollection c3 = createCollection3(); 352 assertEquals(new Long (111), c3.getEndValue(0, 0, 0)); 353 assertEquals(new Long (222), c3.getEndValue(0, 1, 0)); 354 assertEquals(new Long (333), c3.getEndValue(0, 1, 1)); 355 assertTrue(c3.getEndValue(1, 0, 0) == null); 356 assertEquals(new Long (444), c3.getEndValue(1, 1, 0)); 357 assertEquals(new Long (555), c3.getEndValue(1, 1, 1)); 358 assertEquals(new Long (666), c3.getEndValue(1, 1, 2)); 359 } 360 361 364 public void testGetEndValue3() { 365 TaskSeriesCollection c = new TaskSeriesCollection(); 366 TaskSeries s = new TaskSeries("Series 1"); 367 s.add(new Task("Task with null duration", null)); 368 c.add(s); 369 Number millis = c.getEndValue("Series 1", "Task with null duration"); 370 assertTrue(millis == null); 371 } 372 373 376 public void testGetPercentComplete() { 377 TaskSeriesCollection c = createCollection2(); 378 assertEquals(new Double (0.10), c.getPercentComplete("S1", "Task 1")); 379 assertEquals(new Double (0.20), c.getPercentComplete("S1", "Task 2")); 380 assertEquals(new Double (0.30), c.getPercentComplete("S2", "Task 3")); 381 382 assertEquals(new Double (0.10), c.getPercentComplete(0, 0)); 383 assertEquals(new Double (0.20), c.getPercentComplete(0, 1)); 384 assertEquals(null, c.getPercentComplete(0, 2)); 385 assertEquals(null, c.getPercentComplete(1, 0)); 386 assertEquals(null, c.getPercentComplete(1, 1)); 387 assertEquals(new Double (0.30), c.getPercentComplete(1, 2)); 388 389 TaskSeriesCollection c3 = createCollection3(); 391 assertEquals(new Double (0.1), c3.getPercentComplete(0, 0)); 392 assertEquals(new Double (0.2), c3.getPercentComplete(0, 1)); 393 assertTrue(c3.getPercentComplete(1, 0) == null); 394 assertEquals(new Double (0.3), c3.getPercentComplete(1, 1)); 395 396 assertEquals(new Double (0.111), c3.getPercentComplete(0, 0, 0)); 397 398 assertEquals(new Double (0.222), c3.getPercentComplete(0, 1, 0)); 399 assertEquals(new Double (0.333), c3.getPercentComplete(0, 1, 1)); 400 401 assertEquals(new Double (0.444), c3.getPercentComplete(1, 1, 0)); 402 assertEquals(new Double (0.555), c3.getPercentComplete(1, 1, 1)); 403 assertEquals(new Double (0.666), c3.getPercentComplete(1, 1, 2)); 404 } 405 406 409 public void testGetColumnCount() { 410 TaskSeriesCollection c = createCollection1(); 411 assertEquals(3, c.getColumnCount()); 412 } 413 414 417 public void testGetColumnKey() { 418 TaskSeriesCollection c = createCollection1(); 419 assertEquals("Task 1", c.getColumnKey(0)); 420 assertEquals("Task 2", c.getColumnKey(1)); 421 assertEquals("Task 3", c.getColumnKey(2)); 422 } 423 424 427 public void testGetColumnIndex() { 428 TaskSeriesCollection c = createCollection1(); 429 assertEquals(0, c.getColumnIndex("Task 1")); 430 assertEquals(1, c.getColumnIndex("Task 2")); 431 assertEquals(2, c.getColumnIndex("Task 3")); 432 } 433 434 437 public void testEquals() { 438 439 TaskSeries s1 = new TaskSeries("S"); 440 s1.add(new Task("T1", new Date (1), new Date (2))); 441 s1.add(new Task("T2", new Date (11), new Date (22))); 442 TaskSeries s2 = new TaskSeries("S"); 443 s2.add(new Task("T1", new Date (1), new Date (2))); 444 s2.add(new Task("T2", new Date (11), new Date (22))); 445 TaskSeriesCollection c1 = new TaskSeriesCollection(); 446 c1.add(s1); 447 c1.add(s2); 448 449 TaskSeries s1b = new TaskSeries("S"); 450 s1b.add(new Task("T1", new Date (1), new Date (2))); 451 s1b.add(new Task("T2", new Date (11), new Date (22))); 452 TaskSeries s2b = new TaskSeries("S"); 453 s2b.add(new Task("T1", new Date (1), new Date (2))); 454 s2b.add(new Task("T2", new Date (11), new Date (22))); 455 TaskSeriesCollection c2 = new TaskSeriesCollection(); 456 c2.add(s1b); 457 c2.add(s2b); 458 459 assertTrue(c1.equals(c2)); 460 assertTrue(c2.equals(c1)); 461 462 } 463 464 467 public void testCloning() { 468 TaskSeries s1 = new TaskSeries("S"); 469 s1.add(new Task("T1", new Date (1), new Date (2))); 470 s1.add(new Task("T2", new Date (11), new Date (22))); 471 TaskSeries s2 = new TaskSeries("S"); 472 s2.add(new Task("T1", new Date (1), new Date (2))); 473 s2.add(new Task("T2", new Date (11), new Date (22))); 474 TaskSeriesCollection c1 = new TaskSeriesCollection(); 475 c1.add(s1); 476 c1.add(s2); 477 478 TaskSeriesCollection c2 = null; 479 try { 480 c2 = (TaskSeriesCollection) c1.clone(); 481 } 482 catch (CloneNotSupportedException e) { 483 System.err.println("Failed to clone."); 484 } 485 assertTrue(c1 != c2); 486 assertTrue(c1.getClass() == c2.getClass()); 487 assertTrue(c1.equals(c2)); 488 } 489 490 493 public void testSerialization() { 494 495 TaskSeries s1 = new TaskSeries("S"); 496 s1.add(new Task("T1", new Date (1), new Date (2))); 497 s1.add(new Task("T2", new Date (11), new Date (22))); 498 TaskSeries s2 = new TaskSeries("S"); 499 s2.add(new Task("T1", new Date (1), new Date (2))); 500 s2.add(new Task("T2", new Date (11), new Date (22))); 501 TaskSeriesCollection c1 = new TaskSeriesCollection(); 502 c1.add(s1); 503 c1.add(s2); 504 TaskSeriesCollection c2 = null; 505 506 try { 507 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 508 ObjectOutput out = new ObjectOutputStream (buffer); 509 out.writeObject(c1); 510 out.close(); 511 512 ObjectInput in = new ObjectInputStream (new ByteArrayInputStream ( 513 buffer.toByteArray())); 514 c2 = (TaskSeriesCollection) in.readObject(); 515 in.close(); 516 } 517 catch (Exception e) { 518 System.out.println(e.toString()); 519 } 520 assertEquals(c1, c2); 521 522 } 523 524 527 public void test697153() { 528 529 TaskSeries s1 = new TaskSeries("S1"); 530 s1.add(new Task("Task 1", new SimpleTimePeriod(new Date (), 531 new Date ()))); 532 s1.add(new Task("Task 2", new SimpleTimePeriod(new Date (), 533 new Date ()))); 534 s1.add(new Task("Task 3", new SimpleTimePeriod(new Date (), 535 new Date ()))); 536 537 TaskSeries s2 = new TaskSeries("S2"); 538 s2.add(new Task("Task 2", new SimpleTimePeriod(new Date (), 539 new Date ()))); 540 s2.add(new Task("Task 3", new SimpleTimePeriod(new Date (), 541 new Date ()))); 542 s2.add(new Task("Task 4", new SimpleTimePeriod(new Date (), 543 new Date ()))); 544 545 TaskSeriesCollection tsc = new TaskSeriesCollection(); 546 tsc.add(s1); 547 tsc.add(s2); 548 549 s1.removeAll(); 550 551 int taskCount = tsc.getColumnCount(); 552 553 assertEquals(3, taskCount); 554 555 } 556 557 560 public void test800324() { 561 TaskSeries s1 = new TaskSeries("S1"); 562 s1.add(new Task("Task 1", new SimpleTimePeriod(new Date (), 563 new Date ()))); 564 s1.add(new Task("Task 2", new SimpleTimePeriod(new Date (), 565 new Date ()))); 566 s1.add(new Task("Task 3", new SimpleTimePeriod(new Date (), 567 new Date ()))); 568 569 TaskSeriesCollection tsc = new TaskSeriesCollection(); 570 tsc.add(s1); 571 572 try { 575 tsc.getStartValue(0, 3); 576 assertTrue(false); 577 } 578 catch (IndexOutOfBoundsException e) { 579 } 581 try { 582 tsc.getEndValue(0, 3); 583 assertTrue(false); 584 } 585 catch (IndexOutOfBoundsException e) { 586 } 588 try { 589 tsc.getSubIntervalCount(0, 3); 590 assertTrue(false); 591 } 592 catch (IndexOutOfBoundsException e) { 593 } 595 } 596 597 604 public void testGetSubIntervalCount() { 605 TaskSeriesCollection tsc = createCollection3(); 606 assertEquals(1, tsc.getSubIntervalCount(0, 0)); 607 assertEquals(2, tsc.getSubIntervalCount(0, 1)); 608 assertEquals(0, tsc.getSubIntervalCount(1, 0)); 609 assertEquals(3, tsc.getSubIntervalCount(1, 1)); 610 } 611 612 615 public void testGetSeries() { 616 TaskSeries s1 = new TaskSeries("S1"); 617 TaskSeries s2 = new TaskSeries("S2"); 618 TaskSeriesCollection c = new TaskSeriesCollection(); 619 c.add(s1); 620 621 assertEquals(c.getSeries(0), s1); 622 assertEquals(c.getSeries("S1"), s1); 623 assertEquals(c.getSeries("XX"), null); 624 625 c.add(s2); 626 assertEquals(c.getSeries(1), s2); 627 assertEquals(c.getSeries("S2"), s2); 628 629 boolean pass = false; 630 try { 631 c.getSeries(null); 632 } 633 catch (NullPointerException e) { 634 pass = true; 635 } 636 assertTrue(pass); 637 } 638 639 } 640 | Popular Tags |