KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AreaChartTests.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: AreaChartTests.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  * Tests for an area chart.
71  */

72 public class AreaChartTests 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(AreaChartTests.class);
84     }
85
86     /**
87      * Constructs a new set of tests.
88      *
89      * @param name the name of the tests.
90      */

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

98     protected void setUp() {
99         this.chart = createAreaChart();
100     }
101
102     /**
103      * Check that setting a tool tip generator for a series does override the
104      * default generator.
105      */

106     public void testSetSeriesToolTipGenerator() {
107         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
108         CategoryItemRenderer renderer = plot.getRenderer();
109         StandardCategoryToolTipGenerator tt
110             = new StandardCategoryToolTipGenerator();
111         renderer.setSeriesToolTipGenerator(0, tt);
112         CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
113         assertTrue(tt2 == tt);
114     }
115     
116     /**
117      * Check that setting a URL generator for a series does override the
118      * default generator.
119      */

120     public void testSetSeriesURLGenerator() {
121         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
122         CategoryItemRenderer renderer = plot.getRenderer();
123         StandardCategoryURLGenerator url1
124             = new StandardCategoryURLGenerator();
125         renderer.setSeriesItemURLGenerator(0, url1);
126         CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
127         assertTrue(url2 == url1);
128     }
129     
130     /**
131      * Draws the chart with a null info object to make sure that no exceptions
132      * are thrown (a problem that was occurring at one point).
133      */

134     public void testDrawWithNullInfo() {
135         boolean success = false;
136         try {
137             BufferedImage JavaDoc image = new BufferedImage JavaDoc(
138                 200 , 100, BufferedImage.TYPE_INT_RGB
139             );
140             Graphics2D JavaDoc g2 = image.createGraphics();
141             this.chart.draw(
142                 g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null
143             );
144             g2.dispose();
145             success = true;
146         }
147         catch (Exception JavaDoc e) {
148             success = false;
149         }
150         assertTrue(success);
151     }
152
153     /**
154      * Replaces the chart's dataset and then checks that the new dataset is OK.
155      */

156     public void testReplaceDataset() {
157         Number JavaDoc[][] data = new Integer JavaDoc[][]
158             {{new Integer JavaDoc(-30), new Integer JavaDoc(-20)},
159              {new Integer JavaDoc(-10), new Integer JavaDoc(10)},
160              {new Integer JavaDoc(20), new Integer JavaDoc(30)}};
161
162         CategoryDataset newData = DatasetUtilities.createCategoryDataset(
163             "S", "C", data
164         );
165         LocalListener l = new LocalListener();
166         this.chart.addChangeListener(l);
167         this.chart.getCategoryPlot().setDataset(newData);
168         assertEquals(true, l.flag);
169         ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis();
170         Range range = axis.getRange();
171         assertTrue("Expecting the lower bound of the range to be around -30: "
172                    + range.getLowerBound(), range.getLowerBound() <= -30);
173         assertTrue("Expecting the upper bound of the range to be around 30: "
174                    + range.getUpperBound(), range.getUpperBound() >= 30);
175
176     }
177
178     /**
179      * Create an area chart with sample data in the range -3 to +3.
180      *
181      * @return The chart.
182      */

183     private static JFreeChart createAreaChart() {
184         Number JavaDoc[][] data = new Integer JavaDoc[][]
185             {{new Integer JavaDoc(-3), new Integer JavaDoc(-2)},
186              {new Integer JavaDoc(-1), new Integer JavaDoc(1)},
187              {new Integer JavaDoc(2), new Integer JavaDoc(3)}};
188         CategoryDataset dataset
189             = DatasetUtilities.createCategoryDataset("S", "C", data);
190         return ChartFactory.createAreaChart(
191             "Area Chart",
192             "Domain", "Range",
193             dataset,
194             PlotOrientation.HORIZONTAL,
195             true, // include legend
196
true,
197             true
198         );
199
200     }
201
202     /**
203      * A chart change listener.
204      */

205     static class LocalListener implements ChartChangeListener {
206
207         /** A flag. */
208         private boolean flag = false;
209
210         /**
211          * Event handler.
212          *
213          * @param event the event.
214          */

215         public void chartChanged(ChartChangeEvent event) {
216             this.flag = true;
217         }
218
219     }
220
221 }
222
Popular Tags