KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JFreeChartTests.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: JFreeChartTests.java,v 1.4 2005/04/10 06:30:20 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 11-Jun-2002 : Version 1 (DG);
39  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 23-Sep-2003 : Removed null title test, since TM has added code to ensure
41  * null titles cannot be created (DG);
42  *
43  */

44
45 package org.jfree.chart.junit;
46
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectInputStream JavaDoc;
51 import java.io.ObjectOutput JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.chart.ChartFactory;
59 import org.jfree.chart.JFreeChart;
60 import org.jfree.chart.plot.PlotOrientation;
61 import org.jfree.data.category.DefaultCategoryDataset;
62 import org.jfree.data.general.DefaultPieDataset;
63 import org.jfree.data.time.Day;
64 import org.jfree.data.time.RegularTimePeriod;
65 import org.jfree.data.time.TimeSeries;
66 import org.jfree.data.time.TimeSeriesCollection;
67
68 /**
69  * Tests for the {@link JFreeChart} class.
70  *
71  */

72 public class JFreeChartTests extends TestCase {
73
74     /** A pie chart. */
75     private JFreeChart pieChart;
76
77     /**
78      * Returns the tests as a test suite.
79      *
80      * @return The test suite.
81      */

82     public static Test suite() {
83         return new TestSuite(JFreeChartTests.class);
84     }
85
86     /**
87      * Constructs a new set of tests.
88      *
89      * @param name the name of the tests.
90      */

91     public JFreeChartTests(String JavaDoc name) {
92         super(name);
93     }
94
95     /**
96      * Common test setup.
97      */

98     protected void setUp() {
99
100         // create a dataset...
101
DefaultPieDataset data = new DefaultPieDataset();
102         data.setValue("Java", new Double JavaDoc(43.2));
103         data.setValue("Visual Basic", new Double JavaDoc(0.0));
104         data.setValue("C/C++", new Double JavaDoc(17.5));
105
106         // create the chart...
107
this.pieChart = ChartFactory.createPieChart(
108             "Pie Chart", // chart title
109
data, // data
110
true, // include legend
111
true,
112             false
113         );
114
115     }
116
117     /**
118      * Checks the subtitle count - should be 1 (the legend).
119      */

120     public void testSubtitleCount() {
121         int count = this.pieChart.getSubtitleCount();
122         assertEquals(1, count);
123     }
124     
125     /**
126      * Serialize a pie chart, restore it, and check for equality.
127      */

128     public void testSerialization1() {
129
130         DefaultPieDataset data = new DefaultPieDataset();
131         data.setValue("Type 1", 54.5);
132         data.setValue("Type 2", 23.9);
133         data.setValue("Type 3", 45.8);
134
135         JFreeChart c1 = ChartFactory.createPieChart(
136             "Test", data, true, true, true
137         );
138         JFreeChart c2 = null;
139
140         try {
141             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
142             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
143             out.writeObject(c1);
144             out.close();
145
146             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
147                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
148             );
149             c2 = (JFreeChart) in.readObject();
150             in.close();
151         }
152         catch (Exception JavaDoc e) {
153             e.printStackTrace();
154         }
155         assertEquals(c1, c2);
156
157     }
158
159     /**
160      * Serialize a 3D pie chart, restore it, and check for equality.
161      */

162     public void testSerialization2() {
163
164         DefaultPieDataset data = new DefaultPieDataset();
165         data.setValue("Type 1", 54.5);
166         data.setValue("Type 2", 23.9);
167         data.setValue("Type 3", 45.8);
168
169         JFreeChart c1 = ChartFactory.createPieChart3D(
170             "Test", data, true, true, true
171         );
172         JFreeChart c2 = null;
173
174         try {
175             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
176             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
177             out.writeObject(c1);
178             out.close();
179
180             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
181                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
182             );
183             c2 = (JFreeChart) in.readObject();
184             in.close();
185         }
186         catch (Exception JavaDoc e) {
187             e.printStackTrace();
188         }
189         assertEquals(c1, c2);
190
191     }
192
193     /**
194      * Serialize a bar chart, restore it, and check for equality.
195      */

196     public void testSerialization3() {
197
198         // row keys...
199
String JavaDoc series1 = "First";
200         String JavaDoc series2 = "Second";
201         String JavaDoc series3 = "Third";
202
203         // column keys...
204
String JavaDoc category1 = "Category 1";
205         String JavaDoc category2 = "Category 2";
206         String JavaDoc category3 = "Category 3";
207         String JavaDoc category4 = "Category 4";
208         String JavaDoc category5 = "Category 5";
209         String JavaDoc category6 = "Category 6";
210         String JavaDoc category7 = "Category 7";
211         String JavaDoc category8 = "Category 8";
212
213         // create the dataset...
214
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
215
216         dataset.addValue(1.0, series1, category1);
217         dataset.addValue(4.0, series1, category2);
218         dataset.addValue(3.0, series1, category3);
219         dataset.addValue(5.0, series1, category4);
220         dataset.addValue(5.0, series1, category5);
221         dataset.addValue(7.0, series1, category6);
222         dataset.addValue(7.0, series1, category7);
223         dataset.addValue(8.0, series1, category8);
224
225         dataset.addValue(5.0, series2, category1);
226         dataset.addValue(7.0, series2, category2);
227         dataset.addValue(6.0, series2, category3);
228         dataset.addValue(8.0, series2, category4);
229         dataset.addValue(4.0, series2, category5);
230         dataset.addValue(4.0, series2, category6);
231         dataset.addValue(2.0, series2, category7);
232         dataset.addValue(1.0, series2, category8);
233
234         dataset.addValue(4.0, series3, category1);
235         dataset.addValue(3.0, series3, category2);
236         dataset.addValue(2.0, series3, category3);
237         dataset.addValue(3.0, series3, category4);
238         dataset.addValue(6.0, series3, category5);
239         dataset.addValue(3.0, series3, category6);
240         dataset.addValue(4.0, series3, category7);
241         dataset.addValue(3.0, series3, category8);
242
243         // create the chart...
244
JFreeChart c1 = ChartFactory.createBarChart(
245             "Vertical Bar Chart", // chart title
246
"Category", // domain axis label
247
"Value", // range axis label
248
dataset, // data
249
PlotOrientation.VERTICAL, // orientation
250
true, // include legend
251
true,
252             false
253         );
254
255         JFreeChart c2 = null;
256
257         try {
258             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
259             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
260             out.writeObject(c1);
261             out.close();
262
263             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
264                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
265             );
266             c2 = (JFreeChart) in.readObject();
267             in.close();
268         }
269         catch (Exception JavaDoc e) {
270             e.printStackTrace();
271         }
272         assertEquals(c1, c2);
273
274     }
275     
276     /**
277      * Serialize a time seroes chart, restore it, and check for equality.
278      */

279     public void testSerialization4() {
280
281         RegularTimePeriod t = new Day();
282         TimeSeries series = new TimeSeries("Series 1");
283         series.add(t, 36.4);
284         t = t.next();
285         series.add(t, 63.5);
286         TimeSeriesCollection dataset = new TimeSeriesCollection();
287         dataset.addSeries(series);
288
289         JFreeChart c1 = ChartFactory.createTimeSeriesChart(
290             "Test", "Date", "Value", dataset, true, true, true
291         );
292         JFreeChart c2 = null;
293
294         try {
295             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
296             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
297             out.writeObject(c1);
298             out.close();
299
300             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
301                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
302             );
303             c2 = (JFreeChart) in.readObject();
304             in.close();
305         }
306         catch (Exception JavaDoc e) {
307             e.printStackTrace();
308         }
309         assertEquals(c1, c2);
310
311     }
312
313
314 }
315
Popular Tags