PieChartTest.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -----------------
  28. * PieChartTest.java
  29. * -----------------
  30. * (C) Copyright 2002-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes:
  36. * --------
  37. * 11-Jun-2002 : Version 1 (DG);
  38. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39. *
  40. */
  41. package org.jfree.chart;
  42. import org.jfree.chart.event.ChartChangeEvent;
  43. import org.jfree.chart.event.ChartChangeListener;
  44. import org.jfree.chart.plot.PiePlot;
  45. import org.jfree.data.general.DefaultPieDataset;
  46. import org.junit.Before;
  47. import org.junit.Test;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertNull;
  50. /**
  51. * Tests for a pie chart.
  52. *
  53. */
  54. public class PieChartTest {
  55. /** A chart. */
  56. private JFreeChart pieChart;
  57. /**
  58. * Common test setup.
  59. */
  60. @Before
  61. public void setUp() {
  62. this.pieChart = createPieChart();
  63. }
  64. /**
  65. * Using a regular pie chart, we replace the dataset with null. Expect to
  66. * receive notification of a chart change event, and (of course) the
  67. * dataset should be null.
  68. */
  69. @Test
  70. public void testReplaceDatasetOnPieChart() {
  71. LocalListener l = new LocalListener();
  72. this.pieChart.addChangeListener(l);
  73. PiePlot plot = (PiePlot) this.pieChart.getPlot();
  74. plot.setDataset(null);
  75. assertEquals(true, l.flag);
  76. assertNull(plot.getDataset());
  77. }
  78. /**
  79. * Creates a pie chart.
  80. *
  81. * @return The pie chart.
  82. */
  83. private static JFreeChart createPieChart() {
  84. DefaultPieDataset data = new DefaultPieDataset();
  85. data.setValue("Java", new Double(43.2));
  86. data.setValue("Visual Basic", new Double(0.0));
  87. data.setValue("C/C++", new Double(17.5));
  88. return ChartFactory.createPieChart("Pie Chart", data);
  89. }
  90. /**
  91. * A chart change listener.
  92. *
  93. */
  94. static class LocalListener implements ChartChangeListener {
  95. /** A flag. */
  96. private boolean flag;
  97. /**
  98. * Event handler.
  99. *
  100. * @param event the event.
  101. */
  102. @Override
  103. public void chartChanged(ChartChangeEvent event) {
  104. this.flag = true;
  105. }
  106. }
  107. }