1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* --------------------
28
* SerialDateTests.java
29
* --------------------
30
* (C) Copyright 2001-2005, by Object Refinery Limited.
32
* Original Author: David Gilbert (for Object Refinery Limited);
35
* $Id: SerialDateTests.java,v 1.5 2005/10/18 13:15:28 mungady Exp $
39
* 15-Nov-2001 : Version 1 (DG);
40
* 25-Jun-2002 : Removed unnecessary import (DG);
41
* 24-Oct-2002 : Fixed errors reported by Checkstyle (DG);
42
* 13-Mar-2003 : Added serialization test (DG);
43
* 05-Jan-2005 : Added test for bug report 1096282 (DG);
47
package org.jfree.date.junit;
49
import java.io.ByteArrayInputStream;
50
import java.io.ByteArrayOutputStream;
51
import java.io.ObjectInput;
52
import java.io.ObjectInputStream;
53
import java.io.ObjectOutput;
54
import java.io.ObjectOutputStream;
56
import junit.framework.Test;
57
import junit.framework.TestCase;
58
import junit.framework.TestSuite;
60
import org.jfree.date.MonthConstants;
61
import org.jfree.date.SerialDate;
64
* Some JUnit tests for the {@link SerialDate} class.
66
public class SerialDateTests extends TestCase {
68
/** Date representing November 9. */
69
private SerialDate nov9Y2001;
72
* Creates a new test case.
74
* @param name the name.
76
public SerialDateTests(final String name) {
81
* Returns a test suite for the JUnit test runner.
83
* @return The test suite.
85
public static Test suite() {
86
return new TestSuite(SerialDateTests.class);
92
protected void setUp() {
93
this.nov9Y2001 = SerialDate.createInstance(9, MonthConstants.NOVEMBER, 2001);
97
* 9 Nov 2001 plus two months should be 9 Jan 2002.
99
public void testAddMonthsTo9Nov2001() {
100
final SerialDate jan9Y2002 = SerialDate.addMonths(2, this.nov9Y2001);
101
final SerialDate answer = SerialDate.createInstance(9, 1, 2002);
102
assertEquals(answer, jan9Y2002);
106
* A test case for a reported bug, now fixed.
108
public void testAddMonthsTo5Oct2003() {
109
final SerialDate d1 = SerialDate.createInstance(5, MonthConstants.OCTOBER, 2003);
110
final SerialDate d2 = SerialDate.addMonths(2, d1);
111
assertEquals(d2, SerialDate.createInstance(5, MonthConstants.DECEMBER, 2003));
115
* A test case for a reported bug, now fixed.
117
public void testAddMonthsTo1Jan2003() {
118
final SerialDate d1 = SerialDate.createInstance(1, MonthConstants.JANUARY, 2003);
119
final SerialDate d2 = SerialDate.addMonths(0, d1);
120
assertEquals(d2, d1);
124
* Monday preceding Friday 9 November 2001 should be 5 November.
126
public void testMondayPrecedingFriday9Nov2001() {
127
SerialDate mondayBefore = SerialDate.getPreviousDayOfWeek(
128
SerialDate.MONDAY, this.nov9Y2001
130
assertEquals(5, mondayBefore.getDayOfMonth());
134
* Monday following Friday 9 November 2001 should be 12 November.
136
public void testMondayFollowingFriday9Nov2001() {
137
SerialDate mondayAfter = SerialDate.getFollowingDayOfWeek(
138
SerialDate.MONDAY, this.nov9Y2001
140
assertEquals(12, mondayAfter.getDayOfMonth());
144
* Monday nearest Friday 9 November 2001 should be 12 November.
146
public void testMondayNearestFriday9Nov2001() {
147
SerialDate mondayNearest = SerialDate.getNearestDayOfWeek(
148
SerialDate.MONDAY, this.nov9Y2001
150
assertEquals(12, mondayNearest.getDayOfMonth());
154
* The Monday nearest to 22nd January 1970 falls on the 19th.
156
public void testMondayNearest22Jan1970() {
157
SerialDate jan22Y1970 = SerialDate.createInstance(22, MonthConstants.JANUARY, 1970);
158
SerialDate mondayNearest = SerialDate.getNearestDayOfWeek(SerialDate.MONDAY, jan22Y1970);
159
assertEquals(19, mondayNearest.getDayOfMonth());
163
* Problem that the conversion of days to strings returns the right result. Actually, this
164
* result depends on the Locale so this test needs to be modified.
166
public void testWeekdayCodeToString() {
168
final String test = SerialDate.weekdayCodeToString(SerialDate.SATURDAY);
169
assertEquals("Saturday", test);
174
* Test the conversion of a string to a weekday. Note that this test will fail if the
175
* default locale doesn't use English weekday names...devise a better test!
177
public void testStringToWeekday() {
179
int weekday = SerialDate.stringToWeekdayCode("Wednesday");
180
assertEquals(SerialDate.WEDNESDAY, weekday);
182
weekday = SerialDate.stringToWeekdayCode(" Wednesday ");
183
assertEquals(SerialDate.WEDNESDAY, weekday);
185
weekday = SerialDate.stringToWeekdayCode("Wed");
186
assertEquals(SerialDate.WEDNESDAY, weekday);
191
* Test the conversion of a string to a month. Note that this test will fail if the default
192
* locale doesn't use English month names...devise a better test!
194
public void testStringToMonthCode() {
196
int m = SerialDate.stringToMonthCode("January");
197
assertEquals(MonthConstants.JANUARY, m);
199
m = SerialDate.stringToMonthCode(" January ");
200
assertEquals(MonthConstants.JANUARY, m);
202
m = SerialDate.stringToMonthCode("Jan");
203
assertEquals(MonthConstants.JANUARY, m);
208
* Tests the conversion of a month code to a string.
210
public void testMonthCodeToStringCode() {
212
final String test = SerialDate.monthCodeToString(MonthConstants.DECEMBER);
213
assertEquals("December", test);
218
* 1900 is not a leap year.
220
public void testIsNotLeapYear1900() {
221
assertTrue(!SerialDate.isLeapYear(1900));
225
* 2000 is a leap year.
227
public void testIsLeapYear2000() {
228
assertTrue(SerialDate.isLeapYear(2000));
232
* The number of leap years from 1900 up-to-and-including 1899 is 0.
234
public void testLeapYearCount1899() {
235
assertEquals(SerialDate.leapYearCount(1899), 0);
239
* The number of leap years from 1900 up-to-and-including 1903 is 0.
241
public void testLeapYearCount1903() {
242
assertEquals(SerialDate.leapYearCount(1903), 0);
246
* The number of leap years from 1900 up-to-and-including 1904 is 1.
248
public void testLeapYearCount1904() {
249
assertEquals(SerialDate.leapYearCount(1904), 1);
253
* The number of leap years from 1900 up-to-and-including 1999 is 24.
255
public void testLeapYearCount1999() {
256
assertEquals(SerialDate.leapYearCount(1999), 24);
260
* The number of leap years from 1900 up-to-and-including 2000 is 25.
262
public void testLeapYearCount2000() {
263
assertEquals(SerialDate.leapYearCount(2000), 25);
267
* Serialize an instance, restore it, and check for equality.
269
public void testSerialization() {
271
SerialDate d1 = SerialDate.createInstance(15, 4, 2000);
272
SerialDate d2 = null;
275
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
276
ObjectOutput out = new ObjectOutputStream(buffer);
280
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
281
d2 = (SerialDate) in.readObject();
284
catch (Exception e) {
285
System.out.println(e.toString());
287
assertEquals(d1, d2);
292
* A test for bug report 1096282 (now fixed).
294
public void test1096282() {
295
SerialDate d = SerialDate.createInstance(29, 2, 2004);
296
d = SerialDate.addYears(1, d);
297
SerialDate expected = SerialDate.createInstance(28, 2, 2005);
298
assertTrue(d.isOn(expected));
302
* Miscellaneous tests for the addMonths() method.
304
public void testAddMonths() {
305
SerialDate d1 = SerialDate.createInstance(31, 5, 2004);
307
SerialDate d2 = SerialDate.addMonths(1, d1);
308
assertEquals(30, d2.getDayOfMonth());
309
assertEquals(6, d2.getMonth());
310
assertEquals(2004, d2.getYYYY());
312
SerialDate d3 = SerialDate.addMonths(2, d1);
313
assertEquals(31, d3.getDayOfMonth());
314
assertEquals(7, d3.getMonth());
315
assertEquals(2004, d3.getYYYY());
317
SerialDate d4 = SerialDate.addMonths(1, SerialDate.addMonths(1, d1));
318
assertEquals(30, d4.getDayOfMonth());
319
assertEquals(7, d4.getMonth());
320
assertEquals(2004, d4.getYYYY());