KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYBarRendererTests.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: XYBarRendererTests.java,v 1.1.2.1 2006/10/03 15:41:47 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 25-Mar-2003 : Version 1 (DG);
40  * 22-Oct-2003 : Added hashCode test (DG);
41  *
42  */

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

73 public class XYBarRendererTests 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(XYBarRendererTests.class);
82     }
83
84     /**
85      * Constructs a new set of tests.
86      *
87      * @param name the name of the tests.
88      */

89     public XYBarRendererTests(String JavaDoc name) {
90         super(name);
91     }
92
93     /**
94      * Test that the equals() method distinguishes all fields.
95      */

96     public void testEquals() {
97         
98         // default instances
99
XYBarRenderer r1 = new XYBarRenderer();
100         XYBarRenderer r2 = new XYBarRenderer();
101         assertTrue(r1.equals(r2));
102         assertTrue(r2.equals(r1));
103         
104         // setBase()
105
r1.setBase(1.0);
106         assertFalse(r1.equals(r2));
107         r2.setBase(1.0);
108         assertTrue(r1.equals(r2));
109         
110         // setUseYInterval
111
r1.setUseYInterval(!r1.getUseYInterval());
112         assertFalse(r1.equals(r2));
113         r2.setUseYInterval(!r2.getUseYInterval());
114         assertTrue(r1.equals(r2));
115         
116         // setMargin()
117
r1.setMargin(0.10);
118         assertFalse(r1.equals(r2));
119         r2.setMargin(0.10);
120         assertTrue(r1.equals(r2));
121         
122         // setDrawBarOutline()
123
r1.setDrawBarOutline(!r1.isDrawBarOutline());
124         assertFalse(r1.equals(r2));
125         r2.setDrawBarOutline(!r2.isDrawBarOutline());
126         assertTrue(r1.equals(r2));
127         
128         // setGradientPaintTransformer()
129
r1.setGradientPaintTransformer(new StandardGradientPaintTransformer(
130                 GradientPaintTransformType.CENTER_HORIZONTAL));
131         assertFalse(r1.equals(r2));
132         r2.setGradientPaintTransformer(new StandardGradientPaintTransformer(
133                 GradientPaintTransformType.CENTER_HORIZONTAL));
134         assertTrue(r1.equals(r2));
135         
136         // legendBar
137
r1.setLegendBar(new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
138         assertFalse(r1.equals(r2));
139         r2.setLegendBar(new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
140         assertTrue(r1.equals(r2));
141         
142         // positiveItemLabelFallbackPosition
143
r1.setPositiveItemLabelPositionFallback(new ItemLabelPosition());
144         assertFalse(r1.equals(r2));
145         r2.setPositiveItemLabelPositionFallback(new ItemLabelPosition());
146         assertTrue(r1.equals(r2));
147
148         // negativeItemLabelFallbackPosition
149
r1.setNegativeItemLabelPositionFallback(new ItemLabelPosition());
150         assertFalse(r1.equals(r2));
151         r2.setNegativeItemLabelPositionFallback(new ItemLabelPosition());
152         assertTrue(r1.equals(r2));
153     }
154
155     /**
156      * Two objects that are equal are required to return the same hashCode.
157      */

158     public void testHashcode() {
159         XYBarRenderer r1 = new XYBarRenderer();
160         XYBarRenderer r2 = new XYBarRenderer();
161         assertTrue(r1.equals(r2));
162         int h1 = r1.hashCode();
163         int h2 = r2.hashCode();
164         assertEquals(h1, h2);
165     }
166     
167     /**
168      * Confirm that cloning works.
169      */

170     public void testCloning() {
171         XYBarRenderer r1 = new XYBarRenderer();
172         XYBarRenderer r2 = null;
173         try {
174             r2 = (XYBarRenderer) r1.clone();
175         }
176         catch (CloneNotSupportedException JavaDoc e) {
177             System.err.println("Failed to clone.");
178         }
179         assertTrue(r1 != r2);
180         assertTrue(r1.getClass() == r2.getClass());
181         assertTrue(r1.equals(r2));
182     }
183
184     /**
185      * Serialize an instance, restore it, and check for equality.
186      */

187     public void testSerialization() {
188
189         XYBarRenderer r1 = new XYBarRenderer();
190         XYBarRenderer r2 = null;
191
192         try {
193             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
194             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
195             out.writeObject(r1);
196             out.close();
197
198             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
199                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
200             );
201             r2 = (XYBarRenderer) in.readObject();
202             in.close();
203         }
204         catch (Exception JavaDoc e) {
205             System.out.println(e.toString());
206         }
207         assertEquals(r1, r2);
208
209     }
210
211     /**
212      * Serialize an instance, restore it, and check for equality.
213      */

214     public void testSerialization2() {
215
216         XYBarRenderer r1 = new XYBarRenderer();
217         r1.setPositiveItemLabelPositionFallback(new ItemLabelPosition());
218         XYBarRenderer r2 = null;
219
220         try {
221             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
222             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
223             out.writeObject(r1);
224             out.close();
225
226             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
227                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
228             );
229             r2 = (XYBarRenderer) in.readObject();
230             in.close();
231         }
232         catch (Exception JavaDoc e) {
233             System.out.println(e.toString());
234         }
235         assertEquals(r1, r2);
236
237     }
238
239     /**
240      * Check that the renderer is calculating the domain bounds correctly.
241      */

242     public void testFindDomainBounds() {
243         XYSeriesCollection dataset
244             = RendererXYPackageTests.createTestXYSeriesCollection();
245         JFreeChart chart = ChartFactory.createXYBarChart(
246             "Test Chart", "X", false, "Y", dataset,
247             PlotOrientation.VERTICAL, false, false, false
248         );
249         XYPlot plot = (XYPlot) chart.getPlot();
250         NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
251         domainAxis.setAutoRangeIncludesZero(false);
252         Range bounds = domainAxis.getRange();
253         assertFalse(bounds.contains(0.3));
254         assertTrue(bounds.contains(0.5));
255         assertTrue(bounds.contains(2.5));
256         assertFalse(bounds.contains(2.8));
257     }
258
259 }
260
Popular Tags