KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > junit > QuarterTests


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  * QuarterTests.java
28  * -----------------
29  * (C) Copyright 2001-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: QuarterTests.java,v 1.5 2005/03/11 14:37:23 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 16-Nov-2001 : Version 1 (DG);
39  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 13-Mar-2003 : Added serialization test (DG);
41  * 11-Jan-2005 : Added check for non-clonability (DG);
42  *
43  */

44
45 package org.jfree.data.time.junit;
46
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 import java.util.Date JavaDoc;
54 import java.util.TimeZone JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.data.time.Quarter;
61 import org.jfree.data.time.TimePeriodFormatException;
62
63 /**
64  * Tests for the {link Quarter} class.
65  */

66 public class QuarterTests extends TestCase {
67
68     /** A quarter. */
69     private Quarter q1Y1900;
70
71     /** A quarter. */
72     private Quarter q2Y1900;
73
74     /** A quarter. */
75     private Quarter q3Y9999;
76
77     /** A quarter. */
78     private Quarter q4Y9999;
79
80     /**
81      * Returns the tests as a test suite.
82      *
83      * @return The test suite.
84      */

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

94     public QuarterTests(String JavaDoc name) {
95         super(name);
96     }
97
98     /**
99      * Common test setup.
100      */

101     protected void setUp() {
102         this.q1Y1900 = new Quarter(1, 1900);
103         this.q2Y1900 = new Quarter(2, 1900);
104         this.q3Y9999 = new Quarter(3, 9999);
105         this.q4Y9999 = new Quarter(4, 9999);
106     }
107
108     /**
109      * Check that a Quarter instance is equal to itself.
110      *
111      * SourceForge Bug ID: 558850.
112      */

113     public void testEqualsSelf() {
114         Quarter quarter = new Quarter();
115         assertTrue(quarter.equals(quarter));
116     }
117
118     /**
119      * Tests the equals method.
120      */

121     public void testEquals() {
122         Quarter q1 = new Quarter(2, 2002);
123         Quarter q2 = new Quarter(2, 2002);
124         assertTrue(q1.equals(q2));
125     }
126
127     /**
128      * In GMT, the end of Q1 2002 is java.util.Date(1017619199999L). Use this
129      * to check the quarter constructor.
130      */

131     public void testDateConstructor1() {
132
133         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
134         Quarter q1 = new Quarter(new Date JavaDoc(1017619199999L), zone);
135         Quarter q2 = new Quarter(new Date JavaDoc(1017619200000L), zone);
136
137         assertEquals(1, q1.getQuarter());
138         assertEquals(1017619199999L, q1.getLastMillisecond(zone));
139
140         assertEquals(2, q2.getQuarter());
141         assertEquals(1017619200000L, q2.getFirstMillisecond(zone));
142
143     }
144
145     /**
146      * In Istanbul, the end of Q1 2002 is java.util.Date(1017608399999L). Use
147      * this to check the quarter constructor.
148      */

149     public void testDateConstructor2() {
150
151         TimeZone JavaDoc zone = TimeZone.getTimeZone("Europe/Istanbul");
152         Quarter q1 = new Quarter(new Date JavaDoc(1017608399999L), zone);
153         Quarter q2 = new Quarter(new Date JavaDoc(1017608400000L), zone);
154
155         assertEquals(1, q1.getQuarter());
156         assertEquals(1017608399999L, q1.getLastMillisecond(zone));
157
158         assertEquals(2, q2.getQuarter());
159         assertEquals(1017608400000L, q2.getFirstMillisecond(zone));
160
161     }
162
163     /**
164      * Set up a quarter equal to Q1 1900. Request the previous quarter, it
165      * should be null.
166      */

167     public void testQ1Y1900Previous() {
168         Quarter previous = (Quarter) this.q1Y1900.previous();
169         assertNull(previous);
170     }
171
172     /**
173      * Set up a quarter equal to Q1 1900. Request the next quarter, it should
174      * be Q2 1900.
175      */

176     public void testQ1Y1900Next() {
177         Quarter next = (Quarter) this.q1Y1900.next();
178         assertEquals(this.q2Y1900, next);
179     }
180
181     /**
182      * Set up a quarter equal to Q4 9999. Request the previous quarter, it
183      * should be Q3 9999.
184      */

185     public void testQ4Y9999Previous() {
186         Quarter previous = (Quarter) this.q4Y9999.previous();
187         assertEquals(this.q3Y9999, previous);
188     }
189
190     /**
191      * Set up a quarter equal to Q4 9999. Request the next quarter, it should
192      * be null.
193      */

194     public void testQ4Y9999Next() {
195         Quarter next = (Quarter) this.q4Y9999.next();
196         assertNull(next);
197     }
198
199     /**
200      * Test the string parsing code...
201      */

202     public void testParseQuarter() {
203
204         Quarter quarter = null;
205
206         // test 1...
207
try {
208             quarter = Quarter.parseQuarter("Q1-2000");
209         }
210         catch (TimePeriodFormatException e) {
211             quarter = new Quarter(1, 1900);
212         }
213         assertEquals(1, quarter.getQuarter());
214         assertEquals(2000, quarter.getYear().getYear());
215
216         // test 2...
217
try {
218             quarter = Quarter.parseQuarter("2001-Q2");
219         }
220         catch (TimePeriodFormatException e) {
221             quarter = new Quarter(1, 1900);
222         }
223         assertEquals(2, quarter.getQuarter());
224         assertEquals(2001, quarter.getYear().getYear());
225
226         // test 3...
227
try {
228             quarter = Quarter.parseQuarter("Q3, 2002");
229         }
230         catch (TimePeriodFormatException e) {
231             quarter = new Quarter(1, 1900);
232         }
233         assertEquals(3, quarter.getQuarter());
234         assertEquals(2002, quarter.getYear().getYear());
235
236     }
237
238     /**
239      * Serialize an instance, restore it, and check for equality.
240      */

241     public void testSerialization() {
242
243         Quarter q1 = new Quarter(4, 1999);
244         Quarter q2 = null;
245
246         try {
247             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
248             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
249             out.writeObject(q1);
250             out.close();
251
252             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
253                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
254             );
255             q2 = (Quarter) in.readObject();
256             in.close();
257         }
258         catch (Exception JavaDoc e) {
259             System.out.println(e.toString());
260         }
261         assertEquals(q1, q2);
262
263     }
264     
265     /**
266      * Two objects that are equal are required to return the same hashCode.
267      */

268     public void testHashcode() {
269         Quarter q1 = new Quarter(2, 2003);
270         Quarter q2 = new Quarter(2, 2003);
271         assertTrue(q1.equals(q2));
272         int h1 = q1.hashCode();
273         int h2 = q2.hashCode();
274         assertEquals(h1, h2);
275     }
276     
277     /**
278      * The {@link Quarter} class is immutable, so should not be
279      * {@link Cloneable}.
280      */

281     public void testNotCloneable() {
282         Quarter q = new Quarter(2, 2003);
283         assertFalse(q instanceof Cloneable JavaDoc);
284     }
285
286 }
287
Popular Tags