KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > xy > junit > StackedXYAreaRenderer2Tests


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  * StackedXYAreaRenderer2Tests.java
29  * -------------------------------
30  * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StackedXYAreaRenderer2Tests.java,v 1.1.2.1 2006/10/03 15:41:45 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 06-Jan-2005 : Version 1 (DG);
40  * 22-Aug-2006 : Added testDrawWithEmptyDataset() method (DG);
41  *
42  */

43
44 package org.jfree.chart.renderer.xy.junit;
45
46 import java.awt.Graphics2D JavaDoc;
47 import java.awt.geom.Rectangle2D JavaDoc;
48 import java.awt.image.BufferedImage JavaDoc;
49 import java.io.ByteArrayInputStream JavaDoc;
50 import java.io.ByteArrayOutputStream JavaDoc;
51 import java.io.ObjectInput JavaDoc;
52 import java.io.ObjectInputStream JavaDoc;
53 import java.io.ObjectOutput JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.chart.ChartFactory;
61 import org.jfree.chart.JFreeChart;
62 import org.jfree.chart.axis.NumberAxis;
63 import org.jfree.chart.plot.PlotOrientation;
64 import org.jfree.chart.plot.XYPlot;
65 import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
66 import org.jfree.data.Range;
67 import org.jfree.data.xy.DefaultTableXYDataset;
68 import org.jfree.data.xy.TableXYDataset;
69
70 /**
71  * Tests for the {@link StackedXYAreaRenderer2} class.
72  */

73 public class StackedXYAreaRenderer2Tests extends TestCase {
74
75     /**
76      * Returns the tests as a test suite.
77      *
78      * @return The test suite.
79      */

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

89     public StackedXYAreaRenderer2Tests(String JavaDoc name) {
90         super(name);
91     }
92
93     /**
94      * Test chart drawing with an empty dataset to ensure that this special
95      * case doesn't cause any exceptions.
96      */

97     public void testDrawWithEmptyDataset() {
98         boolean success = false;
99         JFreeChart chart = ChartFactory.createStackedXYAreaChart("title", "x",
100                 "y", new DefaultTableXYDataset(), PlotOrientation.VERTICAL,
101                 true, false, false);
102         XYPlot plot = (XYPlot) chart.getPlot();
103         plot.setRenderer(new StackedXYAreaRenderer2());
104         try {
105             BufferedImage JavaDoc image = new BufferedImage JavaDoc(200 , 100,
106                     BufferedImage.TYPE_INT_RGB);
107             Graphics2D JavaDoc g2 = image.createGraphics();
108             chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null);
109             g2.dispose();
110             success = true;
111         }
112         catch (Exception JavaDoc e) {
113             success = false;
114         }
115         assertTrue(success);
116     }
117     
118     /**
119      * Test that the equals() method distinguishes all fields.
120      */

121     public void testEquals() {
122         StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
123         StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2();
124         assertEquals(r1, r2);
125         assertEquals(r2, r1);
126     }
127
128     /**
129      * Two objects that are equal are required to return the same hashCode.
130      */

131     public void testHashcode() {
132         StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
133         StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2();
134         assertTrue(r1.equals(r2));
135         int h1 = r1.hashCode();
136         int h2 = r2.hashCode();
137         assertEquals(h1, h2);
138     }
139     
140     /**
141      * Confirm that cloning works.
142      */

143     public void testCloning() {
144         StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
145         StackedXYAreaRenderer2 r2 = null;
146         try {
147             r2 = (StackedXYAreaRenderer2) r1.clone();
148         }
149         catch (CloneNotSupportedException JavaDoc e) {
150             e.printStackTrace();
151         }
152         assertTrue(r1 != r2);
153         assertTrue(r1.getClass() == r2.getClass());
154         assertTrue(r1.equals(r2));
155     }
156
157     /**
158      * Serialize an instance, restore it, and check for equality.
159      */

160     public void testSerialization() {
161         StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
162         StackedXYAreaRenderer2 r2 = null;
163         try {
164             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
165             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
166             out.writeObject(r1);
167             out.close();
168
169             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
170                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
171             r2 = (StackedXYAreaRenderer2) in.readObject();
172             in.close();
173         }
174         catch (Exception JavaDoc e) {
175             e.printStackTrace();
176         }
177         assertEquals(r1, r2);
178     }
179     
180     /**
181      * Check that the renderer is calculating the range bounds correctly.
182      */

183     public void testFindRangeBounds() {
184         TableXYDataset dataset
185                 = RendererXYPackageTests.createTestTableXYDataset();
186         JFreeChart chart = ChartFactory.createStackedXYAreaChart(
187                 "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL,
188                 false, false, false);
189         XYPlot plot = (XYPlot) chart.getPlot();
190         StackedXYAreaRenderer2 renderer = new StackedXYAreaRenderer2();
191         plot.setRenderer(renderer);
192         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
193         Range bounds = rangeAxis.getRange();
194         assertTrue(bounds.contains(6.0));
195         assertTrue(bounds.contains(8.0));
196         
197         // try null argument
198
assertNull(renderer.findRangeBounds(null));
199         
200         // try empty dataset
201
assertNull(renderer.findRangeBounds(new DefaultTableXYDataset()));
202     }
203
204 }
205
Popular Tags