KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LineChartTests.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: LineChartTests.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.CategoryToolTipGenerator;
59 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
60 import org.jfree.chart.plot.CategoryPlot;
61 import org.jfree.chart.plot.PlotOrientation;
62 import org.jfree.chart.renderer.category.CategoryItemRenderer;
63 import org.jfree.chart.urls.CategoryURLGenerator;
64 import org.jfree.chart.urls.StandardCategoryURLGenerator;
65 import org.jfree.data.Range;
66 import org.jfree.data.category.CategoryDataset;
67 import org.jfree.data.general.DatasetUtilities;
68
69 /**
70  * Some tests for a line chart.
71  */

72 public class LineChartTests extends TestCase {
73
74     /** A chart. */
75     private JFreeChart chart;
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(LineChartTests.class);
84     }
85
86     /**
87      * Constructs a new set of tests.
88      *
89      * @param name the name of the tests.
90      */

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

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

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

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

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

173     public void testSetSeriesURLGenerator() {
174         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
175         CategoryItemRenderer renderer = plot.getRenderer();
176         StandardCategoryURLGenerator url1
177             = new StandardCategoryURLGenerator();
178         renderer.setSeriesItemURLGenerator(0, url1);
179         CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
180         assertTrue(url2 == url1);
181     }
182     
183     /**
184      * Create a line chart with sample data in the range -3 to +3.
185      *
186      * @return The chart.
187      */

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

216     static class LocalListener implements ChartChangeListener {
217
218         /** A flag. */
219         private boolean flag = false;
220
221         /**
222          * Event handler.
223          *
224          * @param event the event.
225          */

226         public void chartChanged(ChartChangeEvent event) {
227             this.flag = true;
228         }
229
230     }
231
232 }
233
Popular Tags