KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > date > junit > SpreadsheetDateTests


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/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  * SpreadsheetDateTests.java
29  * -------------------------
30  * (C) Copyright 2001-2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: SpreadsheetDateTests.java,v 1.5 2006/08/29 13:44:16 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 15-Nov-2001 : Version 1 (DG);
40  * 24-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  * 29-Aug-2006 : Added checks for get/setDescription (DG);
42  *
43  */

44
45 package org.jfree.date.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
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.date.MonthConstants;
59 import org.jfree.date.SerialDate;
60 import org.jfree.date.SpreadsheetDate;
61
62 /**
63  * Tests for the {@link SpreadsheetDate} class.
64  */

65 public class SpreadsheetDateTests extends TestCase {
66
67     /** Date representing 1 January 1900. */
68     private SerialDate jan1Y1900;
69
70     /** Date representing serial day number 2. */
71     private SerialDate s2;
72
73     /**
74      * Returns a test suite for the JUnit test runner.
75      *
76      * @return the test suite.
77      */

78     public static Test suite() {
79         return new TestSuite(SpreadsheetDateTests.class);
80     }
81
82     /**
83      * Creates a new test case.
84      *
85      * @param name the name.
86      */

87     public SpreadsheetDateTests(final String JavaDoc name) {
88         super(name);
89     }
90
91     /**
92      * Setup.
93      */

94     protected void setUp() {
95         this.jan1Y1900 = new SpreadsheetDate(1, MonthConstants.JANUARY, 1900);
96         this.s2 = new SpreadsheetDate(2);
97     }
98
99     /**
100      * 1 January 1900 is a Thursday.
101      */

102     public void test1Jan1900GetDayOfWeek() {
103         final int dayOfWeek = this.jan1Y1900.getDayOfWeek();
104         assertEquals(SerialDate.MONDAY, dayOfWeek);
105     }
106
107     /**
108      * 12 November 2001 is a Monday.
109      */

110     public void test12Nov2001GetDayOfWeek() {
111         SerialDate nov12Y2001 = new SpreadsheetDate(12,
112                 MonthConstants.NOVEMBER, 2001);
113         int dayOfWeek = nov12Y2001.getDayOfWeek();
114         assertEquals(SerialDate.MONDAY, dayOfWeek);
115     }
116
117     /**
118      * Day 2 is the first of the month.
119      */

120     public void testS2GetDayOfMonth() {
121         final int dayOfMonth = this.s2.getDayOfMonth();
122         assertEquals(1, dayOfMonth);
123     }
124
125     /**
126      * Day 2 is in January.
127      */

128     public void testS2GetMonth() {
129         final int month = this.s2.getMonth();
130         assertEquals(MonthConstants.JANUARY, month);
131     }
132
133     /**
134      * Day 2 is in 1900.
135      */

136     public void testS2GetYYYY() {
137         final int year = this.s2.getYYYY();
138         assertEquals(1900, year);
139     }
140
141     /**
142      * Day 37986 is 31 Dec 2003.
143      */

144     public void test37986() {
145         final SpreadsheetDate d = new SpreadsheetDate(37986);
146         assertEquals(31, d.getDayOfMonth());
147         assertEquals(MonthConstants.DECEMBER, d.getMonth());
148         assertEquals(2003, d.getYYYY());
149     }
150
151     /**
152      * Day 37987 is 1 Jan 2004.
153      */

154     public void test37987() {
155         final SpreadsheetDate d = new SpreadsheetDate(37987);
156         assertEquals(1, d.getDayOfMonth());
157         assertEquals(MonthConstants.JANUARY, d.getMonth());
158         assertEquals(2004, d.getYYYY());
159     }
160
161     /**
162      * Day 38352 is 31 Dec 2004.
163      */

164     public void test38352() {
165         final SpreadsheetDate d = new SpreadsheetDate(38352);
166         assertEquals(31, d.getDayOfMonth());
167         assertEquals(MonthConstants.DECEMBER, d.getMonth());
168         assertEquals(2004, d.getYYYY());
169     }
170
171     /**
172      * Day 38353 is 1 Jan 2005.
173      */

174     public void test38353() {
175         final SpreadsheetDate d = new SpreadsheetDate(38353);
176         assertEquals(1, d.getDayOfMonth());
177         assertEquals(MonthConstants.JANUARY, d.getMonth());
178         assertEquals(2005, d.getYYYY());
179     }
180
181     /**
182      * Create a date for serial number 36584: it should be 28-Feb-2000.
183      */

184     public void test36584() {
185         final SpreadsheetDate d = new SpreadsheetDate(36584);
186         assertEquals(28, d.getDayOfMonth());
187         assertEquals(MonthConstants.FEBRUARY, d.getMonth());
188         assertEquals(2000, d.getYYYY());
189     }
190
191     /**
192      * Create a date for serial number 36585: it should be 29-Feb-2000.
193      */

194     public void test36585() {
195         final SpreadsheetDate d = new SpreadsheetDate(36585);
196         assertEquals(29, d.getDayOfMonth());
197         assertEquals(MonthConstants.FEBRUARY, d.getMonth());
198         assertEquals(2000, d.getYYYY());
199     }
200
201     /**
202      * Create a date for serial number 36586: it should be 1-Mar-2000.
203      */

204     public void test36586() {
205         final SpreadsheetDate d = new SpreadsheetDate(36586);
206         assertEquals(1, d.getDayOfMonth());
207         assertEquals(MonthConstants.MARCH, d.getMonth());
208         assertEquals(2000, d.getYYYY());
209     }
210
211     /**
212      * Create a date for 01-Jan-1900: the serial number should be 2.
213      */

214     public void test01Jan1900ToSerial() {
215         final int serial = this.jan1Y1900.toSerial();
216         assertEquals(2, serial);
217     }
218
219     /**
220      * Create a date for 28-Feb-1900: the serial number should be 60.
221      */

222     public void test28Feb1900ToSerial() {
223         SpreadsheetDate d = new SpreadsheetDate(28, MonthConstants.FEBRUARY,
224                 1900);
225         assertEquals(60, d.toSerial());
226     }
227
228     /**
229      * Create a date for 01-Mar-1900: the serial number should be 61.
230      */

231     public void test01Mar1900ToSerial() {
232         SpreadsheetDate d = new SpreadsheetDate(1, MonthConstants.MARCH, 1900);
233         assertEquals(61, d.toSerial());
234     }
235
236     /**
237      * Create a date for 31-Dec-1999: the serial number should be 36525.
238      */

239     public void test31Dec1999ToSerial() {
240         SpreadsheetDate d = new SpreadsheetDate(31, MonthConstants.DECEMBER,
241                 1999);
242         assertEquals(36525, d.toSerial());
243     }
244
245     /**
246      * Create a date for 1-Jan-2000: the serial number should be 36526.
247      */

248     public void test01Jan2000ToSerial() {
249         SpreadsheetDate d = new SpreadsheetDate(1, MonthConstants.JANUARY,
250                 2000);
251         assertEquals(36526, d.toSerial());
252     }
253
254     /**
255      * Create a date for 31-Jan-2000: the serial number should be 36556.
256      */

257     public void test31Jan2000ToSerial() {
258         SpreadsheetDate d = new SpreadsheetDate(31, MonthConstants.JANUARY,
259                 2000);
260         assertEquals(36556, d.toSerial());
261     }
262
263     /**
264      * Create a date for 01-Feb-2000: the serial number should be 36557.
265      */

266     public void test01Feb2000ToSerial() {
267         SpreadsheetDate d = new SpreadsheetDate(1, MonthConstants.FEBRUARY,
268                 2000);
269         assertEquals(36557, d.toSerial());
270     }
271
272     /**
273      * Create a date for 28-Feb-2000: the serial number should be 36584.
274      */

275     public void test28Feb2000ToSerial() {
276         SpreadsheetDate d = new SpreadsheetDate(28, MonthConstants.FEBRUARY,
277                 2000);
278         assertEquals(36584, d.toSerial());
279     }
280
281     /**
282      * Create a date for 29-Feb-2000: the serial number should be 36585.
283      */

284     public void test29feb2000ToSerial() {
285         SpreadsheetDate d = new SpreadsheetDate(29, MonthConstants.FEBRUARY,
286                 2000);
287         assertEquals(36585, d.toSerial());
288     }
289
290     /**
291      * Create a date for 1-Mar-2000: the serial number should be 36586.
292      */

293     public void test1mar2000ToSerial() {
294         SpreadsheetDate d = new SpreadsheetDate(1, MonthConstants.MARCH, 2000);
295         assertEquals(36586, d.toSerial());
296     }
297
298     /**
299      * Serialize an instance, restore it, and check for equality.
300      */

301     public void testSerialization() {
302
303         final SpreadsheetDate d1 = new SpreadsheetDate(15, 4, 2000);
304         SpreadsheetDate d2 = null;
305
306         try {
307             final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
308             final ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
309             out.writeObject(d1);
310             out.close();
311
312             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
313                     buffer.toByteArray()));
314             d2 = (SpreadsheetDate) in.readObject();
315             in.close();
316         }
317         catch (Exception JavaDoc e) {
318             System.out.println(e.toString());
319         }
320         assertEquals(d1, d2);
321
322     }
323
324     /**
325      * Some checks for the getDescription() method.
326      */

327     public void testGetDescription() {
328         SpreadsheetDate d1 = new SpreadsheetDate(15, 4, 2000);
329         assertEquals(null, d1.getDescription());
330         d1.setDescription("XYZ");
331         assertEquals("XYZ", d1.getDescription());
332     }
333
334     /**
335      * Some checks for the setDescription() method.
336      */

337     public void testSetDescription() {
338         SpreadsheetDate d1 = new SpreadsheetDate(15, 4, 2000);
339         assertEquals(null, d1.getDescription());
340         d1.setDescription("XYZ");
341         assertEquals("XYZ", d1.getDescription());
342         d1.setDescription(null);
343         assertEquals(null, d1.getDescription());
344     }
345
346 }
347
Popular Tags