KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PeriodAxisTests.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: PeriodAxisTests.java,v 1.1.2.1 2006/10/03 15:41:22 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-Jun-2003 : Version 1 (DG);
40  * 07-Jan-2005 : Added test for hashCode() method (DG);
41  *
42  */

43
44 package org.jfree.chart.axis.junit;
45
46 import java.awt.BasicStroke JavaDoc;
47 import java.awt.Color JavaDoc;
48 import java.awt.Stroke 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.SimpleDateFormat JavaDoc;
56 import java.util.SimpleTimeZone JavaDoc;
57 import java.util.TimeZone JavaDoc;
58
59 import junit.framework.Test;
60 import junit.framework.TestCase;
61 import junit.framework.TestSuite;
62
63 import org.jfree.chart.axis.PeriodAxis;
64 import org.jfree.chart.axis.PeriodAxisLabelInfo;
65 import org.jfree.data.time.Day;
66 import org.jfree.data.time.Minute;
67 import org.jfree.data.time.Month;
68 import org.jfree.data.time.Quarter;
69 import org.jfree.data.time.Second;
70 import org.jfree.data.time.Year;
71
72 /**
73  * Tests for the {@link PeriodAxis} class.
74  */

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

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

91     public PeriodAxisTests(String JavaDoc name) {
92         super(name);
93     }
94
95     /**
96      * Confirm that the equals() method can distinguish all the required fields.
97      */

98     public void testEquals() {
99         
100         PeriodAxis a1 = new PeriodAxis("Test");
101         PeriodAxis a2 = new PeriodAxis("Test");
102         assertTrue(a1.equals(a2));
103         assertTrue(a2.equals(a1));
104         
105         a1.setFirst(new Year(2000));
106         assertFalse(a1.equals(a2));
107         a2.setFirst(new Year(2000));
108         assertTrue(a1.equals(a2));
109         
110         a1.setLast(new Year(2004));
111         assertFalse(a1.equals(a2));
112         a2.setLast(new Year(2004));
113         assertTrue(a1.equals(a2));
114
115         a1.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
116         assertFalse(a1.equals(a2));
117         a2.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
118         assertTrue(a1.equals(a2));
119         
120         a1.setAutoRangeTimePeriodClass(Quarter.class);
121         assertFalse(a1.equals(a2));
122         a2.setAutoRangeTimePeriodClass(Quarter.class);
123         assertTrue(a1.equals(a2));
124         
125         PeriodAxisLabelInfo info[] = new PeriodAxisLabelInfo[1];
126         info[0] = new PeriodAxisLabelInfo(
127             Month.class, new SimpleDateFormat JavaDoc("MMM")
128         );
129         
130         a1.setLabelInfo(info);
131         assertFalse(a1.equals(a2));
132         a2.setLabelInfo(info);
133         assertTrue(a1.equals(a2));
134         
135         a1.setMajorTickTimePeriodClass(Minute.class);
136         assertFalse(a1.equals(a2));
137         a2.setMajorTickTimePeriodClass(Minute.class);
138         assertTrue(a1.equals(a2));
139         
140         a1.setMinorTickMarksVisible(!a1.isMinorTickMarksVisible());
141         assertFalse(a1.equals(a2));
142         a2.setMinorTickMarksVisible(a1.isMinorTickMarksVisible());
143         assertTrue(a1.equals(a2));
144         
145         a1.setMinorTickTimePeriodClass(Minute.class);
146         assertFalse(a1.equals(a2));
147         a2.setMinorTickTimePeriodClass(Minute.class);
148         assertTrue(a1.equals(a2));
149
150         Stroke JavaDoc s = new BasicStroke JavaDoc(1.23f);
151         a1.setMinorTickMarkStroke(s);
152         assertFalse(a1.equals(a2));
153         a2.setMinorTickMarkStroke(s);
154         assertTrue(a1.equals(a2));
155     
156         a1.setMinorTickMarkPaint(Color.blue);
157         assertFalse(a1.equals(a2));
158         a2.setMinorTickMarkPaint(Color.blue);
159         assertTrue(a1.equals(a2));
160     
161     }
162     
163     /**
164      * Two objects that are equal are required to return the same hashCode.
165      */

166     public void testHashCode() {
167         PeriodAxis a1 = new PeriodAxis("Test");
168         PeriodAxis a2 = new PeriodAxis("Test");
169         assertTrue(a1.equals(a2));
170         int h1 = a1.hashCode();
171         int h2 = a2.hashCode();
172         assertEquals(h1, h2);
173     }
174     
175     /**
176      * Confirm that cloning works.
177      */

178     public void testCloning() {
179         PeriodAxis a1 = new PeriodAxis("Test");
180         PeriodAxis a2 = null;
181         try {
182             a2 = (PeriodAxis) a1.clone();
183         }
184         catch (CloneNotSupportedException JavaDoc e) {
185             System.err.println("Failed to clone.");
186         }
187         assertTrue(a1 != a2);
188         assertTrue(a1.getClass() == a2.getClass());
189         assertTrue(a1.equals(a2));
190         
191         // some checks that the clone is independent of the original
192
a1.setLabel("New Label");
193         assertFalse(a1.equals(a2));
194         a2.setLabel("New Label");
195         assertTrue(a1.equals(a2));
196         
197         a1.setFirst(new Year(1920));
198         assertFalse(a1.equals(a2));
199         a2.setFirst(new Year(1920));
200         assertTrue(a1.equals(a2));
201
202         a1.setLast(new Year(2020));
203         assertFalse(a1.equals(a2));
204         a2.setLast(new Year(2020));
205         assertTrue(a1.equals(a2));
206
207         PeriodAxisLabelInfo[] info = new PeriodAxisLabelInfo[2];
208         info[0] = new PeriodAxisLabelInfo(Day.class, new SimpleDateFormat JavaDoc("d"));
209         info[1] = new PeriodAxisLabelInfo(
210             Year.class, new SimpleDateFormat JavaDoc("yyyy")
211         );
212         a1.setLabelInfo(info);
213         assertFalse(a1.equals(a2));
214         a2.setLabelInfo(info);
215         assertTrue(a1.equals(a2));
216
217         a1.setAutoRangeTimePeriodClass(Second.class);
218         assertFalse(a1.equals(a2));
219         a2.setAutoRangeTimePeriodClass(Second.class);
220         assertTrue(a1.equals(a2));
221
222         a1.setTimeZone(new SimpleTimeZone JavaDoc(123, "Bogus"));
223         assertFalse(a1.equals(a2));
224         a2.setTimeZone(new SimpleTimeZone JavaDoc(123, "Bogus"));
225         assertTrue(a1.equals(a2));
226
227     }
228
229     /**
230      * Serialize an instance, restore it, and check for equality.
231      */

232     public void testSerialization() {
233         PeriodAxis a1 = new PeriodAxis("Test Axis");
234         PeriodAxis a2 = null;
235         try {
236             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
237             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
238             out.writeObject(a1);
239             out.close();
240
241             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
242                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
243             );
244             a2 = (PeriodAxis) in.readObject();
245             in.close();
246         }
247         catch (Exception JavaDoc e) {
248             System.out.println(e.toString());
249         }
250         boolean b = a1.equals(a2);
251         assertTrue(b);
252     }
253
254 }
255
Popular Tags