001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * PieChart3DTests.java
029: * --------------------
030: * (C) Copyright 2004, 2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: PieChart3DTests.java,v 1.1.2.2 2007/03/02 11:16:21 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 21-May-2004 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.junit;
044:
045: import java.awt.Graphics2D;
046: import java.awt.geom.Rectangle2D;
047: import java.awt.image.BufferedImage;
048:
049: import junit.framework.Test;
050: import junit.framework.TestCase;
051: import junit.framework.TestSuite;
052:
053: import org.jfree.chart.ChartFactory;
054: import org.jfree.chart.JFreeChart;
055: import org.jfree.chart.event.ChartChangeEvent;
056: import org.jfree.chart.event.ChartChangeListener;
057: import org.jfree.chart.plot.PiePlot;
058: import org.jfree.data.general.DefaultPieDataset;
059: import org.jfree.data.general.PieDataset;
060:
061: /**
062: * Tests for a pie chart with a 3D effect.
063: */
064: public class PieChart3DTests extends TestCase {
065:
066: /** A chart. */
067: private JFreeChart pieChart;
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(PieChart3DTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public PieChart3DTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Common test setup.
089: */
090: protected void setUp() {
091: // create a dataset...
092: DefaultPieDataset dataset = new DefaultPieDataset();
093: dataset.setValue("Java", new Double(43.2));
094: dataset.setValue("Visual Basic", new Double(0.0));
095: dataset.setValue("C/C++", new Double(17.5));
096: this .pieChart = createPieChart3D(dataset);
097: }
098:
099: /**
100: * Using a regular pie chart, we replace the dataset with null. Expect to
101: * receive notification of a chart change event, and (of course) the
102: * dataset should be null.
103: */
104: public void testReplaceDatasetOnPieChart() {
105: LocalListener l = new LocalListener();
106: this .pieChart.addChangeListener(l);
107: PiePlot plot = (PiePlot) this .pieChart.getPlot();
108: plot.setDataset(null);
109: assertEquals(true, l.flag);
110: assertNull(plot.getDataset());
111: }
112:
113: /**
114: * Tests that no exceptions are thrown when there is a <code>null</code>
115: * value in the dataset.
116: */
117: public void testNullValueInDataset() {
118: DefaultPieDataset dataset = new DefaultPieDataset();
119: dataset.setValue("Section 1", 10.0);
120: dataset.setValue("Section 2", 11.0);
121: dataset.setValue("Section 3", null);
122: JFreeChart chart = createPieChart3D(dataset);
123: boolean success = false;
124: try {
125: BufferedImage image = new BufferedImage(200, 100,
126: BufferedImage.TYPE_INT_RGB);
127: Graphics2D g2 = image.createGraphics();
128: chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
129: null, null);
130: g2.dispose();
131: success = true;
132: } catch (Throwable t) {
133: success = false;
134: }
135: assertTrue(success);
136: }
137:
138: /**
139: * Creates a pie chart.
140: *
141: * @param dataset the dataset.
142: *
143: * @return The pie chart.
144: */
145: private static JFreeChart createPieChart3D(PieDataset dataset) {
146:
147: return ChartFactory.createPieChart3D("Pie Chart", // chart title
148: dataset, // data
149: true, // include legend
150: true, false);
151: }
152:
153: /**
154: * A chart change listener.
155: */
156: static class LocalListener implements ChartChangeListener {
157:
158: /** A flag. */
159: private boolean flag = false;
160:
161: /**
162: * Event handler.
163: *
164: * @param event the event.
165: */
166: public void chartChanged(ChartChangeEvent event) {
167: this .flag = true;
168: }
169:
170: }
171:
172: }
|