PieChart3DTest.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * PieChart3DTest.java
  29. * -------------------
  30. * (C) Copyright 2004-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes:
  36. * --------
  37. * 21-May-2004 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.chart;
  41. import java.awt.Graphics2D;
  42. import java.awt.geom.Rectangle2D;
  43. import java.awt.image.BufferedImage;
  44. import org.jfree.chart.event.ChartChangeEvent;
  45. import org.jfree.chart.event.ChartChangeListener;
  46. import org.jfree.chart.plot.PiePlot;
  47. import org.jfree.data.general.DefaultPieDataset;
  48. import org.jfree.data.general.PieDataset;
  49. import org.junit.Before;
  50. import org.junit.Test;
  51. import static org.junit.Assert.assertNull;
  52. import static org.junit.Assert.assertEquals;
  53. /**
  54. * Tests for a pie chart with a 3D effect.
  55. */
  56. public class PieChart3DTest {
  57. /** A chart. */
  58. private JFreeChart pieChart;
  59. /**
  60. * Common test setup.
  61. */
  62. @Before
  63. public void setUp() {
  64. // create a dataset...
  65. DefaultPieDataset dataset = new DefaultPieDataset();
  66. dataset.setValue("Java", new Double(43.2));
  67. dataset.setValue("Visual Basic", new Double(0.0));
  68. dataset.setValue("C/C++", new Double(17.5));
  69. this.pieChart = createPieChart3D(dataset);
  70. }
  71. /**
  72. * Using a regular pie chart, we replace the dataset with null. Expect to
  73. * receive notification of a chart change event, and (of course) the
  74. * dataset should be null.
  75. */
  76. @Test
  77. public void testReplaceDatasetOnPieChart() {
  78. LocalListener l = new LocalListener();
  79. this.pieChart.addChangeListener(l);
  80. PiePlot plot = (PiePlot) this.pieChart.getPlot();
  81. plot.setDataset(null);
  82. assertEquals(true, l.flag);
  83. assertNull(plot.getDataset());
  84. }
  85. /**
  86. * Tests that no exceptions are thrown when there is a <code>null</code>
  87. * value in the dataset.
  88. */
  89. @Test
  90. public void testNullValueInDataset() {
  91. DefaultPieDataset dataset = new DefaultPieDataset();
  92. dataset.setValue("Section 1", 10.0);
  93. dataset.setValue("Section 2", 11.0);
  94. dataset.setValue("Section 3", null);
  95. JFreeChart chart = createPieChart3D(dataset);
  96. BufferedImage image = new BufferedImage(200 , 100,
  97. BufferedImage.TYPE_INT_RGB);
  98. Graphics2D g2 = image.createGraphics();
  99. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  100. g2.dispose();
  101. //FIXME we should really assert a value here
  102. }
  103. /**
  104. * Creates a pie chart.
  105. *
  106. * @param dataset the dataset.
  107. *
  108. * @return The pie chart.
  109. */
  110. private static JFreeChart createPieChart3D(PieDataset dataset) {
  111. return ChartFactory.createPieChart3D("Pie Chart", dataset);
  112. }
  113. /**
  114. * A chart change listener.
  115. */
  116. static class LocalListener implements ChartChangeListener {
  117. /** A flag. */
  118. private boolean flag;
  119. /**
  120. * Event handler.
  121. *
  122. * @param event the event.
  123. */
  124. @Override
  125. public void chartChanged(ChartChangeEvent event) {
  126. this.flag = true;
  127. }
  128. }
  129. }