KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > junit > NumberAxisTests


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  * NumberAxisTests.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: NumberAxisTests.java,v 1.4 2005/03/16 12:10:45 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 26-Mar-2003 : Version 1 (DG);
39  * 14-Aug-2003 : Added tests for equals() method (DG);
40  * 05-Oct-2004 : Added tests to pick up a bug in the auto-range calculation for
41  * a domain axis on an XYPlot using an XYSeriesCollection (DG);
42  * 07-Jan-2005 : Added test for hashCode() (DG):
43  *
44  */

45
46 package org.jfree.chart.axis.junit;
47
48 import java.awt.geom.Rectangle2D JavaDoc;
49 import java.io.ByteArrayInputStream JavaDoc;
50 import java.io.ByteArrayOutputStream JavaDoc;
51 import java.io.ObjectInput JavaDoc;
52 import java.io.ObjectInputStream JavaDoc;
53 import java.io.ObjectOutput JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55 import java.text.DecimalFormat JavaDoc;
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.axis.NumberTickUnit;
65 import org.jfree.chart.plot.CategoryPlot;
66 import org.jfree.chart.plot.PlotOrientation;
67 import org.jfree.chart.plot.XYPlot;
68 import org.jfree.data.category.DefaultCategoryDataset;
69 import org.jfree.data.xy.XYSeries;
70 import org.jfree.data.xy.XYSeriesCollection;
71 import org.jfree.ui.RectangleEdge;
72
73 /**
74  * Tests for the {@link NumberAxis} class.
75  */

76 public class NumberAxisTests extends TestCase {
77
78     /**
79      * Returns the tests as a test suite.
80      *
81      * @return The test suite.
82      */

83     public static Test suite() {
84         return new TestSuite(NumberAxisTests.class);
85     }
86
87     /**
88      * Constructs a new set of tests.
89      *
90      * @param name the name of the tests.
91      */

92     public NumberAxisTests(String JavaDoc name) {
93         super(name);
94     }
95     
96     /**
97      * Confirm that cloning works.
98      */

99     public void testCloning() {
100         NumberAxis a1 = new NumberAxis("Test");
101         NumberAxis a2 = null;
102         try {
103             a2 = (NumberAxis) a1.clone();
104         }
105         catch (CloneNotSupportedException JavaDoc e) {
106             System.err.println("Failed to clone.");
107         }
108         assertTrue(a1 != a2);
109         assertTrue(a1.getClass() == a2.getClass());
110         assertTrue(a1.equals(a2));
111     }
112
113     /**
114      * Confirm that the equals method can distinguish all the required fields.
115      */

116     public void testEquals() {
117         
118         NumberAxis a1 = new NumberAxis("Test");
119         NumberAxis a2 = new NumberAxis("Test");
120         assertTrue(a1.equals(a2));
121         
122         //private boolean autoRangeIncludesZero;
123
a1.setAutoRangeIncludesZero(false);
124         assertFalse(a1.equals(a2));
125         a2.setAutoRangeIncludesZero(false);
126         assertTrue(a1.equals(a2));
127
128         //private boolean autoRangeStickyZero;
129
a1.setAutoRangeStickyZero(false);
130         assertFalse(a1.equals(a2));
131         a2.setAutoRangeStickyZero(false);
132         assertTrue(a1.equals(a2));
133
134         //private NumberTickUnit tickUnit;
135
a1.setTickUnit(new NumberTickUnit(25.0));
136         assertFalse(a1.equals(a2));
137         a2.setTickUnit(new NumberTickUnit(25.0));
138         assertTrue(a1.equals(a2));
139
140         //private NumberFormat numberFormatOverride;
141
a1.setNumberFormatOverride(new DecimalFormat JavaDoc("0.00"));
142         assertFalse(a1.equals(a2));
143         a2.setNumberFormatOverride(new DecimalFormat JavaDoc("0.00"));
144         assertTrue(a1.equals(a2));
145
146     }
147
148     /**
149      * Two objects that are equal are required to return the same hashCode.
150      */

151     public void testHashCode() {
152         NumberAxis a1 = new NumberAxis("Test");
153         NumberAxis a2 = new NumberAxis("Test");
154         assertTrue(a1.equals(a2));
155         int h1 = a1.hashCode();
156         int h2 = a2.hashCode();
157         assertEquals(h1, h2);
158     }
159
160     private static final double EPSILON = 0.0000001;
161     
162     /**
163      * Test the translation of Java2D values to data values.
164      */

165     public void testTranslateJava2DToValue() {
166         NumberAxis axis = new NumberAxis();
167         axis.setRange(50.0, 100.0);
168         Rectangle2D JavaDoc dataArea = new Rectangle2D.Double JavaDoc(10.0, 50.0, 400.0, 300.0);
169         double y1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
170         assertEquals(y1, 95.8333333, EPSILON);
171         double y2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
172         assertEquals(y2, 95.8333333, EPSILON);
173         double x1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
174         assertEquals(x1, 58.125, EPSILON);
175         double x2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
176         assertEquals(x2, 58.125, EPSILON);
177         axis.setInverted(true);
178         double y3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
179         assertEquals(y3, 54.1666667, EPSILON);
180         double y4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
181         assertEquals(y4, 54.1666667, EPSILON);
182         double x3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
183         assertEquals(x3, 91.875, EPSILON);
184         double x4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
185         assertEquals(x4, 91.875, EPSILON);
186         
187     }
188     
189     /**
190      * Serialize an instance, restore it, and check for equality.
191      */

192     public void testSerialization() {
193
194         NumberAxis a1 = new NumberAxis("Test Axis");
195         NumberAxis a2 = null;
196
197         try {
198             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
199             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
200             out.writeObject(a1);
201             out.close();
202
203             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
204                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
205             );
206             a2 = (NumberAxis) in.readObject();
207             in.close();
208         }
209         catch (Exception JavaDoc e) {
210             System.out.println(e.toString());
211         }
212         assertEquals(a1, a2);
213
214     }
215
216     /**
217      * A simple test for the auto-range calculation looking at a
218      * NumberAxis used as the range axis for a CategoryPlot.
219      */

220     public void testAutoRange1() {
221         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
222         dataset.setValue(100.0, "Row 1", "Column 1");
223         dataset.setValue(200.0, "Row 1", "Column 2");
224         JFreeChart chart = ChartFactory.createBarChart(
225             "Test",
226             "Categories",
227             "Value",
228             dataset,
229             PlotOrientation.VERTICAL,
230             false,
231             false,
232             false
233         );
234         CategoryPlot plot = (CategoryPlot) chart.getPlot();
235         NumberAxis axis = (NumberAxis) plot.getRangeAxis();
236         assertEquals(axis.getLowerBound(), 0.0, EPSILON);
237         assertEquals(axis.getUpperBound(), 210.0, EPSILON);
238     }
239     
240     /**
241      * A simple test for the auto-range calculation looking at a
242      * NumberAxis used as the range axis for a CategoryPlot. In this
243      * case, the 'autoRangeIncludesZero' flag is set to false.
244      */

245     public void testAutoRange2() {
246         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
247         dataset.setValue(100.0, "Row 1", "Column 1");
248         dataset.setValue(200.0, "Row 1", "Column 2");
249         JFreeChart chart = ChartFactory.createBarChart(
250             "Test",
251             "Categories",
252             "Value",
253             dataset,
254             PlotOrientation.VERTICAL,
255             false,
256             false,
257             false
258         );
259         CategoryPlot plot = (CategoryPlot) chart.getPlot();
260         NumberAxis axis = (NumberAxis) plot.getRangeAxis();
261         axis.setAutoRangeIncludesZero(false);
262         assertEquals(axis.getLowerBound(), 95.0, EPSILON);
263         assertEquals(axis.getUpperBound(), 205.0, EPSILON);
264     }
265     
266     /**
267      * A simple test for the auto-range calculation looking at a
268      * NumberAxis used as the range axis for a CategoryPlot. In this
269      * case, the 'autoRangeIncludesZero' flag is set to false AND the
270      * original dataset is replaced with a new dataset.
271      */

272     public void testAutoRange3() {
273         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
274         dataset.setValue(100.0, "Row 1", "Column 1");
275         dataset.setValue(200.0, "Row 1", "Column 2");
276         JFreeChart chart = ChartFactory.createBarChart(
277             "Test",
278             "Categories",
279             "Value",
280             dataset,
281             PlotOrientation.VERTICAL,
282             false,
283             false,
284             false
285         );
286         CategoryPlot plot = (CategoryPlot) chart.getPlot();
287         NumberAxis axis = (NumberAxis) plot.getRangeAxis();
288         axis.setAutoRangeIncludesZero(false);
289         assertEquals(axis.getLowerBound(), 95.0, EPSILON);
290         assertEquals(axis.getUpperBound(), 205.0, EPSILON);
291         
292         // now replacing the dataset should update the axis range...
293
DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
294         dataset2.setValue(900.0, "Row 1", "Column 1");
295         dataset2.setValue(1000.0, "Row 1", "Column 2");
296         plot.setDataset(dataset2);
297         assertEquals(axis.getLowerBound(), 895.0, EPSILON);
298         assertEquals(axis.getUpperBound(), 1005.0, EPSILON);
299     }
300     
301     /**
302      * Checks that the auto-range for the domain axis on an XYPlot is
303      * working as expected.
304      */

305     public void testXYAutoRange1() {
306         XYSeries series = new XYSeries("Series 1");
307         series.add(1.0, 1.0);
308         series.add(2.0, 2.0);
309         series.add(3.0, 3.0);
310         XYSeriesCollection dataset = new XYSeriesCollection();
311         dataset.addSeries(series);
312         JFreeChart chart = ChartFactory.createScatterPlot(
313             "Test",
314             "X",
315             "Y",
316             dataset,
317             PlotOrientation.VERTICAL,
318             false,
319             false,
320             false
321         );
322         XYPlot plot = (XYPlot) chart.getPlot();
323         NumberAxis axis = (NumberAxis) plot.getDomainAxis();
324         axis.setAutoRangeIncludesZero(false);
325         assertEquals(0.9, axis.getLowerBound(), EPSILON);
326         assertEquals(3.1, axis.getUpperBound(), EPSILON);
327     }
328     
329     /**
330      * Checks that the auto-range for the range axis on an XYPlot is
331      * working as expected.
332      */

333     public void testXYAutoRange2() {
334         XYSeries series = new XYSeries("Series 1");
335         series.add(1.0, 1.0);
336         series.add(2.0, 2.0);
337         series.add(3.0, 3.0);
338         XYSeriesCollection dataset = new XYSeriesCollection();
339         dataset.addSeries(series);
340         JFreeChart chart = ChartFactory.createScatterPlot(
341             "Test",
342             "X",
343             "Y",
344             dataset,
345             PlotOrientation.VERTICAL,
346             false,
347             false,
348             false
349         );
350         XYPlot plot = (XYPlot) chart.getPlot();
351         NumberAxis axis = (NumberAxis) plot.getRangeAxis();
352         axis.setAutoRangeIncludesZero(false);
353         assertEquals(0.9, axis.getLowerBound(), EPSILON);
354         assertEquals(3.1, axis.getUpperBound(), EPSILON);
355     }
356
357 }
358
Popular Tags