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 * StackedXYAreaRendererTests.java
28 * -------------------------------
29 * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30 *
31 * Original Author: David Gilbert (for Object Refinery Limited);
32 * Contributor(s): -;
33 *
34 * $Id: StackedXYAreaRendererTests.java,v 1.3 2005/04/20 22:19:11 mungady Exp $
35 *
36 * Changes
37 * -------
38 * 25-Mar-2003 : Version 1 (DG);
39 * 06-Jan-2005 : Renamed StackedAreaXYRendererTests -->
40 * StackedXYAreaRendererTests, improved testEquals() method,
41 * added check for auto range calculation (DG);
42 *
43 */
44
45 package org.jfree.chart.renderer.xy.junit;
46
47 import java.awt.BasicStroke;
48 import java.awt.Color;
49 import java.awt.Stroke;
50 import java.io.ByteArrayInputStream;
51 import java.io.ByteArrayOutputStream;
52 import java.io.ObjectInput;
53 import java.io.ObjectInputStream;
54 import java.io.ObjectOutput;
55 import java.io.ObjectOutputStream;
56
57 import junit.framework.Test;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 import org.jfree.chart.ChartFactory;
62 import org.jfree.chart.JFreeChart;
63 import org.jfree.chart.axis.NumberAxis;
64 import org.jfree.chart.plot.PlotOrientation;
65 import org.jfree.chart.plot.XYPlot;
66 import org.jfree.chart.renderer.xy.StackedXYAreaRenderer;
67 import org.jfree.data.Range;
68 import org.jfree.data.xy.TableXYDataset;
69
70 /**
71 * Tests for the {@link StackedXYAreaRenderer} class.
72 */
73 public class StackedXYAreaRendererTests 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(StackedXYAreaRendererTests.class);
82 }
83
84 /**
85 * Constructs a new set of tests.
86 *
87 * @param name the name of the tests.
88 */
89 public StackedXYAreaRendererTests(String name) {
90 super(name);
91 }
92
93 /**
94 * Test that the equals() method distinguishes all fields.
95 */
96 public void testEquals() {
97 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
98 StackedXYAreaRenderer r2 = new StackedXYAreaRenderer();
99 assertEquals(r1, r2);
100 assertEquals(r2, r1);
101
102 r1.setShapePaint(Color.yellow);
103 assertFalse(r1.equals(r2));
104 r2.setShapePaint(Color.yellow);
105 assertTrue(r1.equals(r2));
106
107 Stroke s = new BasicStroke(1.23f);
108 r1.setShapeStroke(s);
109 assertFalse(r1.equals(r2));
110 r2.setShapeStroke(s);
111 assertTrue(r1.equals(r2));
112 }
113
114 /**
115 * Two objects that are equal are required to return the same hashCode.
116 */
117 public void testHashcode() {
118 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
119 StackedXYAreaRenderer r2 = new StackedXYAreaRenderer();
120 assertTrue(r1.equals(r2));
121 int h1 = r1.hashCode();
122 int h2 = r2.hashCode();
123 assertEquals(h1, h2);
124 }
125
126 /**
127 * Confirm that cloning works.
128 */
129 public void testCloning() {
130 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
131 StackedXYAreaRenderer r2 = null;
132 try {
133 r2 = (StackedXYAreaRenderer) r1.clone();
134 }
135 catch (CloneNotSupportedException e) {
136 System.err.println("Failed to clone.");
137 }
138 assertTrue(r1 != r2);
139 assertTrue(r1.getClass() == r2.getClass());
140 assertTrue(r1.equals(r2));
141 }
142
143 /**
144 * Serialize an instance, restore it, and check for equality.
145 */
146 public void testSerialization() {
147 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
148 StackedXYAreaRenderer r2 = null;
149 try {
150 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
151 ObjectOutput out = new ObjectOutputStream(buffer);
152 out.writeObject(r1);
153 out.close();
154
155 ObjectInput in = new ObjectInputStream(
156 new ByteArrayInputStream(buffer.toByteArray())
157 );
158 r2 = (StackedXYAreaRenderer) in.readObject();
159 in.close();
160 }
161 catch (Exception e) {
162 System.out.println(e.toString());
163 }
164 assertEquals(r1, r2);
165 }
166
167 /**
168 * Check that the renderer is calculating the range bounds correctly.
169 */
170 public void testFindRangeBounds() {
171 TableXYDataset dataset
172 = RendererXYPackageTests.createTestTableXYDataset();
173 JFreeChart chart = ChartFactory.createStackedXYAreaChart(
174 "Test Chart", "X", "Y", dataset,
175 PlotOrientation.VERTICAL, false, false, false
176 );
177 XYPlot plot = (XYPlot) chart.getPlot();
178 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
179 Range bounds = rangeAxis.getRange();
180 assertTrue(bounds.contains(6.0));
181 assertTrue(bounds.contains(8.0));
182 }
183
184 }