1 43 44 package org.jfree.chart.plot.junit; 45 46 import java.awt.BasicStroke ; 47 import java.awt.Color ; 48 import java.awt.Stroke ; 49 import java.io.ByteArrayInputStream ; 50 import java.io.ByteArrayOutputStream ; 51 import java.io.ObjectInput ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutput ; 54 import java.io.ObjectOutputStream ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.chart.ChartFactory; 61 import org.jfree.chart.JFreeChart; 62 import org.jfree.chart.annotations.CategoryTextAnnotation; 63 import org.jfree.chart.axis.AxisLocation; 64 import org.jfree.chart.axis.AxisSpace; 65 import org.jfree.chart.axis.CategoryAnchor; 66 import org.jfree.chart.axis.CategoryAxis; 67 import org.jfree.chart.axis.NumberAxis; 68 import org.jfree.chart.plot.CategoryPlot; 69 import org.jfree.chart.plot.DatasetRenderingOrder; 70 import org.jfree.chart.plot.IntervalMarker; 71 import org.jfree.chart.plot.PlotOrientation; 72 import org.jfree.chart.plot.ValueMarker; 73 import org.jfree.chart.renderer.category.AreaRenderer; 74 import org.jfree.chart.renderer.category.BarRenderer; 75 import org.jfree.chart.renderer.category.CategoryItemRenderer; 76 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 77 import org.jfree.data.Range; 78 import org.jfree.data.category.DefaultCategoryDataset; 79 import org.jfree.ui.Layer; 80 import org.jfree.ui.RectangleInsets; 81 import org.jfree.util.SortOrder; 82 83 86 public class CategoryPlotTests extends TestCase { 87 88 93 public static Test suite() { 94 return new TestSuite(CategoryPlotTests.class); 95 } 96 97 102 public CategoryPlotTests(String name) { 103 super(name); 104 } 105 106 109 public void testAxisRange() { 110 DefaultCategoryDataset datasetA = new DefaultCategoryDataset(); 111 DefaultCategoryDataset datasetB = new DefaultCategoryDataset(); 112 datasetB.addValue(50.0, "R1", "C1"); 113 datasetB.addValue(80.0, "R1", "C1"); 114 CategoryPlot plot = new CategoryPlot( 115 datasetA, new CategoryAxis(null), new NumberAxis(null), 116 new LineAndShapeRenderer() 117 ); 118 plot.setDataset(1, datasetB); 119 plot.setRenderer(1, new LineAndShapeRenderer()); 120 Range r = plot.getRangeAxis().getRange(); 121 assertEquals(84.0, r.getUpperBound(), 0.00001); 122 } 123 124 127 public void testEquals() { 128 129 CategoryPlot plot1 = new CategoryPlot(); 130 CategoryPlot plot2 = new CategoryPlot(); 131 assertTrue(plot1.equals(plot2)); 132 assertTrue(plot2.equals(plot1)); 133 134 plot1.setOrientation(PlotOrientation.HORIZONTAL); 136 assertFalse(plot1.equals(plot2)); 137 plot2.setOrientation(PlotOrientation.HORIZONTAL); 138 assertTrue(plot1.equals(plot2)); 139 140 plot1.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05)); 142 assertFalse(plot1.equals(plot2)); 143 plot2.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05)); 144 assertTrue(plot1.equals(plot2)); 145 146 plot1.setDomainAxis(new CategoryAxis("Category Axis")); 148 assertFalse(plot1.equals(plot2)); 149 plot2.setDomainAxis(new CategoryAxis("Category Axis")); 150 assertTrue(plot1.equals(plot2)); 151 152 plot1.setDomainAxis(11, new CategoryAxis("Secondary Axis")); 154 assertFalse(plot1.equals(plot2)); 155 plot2.setDomainAxis(11, new CategoryAxis("Secondary Axis")); 156 assertTrue(plot1.equals(plot2)); 157 158 plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); 160 assertFalse(plot1.equals(plot2)); 161 plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); 162 assertTrue(plot1.equals(plot2)); 163 164 plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT); 166 assertFalse(plot1.equals(plot2)); 167 plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT); 168 assertTrue(plot1.equals(plot2)); 169 170 plot1.setDrawSharedDomainAxis(!plot1.getDrawSharedDomainAxis()); 172 assertFalse(plot1.equals(plot2)); 173 plot2.setDrawSharedDomainAxis(!plot2.getDrawSharedDomainAxis()); 174 assertTrue(plot1.equals(plot2)); 175 176 plot1.setRangeAxis(new NumberAxis("Range Axis")); 178 assertFalse(plot1.equals(plot2)); 179 plot2.setRangeAxis(new NumberAxis("Range Axis")); 180 assertTrue(plot1.equals(plot2)); 181 182 plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis")); 184 assertFalse(plot1.equals(plot2)); 185 plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis")); 186 assertTrue(plot1.equals(plot2)); 187 188 plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT); 190 assertFalse(plot1.equals(plot2)); 191 plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT); 192 assertTrue(plot1.equals(plot2)); 193 194 plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT); 196 assertFalse(plot1.equals(plot2)); 197 plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT); 198 assertTrue(plot1.equals(plot2)); 199 200 plot1.mapDatasetToDomainAxis(11, 11); 202 assertFalse(plot1.equals(plot2)); 203 plot2.mapDatasetToDomainAxis(11, 11); 204 assertTrue(plot1.equals(plot2)); 205 206 plot1.mapDatasetToRangeAxis(11, 11); 208 assertFalse(plot1.equals(plot2)); 209 plot2.mapDatasetToRangeAxis(11, 11); 210 assertTrue(plot1.equals(plot2)); 211 212 plot1.setRenderer(new AreaRenderer()); 214 assertFalse(plot1.equals(plot2)); 215 plot2.setRenderer(new AreaRenderer()); 216 assertTrue(plot1.equals(plot2)); 217 218 plot1.setRenderer(11, new AreaRenderer()); 220 assertFalse(plot1.equals(plot2)); 221 plot2.setRenderer(11, new AreaRenderer()); 222 assertTrue(plot1.equals(plot2)); 223 224 plot1.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); 226 assertFalse(plot1.equals(plot2)); 227 plot2.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); 228 assertTrue(plot1.equals(plot2)); 229 230 plot1.setColumnRenderingOrder(SortOrder.DESCENDING); 232 assertFalse(plot1.equals(plot2)); 233 plot2.setColumnRenderingOrder(SortOrder.DESCENDING); 234 assertTrue(plot1.equals(plot2)); 235 236 plot1.setRowRenderingOrder(SortOrder.DESCENDING); 238 assertFalse(plot1.equals(plot2)); 239 plot2.setRowRenderingOrder(SortOrder.DESCENDING); 240 assertTrue(plot1.equals(plot2)); 241 242 plot1.setDomainGridlinesVisible(true); 244 assertFalse(plot1.equals(plot2)); 245 plot2.setDomainGridlinesVisible(true); 246 assertTrue(plot1.equals(plot2)); 247 248 plot1.setDomainGridlinePosition(CategoryAnchor.END); 250 assertFalse(plot1.equals(plot2)); 251 plot2.setDomainGridlinePosition(CategoryAnchor.END); 252 assertTrue(plot1.equals(plot2)); 253 254 Stroke stroke = new BasicStroke (2.0f); 256 plot1.setDomainGridlineStroke(stroke); 257 assertFalse(plot1.equals(plot2)); 258 plot2.setDomainGridlineStroke(stroke); 259 assertTrue(plot1.equals(plot2)); 260 261 plot1.setDomainGridlinePaint(Color.blue); 263 assertFalse(plot1.equals(plot2)); 264 plot2.setDomainGridlinePaint(Color.blue); 265 assertTrue(plot1.equals(plot2)); 266 267 plot1.setRangeGridlinesVisible(false); 269 assertFalse(plot1.equals(plot2)); 270 plot2.setRangeGridlinesVisible(false); 271 assertTrue(plot1.equals(plot2)); 272 273 plot1.setRangeGridlineStroke(stroke); 275 assertFalse(plot1.equals(plot2)); 276 plot2.setRangeGridlineStroke(stroke); 277 assertTrue(plot1.equals(plot2)); 278 279 plot1.setRangeGridlinePaint(Color.blue); 281 assertFalse(plot1.equals(plot2)); 282 plot2.setRangeGridlinePaint(Color.blue); 283 assertTrue(plot1.equals(plot2)); 284 285 plot1.setAnchorValue(100.0); 287 assertFalse(plot1.equals(plot2)); 288 plot2.setAnchorValue(100.0); 289 assertTrue(plot1.equals(plot2)); 290 291 plot1.setRangeCrosshairVisible(true); 293 assertFalse(plot1.equals(plot2)); 294 plot2.setRangeCrosshairVisible(true); 295 assertTrue(plot1.equals(plot2)); 296 297 plot1.setRangeCrosshairValue(100.0); 299 assertFalse(plot1.equals(plot2)); 300 plot2.setRangeCrosshairValue(100.0); 301 assertTrue(plot1.equals(plot2)); 302 303 plot1.setRangeCrosshairStroke(stroke); 305 assertFalse(plot1.equals(plot2)); 306 plot2.setRangeCrosshairStroke(stroke); 307 assertTrue(plot1.equals(plot2)); 308 309 plot1.setRangeCrosshairPaint(Color.blue); 311 assertFalse(plot1.equals(plot2)); 312 plot2.setRangeCrosshairPaint(Color.blue); 313 assertTrue(plot1.equals(plot2)); 314 315 plot1.setRangeCrosshairLockedOnData(false); 317 assertFalse(plot1.equals(plot2)); 318 plot2.setRangeCrosshairLockedOnData(false); 319 assertTrue(plot1.equals(plot2)); 320 321 plot1.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND); 323 assertFalse(plot1.equals(plot2)); 324 plot2.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND); 325 assertTrue(plot1.equals(plot2)); 326 327 plot1.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND); 328 assertFalse(plot1.equals(plot2)); 329 plot2.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND); 330 assertTrue(plot1.equals(plot2)); 331 332 plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND); 334 assertFalse(plot1.equals(plot2)); 335 plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND); 336 assertTrue(plot1.equals(plot2)); 337 338 plot1.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND); 340 assertFalse(plot1.equals(plot2)); 341 plot2.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND); 342 assertTrue(plot1.equals(plot2)); 343 344 plot1.addAnnotation( 346 new CategoryTextAnnotation("Text", "Category", 43.0) 347 ); 348 assertFalse(plot1.equals(plot2)); 349 plot2.addAnnotation( 350 new CategoryTextAnnotation("Text", "Category", 43.0) 351 ); 352 assertTrue(plot1.equals(plot2)); 353 354 plot1.setWeight(3); 356 assertFalse(plot1.equals(plot2)); 357 plot2.setWeight(3); 358 assertTrue(plot1.equals(plot2)); 359 360 plot1.setFixedDomainAxisSpace(new AxisSpace()); 362 assertFalse(plot1.equals(plot2)); 363 plot2.setFixedDomainAxisSpace(new AxisSpace()); 364 assertTrue(plot1.equals(plot2)); 365 366 plot1.setFixedRangeAxisSpace(new AxisSpace()); 368 assertFalse(plot1.equals(plot2)); 369 plot2.setFixedRangeAxisSpace(new AxisSpace()); 370 assertTrue(plot1.equals(plot2)); 371 372 373 } 374 375 378 public void testCloning() { 379 CategoryPlot p1 = new CategoryPlot(); 380 CategoryPlot p2 = null; 381 try { 382 p2 = (CategoryPlot) p1.clone(); 383 } 384 catch (CloneNotSupportedException e) { 385 e.printStackTrace(); 386 System.err.println("Failed to clone."); 387 } 388 assertTrue(p1 != p2); 389 assertTrue(p1.getClass() == p2.getClass()); 390 assertTrue(p1.equals(p2)); 391 } 392 393 396 public void testSerialization() { 397 398 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 399 CategoryAxis domainAxis = new CategoryAxis("Domain"); 400 NumberAxis rangeAxis = new NumberAxis("Range"); 401 BarRenderer renderer = new BarRenderer(); 402 CategoryPlot p1 = new CategoryPlot( 403 dataset, domainAxis, rangeAxis, renderer 404 ); 405 p1.setOrientation(PlotOrientation.HORIZONTAL); 406 CategoryPlot p2 = null; 407 try { 408 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 409 ObjectOutput out = new ObjectOutputStream (buffer); 410 out.writeObject(p1); 411 out.close(); 412 413 ObjectInput in = new ObjectInputStream ( 414 new ByteArrayInputStream (buffer.toByteArray()) 415 ); 416 p2 = (CategoryPlot) in.readObject(); 417 in.close(); 418 } 419 catch (Exception e) { 420 e.printStackTrace(); 421 } 422 assertTrue(p1.equals(p2)); 423 } 424 425 428 public void testSerialization2() { 429 430 DefaultCategoryDataset data = new DefaultCategoryDataset(); 431 CategoryAxis domainAxis = new CategoryAxis("Domain"); 432 NumberAxis rangeAxis = new NumberAxis("Range"); 433 BarRenderer renderer = new BarRenderer(); 434 CategoryPlot p1 = new CategoryPlot( 435 data, domainAxis, rangeAxis, renderer 436 ); 437 p1.setOrientation(PlotOrientation.VERTICAL); 438 CategoryPlot p2 = null; 439 440 try { 441 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 442 ObjectOutput out = new ObjectOutputStream (buffer); 443 out.writeObject(p1); 444 out.close(); 445 446 ObjectInput in = new ObjectInputStream ( 447 new ByteArrayInputStream (buffer.toByteArray()) 448 ); 449 p2 = (CategoryPlot) in.readObject(); 450 in.close(); 451 } 452 catch (Exception e) { 453 fail(e.toString()); 454 } 455 assertEquals(p1, p2); 456 457 } 458 459 462 public void testSerialization3() { 463 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 464 JFreeChart chart = ChartFactory.createBarChart( 465 "Test Chart", 466 "Category Axis", 467 "Value Axis", 468 dataset, 469 PlotOrientation.VERTICAL, 470 true, 471 true, 472 false 473 ); 474 JFreeChart chart2 = null; 475 476 try { 478 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 479 ObjectOutput out = new ObjectOutputStream (buffer); 480 out.writeObject(chart); 481 out.close(); 482 483 ObjectInput in = new ObjectInputStream ( 484 new ByteArrayInputStream (buffer.toByteArray()) 485 ); 486 chart2 = (JFreeChart) in.readObject(); 487 in.close(); 488 } 489 catch (Exception e) { 490 fail(e.toString()); 491 } 492 493 boolean passed = true; 495 try { 496 chart2.createBufferedImage(300, 200); 497 } 498 catch (Exception e) { 499 passed = false; 500 e.printStackTrace(); 501 } 502 assertTrue(passed); 503 } 504 505 508 public void testSerialization4() { 509 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 510 JFreeChart chart = ChartFactory.createBarChart( 511 "Test Chart", 512 "Category Axis", 513 "Value Axis", 514 dataset, 515 PlotOrientation.VERTICAL, 516 true, 517 true, 518 false 519 ); 520 CategoryPlot plot = (CategoryPlot) chart.getPlot(); 521 plot.addRangeMarker(new ValueMarker(1.1), Layer.FOREGROUND); 522 plot.addRangeMarker(new IntervalMarker(2.2, 3.3), Layer.BACKGROUND); 523 JFreeChart chart2 = null; 524 525 try { 527 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 528 ObjectOutput out = new ObjectOutputStream (buffer); 529 out.writeObject(chart); 530 out.close(); 531 532 ObjectInput in = new ObjectInputStream ( 533 new ByteArrayInputStream (buffer.toByteArray()) 534 ); 535 chart2 = (JFreeChart) in.readObject(); 536 in.close(); 537 } 538 catch (Exception e) { 539 fail(e.toString()); 540 } 541 assertEquals(chart, chart2); 542 543 boolean passed = true; 545 try { 546 chart2.createBufferedImage(300, 200); 547 } 548 catch (Exception e) { 549 passed = false; 550 e.printStackTrace(); 551 } 552 assertTrue(passed); 553 } 554 555 560 public void testSerialization5() { 561 DefaultCategoryDataset dataset1 = new DefaultCategoryDataset(); 562 CategoryAxis domainAxis1 = new CategoryAxis("Domain 1"); 563 NumberAxis rangeAxis1 = new NumberAxis("Range 1"); 564 BarRenderer renderer1 = new BarRenderer(); 565 CategoryPlot p1 = new CategoryPlot(dataset1, domainAxis1, rangeAxis1, 566 renderer1); 567 CategoryAxis domainAxis2 = new CategoryAxis("Domain 2"); 568 NumberAxis rangeAxis2 = new NumberAxis("Range 2"); 569 BarRenderer renderer2 = new BarRenderer(); 570 DefaultCategoryDataset dataset2 = new DefaultCategoryDataset(); 571 p1.setDataset(1, dataset2); 572 p1.setDomainAxis(1, domainAxis2); 573 p1.setRangeAxis(1, rangeAxis2); 574 p1.setRenderer(1, renderer2); 575 CategoryPlot p2 = null; 576 try { 577 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 578 ObjectOutput out = new ObjectOutputStream (buffer); 579 out.writeObject(p1); 580 out.close(); 581 ObjectInput in = new ObjectInputStream ( 582 new ByteArrayInputStream (buffer.toByteArray()) 583 ); 584 p2 = (CategoryPlot) in.readObject(); 585 in.close(); 586 } 587 catch (Exception e) { 588 fail(e.toString()); 589 } 590 assertEquals(p1, p2); 591 592 CategoryAxis domainAxisA = p2.getDomainAxis(0); 595 NumberAxis rangeAxisA = (NumberAxis) p2.getRangeAxis(0); 596 DefaultCategoryDataset datasetA 597 = (DefaultCategoryDataset) p2.getDataset(0); 598 BarRenderer rendererA = (BarRenderer) p2.getRenderer(0); 599 CategoryAxis domainAxisB = p2.getDomainAxis(1); 600 NumberAxis rangeAxisB = (NumberAxis) p2.getRangeAxis(1); 601 DefaultCategoryDataset datasetB 602 = (DefaultCategoryDataset) p2.getDataset(1); 603 BarRenderer rendererB = (BarRenderer) p2.getRenderer(1); 604 assertTrue(datasetA.hasListener(p2)); 605 assertTrue(domainAxisA.hasListener(p2)); 606 assertTrue(rangeAxisA.hasListener(p2)); 607 assertTrue(rendererA.hasListener(p2)); 608 assertTrue(datasetB.hasListener(p2)); 609 assertTrue(domainAxisB.hasListener(p2)); 610 assertTrue(rangeAxisB.hasListener(p2)); 611 assertTrue(rendererB.hasListener(p2)); 612 } 613 614 618 public void testSetRenderer() { 619 CategoryPlot plot = new CategoryPlot(); 620 CategoryItemRenderer renderer = new LineAndShapeRenderer(); 621 plot.setRenderer(renderer); 622 MyPlotChangeListener listener = new MyPlotChangeListener(); 625 plot.addChangeListener(listener); 626 renderer.setSeriesPaint(0, Color.black); 627 assertTrue(listener.getEvent() != null); 628 } 629 630 633 public void test1169972() { 634 CategoryPlot plot = new CategoryPlot(null, null, null, null); 635 plot.setDomainAxis(new CategoryAxis("C")); 636 plot.setRangeAxis(new NumberAxis("Y")); 637 plot.setRenderer(new BarRenderer()); 638 plot.setDataset(new DefaultCategoryDataset()); 639 assertTrue(plot != null); 640 } 641 642 } 643 | Popular Tags |