001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------------
028: * SpreadsheetDateTests.java
029: * -------------------------
030: * (C) Copyright 2001-2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: SpreadsheetDateTests.java,v 1.5 2006/08/29 13:44:16 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 15-Nov-2001 : Version 1 (DG);
040: * 24-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041: * 29-Aug-2006 : Added checks for get/setDescription (DG);
042: *
043: */
044:
045: package org.jfree.date.junit;
046:
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.date.MonthConstants;
059: import org.jfree.date.SerialDate;
060: import org.jfree.date.SpreadsheetDate;
061:
062: /**
063: * Tests for the {@link SpreadsheetDate} class.
064: */
065: public class SpreadsheetDateTests extends TestCase {
066:
067: /** Date representing 1 January 1900. */
068: private SerialDate jan1Y1900;
069:
070: /** Date representing serial day number 2. */
071: private SerialDate s2;
072:
073: /**
074: * Returns a test suite for the JUnit test runner.
075: *
076: * @return the test suite.
077: */
078: public static Test suite() {
079: return new TestSuite(SpreadsheetDateTests.class);
080: }
081:
082: /**
083: * Creates a new test case.
084: *
085: * @param name the name.
086: */
087: public SpreadsheetDateTests(final String name) {
088: super (name);
089: }
090:
091: /**
092: * Setup.
093: */
094: protected void setUp() {
095: this .jan1Y1900 = new SpreadsheetDate(1, MonthConstants.JANUARY,
096: 1900);
097: this .s2 = new SpreadsheetDate(2);
098: }
099:
100: /**
101: * 1 January 1900 is a Thursday.
102: */
103: public void test1Jan1900GetDayOfWeek() {
104: final int dayOfWeek = this .jan1Y1900.getDayOfWeek();
105: assertEquals(SerialDate.MONDAY, dayOfWeek);
106: }
107:
108: /**
109: * 12 November 2001 is a Monday.
110: */
111: public void test12Nov2001GetDayOfWeek() {
112: SerialDate nov12Y2001 = new SpreadsheetDate(12,
113: MonthConstants.NOVEMBER, 2001);
114: int dayOfWeek = nov12Y2001.getDayOfWeek();
115: assertEquals(SerialDate.MONDAY, dayOfWeek);
116: }
117:
118: /**
119: * Day 2 is the first of the month.
120: */
121: public void testS2GetDayOfMonth() {
122: final int dayOfMonth = this .s2.getDayOfMonth();
123: assertEquals(1, dayOfMonth);
124: }
125:
126: /**
127: * Day 2 is in January.
128: */
129: public void testS2GetMonth() {
130: final int month = this .s2.getMonth();
131: assertEquals(MonthConstants.JANUARY, month);
132: }
133:
134: /**
135: * Day 2 is in 1900.
136: */
137: public void testS2GetYYYY() {
138: final int year = this .s2.getYYYY();
139: assertEquals(1900, year);
140: }
141:
142: /**
143: * Day 37986 is 31 Dec 2003.
144: */
145: public void test37986() {
146: final SpreadsheetDate d = new SpreadsheetDate(37986);
147: assertEquals(31, d.getDayOfMonth());
148: assertEquals(MonthConstants.DECEMBER, d.getMonth());
149: assertEquals(2003, d.getYYYY());
150: }
151:
152: /**
153: * Day 37987 is 1 Jan 2004.
154: */
155: public void test37987() {
156: final SpreadsheetDate d = new SpreadsheetDate(37987);
157: assertEquals(1, d.getDayOfMonth());
158: assertEquals(MonthConstants.JANUARY, d.getMonth());
159: assertEquals(2004, d.getYYYY());
160: }
161:
162: /**
163: * Day 38352 is 31 Dec 2004.
164: */
165: public void test38352() {
166: final SpreadsheetDate d = new SpreadsheetDate(38352);
167: assertEquals(31, d.getDayOfMonth());
168: assertEquals(MonthConstants.DECEMBER, d.getMonth());
169: assertEquals(2004, d.getYYYY());
170: }
171:
172: /**
173: * Day 38353 is 1 Jan 2005.
174: */
175: public void test38353() {
176: final SpreadsheetDate d = new SpreadsheetDate(38353);
177: assertEquals(1, d.getDayOfMonth());
178: assertEquals(MonthConstants.JANUARY, d.getMonth());
179: assertEquals(2005, d.getYYYY());
180: }
181:
182: /**
183: * Create a date for serial number 36584: it should be 28-Feb-2000.
184: */
185: public void test36584() {
186: final SpreadsheetDate d = new SpreadsheetDate(36584);
187: assertEquals(28, d.getDayOfMonth());
188: assertEquals(MonthConstants.FEBRUARY, d.getMonth());
189: assertEquals(2000, d.getYYYY());
190: }
191:
192: /**
193: * Create a date for serial number 36585: it should be 29-Feb-2000.
194: */
195: public void test36585() {
196: final SpreadsheetDate d = new SpreadsheetDate(36585);
197: assertEquals(29, d.getDayOfMonth());
198: assertEquals(MonthConstants.FEBRUARY, d.getMonth());
199: assertEquals(2000, d.getYYYY());
200: }
201:
202: /**
203: * Create a date for serial number 36586: it should be 1-Mar-2000.
204: */
205: public void test36586() {
206: final SpreadsheetDate d = new SpreadsheetDate(36586);
207: assertEquals(1, d.getDayOfMonth());
208: assertEquals(MonthConstants.MARCH, d.getMonth());
209: assertEquals(2000, d.getYYYY());
210: }
211:
212: /**
213: * Create a date for 01-Jan-1900: the serial number should be 2.
214: */
215: public void test01Jan1900ToSerial() {
216: final int serial = this .jan1Y1900.toSerial();
217: assertEquals(2, serial);
218: }
219:
220: /**
221: * Create a date for 28-Feb-1900: the serial number should be 60.
222: */
223: public void test28Feb1900ToSerial() {
224: SpreadsheetDate d = new SpreadsheetDate(28,
225: MonthConstants.FEBRUARY, 1900);
226: assertEquals(60, d.toSerial());
227: }
228:
229: /**
230: * Create a date for 01-Mar-1900: the serial number should be 61.
231: */
232: public void test01Mar1900ToSerial() {
233: SpreadsheetDate d = new SpreadsheetDate(1,
234: MonthConstants.MARCH, 1900);
235: assertEquals(61, d.toSerial());
236: }
237:
238: /**
239: * Create a date for 31-Dec-1999: the serial number should be 36525.
240: */
241: public void test31Dec1999ToSerial() {
242: SpreadsheetDate d = new SpreadsheetDate(31,
243: MonthConstants.DECEMBER, 1999);
244: assertEquals(36525, d.toSerial());
245: }
246:
247: /**
248: * Create a date for 1-Jan-2000: the serial number should be 36526.
249: */
250: public void test01Jan2000ToSerial() {
251: SpreadsheetDate d = new SpreadsheetDate(1,
252: MonthConstants.JANUARY, 2000);
253: assertEquals(36526, d.toSerial());
254: }
255:
256: /**
257: * Create a date for 31-Jan-2000: the serial number should be 36556.
258: */
259: public void test31Jan2000ToSerial() {
260: SpreadsheetDate d = new SpreadsheetDate(31,
261: MonthConstants.JANUARY, 2000);
262: assertEquals(36556, d.toSerial());
263: }
264:
265: /**
266: * Create a date for 01-Feb-2000: the serial number should be 36557.
267: */
268: public void test01Feb2000ToSerial() {
269: SpreadsheetDate d = new SpreadsheetDate(1,
270: MonthConstants.FEBRUARY, 2000);
271: assertEquals(36557, d.toSerial());
272: }
273:
274: /**
275: * Create a date for 28-Feb-2000: the serial number should be 36584.
276: */
277: public void test28Feb2000ToSerial() {
278: SpreadsheetDate d = new SpreadsheetDate(28,
279: MonthConstants.FEBRUARY, 2000);
280: assertEquals(36584, d.toSerial());
281: }
282:
283: /**
284: * Create a date for 29-Feb-2000: the serial number should be 36585.
285: */
286: public void test29feb2000ToSerial() {
287: SpreadsheetDate d = new SpreadsheetDate(29,
288: MonthConstants.FEBRUARY, 2000);
289: assertEquals(36585, d.toSerial());
290: }
291:
292: /**
293: * Create a date for 1-Mar-2000: the serial number should be 36586.
294: */
295: public void test1mar2000ToSerial() {
296: SpreadsheetDate d = new SpreadsheetDate(1,
297: MonthConstants.MARCH, 2000);
298: assertEquals(36586, d.toSerial());
299: }
300:
301: /**
302: * Serialize an instance, restore it, and check for equality.
303: */
304: public void testSerialization() {
305:
306: final SpreadsheetDate d1 = new SpreadsheetDate(15, 4, 2000);
307: SpreadsheetDate d2 = null;
308:
309: try {
310: final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
311: final ObjectOutput out = new ObjectOutputStream(buffer);
312: out.writeObject(d1);
313: out.close();
314:
315: ObjectInput in = new ObjectInputStream(
316: new ByteArrayInputStream(buffer.toByteArray()));
317: d2 = (SpreadsheetDate) in.readObject();
318: in.close();
319: } catch (Exception e) {
320: System.out.println(e.toString());
321: }
322: assertEquals(d1, d2);
323:
324: }
325:
326: /**
327: * Some checks for the getDescription() method.
328: */
329: public void testGetDescription() {
330: SpreadsheetDate d1 = new SpreadsheetDate(15, 4, 2000);
331: assertEquals(null, d1.getDescription());
332: d1.setDescription("XYZ");
333: assertEquals("XYZ", d1.getDescription());
334: }
335:
336: /**
337: * Some checks for the setDescription() method.
338: */
339: public void testSetDescription() {
340: SpreadsheetDate d1 = new SpreadsheetDate(15, 4, 2000);
341: assertEquals(null, d1.getDescription());
342: d1.setDescription("XYZ");
343: assertEquals("XYZ", d1.getDescription());
344: d1.setDescription(null);
345: assertEquals(null, d1.getDescription());
346: }
347:
348: }
|