KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BarChart3DTests.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: BarChart3DTests.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 BarChart3DTests.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 3D bar chart.
73  */

74 public class BarChart3DTests extends TestCase {
75
76     /** The 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(BarChart3DTests.class);
86     }
87
88     /**
89      * Constructs a new set of tests.
90      *
91      * @param name the name of the tests.
92      */

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

100     protected void setUp() {
101
102         this.chart = createBarChart3D();
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         boolean success = false;
112         try {
113             BufferedImage JavaDoc image = new BufferedImage JavaDoc(
114                 200 , 100, BufferedImage.TYPE_INT_RGB
115             );
116             Graphics2D JavaDoc g2 = image.createGraphics();
117             this.chart.draw(
118                 g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null
119             );
120             g2.dispose();
121             success = true;
122         }
123         catch (Exception JavaDoc e) {
124             success = false;
125         }
126         assertTrue(success);
127     }
128
129     /**
130      * Replaces the dataset and checks that the data range is as expected.
131      */

132     public void testReplaceDataset() {
133
134         // create a dataset...
135
Number JavaDoc[][] data = new Integer JavaDoc[][]
136             {{new Integer JavaDoc(-30), new Integer JavaDoc(-20)},
137              {new Integer JavaDoc(-10), new Integer JavaDoc(10)},
138              {new Integer JavaDoc(20), new Integer JavaDoc(30)}};
139
140         CategoryDataset newData = DatasetUtilities.createCategoryDataset(
141             "S", "C", data
142         );
143
144         LocalListener l = new LocalListener();
145         this.chart.addChangeListener(l);
146         this.chart.getCategoryPlot().setDataset(newData);
147         assertEquals(true, l.flag);
148         ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis();
149         Range range = axis.getRange();
150         assertTrue(
151             "Expecting the lower bound of the range to be around -30: "
152             + range.getLowerBound(), range.getLowerBound() <= -30
153         );
154         assertTrue(
155             "Expecting the upper bound of the range to be around 30: "
156             + range.getUpperBound(), range.getUpperBound() >= 30
157         );
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 horizontal bar chart with sample data in the range -3 to +3.
191      *
192      * @return The chart.
193      */

194     private static JFreeChart createBarChart3D() {
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 = DatasetUtilities.createCategoryDataset(
203             "S", "C", data
204         );
205
206         // create the chart...
207
return ChartFactory.createBarChart3D(
208             "Bar Chart 3D",
209             "Domain",
210             "Range",
211             dataset,
212             PlotOrientation.HORIZONTAL,
213             true, // include legend
214
true,
215             true
216         );
217
218     }
219
220     /**
221      * A chart change listener.
222      *
223      */

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

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