KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYBarChartTests.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: XYBarChartTests.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.IntervalXYDataset;
65 import org.jfree.data.xy.XYBarDataset;
66 import org.jfree.data.xy.XYDataset;
67 import org.jfree.data.xy.XYSeries;
68 import org.jfree.data.xy.XYSeriesCollection;
69
70 /**
71  * Some tests for an XY bar chart.
72  */

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

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

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

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

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

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

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

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

201     static class LocalListener implements ChartChangeListener {
202
203         /** A flag. */
204         private boolean flag = false;
205
206         /**
207          * Event handler.
208          *
209          * @param event the event.
210          */

211         public void chartChanged(ChartChangeEvent event) {
212             this.flag = true;
213         }
214
215     }
216
217 }
218
Popular Tags