KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > junit > PieChart3DTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------
28  * PieChart3DTests.java
29  * --------------------
30  * (C) Copyright 2004, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: PieChart3DTests.java,v 1.1.2.1 2006/10/03 15:41:33 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 21-May-2004 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.junit;
44
45 import java.awt.Graphics2D JavaDoc;
46 import java.awt.geom.Rectangle2D JavaDoc;
47 import java.awt.image.BufferedImage JavaDoc;
48
49 import junit.framework.Test;
50 import junit.framework.TestCase;
51 import junit.framework.TestSuite;
52
53 import org.jfree.chart.ChartFactory;
54 import org.jfree.chart.JFreeChart;
55 import org.jfree.chart.event.ChartChangeEvent;
56 import org.jfree.chart.event.ChartChangeListener;
57 import org.jfree.chart.plot.PiePlot;
58 import org.jfree.data.general.DefaultPieDataset;
59 import org.jfree.data.general.PieDataset;
60
61 /**
62  * Tests for a pie chart with a 3D effect.
63  */

64 public class PieChart3DTests extends TestCase {
65
66     /** A chart. */
67     private JFreeChart pieChart;
68
69     /**
70      * Returns the tests as a test suite.
71      *
72      * @return The test suite.
73      */

74     public static Test suite() {
75         return new TestSuite(PieChart3DTests.class);
76     }
77
78     /**
79      * Constructs a new set of tests.
80      *
81      * @param name the name of the tests.
82      */

83     public PieChart3DTests(String JavaDoc name) {
84         super(name);
85     }
86
87     /**
88      * Common test setup.
89      */

90     protected void setUp() {
91         // create a dataset...
92
DefaultPieDataset dataset = new DefaultPieDataset();
93         dataset.setValue("Java", new Double JavaDoc(43.2));
94         dataset.setValue("Visual Basic", new Double JavaDoc(0.0));
95         dataset.setValue("C/C++", new Double JavaDoc(17.5));
96         this.pieChart = createPieChart3D(dataset);
97     }
98
99     /**
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 JavaDoc image = new BufferedImage JavaDoc(
126                 200 , 100, BufferedImage.TYPE_INT_RGB
127             );
128             Graphics2D JavaDoc g2 = image.createGraphics();
129             chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null);
130             g2.dispose();
131             success = true;
132         }
133         catch (Throwable JavaDoc t) {
134             success = false;
135         }
136         assertTrue(success);
137     }
138     
139     /**
140      * Creates a pie chart.
141      *
142      * @param dataset the dataset.
143      *
144      * @return The pie chart.
145      */

146     private static JFreeChart createPieChart3D(PieDataset dataset) {
147
148         return ChartFactory.createPieChart3D(
149             "Pie Chart", // chart title
150
dataset, // data
151
true, // include legend
152
true,
153             false
154         );
155     }
156
157     /**
158      * A chart change listener.
159      */

160     static class LocalListener implements ChartChangeListener {
161
162         /** A flag. */
163         private boolean flag = false;
164
165         /**
166          * Event handler.
167          *
168          * @param event the event.
169          */

170         public void chartChanged(ChartChangeEvent event) {
171             this.flag = true;
172         }
173
174     }
175
176 }
177
Popular Tags