001: /* ===========================================================
002: * JFreeChart : a free chart 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/jfreechart/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: * DayTests.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: DayTests.java,v 1.1.2.2 2006/10/05 13:35:28 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 15-Nov-2001 : Version 1 (DG);
040: * 20-Mar-2002 : Added new tests for Day constructor and getStart() and
041: * getEnd() in different time zones (DG);
042: * 26-Jun-2002 : Removed unnecessary imports (DG);
043: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
044: * 13-Mar-2003 : Added serialization test (DG);
045: * 21-Oct-2003 : Added hashCode test (DG);
046: * 11-Jan-2005 : Added test for non-clonability (DG);
047: * 03-Oct-2006 : Added testGetSerialIndex() (DG);
048: *
049: */
050:
051: package org.jfree.data.time.junit;
052:
053: import java.io.ByteArrayInputStream;
054: import java.io.ByteArrayOutputStream;
055: import java.io.ObjectInput;
056: import java.io.ObjectInputStream;
057: import java.io.ObjectOutput;
058: import java.io.ObjectOutputStream;
059: import java.text.ParseException;
060: import java.text.SimpleDateFormat;
061: import java.util.Calendar;
062: import java.util.Date;
063: import java.util.GregorianCalendar;
064: import java.util.Locale;
065: import java.util.TimeZone;
066:
067: import junit.framework.Test;
068: import junit.framework.TestCase;
069: import junit.framework.TestSuite;
070:
071: import org.jfree.data.time.Day;
072: import org.jfree.date.MonthConstants;
073:
074: /**
075: * Tests for the {@link Day} class.
076: */
077: public class DayTests extends TestCase {
078:
079: /**
080: * Returns the tests as a test suite.
081: *
082: * @return The test suite.
083: */
084: public static Test suite() {
085: return new TestSuite(DayTests.class);
086: }
087:
088: /**
089: * Constructs a new set of tests.
090: *
091: * @param name the name of the tests.
092: */
093: public DayTests(String name) {
094: super (name);
095: }
096:
097: /**
098: * Common test setup.
099: */
100: protected void setUp() {
101: // no setup required
102: }
103:
104: /**
105: * Check that a Day instance is equal to itself.
106: *
107: * SourceForge Bug ID: 558850.
108: */
109: public void testEqualsSelf() {
110: Day day = new Day();
111: assertTrue(day.equals(day));
112: }
113:
114: /**
115: * Tests the equals method.
116: */
117: public void testEquals() {
118: Day day1 = new Day(29, MonthConstants.MARCH, 2002);
119: Day day2 = new Day(29, MonthConstants.MARCH, 2002);
120: assertTrue(day1.equals(day2));
121: }
122:
123: /**
124: * In GMT, the end of 29 Feb 2004 is java.util.Date(1,078,099,199,999L).
125: * Use this to check the day constructor.
126: */
127: public void testDateConstructor1() {
128:
129: TimeZone zone = TimeZone.getTimeZone("GMT");
130: Day d1 = new Day(new Date(1078099199999L), zone);
131: Day d2 = new Day(new Date(1078099200000L), zone);
132:
133: assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
134: assertEquals(1078099199999L, d1.getLastMillisecond(zone));
135:
136: assertEquals(MonthConstants.MARCH, d2.getMonth());
137: assertEquals(1078099200000L, d2.getFirstMillisecond(zone));
138:
139: }
140:
141: /**
142: * In Helsinki, the end of 29 Feb 2004 is
143: * java.util.Date(1,078,091,999,999L). Use this to check the Day
144: * constructor.
145: */
146: public void testDateConstructor2() {
147:
148: TimeZone zone = TimeZone.getTimeZone("Europe/Helsinki");
149: Day d1 = new Day(new Date(1078091999999L), zone);
150: Day d2 = new Day(new Date(1078092000000L), zone);
151:
152: assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
153: assertEquals(1078091999999L, d1.getLastMillisecond(zone));
154:
155: assertEquals(MonthConstants.MARCH, d2.getMonth());
156: assertEquals(1078092000000L, d2.getFirstMillisecond(zone));
157:
158: }
159:
160: /**
161: * Set up a day equal to 1 January 1900. Request the previous day, it
162: * should be null.
163: */
164: public void test1Jan1900Previous() {
165:
166: Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
167: Day previous = (Day) jan1st1900.previous();
168: assertNull(previous);
169:
170: }
171:
172: /**
173: * Set up a day equal to 1 January 1900. Request the next day, it should
174: * be 2 January 1900.
175: */
176: public void test1Jan1900Next() {
177:
178: Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
179: Day next = (Day) jan1st1900.next();
180: assertEquals(2, next.getDayOfMonth());
181:
182: }
183:
184: /**
185: * Set up a day equal to 31 December 9999. Request the previous day, it
186: * should be 30 December 9999.
187: */
188: public void test31Dec9999Previous() {
189:
190: Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
191: Day previous = (Day) dec31st9999.previous();
192: assertEquals(30, previous.getDayOfMonth());
193:
194: }
195:
196: /**
197: * Set up a day equal to 31 December 9999. Request the next day, it should
198: * be null.
199: */
200: public void test31Dec9999Next() {
201:
202: Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
203: Day next = (Day) dec31st9999.next();
204: assertNull(next);
205:
206: }
207:
208: /**
209: * Problem for date parsing.
210: * <p>
211: * This test works only correct if the short pattern of the date
212: * format is "dd/MM/yyyy". If not, this test will result in a
213: * false negative.
214: *
215: * @throws ParseException on parsing errors.
216: */
217: public void testParseDay() throws ParseException {
218:
219: GregorianCalendar gc = new GregorianCalendar(2001, 12, 31);
220: SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
221: Date reference = format.parse("31/12/2001");
222: if (reference.equals(gc.getTime())) {
223: // test 1...
224: Day d = Day.parseDay("31/12/2001");
225: assertEquals(37256, d.getSerialDate().toSerial());
226: }
227:
228: // test 2...
229: Day d = Day.parseDay("2001-12-31");
230: assertEquals(37256, d.getSerialDate().toSerial());
231:
232: }
233:
234: /**
235: * Serialize an instance, restore it, and check for equality.
236: */
237: public void testSerialization() {
238:
239: Day d1 = new Day(15, 4, 2000);
240: Day d2 = null;
241:
242: try {
243: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
244: ObjectOutput out = new ObjectOutputStream(buffer);
245: out.writeObject(d1);
246: out.close();
247:
248: ObjectInput in = new ObjectInputStream(
249: new ByteArrayInputStream(buffer.toByteArray()));
250: d2 = (Day) in.readObject();
251: in.close();
252: } catch (Exception e) {
253: System.out.println(e.toString());
254: }
255: assertEquals(d1, d2);
256:
257: }
258:
259: /**
260: * Two objects that are equal are required to return the same hashCode.
261: */
262: public void testHashcode() {
263: Day d1 = new Day(1, 2, 2003);
264: Day d2 = new Day(1, 2, 2003);
265: assertTrue(d1.equals(d2));
266: int h1 = d1.hashCode();
267: int h2 = d2.hashCode();
268: assertEquals(h1, h2);
269: }
270:
271: /**
272: * The {@link Day} class is immutable, so should not be {@link Cloneable}.
273: */
274: public void testNotCloneable() {
275: Day d = new Day(1, 2, 2003);
276: assertFalse(d instanceof Cloneable);
277: }
278:
279: /**
280: * Some checks for the getSerialIndex() method.
281: */
282: public void testGetSerialIndex() {
283: Day d = new Day(1, 1, 1900);
284: assertEquals(2, d.getSerialIndex());
285: d = new Day(15, 4, 2000);
286: assertEquals(36631, d.getSerialIndex());
287: }
288:
289: /**
290: * Some checks for the getFirstMillisecond() method.
291: */
292: public void testGetFirstMillisecond() {
293: Locale saved = Locale.getDefault();
294: Locale.setDefault(Locale.UK);
295: Day d = new Day(1, 3, 1970);
296: assertEquals(5094000000L, d.getFirstMillisecond());
297: Locale.setDefault(saved);
298: }
299:
300: /**
301: * Some checks for the getFirstMillisecond(TimeZone) method.
302: */
303: public void testGetFirstMillisecondWithTimeZone() {
304: Day d = new Day(26, 4, 1950);
305: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
306: assertEquals(-621187200000L, d.getFirstMillisecond(zone));
307:
308: // try null calendar
309: boolean pass = false;
310: try {
311: d.getFirstMillisecond((TimeZone) null);
312: } catch (NullPointerException e) {
313: pass = true;
314: }
315: assertTrue(pass);
316: }
317:
318: /**
319: * Some checks for the getFirstMillisecond(TimeZone) method.
320: */
321: public void testGetFirstMillisecondWithCalendar() {
322: Day d = new Day(1, 12, 2001);
323: GregorianCalendar calendar = new GregorianCalendar(
324: Locale.GERMANY);
325: assertEquals(1007164800000L, d.getFirstMillisecond(calendar));
326:
327: // try null calendar
328: boolean pass = false;
329: try {
330: d.getFirstMillisecond((Calendar) null);
331: } catch (NullPointerException e) {
332: pass = true;
333: }
334: assertTrue(pass);
335: }
336:
337: /**
338: * Some checks for the getLastMillisecond() method.
339: */
340: public void testGetLastMillisecond() {
341: Locale saved = Locale.getDefault();
342: Locale.setDefault(Locale.UK);
343: Day d = new Day(1, 1, 1970);
344: assertEquals(82799999L, d.getLastMillisecond());
345: Locale.setDefault(saved);
346: }
347:
348: /**
349: * Some checks for the getLastMillisecond(TimeZone) method.
350: */
351: public void testGetLastMillisecondWithTimeZone() {
352: Day d = new Day(1, 2, 1950);
353: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
354: assertEquals(-628358400001L, d.getLastMillisecond(zone));
355:
356: // try null calendar
357: boolean pass = false;
358: try {
359: d.getLastMillisecond((TimeZone) null);
360: } catch (NullPointerException e) {
361: pass = true;
362: }
363: assertTrue(pass);
364: }
365:
366: /**
367: * Some checks for the getLastMillisecond(TimeZone) method.
368: */
369: public void testGetLastMillisecondWithCalendar() {
370: Day d = new Day(4, 5, 2001);
371: GregorianCalendar calendar = new GregorianCalendar(
372: Locale.GERMANY);
373: assertEquals(989017199999L, d.getLastMillisecond(calendar));
374:
375: // try null calendar
376: boolean pass = false;
377: try {
378: d.getLastMillisecond((Calendar) null);
379: } catch (NullPointerException e) {
380: pass = true;
381: }
382: assertTrue(pass);
383: }
384:
385: /**
386: * Some checks for the testNext() method.
387: */
388: public void testNext() {
389: Day d = new Day(25, 12, 2000);
390: d = (Day) d.next();
391: assertEquals(2000, d.getYear());
392: assertEquals(12, d.getMonth());
393: assertEquals(26, d.getDayOfMonth());
394: d = new Day(31, 12, 9999);
395: assertNull(d.next());
396: }
397:
398: /**
399: * Some checks for the getStart() method.
400: */
401: public void testGetStart() {
402: Locale saved = Locale.getDefault();
403: Locale.setDefault(Locale.ITALY);
404: Calendar cal = Calendar.getInstance(Locale.ITALY);
405: cal.set(2006, Calendar.NOVEMBER, 3, 0, 0, 0);
406: cal.set(Calendar.MILLISECOND, 0);
407: Day d = new Day(3, 11, 2006);
408: assertEquals(cal.getTime(), d.getStart());
409: Locale.setDefault(saved);
410: }
411:
412: /**
413: * Some checks for the getEnd() method.
414: */
415: public void testGetEnd() {
416: Locale saved = Locale.getDefault();
417: Locale.setDefault(Locale.ITALY);
418: Calendar cal = Calendar.getInstance(Locale.ITALY);
419: cal.set(1900, Calendar.JANUARY, 1, 23, 59, 59);
420: cal.set(Calendar.MILLISECOND, 999);
421: Day d = new Day(1, 1, 1900);
422: assertEquals(cal.getTime(), d.getEnd());
423: Locale.setDefault(saved);
424: }
425:
426: }
|