001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, 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: * SerialDateTests.java
029: * --------------------
030: * (C) Copyright 2001-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: SerialDateTests.java,v 1.6 2005/11/16 15:58:40 taqua Exp $
036: *
037: * Changes
038: * -------
039: * 15-Nov-2001 : Version 1 (DG);
040: * 25-Jun-2002 : Removed unnecessary import (DG);
041: * 24-Oct-2002 : Fixed errors reported by Checkstyle (DG);
042: * 13-Mar-2003 : Added serialization test (DG);
043: * 05-Jan-2005 : Added test for bug report 1096282 (DG);
044: *
045: */
046:
047: package org.jfree.date.junit;
048:
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.date.MonthConstants;
061: import org.jfree.date.SerialDate;
062:
063: /**
064: * Some JUnit tests for the {@link SerialDate} class.
065: */
066: public class SerialDateTests extends TestCase {
067:
068: /** Date representing November 9. */
069: private SerialDate nov9Y2001;
070:
071: /**
072: * Creates a new test case.
073: *
074: * @param name the name.
075: */
076: public SerialDateTests(final String name) {
077: super (name);
078: }
079:
080: /**
081: * Returns a test suite for the JUnit test runner.
082: *
083: * @return The test suite.
084: */
085: public static Test suite() {
086: return new TestSuite(SerialDateTests.class);
087: }
088:
089: /**
090: * Problem set up.
091: */
092: protected void setUp() {
093: this .nov9Y2001 = SerialDate.createInstance(9,
094: MonthConstants.NOVEMBER, 2001);
095: }
096:
097: /**
098: * 9 Nov 2001 plus two months should be 9 Jan 2002.
099: */
100: public void testAddMonthsTo9Nov2001() {
101: final SerialDate jan9Y2002 = SerialDate.addMonths(2,
102: this .nov9Y2001);
103: final SerialDate answer = SerialDate.createInstance(9, 1, 2002);
104: assertEquals(answer, jan9Y2002);
105: }
106:
107: /**
108: * A test case for a reported bug, now fixed.
109: */
110: public void testAddMonthsTo5Oct2003() {
111: final SerialDate d1 = SerialDate.createInstance(5,
112: MonthConstants.OCTOBER, 2003);
113: final SerialDate d2 = SerialDate.addMonths(2, d1);
114: assertEquals(d2, SerialDate.createInstance(5,
115: MonthConstants.DECEMBER, 2003));
116: }
117:
118: /**
119: * A test case for a reported bug, now fixed.
120: */
121: public void testAddMonthsTo1Jan2003() {
122: final SerialDate d1 = SerialDate.createInstance(1,
123: MonthConstants.JANUARY, 2003);
124: final SerialDate d2 = SerialDate.addMonths(0, d1);
125: assertEquals(d2, d1);
126: }
127:
128: /**
129: * Monday preceding Friday 9 November 2001 should be 5 November.
130: */
131: public void testMondayPrecedingFriday9Nov2001() {
132: SerialDate mondayBefore = SerialDate.getPreviousDayOfWeek(
133: SerialDate.MONDAY, this .nov9Y2001);
134: assertEquals(5, mondayBefore.getDayOfMonth());
135: }
136:
137: /**
138: * Monday following Friday 9 November 2001 should be 12 November.
139: */
140: public void testMondayFollowingFriday9Nov2001() {
141: SerialDate mondayAfter = SerialDate.getFollowingDayOfWeek(
142: SerialDate.MONDAY, this .nov9Y2001);
143: assertEquals(12, mondayAfter.getDayOfMonth());
144: }
145:
146: /**
147: * Monday nearest Friday 9 November 2001 should be 12 November.
148: */
149: public void testMondayNearestFriday9Nov2001() {
150: SerialDate mondayNearest = SerialDate.getNearestDayOfWeek(
151: SerialDate.MONDAY, this .nov9Y2001);
152: assertEquals(12, mondayNearest.getDayOfMonth());
153: }
154:
155: /**
156: * The Monday nearest to 22nd January 1970 falls on the 19th.
157: */
158: public void testMondayNearest22Jan1970() {
159: SerialDate jan22Y1970 = SerialDate.createInstance(22,
160: MonthConstants.JANUARY, 1970);
161: SerialDate mondayNearest = SerialDate.getNearestDayOfWeek(
162: SerialDate.MONDAY, jan22Y1970);
163: assertEquals(19, mondayNearest.getDayOfMonth());
164: }
165:
166: /**
167: * Problem that the conversion of days to strings returns the right result. Actually, this
168: * result depends on the Locale so this test needs to be modified.
169: */
170: public void testWeekdayCodeToString() {
171:
172: final String test = SerialDate
173: .weekdayCodeToString(SerialDate.SATURDAY);
174: assertEquals("Saturday", test);
175:
176: }
177:
178: /**
179: * Test the conversion of a string to a weekday. Note that this test will fail if the
180: * default locale doesn't use English weekday names...devise a better test!
181: */
182: public void testStringToWeekday() {
183:
184: int weekday = SerialDate.stringToWeekdayCode("Wednesday");
185: assertEquals(SerialDate.WEDNESDAY, weekday);
186:
187: weekday = SerialDate.stringToWeekdayCode(" Wednesday ");
188: assertEquals(SerialDate.WEDNESDAY, weekday);
189:
190: weekday = SerialDate.stringToWeekdayCode("Wed");
191: assertEquals(SerialDate.WEDNESDAY, weekday);
192:
193: }
194:
195: /**
196: * Test the conversion of a string to a month. Note that this test will fail if the default
197: * locale doesn't use English month names...devise a better test!
198: */
199: public void testStringToMonthCode() {
200:
201: int m = SerialDate.stringToMonthCode("January");
202: assertEquals(MonthConstants.JANUARY, m);
203:
204: m = SerialDate.stringToMonthCode(" January ");
205: assertEquals(MonthConstants.JANUARY, m);
206:
207: m = SerialDate.stringToMonthCode("Jan");
208: assertEquals(MonthConstants.JANUARY, m);
209:
210: }
211:
212: /**
213: * Tests the conversion of a month code to a string.
214: */
215: public void testMonthCodeToStringCode() {
216:
217: final String test = SerialDate
218: .monthCodeToString(MonthConstants.DECEMBER);
219: assertEquals("December", test);
220:
221: }
222:
223: /**
224: * 1900 is not a leap year.
225: */
226: public void testIsNotLeapYear1900() {
227: assertTrue(!SerialDate.isLeapYear(1900));
228: }
229:
230: /**
231: * 2000 is a leap year.
232: */
233: public void testIsLeapYear2000() {
234: assertTrue(SerialDate.isLeapYear(2000));
235: }
236:
237: /**
238: * The number of leap years from 1900 up-to-and-including 1899 is 0.
239: */
240: public void testLeapYearCount1899() {
241: assertEquals(SerialDate.leapYearCount(1899), 0);
242: }
243:
244: /**
245: * The number of leap years from 1900 up-to-and-including 1903 is 0.
246: */
247: public void testLeapYearCount1903() {
248: assertEquals(SerialDate.leapYearCount(1903), 0);
249: }
250:
251: /**
252: * The number of leap years from 1900 up-to-and-including 1904 is 1.
253: */
254: public void testLeapYearCount1904() {
255: assertEquals(SerialDate.leapYearCount(1904), 1);
256: }
257:
258: /**
259: * The number of leap years from 1900 up-to-and-including 1999 is 24.
260: */
261: public void testLeapYearCount1999() {
262: assertEquals(SerialDate.leapYearCount(1999), 24);
263: }
264:
265: /**
266: * The number of leap years from 1900 up-to-and-including 2000 is 25.
267: */
268: public void testLeapYearCount2000() {
269: assertEquals(SerialDate.leapYearCount(2000), 25);
270: }
271:
272: /**
273: * Serialize an instance, restore it, and check for equality.
274: */
275: public void testSerialization() {
276:
277: SerialDate d1 = SerialDate.createInstance(15, 4, 2000);
278: SerialDate d2 = null;
279:
280: try {
281: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
282: ObjectOutput out = new ObjectOutputStream(buffer);
283: out.writeObject(d1);
284: out.close();
285:
286: ObjectInput in = new ObjectInputStream(
287: new ByteArrayInputStream(buffer.toByteArray()));
288: d2 = (SerialDate) in.readObject();
289: in.close();
290: } catch (Exception e) {
291: System.out.println(e.toString());
292: }
293: assertEquals(d1, d2);
294:
295: }
296:
297: /**
298: * A test for bug report 1096282 (now fixed).
299: */
300: public void test1096282() {
301: SerialDate d = SerialDate.createInstance(29, 2, 2004);
302: d = SerialDate.addYears(1, d);
303: SerialDate expected = SerialDate.createInstance(28, 2, 2005);
304: assertTrue(d.isOn(expected));
305: }
306:
307: /**
308: * Miscellaneous tests for the addMonths() method.
309: */
310: public void testAddMonths() {
311: SerialDate d1 = SerialDate.createInstance(31, 5, 2004);
312:
313: SerialDate d2 = SerialDate.addMonths(1, d1);
314: assertEquals(30, d2.getDayOfMonth());
315: assertEquals(6, d2.getMonth());
316: assertEquals(2004, d2.getYYYY());
317:
318: SerialDate d3 = SerialDate.addMonths(2, d1);
319: assertEquals(31, d3.getDayOfMonth());
320: assertEquals(7, d3.getMonth());
321: assertEquals(2004, d3.getYYYY());
322:
323: SerialDate d4 = SerialDate.addMonths(1, SerialDate.addMonths(1,
324: d1));
325: assertEquals(30, d4.getDayOfMonth());
326: assertEquals(7, d4.getMonth());
327: assertEquals(2004, d4.getYYYY());
328: }
329: }
|