KickJava   Java API By Example, From Geeks To Geeks.

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


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

43
44 package org.jfree.chart.renderer.xy.junit;
45
46 import java.awt.geom.Line2D 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.plot.PlotOrientation;
62 import org.jfree.chart.plot.XYPlot;
63 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
64 import org.jfree.data.Range;
65 import org.jfree.data.xy.TableXYDataset;
66 import org.jfree.data.xy.XYSeriesCollection;
67
68 /**
69  * Tests for the {@link XYLineAndShapeRenderer} class.
70  */

71 public class XYLineAndShapeRendererTests extends TestCase {
72
73     /**
74      * Returns the tests as a test suite.
75      *
76      * @return The test suite.
77      */

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

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

94     public void testEquals() {
95         
96         XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
97         XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer();
98         assertEquals(r1, r2);
99         assertEquals(r2, r1);
100     
101         r1.setLinesVisible(true);
102         assertFalse(r1.equals(r2));
103         r2.setLinesVisible(true);
104         assertTrue(r1.equals(r2));
105         
106         r1.setSeriesLinesVisible(3, true);
107         assertFalse(r1.equals(r2));
108         r2.setSeriesLinesVisible(3, true);
109         assertTrue(r1.equals(r2));
110         
111         r1.setBaseLinesVisible(false);
112         assertFalse(r1.equals(r2));
113         r2.setBaseLinesVisible(false);
114         assertTrue(r1.equals(r2));
115         
116         r1.setLegendLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
117         assertFalse(r1.equals(r2));
118         r2.setLegendLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
119         assertTrue(r1.equals(r2));
120         
121         r1.setShapesVisible(true);
122         assertFalse(r1.equals(r2));
123         r2.setShapesVisible(true);
124         assertTrue(r1.equals(r2));
125         
126         r1.setSeriesShapesVisible(3, true);
127         assertFalse(r1.equals(r2));
128         r2.setSeriesShapesVisible(3, true);
129         assertTrue(r1.equals(r2));
130         
131         r1.setBaseShapesVisible(false);
132         assertFalse(r1.equals(r2));
133         r2.setBaseShapesVisible(false);
134         assertTrue(r1.equals(r2));
135         
136         r1.setShapesFilled(true);
137         assertFalse(r1.equals(r2));
138         r2.setShapesFilled(true);
139         assertTrue(r1.equals(r2));
140         
141         r1.setSeriesShapesFilled(3, true);
142         assertFalse(r1.equals(r2));
143         r2.setSeriesShapesFilled(3, true);
144         assertTrue(r1.equals(r2));
145         
146         r1.setBaseShapesFilled(false);
147         assertFalse(r1.equals(r2));
148         r2.setBaseShapesFilled(false);
149         assertTrue(r1.equals(r2));
150     
151         r1.setDrawOutlines(!r1.getDrawOutlines());
152         assertFalse(r1.equals(r2));
153         r2.setDrawOutlines(r1.getDrawOutlines());
154         assertTrue(r1.equals(r2));
155     
156         r1.setUseOutlinePaint(true);
157         assertFalse(r1.equals(r2));
158         r2.setUseOutlinePaint(true);
159         assertTrue(r1.equals(r2));
160     }
161
162     /**
163      * Two objects that are equal are required to return the same hashCode.
164      */

165     public void testHashcode() {
166         XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
167         XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer();
168         assertTrue(r1.equals(r2));
169         int h1 = r1.hashCode();
170         int h2 = r2.hashCode();
171         assertEquals(h1, h2);
172     }
173     
174     /**
175      * Confirm that cloning works.
176      */

177     public void testCloning() {
178         XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
179         XYLineAndShapeRenderer r2 = null;
180         try {
181             r2 = (XYLineAndShapeRenderer) r1.clone();
182         }
183         catch (CloneNotSupportedException JavaDoc e) {
184             System.err.println("Failed to clone.");
185         }
186         assertTrue(r1 != r2);
187         assertTrue(r1.getClass() == r2.getClass());
188         assertTrue(r1.equals(r2));
189     }
190
191     /**
192      * Serialize an instance, restore it, and check for equality.
193      */

194     public void testSerialization() {
195
196         XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
197         XYLineAndShapeRenderer r2 = null;
198
199         try {
200             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
201             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
202             out.writeObject(r1);
203             out.close();
204
205             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
206                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
207             );
208             r2 = (XYLineAndShapeRenderer) in.readObject();
209             in.close();
210         }
211         catch (Exception JavaDoc e) {
212             System.out.println(e.toString());
213         }
214         assertEquals(r1, r2);
215
216     }
217
218     /**
219      * Check that the renderer is calculating the domain bounds correctly.
220      */

221     public void testFindDomainBounds() {
222         XYSeriesCollection dataset
223             = RendererXYPackageTests.createTestXYSeriesCollection();
224         JFreeChart chart = ChartFactory.createXYLineChart(
225             "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL,
226             false, false, false
227         );
228         XYPlot plot = (XYPlot) chart.getPlot();
229         NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
230         domainAxis.setAutoRangeIncludesZero(false);
231         Range bounds = domainAxis.getRange();
232         assertFalse(bounds.contains(0.9));
233         assertTrue(bounds.contains(1.0));
234         assertTrue(bounds.contains(2.0));
235         assertFalse(bounds.contains(2.10));
236     }
237     
238     /**
239      * Check that the renderer is calculating the range bounds correctly.
240      */

241     public void testFindRangeBounds() {
242         TableXYDataset dataset
243             = RendererXYPackageTests.createTestTableXYDataset();
244         JFreeChart chart = ChartFactory.createXYLineChart(
245             "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL,
246             false, false, false
247         );
248         XYPlot plot = (XYPlot) chart.getPlot();
249         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
250         rangeAxis.setAutoRangeIncludesZero(false);
251         Range bounds = rangeAxis.getRange();
252         assertFalse(bounds.contains(1.0));
253         assertTrue(bounds.contains(2.0));
254         assertTrue(bounds.contains(5.0));
255         assertFalse(bounds.contains(6.0));
256     }
257
258 }
259
Popular Tags