KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MonthTests.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: MonthTests.java,v 1.5 2005/03/11 14:37:24 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 16-Nov-2001 : Version 1 (DG);
39  * 14-Feb-2002 : Order of parameters in Month(int, int) constructor
40  * changed (DG);
41  * 26-Jun-2002 : Removed unnecessary import (DG);
42  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
43  * 13-Mar-2003 : Added serialization test (DG);
44  * 21-Oct-2003 : Added hashCode test (DG);
45  * 11-Jan-2005 : Added non-clonability test (DG);
46  *
47  */

48
49 package org.jfree.data.time.junit;
50
51 import java.io.ByteArrayInputStream JavaDoc;
52 import java.io.ByteArrayOutputStream JavaDoc;
53 import java.io.ObjectInput JavaDoc;
54 import java.io.ObjectInputStream JavaDoc;
55 import java.io.ObjectOutput JavaDoc;
56 import java.io.ObjectOutputStream JavaDoc;
57 import java.util.Date JavaDoc;
58 import java.util.TimeZone JavaDoc;
59
60 import junit.framework.Test;
61 import junit.framework.TestCase;
62 import junit.framework.TestSuite;
63
64 import org.jfree.data.time.Month;
65 import org.jfree.data.time.TimePeriodFormatException;
66 import org.jfree.date.MonthConstants;
67
68 /**
69  * Tests for the {@link Month} class.
70  */

71 public class MonthTests extends TestCase {
72
73     /** A month. */
74     private Month jan1900;
75
76     /** A month. */
77     private Month feb1900;
78
79     /** A month. */
80     private Month nov9999;
81
82     /** A month. */
83     private Month dec9999;
84
85     /**
86      * Returns the tests as a test suite.
87      *
88      * @return The test suite.
89      */

90     public static Test suite() {
91         return new TestSuite(MonthTests.class);
92     }
93
94     /**
95      * Constructs a new set of tests.
96      *
97      * @param name the name of the tests.
98      */

99     public MonthTests(String JavaDoc name) {
100         super(name);
101     }
102
103     /**
104      * Common test setup.
105      */

106     protected void setUp() {
107         this.jan1900 = new Month(MonthConstants.JANUARY, 1900);
108         this.feb1900 = new Month(MonthConstants.FEBRUARY, 1900);
109         this.nov9999 = new Month(MonthConstants.NOVEMBER, 9999);
110         this.dec9999 = new Month(MonthConstants.DECEMBER, 9999);
111     }
112
113     /**
114      * Check that a Month instance is equal to itself.
115      *
116      * SourceForge Bug ID: 558850.
117      */

118     public void testEqualsSelf() {
119         Month month = new Month();
120         assertTrue(month.equals(month));
121     }
122
123     /**
124      * Tests the equals method.
125      */

126     public void testEquals() {
127         Month m1 = new Month(MonthConstants.MAY, 2002);
128         Month m2 = new Month(MonthConstants.MAY, 2002);
129         assertTrue(m1.equals(m2));
130     }
131
132     /**
133      * In GMT, the end of Feb 2000 is java.util.Date(951,868,799,999L). Use
134      * this to check the Month constructor.
135      */

136     public void testDateConstructor1() {
137
138         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
139         Month m1 = new Month(new Date JavaDoc(951868799999L), zone);
140         Month m2 = new Month(new Date JavaDoc(951868800000L), zone);
141
142         assertEquals(MonthConstants.FEBRUARY, m1.getMonth());
143         assertEquals(951868799999L, m1.getLastMillisecond(zone));
144
145         assertEquals(MonthConstants.MARCH, m2.getMonth());
146         assertEquals(951868800000L, m2.getFirstMillisecond(zone));
147
148     }
149
150     /**
151      * In Auckland, the end of Feb 2000 is java.util.Date(951,821,999,999L).
152      * Use this to check the Month constructor.
153      */

154     public void testDateConstructor2() {
155
156         TimeZone JavaDoc zone = TimeZone.getTimeZone("Pacific/Auckland");
157         Month m1 = new Month(new Date JavaDoc(951821999999L), zone);
158         Month m2 = new Month(new Date JavaDoc(951822000000L), zone);
159
160         assertEquals(MonthConstants.FEBRUARY, m1.getMonth());
161         assertEquals(951821999999L, m1.getLastMillisecond(zone));
162
163         assertEquals(MonthConstants.MARCH, m2.getMonth());
164         assertEquals(951822000000L, m2.getFirstMillisecond(zone));
165
166     }
167
168     /**
169      * Set up a month equal to Jan 1900. Request the previous month, it should
170      * be null.
171      */

172     public void testJan1900Previous() {
173         Month previous = (Month) this.jan1900.previous();
174         assertNull(previous);
175     }
176
177     /**
178      * Set up a month equal to Jan 1900. Request the next month, it should be
179      * Feb 1900.
180      */

181     public void testJan1900Next() {
182         Month next = (Month) this.jan1900.next();
183         assertEquals(this.feb1900, next);
184     }
185
186     /**
187      * Set up a month equal to Dec 9999. Request the previous month, it should
188      * be Nov 9999.
189      */

190     public void testDec9999Previous() {
191         Month previous = (Month) this.dec9999.previous();
192         assertEquals(this.nov9999, previous);
193     }
194
195     /**
196      * Set up a month equal to Dec 9999. Request the next month, it should be
197      * null.
198      */

199     public void testDec9999Next() {
200         Month next = (Month) this.dec9999.next();
201         assertNull(next);
202     }
203
204     /**
205      * Tests the string parsing code...
206      */

207     public void testParseMonth() {
208
209         Month month = null;
210
211         // test 1...
212
try {
213             month = Month.parseMonth("1990-01");
214         }
215         catch (TimePeriodFormatException e) {
216             month = new Month(1, 1900);
217         }
218         assertEquals(1, month.getMonth());
219         assertEquals(1990, month.getYear().getYear());
220
221         // test 2...
222
try {
223             month = Month.parseMonth("02-1991");
224         }
225         catch (TimePeriodFormatException e) {
226             month = new Month(1, 1900);
227         }
228         assertEquals(2, month.getMonth());
229         assertEquals(1991, month.getYear().getYear());
230
231         // test 3...
232
try {
233             month = Month.parseMonth("March 1993");
234         }
235         catch (TimePeriodFormatException e) {
236             month = new Month(1, 1900);
237         }
238         assertEquals(3, month.getMonth());
239         assertEquals(1993, month.getYear().getYear());
240
241     }
242
243     /**
244      * Serialize an instance, restore it, and check for equality.
245      */

246     public void testSerialization() {
247
248         Month m1 = new Month(12, 1999);
249         Month m2 = null;
250
251         try {
252             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
253             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
254             out.writeObject(m1);
255             out.close();
256
257             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
258                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
259             );
260             m2 = (Month) in.readObject();
261             in.close();
262         }
263         catch (Exception JavaDoc e) {
264             System.out.println(e.toString());
265         }
266         assertEquals(m1, m2);
267
268     }
269     
270     /**
271      * Two objects that are equal are required to return the same hashCode.
272      */

273     public void testHashcode() {
274         Month m1 = new Month(2, 2003);
275         Month m2 = new Month(2, 2003);
276         assertTrue(m1.equals(m2));
277         int h1 = m1.hashCode();
278         int h2 = m2.hashCode();
279         assertEquals(h1, h2);
280     }
281     
282     /**
283      * The {@link Month} class is immutable, so should not be {@link Cloneable}.
284      */

285     public void testNotCloneable() {
286         Month m = new Month(2, 2003);
287         assertFalse(m instanceof Cloneable JavaDoc);
288     }
289
290 }
291
Popular Tags