KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StackedXYAreaRendererTests.java
29  * -------------------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StackedXYAreaRendererTests.java,v 1.1.2.2 2006/11/10 11:09:37 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 25-Mar-2003 : Version 1 (DG);
40  * 06-Jan-2005 : Renamed StackedAreaXYRendererTests -->
41  * StackedXYAreaRendererTests, improved testEquals() method,
42  * added check for auto range calculation (DG);
43  * 10-Nov-2006 : Added testBug1593156() (DG);
44  *
45  */

46
47 package org.jfree.chart.renderer.xy.junit;
48
49 import java.awt.BasicStroke JavaDoc;
50 import java.awt.Color JavaDoc;
51 import java.awt.GradientPaint JavaDoc;
52 import java.awt.Stroke JavaDoc;
53 import java.io.ByteArrayInputStream JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.ObjectInput JavaDoc;
56 import java.io.ObjectInputStream JavaDoc;
57 import java.io.ObjectOutput JavaDoc;
58 import java.io.ObjectOutputStream JavaDoc;
59
60 import junit.framework.Test;
61 import junit.framework.TestCase;
62 import junit.framework.TestSuite;
63
64 import org.jfree.chart.ChartFactory;
65 import org.jfree.chart.JFreeChart;
66 import org.jfree.chart.axis.NumberAxis;
67 import org.jfree.chart.plot.PlotOrientation;
68 import org.jfree.chart.plot.XYPlot;
69 import org.jfree.chart.renderer.xy.StackedXYAreaRenderer;
70 import org.jfree.chart.renderer.xy.XYAreaRenderer;
71 import org.jfree.data.Range;
72 import org.jfree.data.xy.DefaultTableXYDataset;
73 import org.jfree.data.xy.TableXYDataset;
74 import org.jfree.data.xy.XYSeries;
75
76 /**
77  * Tests for the {@link StackedXYAreaRenderer} class.
78  */

79 public class StackedXYAreaRendererTests extends TestCase {
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(StackedXYAreaRendererTests.class);
88     }
89
90     /**
91      * Constructs a new set of tests.
92      *
93      * @param name the name of the tests.
94      */

95     public StackedXYAreaRendererTests(String JavaDoc name) {
96         super(name);
97     }
98
99     /**
100      * Test that the equals() method distinguishes all fields.
101      */

102     public void testEquals() {
103         StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
104         StackedXYAreaRenderer r2 = new StackedXYAreaRenderer();
105         assertEquals(r1, r2);
106         assertEquals(r2, r1);
107         
108         r1.setShapePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
109                 3.0f, 4.0f, Color.green));
110         assertFalse(r1.equals(r2));
111         r2.setShapePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
112                 3.0f, 4.0f, Color.green));
113         assertTrue(r1.equals(r2));
114         
115         Stroke JavaDoc s = new BasicStroke JavaDoc(1.23f);
116         r1.setShapeStroke(s);
117         assertFalse(r1.equals(r2));
118         r2.setShapeStroke(s);
119         assertTrue(r1.equals(r2));
120     }
121
122     /**
123      * Two objects that are equal are required to return the same hashCode.
124      */

125     public void testHashcode() {
126         StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
127         StackedXYAreaRenderer r2 = new StackedXYAreaRenderer();
128         assertTrue(r1.equals(r2));
129         int h1 = r1.hashCode();
130         int h2 = r2.hashCode();
131         assertEquals(h1, h2);
132     }
133     
134     /**
135      * Confirm that cloning works.
136      */

137     public void testCloning() {
138         StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
139         StackedXYAreaRenderer r2 = null;
140         try {
141             r2 = (StackedXYAreaRenderer) r1.clone();
142         }
143         catch (CloneNotSupportedException JavaDoc e) {
144             System.err.println("Failed to clone.");
145         }
146         assertTrue(r1 != r2);
147         assertTrue(r1.getClass() == r2.getClass());
148         assertTrue(r1.equals(r2));
149     }
150
151     /**
152      * Serialize an instance, restore it, and check for equality.
153      */

154     public void testSerialization() {
155         StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
156         r1.setShapePaint(Color.red);
157         r1.setShapeStroke(new BasicStroke JavaDoc(1.23f));
158         StackedXYAreaRenderer r2 = null;
159         try {
160             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
161             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
162             out.writeObject(r1);
163             out.close();
164
165             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
166                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
167             );
168             r2 = (StackedXYAreaRenderer) in.readObject();
169             in.close();
170         }
171         catch (Exception JavaDoc e) {
172             System.out.println(e.toString());
173         }
174         assertEquals(r1, r2);
175     }
176     
177     /**
178      * Check that the renderer is calculating the range bounds correctly.
179      */

180     public void testFindRangeBounds() {
181         TableXYDataset dataset
182             = RendererXYPackageTests.createTestTableXYDataset();
183         JFreeChart chart = ChartFactory.createStackedXYAreaChart(
184             "Test Chart", "X", "Y", dataset,
185             PlotOrientation.VERTICAL, false, false, false
186         );
187         XYPlot plot = (XYPlot) chart.getPlot();
188         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
189         Range bounds = rangeAxis.getRange();
190         assertTrue(bounds.contains(6.0));
191         assertTrue(bounds.contains(8.0));
192     }
193
194     /**
195      * Draws the chart with a <code>null</code> info object to make sure that
196      * no exceptions are thrown (particularly by code in the renderer).
197      */

198     public void testDrawWithNullInfo() {
199         boolean success = false;
200         try {
201             DefaultTableXYDataset dataset = new DefaultTableXYDataset();
202         
203             XYSeries s1 = new XYSeries("Series 1", true, false);
204             s1.add(5.0, 5.0);
205             s1.add(10.0, 15.5);
206             s1.add(15.0, 9.5);
207             s1.add(20.0, 7.5);
208             dataset.addSeries(s1);
209         
210             XYSeries s2 = new XYSeries("Series 2", true, false);
211             s2.add(5.0, 5.0);
212             s2.add(10.0, 15.5);
213             s2.add(15.0, 9.5);
214             s2.add(20.0, 3.5);
215             dataset.addSeries(s2);
216             XYPlot plot = new XYPlot(dataset,
217                     new NumberAxis("X"), new NumberAxis("Y"),
218                     new StackedXYAreaRenderer());
219             JFreeChart chart = new JFreeChart(plot);
220             /* BufferedImage image = */ chart.createBufferedImage(300, 200,
221                     null);
222             success = true;
223         }
224         catch (NullPointerException JavaDoc e) {
225             e.printStackTrace();
226             success = false;
227         }
228         assertTrue(success);
229     }
230
231     /**
232      * A test for bug 1593156.
233      */

234     public void testBug1593156() {
235         boolean success = false;
236         try {
237             DefaultTableXYDataset dataset = new DefaultTableXYDataset();
238         
239             XYSeries s1 = new XYSeries("Series 1", true, false);
240             s1.add(5.0, 5.0);
241             s1.add(10.0, 15.5);
242             s1.add(15.0, 9.5);
243             s1.add(20.0, 7.5);
244             dataset.addSeries(s1);
245         
246             XYSeries s2 = new XYSeries("Series 2", true, false);
247             s2.add(5.0, 5.0);
248             s2.add(10.0, 15.5);
249             s2.add(15.0, 9.5);
250             s2.add(20.0, 3.5);
251             dataset.addSeries(s2);
252             StackedXYAreaRenderer renderer = new StackedXYAreaRenderer(
253                     XYAreaRenderer.LINES);
254             XYPlot plot = new XYPlot(dataset,
255                     new NumberAxis("X"), new NumberAxis("Y"),
256                     renderer);
257             JFreeChart chart = new JFreeChart(plot);
258             /* BufferedImage image = */ chart.createBufferedImage(300, 200,
259                     null);
260             success = true;
261         }
262         catch (NullPointerException JavaDoc e) {
263             e.printStackTrace();
264             success = false;
265         }
266         assertTrue(success);
267     }
268
269 }
270
Popular Tags