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