KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYAreaChartTests.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: XYAreaChartTests.java,v 1.1.2.1 2006/10/03 15:41:32 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.PlotOrientation;
61 import org.jfree.chart.plot.XYPlot;
62 import org.jfree.chart.renderer.xy.XYItemRenderer;
63 import org.jfree.data.Range;
64 import org.jfree.data.xy.XYDataset;
65 import org.jfree.data.xy.XYSeries;
66 import org.jfree.data.xy.XYSeriesCollection;
67
68 /**
69  * Some tests for an XY area chart.
70  */

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

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

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

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

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

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

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

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

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

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