1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------------
  25. * PieChart3DTests.java
  26. * --------------------
  27. * (C) Copyright 2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: PieChart3DTests.java,v 1.1 2004/08/31 14:35:34 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 21-May-2004 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.chart.junit;
  40. import java.awt.Graphics2D;
  41. import java.awt.geom.Rectangle2D;
  42. import java.awt.image.BufferedImage;
  43. import junit.framework.Test;
  44. import junit.framework.TestCase;
  45. import junit.framework.TestSuite;
  46. import org.jfree.chart.ChartFactory;
  47. import org.jfree.chart.JFreeChart;
  48. import org.jfree.chart.event.ChartChangeEvent;
  49. import org.jfree.chart.event.ChartChangeListener;
  50. import org.jfree.chart.plot.PiePlot;
  51. import org.jfree.data.general.DefaultPieDataset;
  52. import org.jfree.data.general.PieDataset;
  53. /**
  54. * Tests for a pie chart with a 3D effect.
  55. */
  56. public class PieChart3DTests extends TestCase {
  57. /** A chart. */
  58. private JFreeChart pieChart;
  59. /**
  60. * Returns the tests as a test suite.
  61. *
  62. * @return The test suite.
  63. */
  64. public static Test suite() {
  65. return new TestSuite(PieChart3DTests.class);
  66. }
  67. /**
  68. * Constructs a new set of tests.
  69. *
  70. * @param name the name of the tests.
  71. */
  72. public PieChart3DTests(String name) {
  73. super(name);
  74. }
  75. /**
  76. * Common test setup.
  77. */
  78. protected void setUp() {
  79. // create a dataset...
  80. DefaultPieDataset dataset = new DefaultPieDataset();
  81. dataset.setValue("Java", new Double(43.2));
  82. dataset.setValue("Visual Basic", new Double(0.0));
  83. dataset.setValue("C/C++", new Double(17.5));
  84. this.pieChart = createPieChart3D(dataset);
  85. }
  86. /**
  87. * Using a regular pie chart, we replace the dataset with null. Expect to receive notification
  88. * of a chart change event, and (of course) the dataset should be null.
  89. */
  90. public void testReplaceDatasetOnPieChart() {
  91. LocalListener l = new LocalListener();
  92. this.pieChart.addChangeListener(l);
  93. PiePlot plot = (PiePlot) this.pieChart.getPlot();
  94. plot.setDataset(null);
  95. assertEquals(true, l.flag);
  96. assertNull(plot.getDataset());
  97. }
  98. /**
  99. * Tests that no exceptions are thrown when there is a <code>null</code> value in the dataset.
  100. */
  101. public void testNullValueInDataset() {
  102. DefaultPieDataset dataset = new DefaultPieDataset();
  103. dataset.setValue("Section 1", 10.0);
  104. dataset.setValue("Section 2", 11.0);
  105. dataset.setValue("Section 3", null);
  106. JFreeChart chart = createPieChart3D(dataset);
  107. boolean success = false;
  108. try {
  109. BufferedImage image = new BufferedImage(200 , 100, BufferedImage.TYPE_INT_RGB);
  110. Graphics2D g2 = image.createGraphics();
  111. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  112. g2.dispose();
  113. success = true;
  114. }
  115. catch (Throwable t) {
  116. success = false;
  117. }
  118. assertTrue(success);
  119. }
  120. /**
  121. * Creates a pie chart.
  122. *
  123. * @param dataset the dataset.
  124. *
  125. * @return The pie chart.
  126. */
  127. private static JFreeChart createPieChart3D(PieDataset dataset) {
  128. return ChartFactory.createPieChart3D(
  129. "Pie Chart", // chart title
  130. dataset, // data
  131. true, // include legend
  132. true,
  133. false
  134. );
  135. }
  136. /**
  137. * A chart change listener.
  138. */
  139. static class LocalListener implements ChartChangeListener {
  140. /** A flag. */
  141. private boolean flag = false;
  142. /**
  143. * Event handler.
  144. *
  145. * @param event the event.
  146. */
  147. public void chartChanged(ChartChangeEvent event) {
  148. this.flag = true;
  149. }
  150. }
  151. }