KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------------------------
23  * StackedVerticalBarChartTests.java
24  * ---------------------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: StackedVerticalBarChartTests.java,v 1.7 2003/07/21 10:26:28 mungady Exp $
31  *
32  * Changes:
33  * --------
34  * 11-Jun-2002 : Version 1 (DG);
35  * 25-Jun-2002 : Removed unnecessary import (DG);
36  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
37  *
38  */

39
40 package org.jfree.chart.junit;
41
42 import java.awt.Graphics2D JavaDoc;
43 import java.awt.geom.Rectangle2D JavaDoc;
44 import java.awt.image.BufferedImage JavaDoc;
45
46 import junit.framework.Test;
47 import junit.framework.TestCase;
48 import junit.framework.TestSuite;
49
50 import org.jfree.chart.ChartFactory;
51 import org.jfree.chart.JFreeChart;
52 import org.jfree.chart.axis.ValueAxis;
53 import org.jfree.chart.event.ChartChangeEvent;
54 import org.jfree.chart.event.ChartChangeListener;
55 import org.jfree.chart.plot.PlotOrientation;
56 import org.jfree.data.CategoryDataset;
57 import org.jfree.data.DatasetUtilities;
58 import org.jfree.data.Range;
59
60 /**
61  * Tests for a stacked vertical bar chart.
62  *
63  * @author David Gilbert
64  */

65 public class StackedVerticalBarChartTests extends TestCase {
66
67     /** A chart. */
68     private JFreeChart chart;
69
70     /**
71      * Returns the tests as a test suite.
72      *
73      * @return the test suite.
74      */

75     public static Test suite() {
76         return new TestSuite(StackedVerticalBarChartTests.class);
77     }
78
79     /**
80      * Constructs a new set of tests.
81      *
82      * @param name the name of the tests.
83      */

84     public StackedVerticalBarChartTests(String JavaDoc name) {
85         super(name);
86     }
87
88     /**
89      * Common test setup.
90      */

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

101     public void testDrawWithNullInfo() {
102
103         boolean success = false;
104
105         try {
106             BufferedImage JavaDoc image = new BufferedImage JavaDoc(200 , 100, BufferedImage.TYPE_INT_RGB);
107             Graphics2D JavaDoc g2 = image.createGraphics();
108             chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null);
109             g2.dispose();
110             success = true;
111         }
112         catch (Exception JavaDoc e) {
113           success = false;
114         }
115
116         assertTrue(success);
117
118     }
119
120     /**
121      * Replaces the dataset and checks that it has changed as expected.
122      */

123     public void testReplaceDataset() {
124
125         // create a dataset...
126
Number JavaDoc[][] data = new Integer JavaDoc[][]
127             {{new Integer JavaDoc(-30), new Integer JavaDoc(-20)},
128              {new Integer JavaDoc(-10), new Integer JavaDoc(10)},
129              {new Integer JavaDoc(20), new Integer JavaDoc(30)}};
130
131         CategoryDataset newData = DatasetUtilities.createCategoryDataset("S", "C", data);
132
133         LocalListener l = new LocalListener();
134         chart.addChangeListener(l);
135         chart.getCategoryPlot().setDataset(newData);
136         assertEquals(true, l.flag);
137         ValueAxis axis = chart.getCategoryPlot().getRangeAxis();
138         Range range = axis.getRange();
139         assertTrue("Expecting the lower bound of the range to be around -30: "
140                    + range.getLowerBound(), range.getLowerBound() <= -30);
141         assertTrue("Expecting the upper bound of the range to be around 30:"
142                    + range.getUpperBound(), range.getUpperBound() >= 30);
143
144     }
145
146     /**
147      * Create a horizontal bar chart with sample data in the range -3 to +3.
148      *
149      * @return the chart.
150      */

151     private static JFreeChart createChart() {
152
153         // create a dataset...
154
Number JavaDoc[][] data = new Integer JavaDoc[][]
155             {{new Integer JavaDoc(-3), new Integer JavaDoc(-2)},
156              {new Integer JavaDoc(-1), new Integer JavaDoc(1)},
157              {new Integer JavaDoc(2), new Integer JavaDoc(3)}};
158
159         CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S", "C", data);
160
161         // create the chart...
162
return ChartFactory.createStackedBarChart(
163             "Stacked Vertical Bar Chart", // chart title
164
"Domain", "Range",
165             dataset, // data
166
PlotOrientation.VERTICAL,
167             true, // include legend
168
true,
169             false
170         );
171
172     }
173
174     /**
175      * A chart change listener.
176      *
177      * @author David Gilbert
178      */

179     static class LocalListener implements ChartChangeListener {
180
181         /** A flag. */
182         private boolean flag = false;
183
184         /**
185          * Event handler.
186          *
187          * @param event the event.
188          */

189         public void chartChanged(ChartChangeEvent event) {
190             flag = true;
191         }
192
193     }
194
195 }
196
Popular Tags