KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DateAxisTests.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: DateAxisTests.java,v 1.3 2005/03/16 12:10:45 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 22-Apr-2003 : Version 1 (DG);
39  * 07-Jan-2005 : Added test for hashCode() method (DG);
40  *
41  */

42
43 package org.jfree.chart.axis.junit;
44
45 import java.awt.geom.Rectangle2D JavaDoc;
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52 import java.text.SimpleDateFormat JavaDoc;
53 import java.util.Calendar JavaDoc;
54 import java.util.Date JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.chart.axis.DateAxis;
61 import org.jfree.chart.axis.DateTickMarkPosition;
62 import org.jfree.chart.axis.DateTickUnit;
63 import org.jfree.chart.axis.SegmentedTimeline;
64 import org.jfree.data.time.DateRange;
65 import org.jfree.ui.RectangleEdge;
66
67 /**
68  * Tests for the {@link DateAxis} class.
69  */

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

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

86     public DateAxisTests(String JavaDoc name) {
87         super(name);
88     }
89
90     /**
91      * Confirm that the equals method can distinguish all the required fields.
92      */

93     public void testEquals() {
94         
95         DateAxis a1 = new DateAxis("Test");
96         DateAxis a2 = new DateAxis("Test");
97         assertTrue(a1.equals(a2));
98         
99         // tickUnit
100
a1.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7));
101         assertFalse(a1.equals(a2));
102         a2.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7));
103         assertTrue(a1.equals(a2));
104
105         // dateFormatOverride
106
a1.setDateFormatOverride(new SimpleDateFormat JavaDoc("yyyy"));
107         assertFalse(a1.equals(a2));
108         a2.setDateFormatOverride(new SimpleDateFormat JavaDoc("yyyy"));
109         assertTrue(a1.equals(a2));
110
111         // tickMarkPosition
112
a1.setTickMarkPosition(DateTickMarkPosition.END);
113         assertFalse(a1.equals(a2));
114         a2.setTickMarkPosition(DateTickMarkPosition.END);
115         assertTrue(a1.equals(a2));
116         
117         // timeline
118
a1.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());
119         assertFalse(a1.equals(a2));
120         a2.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());
121         assertTrue(a1.equals(a2));
122         
123     }
124     
125     /**
126      * Two objects that are equal are required to return the same hashCode.
127      */

128     public void testHashCode() {
129         DateAxis a1 = new DateAxis("Test");
130         DateAxis a2 = new DateAxis("Test");
131         assertTrue(a1.equals(a2));
132         int h1 = a1.hashCode();
133         int h2 = a2.hashCode();
134         assertEquals(h1, h2);
135     }
136     
137     /**
138      * Confirm that cloning works.
139      */

140     public void testCloning() {
141         DateAxis a1 = new DateAxis("Test");
142         DateAxis a2 = null;
143         try {
144             a2 = (DateAxis) a1.clone();
145         }
146         catch (CloneNotSupportedException JavaDoc e) {
147             System.err.println("Failed to clone.");
148         }
149         assertTrue(a1 != a2);
150         assertTrue(a1.getClass() == a2.getClass());
151         assertTrue(a1.equals(a2));
152     }
153
154     /**
155      * Test that the setRange() method works.
156      */

157     public void testSetRange() {
158
159         DateAxis axis = new DateAxis("Test Axis");
160         Calendar JavaDoc calendar = Calendar.getInstance();
161         calendar.set(1999, Calendar.JANUARY, 3);
162         Date JavaDoc d1 = calendar.getTime();
163         calendar.set(1999, Calendar.JANUARY, 31);
164         Date JavaDoc d2 = calendar.getTime();
165         axis.setRange(d1, d2);
166
167         DateRange range = (DateRange) axis.getRange();
168         assertEquals(d1, range.getLowerDate());
169         assertEquals(d2, range.getUpperDate());
170
171     }
172
173     /**
174      * Test that the setMaximumDate() method works.
175      */

176     public void testSetMaximumDate() {
177
178         DateAxis axis = new DateAxis("Test Axis");
179         Date JavaDoc date = new Date JavaDoc();
180         axis.setMaximumDate(date);
181         assertEquals(date, axis.getMaximumDate());
182
183     }
184
185     /**
186      * Test that the setMinimumDate() method works.
187      */

188     public void testSetMinimumDate() {
189
190         DateAxis axis = new DateAxis("Test Axis");
191         Date JavaDoc d1 = new Date JavaDoc();
192         Date JavaDoc d2 = new Date JavaDoc(d1.getTime() + 1);
193         axis.setMaximumDate(d2);
194         axis.setMinimumDate(d1);
195         assertEquals(d1, axis.getMinimumDate());
196
197     }
198     
199     /**
200      * Tests two doubles for 'near enough' equality.
201      *
202      * @param d1 number 1.
203      * @param d2 number 2.
204      * @param tolerance maximum tolerance.
205      *
206      * @return A boolean.
207      */

208     private boolean same(double d1, double d2, double tolerance) {
209         return (Math.abs(d1 - d2) < tolerance);
210     }
211     
212     /**
213      * Test the translation of Java2D values to data values.
214      */

215     public void testJava2DToValue() {
216         DateAxis axis = new DateAxis();
217         axis.setRange(50.0, 100.0);
218         Rectangle2D JavaDoc dataArea = new Rectangle2D.Double JavaDoc(10.0, 50.0, 400.0, 300.0);
219         double y1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
220         assertTrue(same(y1, 95.8333333, 1.0));
221         double y2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
222         assertTrue(same(y2, 95.8333333, 1.0));
223         double x1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
224         assertTrue(same(x1, 58.125, 1.0));
225         double x2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
226         assertTrue(same(x2, 58.125, 1.0));
227         axis.setInverted(true);
228         double y3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
229         assertTrue(same(y3, 54.1666667, 1.0));
230         double y4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
231         assertTrue(same(y4, 54.1666667, 1.0));
232         double x3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
233         assertTrue(same(x3, 91.875, 1.0));
234         double x4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
235         assertTrue(same(x4, 91.875, 1.0));
236     }
237     
238     /**
239      * Serialize an instance, restore it, and check for equality.
240      */

241     public void testSerialization() {
242
243         DateAxis a1 = new DateAxis("Test Axis");
244         DateAxis a2 = null;
245
246         try {
247             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
248             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
249             out.writeObject(a1);
250             out.close();
251
252             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
253                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
254             );
255             a2 = (DateAxis) in.readObject();
256             in.close();
257         }
258         catch (Exception JavaDoc e) {
259             System.out.println(e.toString());
260         }
261         boolean b = a1.equals(a2);
262         assertTrue(b);
263
264     }
265
266 }
267
Popular Tags