KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BarChartTests.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: BarChartTests.java,v 1.4 2005/04/13 08:43:26 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 11-Jun-2002 : Version 1 (DG);
39  * 25-Jun-2002 : Removed redundant code (DG);
40  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  * 14-Jul-2003 : Renamed BarChartTests.java (DG);
42  *
43  */

44
45 package org.jfree.chart.junit;
46
47 import java.awt.Graphics2D JavaDoc;
48 import java.awt.geom.Rectangle2D JavaDoc;
49 import java.awt.image.BufferedImage JavaDoc;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.chart.ChartFactory;
56 import org.jfree.chart.JFreeChart;
57 import org.jfree.chart.axis.ValueAxis;
58 import org.jfree.chart.event.ChartChangeEvent;
59 import org.jfree.chart.event.ChartChangeListener;
60 import org.jfree.chart.labels.CategoryToolTipGenerator;
61 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
62 import org.jfree.chart.plot.CategoryPlot;
63 import org.jfree.chart.plot.PlotOrientation;
64 import org.jfree.chart.renderer.category.CategoryItemRenderer;
65 import org.jfree.chart.urls.CategoryURLGenerator;
66 import org.jfree.chart.urls.StandardCategoryURLGenerator;
67 import org.jfree.data.Range;
68 import org.jfree.data.category.CategoryDataset;
69 import org.jfree.data.general.DatasetUtilities;
70
71 /**
72  * Tests for a bar chart.
73  */

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

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

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

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

110     public void testDrawWithNullInfo() {
111
112         boolean success = false;
113
114         try {
115             BufferedImage JavaDoc image = new BufferedImage JavaDoc(
116                 200 , 100, BufferedImage.TYPE_INT_RGB
117             );
118             Graphics2D JavaDoc g2 = image.createGraphics();
119             this.chart.draw(
120                 g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null
121             );
122             g2.dispose();
123             success = true;
124         }
125         catch (Exception JavaDoc e) {
126             success = false;
127         }
128
129         assertTrue(success);
130
131     }
132
133     /**
134      * Replaces the chart's dataset and then checks that the new dataset is OK.
135      */

136     public void testReplaceDataset() {
137
138         // create a dataset...
139
Number JavaDoc[][] data = new Integer JavaDoc[][]
140             {{new Integer JavaDoc(-30), new Integer JavaDoc(-20)},
141              {new Integer JavaDoc(-10), new Integer JavaDoc(10)},
142              {new Integer JavaDoc(20), new Integer JavaDoc(30)}};
143
144         CategoryDataset newData = DatasetUtilities.createCategoryDataset(
145             "S", "C", data
146         );
147
148         LocalListener l = new LocalListener();
149         this.chart.addChangeListener(l);
150         this.chart.getCategoryPlot().setDataset(newData);
151         assertEquals(true, l.flag);
152         ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis();
153         Range range = axis.getRange();
154         assertTrue("Expecting the lower bound of the range to be around -30: "
155                    + range.getLowerBound(), range.getLowerBound() <= -30);
156         assertTrue("Expecting the upper bound of the range to be around 30: "
157                    + range.getUpperBound(), range.getUpperBound() >= 30);
158
159     }
160
161     /**
162      * Check that setting a tool tip generator for a series does override the
163      * default generator.
164      */

165     public void testSetSeriesToolTipGenerator() {
166         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
167         CategoryItemRenderer renderer = plot.getRenderer();
168         StandardCategoryToolTipGenerator tt
169             = new StandardCategoryToolTipGenerator();
170         renderer.setSeriesToolTipGenerator(0, tt);
171         CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
172         assertTrue(tt2 == tt);
173     }
174     
175     /**
176      * Check that setting a URL generator for a series does override the
177      * default generator.
178      */

179     public void testSetSeriesURLGenerator() {
180         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
181         CategoryItemRenderer renderer = plot.getRenderer();
182         StandardCategoryURLGenerator url1
183             = new StandardCategoryURLGenerator();
184         renderer.setSeriesItemURLGenerator(0, url1);
185         CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
186         assertTrue(url2 == url1);
187     }
188     
189     /**
190      * Create a bar chart with sample data in the range -3 to +3.
191      *
192      * @return The chart.
193      */

194     private static JFreeChart createBarChart() {
195
196         // create a dataset...
197
Number JavaDoc[][] data = new Integer JavaDoc[][]
198             {{new Integer JavaDoc(-3), new Integer JavaDoc(-2)},
199              {new Integer JavaDoc(-1), new Integer JavaDoc(1)},
200              {new Integer JavaDoc(2), new Integer JavaDoc(3)}};
201
202         CategoryDataset dataset
203             = DatasetUtilities.createCategoryDataset("S", "C", data);
204
205         // create the chart...
206
return ChartFactory.createBarChart(
207             "Bar Chart",
208             "Domain", "Range",
209             dataset,
210             PlotOrientation.HORIZONTAL,
211             true, // include legend
212
true,
213             true
214         );
215
216     }
217
218     /**
219      * A chart change listener.
220      *
221      */

222     static class LocalListener implements ChartChangeListener {
223
224         /** A flag. */
225         private boolean flag = false;
226
227         /**
228          * Event handler.
229          *
230          * @param event the event.
231          */

232         public void chartChanged(ChartChangeEvent event) {
233             this.flag = true;
234         }
235
236     }
237
238 }
239
Popular Tags