KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TimeSeriesChartTests.java
29  * -------------------------
30  * (C) Copyright 2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: TimeSeriesChartTests.java,v 1.1.2.1 2006/10/03 15:41:29 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 12-Apr-2005 : 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.axis.ValueAxis;
56 import org.jfree.chart.event.ChartChangeEvent;
57 import org.jfree.chart.event.ChartChangeListener;
58 import org.jfree.chart.labels.StandardXYToolTipGenerator;
59 import org.jfree.chart.labels.XYToolTipGenerator;
60 import org.jfree.chart.plot.XYPlot;
61 import org.jfree.chart.renderer.xy.XYItemRenderer;
62 import org.jfree.data.Range;
63 import org.jfree.data.xy.XYDataset;
64 import org.jfree.data.xy.XYSeries;
65 import org.jfree.data.xy.XYSeriesCollection;
66
67 /**
68  * Some tests for a time series chart.
69  */

70 public class TimeSeriesChartTests extends TestCase {
71
72     /** A chart. */
73     private JFreeChart chart;
74
75     /**
76      * Returns the tests as a test suite.
77      *
78      * @return The test suite.
79      */

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

89     public TimeSeriesChartTests(String JavaDoc name) {
90         super(name);
91     }
92
93     /**
94      * Common test setup.
95      */

96     protected void setUp() {
97         this.chart = createChart();
98     }
99
100     /**
101      * Draws the chart with a null info object to make sure that no exceptions
102      * are thrown (a problem that was occurring at one point).
103      */

104     public void testDrawWithNullInfo() {
105
106         boolean success = false;
107         try {
108             BufferedImage JavaDoc image = new BufferedImage JavaDoc(
109                 200 , 100, BufferedImage.TYPE_INT_RGB
110             );
111             Graphics2D JavaDoc g2 = image.createGraphics();
112             this.chart.draw(
113                 g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null
114             );
115             g2.dispose();
116             success = true;
117         }
118         catch (Exception JavaDoc e) {
119           success = false;
120           e.printStackTrace();
121         }
122         assertTrue(success);
123
124     }
125
126     /**
127      * Replaces the dataset and checks that it has changed as expected.
128      */

129     public void testReplaceDataset() {
130
131         // create a dataset...
132
XYSeries series1 = new XYSeries("Series 1");
133         series1.add(10.0, 10.0);
134         series1.add(20.0, 20.0);
135         series1.add(30.0, 30.0);
136         XYDataset dataset = new XYSeriesCollection(series1);
137
138         LocalListener l = new LocalListener();
139         this.chart.addChangeListener(l);
140         this.chart.getXYPlot().setDataset(dataset);
141         assertEquals(true, l.flag);
142         ValueAxis axis = this.chart.getXYPlot().getRangeAxis();
143         Range range = axis.getRange();
144         assertTrue("Expecting the lower bound of the range to be around 10: "
145                    + range.getLowerBound(), range.getLowerBound() <= 10);
146         assertTrue("Expecting the upper bound of the range to be around 30: "
147                    + range.getUpperBound(), range.getUpperBound() >= 30);
148
149     }
150
151     /**
152      * Check that setting a tool tip generator for a series does override the
153      * default generator.
154      */

155     public void testSetSeriesToolTipGenerator() {
156         XYPlot plot = (XYPlot) this.chart.getPlot();
157         XYItemRenderer renderer = plot.getRenderer();
158         StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator();
159         renderer.setSeriesToolTipGenerator(0, tt);
160         XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
161         assertTrue(tt2 == tt);
162     }
163     
164     /**
165      * Create a horizontal bar chart with sample data in the range -3 to +3.
166      *
167      * @return The chart.
168      */

169     private static JFreeChart createChart() {
170
171         // create a dataset...
172
XYSeries series1 = new XYSeries("Series 1");
173         series1.add(1.0, 1.0);
174         series1.add(2.0, 2.0);
175         series1.add(3.0, 3.0);
176         XYDataset dataset = new XYSeriesCollection(series1);
177
178         // create the chart...
179
return ChartFactory.createTimeSeriesChart(
180             "XY Line Chart", // chart title
181
"Domain",
182             "Range",
183             dataset, // data
184
true, // include legend
185
true, // tooltips
186
true // urls
187
);
188
189     }
190
191     /**
192      * A chart change listener.
193      *
194      */

195     static class LocalListener implements ChartChangeListener {
196
197         /** A flag. */
198         private boolean flag = false;
199
200         /**
201          * Event handler.
202          *
203          * @param event the event.
204          */

205         public void chartChanged(ChartChangeEvent event) {
206             this.flag = true;
207         }
208
209     }
210
211 }
212
Popular Tags