KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DayTests.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: DayTests.java,v 1.6 2005/03/11 14:37:23 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 15-Nov-2001 : Version 1 (DG);
39  * 20-Mar-2002 : Added new tests for Day constructor and getStart() and
40  * getEnd() in different time zones (DG);
41  * 26-Jun-2002 : Removed unnecessary imports (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 test for non-clonability (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.text.ParseException JavaDoc;
58 import java.text.SimpleDateFormat JavaDoc;
59 import java.util.Date JavaDoc;
60 import java.util.GregorianCalendar JavaDoc;
61 import java.util.TimeZone JavaDoc;
62
63 import junit.framework.Test;
64 import junit.framework.TestCase;
65 import junit.framework.TestSuite;
66
67 import org.jfree.data.time.Day;
68 import org.jfree.date.MonthConstants;
69
70 /**
71  * Tests for the {@link Day} class.
72  */

73 public class DayTests extends TestCase {
74
75     /**
76      * Returns the tests as a test suite.
77      *
78      * @return The test suite.
79      */

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

89     public DayTests(String JavaDoc name) {
90         super(name);
91     }
92
93     /**
94      * Common test setup.
95      */

96     protected void setUp() {
97         // no setup required
98
}
99
100     /**
101      * Check that a Day instance is equal to itself.
102      *
103      * SourceForge Bug ID: 558850.
104      */

105     public void testEqualsSelf() {
106         Day day = new Day();
107         assertTrue(day.equals(day));
108     }
109
110     /**
111      * Tests the equals method.
112      */

113     public void testEquals() {
114         Day day1 = new Day(29, MonthConstants.MARCH, 2002);
115         Day day2 = new Day(29, MonthConstants.MARCH, 2002);
116         assertTrue(day1.equals(day2));
117     }
118
119     /**
120      * In GMT, the end of 29 Feb 2004 is java.util.Date(1,078,099,199,999L).
121      * Use this to check the day constructor.
122      */

123     public void testDateConstructor1() {
124
125         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
126         Day d1 = new Day(new Date JavaDoc(1078099199999L), zone);
127         Day d2 = new Day(new Date JavaDoc(1078099200000L), zone);
128
129         assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
130         assertEquals(1078099199999L, d1.getLastMillisecond(zone));
131
132         assertEquals(MonthConstants.MARCH, d2.getMonth());
133         assertEquals(1078099200000L, d2.getFirstMillisecond(zone));
134
135     }
136
137     /**
138      * In Helsinki, the end of 29 Feb 2004 is
139      * java.util.Date(1,078,091,999,999L). Use this to check the Day
140      * constructor.
141      */

142     public void testDateConstructor2() {
143
144         TimeZone JavaDoc zone = TimeZone.getTimeZone("Europe/Helsinki");
145         Day d1 = new Day(new Date JavaDoc(1078091999999L), zone);
146         Day d2 = new Day(new Date JavaDoc(1078092000000L), zone);
147
148         assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
149         assertEquals(1078091999999L, d1.getLastMillisecond(zone));
150
151         assertEquals(MonthConstants.MARCH, d2.getMonth());
152         assertEquals(1078092000000L, d2.getFirstMillisecond(zone));
153
154     }
155
156     /**
157      * Set up a day equal to 1 January 1900. Request the previous day, it
158      * should be null.
159      */

160     public void test1Jan1900Previous() {
161
162         Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
163         Day previous = (Day) jan1st1900.previous();
164         assertNull(previous);
165
166     }
167
168     /**
169      * Set up a day equal to 1 January 1900. Request the next day, it should
170      * be 2 January 1900.
171      */

172     public void test1Jan1900Next() {
173
174         Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
175         Day next = (Day) jan1st1900.next();
176         assertEquals(2, next.getDayOfMonth());
177
178     }
179
180     /**
181      * Set up a day equal to 31 December 9999. Request the previous day, it
182      * should be 30 December 9999.
183      */

184     public void test31Dec9999Previous() {
185
186         Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
187         Day previous = (Day) dec31st9999.previous();
188         assertEquals(30, previous.getDayOfMonth());
189
190     }
191
192     /**
193      * Set up a day equal to 31 December 9999. Request the next day, it should
194      * be null.
195      */

196     public void test31Dec9999Next() {
197
198         Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
199         Day next = (Day) dec31st9999.next();
200         assertNull(next);
201
202     }
203
204     /**
205      * Problem for date parsing.
206      * <p>
207      * This test works only correct if the short pattern of the date
208      * format is "dd/MM/yyyy". If not, this test will result in a
209      * false negative.
210      *
211      * @throws ParseException on parsing errors.
212      */

213     public void testParseDay() throws ParseException JavaDoc {
214
215         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc(2001, 12, 31);
216         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("dd/MM/yyyy");
217         Date JavaDoc reference = format.parse("31/12/2001");
218         if (reference.equals(gc.getTime())) {
219             // test 1...
220
Day d = Day.parseDay("31/12/2001");
221             assertEquals(37256, d.getSerialDate().toSerial());
222         }
223
224         // test 2...
225
Day d = Day.parseDay("2001-12-31");
226         assertEquals(37256, d.getSerialDate().toSerial());
227
228     }
229
230     /**
231      * Serialize an instance, restore it, and check for equality.
232      */

233     public void testSerialization() {
234
235         Day d1 = new Day(15, 4, 2000);
236         Day d2 = null;
237
238         try {
239             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
240             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
241             out.writeObject(d1);
242             out.close();
243
244             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
245                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
246             );
247             d2 = (Day) in.readObject();
248             in.close();
249         }
250         catch (Exception JavaDoc e) {
251             System.out.println(e.toString());
252         }
253         assertEquals(d1, d2);
254
255     }
256     
257     /**
258      * Two objects that are equal are required to return the same hashCode.
259      */

260     public void testHashcode() {
261         Day d1 = new Day(1, 2, 2003);
262         Day d2 = new Day(1, 2, 2003);
263         assertTrue(d1.equals(d2));
264         int h1 = d1.hashCode();
265         int h2 = d2.hashCode();
266         assertEquals(h1, h2);
267     }
268     
269     /**
270      * The {@link Day} class is immutable, so should not be {@link Cloneable}.
271      */

272     public void testNotCloneable() {
273         Day d = new Day(1, 2, 2003);
274         assertFalse(d instanceof Cloneable JavaDoc);
275     }
276
277 }
278
Popular Tags