KickJava   Java API By Example, From Geeks To Geeks.

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


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ------------------
27  * PieChartTests.java
28  * ------------------
29  * (C) Copyright 2002-2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: PieChartTests.java,v 1.3 2005/03/29 12:54:54 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 11-Jun-2002 : Version 1 (DG);
39  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  *
41  */

42
43 package org.jfree.chart.junit;
44
45 import junit.framework.Test;
46 import junit.framework.TestCase;
47 import junit.framework.TestSuite;
48
49 import org.jfree.chart.ChartFactory;
50 import org.jfree.chart.JFreeChart;
51 import org.jfree.chart.event.ChartChangeEvent;
52 import org.jfree.chart.event.ChartChangeListener;
53 import org.jfree.chart.plot.PiePlot;
54 import org.jfree.data.general.DefaultPieDataset;
55
56 /**
57  * Tests for a pie chart.
58  *
59  */

60 public class PieChartTests extends TestCase {
61
62     /** A chart. */
63     private JFreeChart pieChart;
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(PieChartTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public PieChartTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Common test setup.
85      */

86     protected void setUp() {
87
88         this.pieChart = createPieChart();
89
90     }
91
92     /**
93      * Using a regular pie chart, we replace the dataset with null. Expect to
94      * receive notification of a chart change event, and (of course) the
95      * dataset should be null.
96      */

97     public void testReplaceDatasetOnPieChart() {
98
99         LocalListener l = new LocalListener();
100         this.pieChart.addChangeListener(l);
101         PiePlot plot = (PiePlot) this.pieChart.getPlot();
102         plot.setDataset(null);
103         assertEquals(true, l.flag);
104         assertNull(plot.getDataset());
105
106     }
107
108     /**
109      * Creates a pie chart.
110      *
111      * @return The pie chart.
112      */

113     private static JFreeChart createPieChart() {
114         // create a dataset...
115
DefaultPieDataset data = new DefaultPieDataset();
116         data.setValue("Java", new Double JavaDoc(43.2));
117         data.setValue("Visual Basic", new Double JavaDoc(0.0));
118         data.setValue("C/C++", new Double JavaDoc(17.5));
119
120         // create the chart...
121
return ChartFactory.createPieChart("Pie Chart", // chart title
122
data, // data
123
true, // include legend
124
true,
125                                            false
126                                            );
127     }
128
129     /**
130      * A chart change listener.
131      *
132      */

133     static class LocalListener implements ChartChangeListener {
134
135         /** A flag. */
136         private boolean flag = false;
137
138         /**
139          * Event handler.
140          *
141          * @param event the event.
142          */

143         public void chartChanged(ChartChangeEvent event) {
144             this.flag = true;
145         }
146
147     }
148
149 }
150
Popular Tags