KickJava   Java API By Example, From Geeks To Geeks.

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


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  * YearTests.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: YearTests.java,v 1.4 2005/03/11 14:37:23 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 16-Nov-2001 : Version 1 (DG);
39  * 19-Mar-2002 : Added tests for constructor that uses java.util.Date to ensure
40  * it is consistent with the getStart() and getEnd() methods (DG);
41  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
42  * 13-Mar-2003 : Added serialization test (DG);
43  * 11-Jan-2005 : Added test for non-clonability (DG);
44  *
45  */

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

68 public class YearTests extends TestCase {
69
70     /**
71      * Returns the tests as a test suite.
72      *
73      * @return The test suite.
74      */

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

84     public YearTests(String JavaDoc name) {
85         super(name);
86     }
87
88     /**
89      * Common test setup.
90      */

91     protected void setUp() {
92         // no setup
93
}
94
95     /**
96      * Check that a Year instance is equal to itself.
97      *
98      * SourceForge Bug ID: 558850.
99      */

100     public void testEqualsSelf() {
101         Year year = new Year();
102         assertTrue(year.equals(year));
103     }
104
105     /**
106      * Tests the equals method.
107      */

108     public void testEquals() {
109         Year year1 = new Year(2002);
110         Year year2 = new Year(2002);
111         assertTrue(year1.equals(year2));
112     }
113
114     /**
115      * In GMT, the end of 2001 is java.util.Date(1009843199999L). Use this to
116      * check the year constructor.
117      */

118     public void testDateConstructor1() {
119
120         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
121         Date JavaDoc d1 = new Date JavaDoc(1009843199999L);
122         Date JavaDoc d2 = new Date JavaDoc(1009843200000L);
123         Year y1 = new Year(d1, zone);
124         Year y2 = new Year(d2, zone);
125
126         assertEquals(2001, y1.getYear());
127         assertEquals(1009843199999L, y1.getLastMillisecond(zone));
128
129         assertEquals(2002, y2.getYear());
130         assertEquals(1009843200000L, y2.getFirstMillisecond(zone));
131
132     }
133
134     /**
135      * In Los Angeles, the end of 2001 is java.util.Date(1009871999999L). Use
136      * this to check the year constructor.
137      */

138     public void testDateConstructor2() {
139
140         TimeZone JavaDoc zone = TimeZone.getTimeZone("America/Los_Angeles");
141         Year y1 = new Year(new Date JavaDoc(1009871999999L), zone);
142         Year y2 = new Year(new Date JavaDoc(1009872000000L), zone);
143
144         assertEquals(2001, y1.getYear());
145         assertEquals(1009871999999L, y1.getLastMillisecond(zone));
146
147         assertEquals(2002, y2.getYear());
148         assertEquals(1009872000000L, y2.getFirstMillisecond(zone));
149
150     }
151
152     /**
153      * Set up a year equal to 1900. Request the previous year, it should be
154      * null.
155      */

156     public void test1900Previous() {
157         Year current = new Year(1900);
158         Year previous = (Year) current.previous();
159         assertNull(previous);
160     }
161
162     /**
163      * Set up a year equal to 1900. Request the next year, it should be 1901.
164      */

165     public void test1900Next() {
166         Year current = new Year(1900);
167         Year next = (Year) current.next();
168         assertEquals(1901, next.getYear());
169     }
170
171     /**
172      * Set up a year equal to 9999. Request the previous year, it should be
173      * 9998.
174      */

175     public void test9999Previous() {
176         Year current = new Year(9999);
177         Year previous = (Year) current.previous();
178         assertEquals(9998, previous.getYear());
179     }
180
181     /**
182      * Set up a year equal to 9999. Request the next year, it should be null.
183      */

184     public void test9999Next() {
185         Year current = new Year(9999);
186         Year next = (Year) current.next();
187         assertNull(next);
188     }
189
190     /**
191      * Tests the year string parser.
192      */

193     public void testParseYear() {
194
195         Year year = null;
196
197         // test 1...
198
try {
199             year = Year.parseYear("2000");
200         }
201         catch (TimePeriodFormatException e) {
202             year = new Year(1900);
203         }
204         assertEquals(2000, year.getYear());
205
206         // test 2...
207
try {
208             year = Year.parseYear(" 2001 ");
209         }
210         catch (TimePeriodFormatException e) {
211             year = new Year(1900);
212         }
213         assertEquals(2001, year.getYear());
214
215         // test 3...
216
try {
217             year = Year.parseYear("99");
218         }
219         catch (TimePeriodFormatException e) {
220             year = new Year(1900);
221         }
222         assertEquals(1900, year.getYear());
223
224     }
225
226     /**
227      * Serialize an instance, restore it, and check for equality.
228      */

229     public void testSerialization() {
230
231         Year y1 = new Year(1999);
232         Year y2 = null;
233
234         try {
235             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
236             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
237             out.writeObject(y1);
238             out.close();
239
240             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
241                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
242             );
243             y2 = (Year) in.readObject();
244             in.close();
245         }
246         catch (Exception JavaDoc e) {
247             System.out.println(e.toString());
248         }
249         assertEquals(y1, y2);
250
251     }
252     
253     /**
254      * The {@link Year} class is immutable, so should not be {@link Cloneable}.
255      */

256     public void testNotCloneable() {
257         Year y = new Year(1999);
258         assertFalse(y instanceof Cloneable JavaDoc);
259     }
260     
261     /**
262      * Two objects that are equal are required to return the same hashCode.
263      */

264     public void testHashcode() {
265         Year y1 = new Year(1988);
266         Year y2 = new Year(1988);
267         assertTrue(y1.equals(y2));
268         int h1 = y1.hashCode();
269         int h2 = y2.hashCode();
270         assertEquals(h1, h2);
271     }
272
273 }
274
Popular Tags