KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * WaterfallChartTests.java
29  * ------------------------
30  * (C) Copyright 2002-2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: WaterfallChartTests.java,v 1.1.2.1 2006/10/03 15:41:30 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.labels.CategoryToolTipGenerator;
56 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
57 import org.jfree.chart.plot.CategoryPlot;
58 import org.jfree.chart.plot.PlotOrientation;
59 import org.jfree.chart.renderer.category.CategoryItemRenderer;
60 import org.jfree.chart.urls.CategoryURLGenerator;
61 import org.jfree.chart.urls.StandardCategoryURLGenerator;
62 import org.jfree.data.category.CategoryDataset;
63 import org.jfree.data.general.DatasetUtilities;
64
65 /**
66  * Some tests for a waterfall chart.
67  */

68 public class WaterfallChartTests extends TestCase {
69
70     /** A chart. */
71     private JFreeChart chart;
72
73     /**
74      * Returns the tests as a test suite.
75      *
76      * @return The test suite.
77      */

78     public static Test suite() {
79         return new TestSuite(WaterfallChartTests.class);
80     }
81
82     /**
83      * Constructs a new set of tests.
84      *
85      * @param name the name of the tests.
86      */

87     public WaterfallChartTests(String JavaDoc name) {
88         super(name);
89     }
90
91     /**
92      * Common test setup.
93      */

94     protected void setUp() {
95         this.chart = createWaterfallChart();
96     }
97
98     /**
99      * Draws the chart with a null info object to make sure that no exceptions
100      * are thrown (a problem that was occurring at one point).
101      */

102     public void testDrawWithNullInfo() {
103
104         boolean success = false;
105
106         try {
107             BufferedImage JavaDoc image = new BufferedImage JavaDoc(200 , 100,
108                     BufferedImage.TYPE_INT_RGB);
109             Graphics2D JavaDoc g2 = image.createGraphics();
110             this.chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null,
111                     null);
112             g2.dispose();
113             success = true;
114         }
115         catch (Exception JavaDoc e) {
116             success = false;
117         }
118
119         assertTrue(success);
120
121     }
122
123     /**
124      * Check that setting a tool tip generator for a series does override the
125      * default generator.
126      */

127     public void testSetSeriesToolTipGenerator() {
128         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
129         CategoryItemRenderer renderer = plot.getRenderer();
130         StandardCategoryToolTipGenerator tt
131             = new StandardCategoryToolTipGenerator();
132         renderer.setSeriesToolTipGenerator(0, tt);
133         CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
134         assertTrue(tt2 == tt);
135     }
136     
137     /**
138      * Check that setting a URL generator for a series does override the
139      * default generator.
140      */

141     public void testSetSeriesURLGenerator() {
142         CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
143         CategoryItemRenderer renderer = plot.getRenderer();
144         StandardCategoryURLGenerator url1
145             = new StandardCategoryURLGenerator();
146         renderer.setSeriesItemURLGenerator(0, url1);
147         CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
148         assertTrue(url2 == url1);
149     }
150     
151     /**
152      * Create a bar chart with sample data in the range -3 to +3.
153      *
154      * @return The chart.
155      */

156     private static JFreeChart createWaterfallChart() {
157
158         // create a dataset...
159
Number JavaDoc[][] data = new Integer JavaDoc[][]
160             {{new Integer JavaDoc(-3), new Integer JavaDoc(-2)},
161              {new Integer JavaDoc(-1), new Integer JavaDoc(1)},
162              {new Integer JavaDoc(2), new Integer JavaDoc(3)}};
163
164         CategoryDataset dataset
165             = DatasetUtilities.createCategoryDataset("S", "C", data);
166
167         // create the chart...
168
return ChartFactory.createWaterfallChart(
169             "Waterfall Chart",
170             "Domain", "Range",
171             dataset,
172             PlotOrientation.HORIZONTAL,
173             true, // include legend
174
true,
175             true
176         );
177
178     }
179
180 }
181
Popular Tags