KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StackedBarChartTests.java
29  * -------------------------
30  * (C) Copyright 2002-2004, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StackedBarChartTests.java,v 1.1.2.1 2006/10/03 15:41:32 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 11-Jun-2002 : Version 1 (DG);
40  * 25-Jun-2002 : Removed unnecessary import (DG);
41  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
42  * 29-Jan-2004 : Renamed StackedHorizontalBarChartTests
43  * --> StackedBarChartTests (DG);
44  *
45  */

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

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

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

95     public StackedBarChartTests(String JavaDoc name) {
96         super(name);
97     }
98
99     /**
100      * Common test setup.
101      */

102     protected void setUp() {
103         this.chart = createChart();
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 dataset and checks that it has changed as expected.
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
145             = DatasetUtilities.createCategoryDataset("S", "C", data);
146
147         LocalListener l = new LocalListener();
148         this.chart.addChangeListener(l);
149         this.chart.getCategoryPlot().setDataset(newData);
150         assertEquals(true, l.flag);
151         ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis();
152         Range range = axis.getRange();
153         assertTrue("Expecting the lower bound of the range to be around -30: "
154                     + range.getLowerBound(), range.getLowerBound() <= -30);
155         assertTrue("Expecting the upper bound of the range to be around 30: "
156                    + range.getUpperBound(), range.getUpperBound() >= 30);
157
158     }
159
160     /**
161      * Check that setting a tool tip generator for a series does override the
162      * default generator.
163      */

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

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

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

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

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