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: * MonthTests.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: MonthTests.java,v 1.1.2.2 2006/10/05 10:15:18 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 16-Nov-2001 : Version 1 (DG);
040: * 14-Feb-2002 : Order of parameters in Month(int, int) constructor
041: * changed (DG);
042: * 26-Jun-2002 : Removed unnecessary import (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 non-clonability test (DG);
047: * 05-Oct-2006 : Added some new tests (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.util.Calendar;
060: import java.util.Date;
061: import java.util.GregorianCalendar;
062: import java.util.Locale;
063: import java.util.TimeZone;
064:
065: import junit.framework.Test;
066: import junit.framework.TestCase;
067: import junit.framework.TestSuite;
068:
069: import org.jfree.data.time.Month;
070: import org.jfree.data.time.TimePeriodFormatException;
071: import org.jfree.data.time.Year;
072: import org.jfree.date.MonthConstants;
073:
074: /**
075: * Tests for the {@link Month} class.
076: */
077: public class MonthTests extends TestCase {
078:
079: /** A month. */
080: private Month jan1900;
081:
082: /** A month. */
083: private Month feb1900;
084:
085: /** A month. */
086: private Month nov9999;
087:
088: /** A month. */
089: private Month dec9999;
090:
091: /**
092: * Returns the tests as a test suite.
093: *
094: * @return The test suite.
095: */
096: public static Test suite() {
097: return new TestSuite(MonthTests.class);
098: }
099:
100: /**
101: * Constructs a new set of tests.
102: *
103: * @param name the name of the tests.
104: */
105: public MonthTests(String name) {
106: super (name);
107: }
108:
109: /**
110: * Common test setup.
111: */
112: protected void setUp() {
113: this .jan1900 = new Month(MonthConstants.JANUARY, 1900);
114: this .feb1900 = new Month(MonthConstants.FEBRUARY, 1900);
115: this .nov9999 = new Month(MonthConstants.NOVEMBER, 9999);
116: this .dec9999 = new Month(MonthConstants.DECEMBER, 9999);
117: }
118:
119: /**
120: * Check that a Month instance is equal to itself.
121: *
122: * SourceForge Bug ID: 558850.
123: */
124: public void testEqualsSelf() {
125: Month month = new Month();
126: assertTrue(month.equals(month));
127: }
128:
129: /**
130: * Tests the equals method.
131: */
132: public void testEquals() {
133: Month m1 = new Month(MonthConstants.MAY, 2002);
134: Month m2 = new Month(MonthConstants.MAY, 2002);
135: assertTrue(m1.equals(m2));
136: }
137:
138: /**
139: * In GMT, the end of Feb 2000 is java.util.Date(951,868,799,999L). Use
140: * this to check the Month constructor.
141: */
142: public void testDateConstructor1() {
143:
144: TimeZone zone = TimeZone.getTimeZone("GMT");
145: Month m1 = new Month(new Date(951868799999L), zone);
146: Month m2 = new Month(new Date(951868800000L), zone);
147:
148: assertEquals(MonthConstants.FEBRUARY, m1.getMonth());
149: assertEquals(951868799999L, m1.getLastMillisecond(zone));
150:
151: assertEquals(MonthConstants.MARCH, m2.getMonth());
152: assertEquals(951868800000L, m2.getFirstMillisecond(zone));
153:
154: }
155:
156: /**
157: * In Auckland, the end of Feb 2000 is java.util.Date(951,821,999,999L).
158: * Use this to check the Month constructor.
159: */
160: public void testDateConstructor2() {
161:
162: TimeZone zone = TimeZone.getTimeZone("Pacific/Auckland");
163: Month m1 = new Month(new Date(951821999999L), zone);
164: Month m2 = new Month(new Date(951822000000L), zone);
165:
166: assertEquals(MonthConstants.FEBRUARY, m1.getMonth());
167: assertEquals(951821999999L, m1.getLastMillisecond(zone));
168:
169: assertEquals(MonthConstants.MARCH, m2.getMonth());
170: assertEquals(951822000000L, m2.getFirstMillisecond(zone));
171:
172: }
173:
174: /**
175: * Set up a month equal to Jan 1900. Request the previous month, it should
176: * be null.
177: */
178: public void testJan1900Previous() {
179: Month previous = (Month) this .jan1900.previous();
180: assertNull(previous);
181: }
182:
183: /**
184: * Set up a month equal to Jan 1900. Request the next month, it should be
185: * Feb 1900.
186: */
187: public void testJan1900Next() {
188: Month next = (Month) this .jan1900.next();
189: assertEquals(this .feb1900, next);
190: }
191:
192: /**
193: * Set up a month equal to Dec 9999. Request the previous month, it should
194: * be Nov 9999.
195: */
196: public void testDec9999Previous() {
197: Month previous = (Month) this .dec9999.previous();
198: assertEquals(this .nov9999, previous);
199: }
200:
201: /**
202: * Set up a month equal to Dec 9999. Request the next month, it should be
203: * null.
204: */
205: public void testDec9999Next() {
206: Month next = (Month) this .dec9999.next();
207: assertNull(next);
208: }
209:
210: /**
211: * Tests the string parsing code...
212: */
213: public void testParseMonth() {
214:
215: Month month = null;
216:
217: // test 1...
218: try {
219: month = Month.parseMonth("1990-01");
220: } catch (TimePeriodFormatException e) {
221: month = new Month(1, 1900);
222: }
223: assertEquals(1, month.getMonth());
224: assertEquals(1990, month.getYear().getYear());
225:
226: // test 2...
227: try {
228: month = Month.parseMonth("02-1991");
229: } catch (TimePeriodFormatException e) {
230: month = new Month(1, 1900);
231: }
232: assertEquals(2, month.getMonth());
233: assertEquals(1991, month.getYear().getYear());
234:
235: // test 3...
236: try {
237: month = Month.parseMonth("March 1993");
238: } catch (TimePeriodFormatException e) {
239: month = new Month(1, 1900);
240: }
241: assertEquals(3, month.getMonth());
242: assertEquals(1993, month.getYear().getYear());
243:
244: }
245:
246: /**
247: * Serialize an instance, restore it, and check for equality.
248: */
249: public void testSerialization() {
250:
251: Month m1 = new Month(12, 1999);
252: Month m2 = null;
253:
254: try {
255: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
256: ObjectOutput out = new ObjectOutputStream(buffer);
257: out.writeObject(m1);
258: out.close();
259:
260: ObjectInput in = new ObjectInputStream(
261: new ByteArrayInputStream(buffer.toByteArray()));
262: m2 = (Month) in.readObject();
263: in.close();
264: } catch (Exception e) {
265: System.out.println(e.toString());
266: }
267: assertEquals(m1, m2);
268:
269: }
270:
271: /**
272: * Two objects that are equal are required to return the same hashCode.
273: */
274: public void testHashcode() {
275: Month m1 = new Month(2, 2003);
276: Month m2 = new Month(2, 2003);
277: assertTrue(m1.equals(m2));
278: int h1 = m1.hashCode();
279: int h2 = m2.hashCode();
280: assertEquals(h1, h2);
281: }
282:
283: /**
284: * The {@link Month} class is immutable, so should not be {@link Cloneable}.
285: */
286: public void testNotCloneable() {
287: Month m = new Month(2, 2003);
288: assertFalse(m instanceof Cloneable);
289: }
290:
291: /**
292: * Some checks for the getFirstMillisecond() method.
293: */
294: public void testGetFirstMillisecond() {
295: Locale saved = Locale.getDefault();
296: Locale.setDefault(Locale.UK);
297: Month m = new Month(3, 1970);
298: assertEquals(5094000000L, m.getFirstMillisecond());
299: Locale.setDefault(saved);
300: }
301:
302: /**
303: * Some checks for the getFirstMillisecond(TimeZone) method.
304: */
305: public void testGetFirstMillisecondWithTimeZone() {
306: Month m = new Month(2, 1950);
307: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
308: assertEquals(-628444800000L, m.getFirstMillisecond(zone));
309:
310: // try null calendar
311: boolean pass = false;
312: try {
313: m.getFirstMillisecond((TimeZone) null);
314: } catch (NullPointerException e) {
315: pass = true;
316: }
317: assertTrue(pass);
318: }
319:
320: /**
321: * Some checks for the getFirstMillisecond(TimeZone) method.
322: */
323: public void testGetFirstMillisecondWithCalendar() {
324: Month m = new Month(1, 2001);
325: GregorianCalendar calendar = new GregorianCalendar(
326: Locale.GERMANY);
327: assertEquals(978307200000L, m.getFirstMillisecond(calendar));
328:
329: // try null calendar
330: boolean pass = false;
331: try {
332: m.getFirstMillisecond((Calendar) null);
333: } catch (NullPointerException e) {
334: pass = true;
335: }
336: assertTrue(pass);
337: }
338:
339: /**
340: * Some checks for the getLastMillisecond() method.
341: */
342: public void testGetLastMillisecond() {
343: Locale saved = Locale.getDefault();
344: Locale.setDefault(Locale.UK);
345: Month m = new Month(3, 1970);
346: assertEquals(7772399999L, m.getLastMillisecond());
347: Locale.setDefault(saved);
348: }
349:
350: /**
351: * Some checks for the getLastMillisecond(TimeZone) method.
352: */
353: public void testGetLastMillisecondWithTimeZone() {
354: Month m = new Month(2, 1950);
355: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
356: assertEquals(-626025600001L, m.getLastMillisecond(zone));
357:
358: // try null calendar
359: boolean pass = false;
360: try {
361: m.getLastMillisecond((TimeZone) null);
362: } catch (NullPointerException e) {
363: pass = true;
364: }
365: assertTrue(pass);
366: }
367:
368: /**
369: * Some checks for the getLastMillisecond(TimeZone) method.
370: */
371: public void testGetLastMillisecondWithCalendar() {
372: Month m = new Month(3, 2001);
373: GregorianCalendar calendar = new GregorianCalendar(
374: Locale.GERMANY);
375: assertEquals(986079599999L, m.getLastMillisecond(calendar));
376:
377: // try null calendar
378: boolean pass = false;
379: try {
380: m.getLastMillisecond((Calendar) null);
381: } catch (NullPointerException e) {
382: pass = true;
383: }
384: assertTrue(pass);
385: }
386:
387: /**
388: * Some checks for the getSerialIndex() method.
389: */
390: public void testGetSerialIndex() {
391: Month m = new Month(1, 2000);
392: assertEquals(24001L, m.getSerialIndex());
393: m = new Month(1, 1900);
394: assertEquals(22801L, m.getSerialIndex());
395: }
396:
397: /**
398: * Some checks for the testNext() method.
399: */
400: public void testNext() {
401: Month m = new Month(12, 2000);
402: m = (Month) m.next();
403: assertEquals(new Year(2001), m.getYear());
404: assertEquals(1, m.getMonth());
405: m = new Month(12, 9999);
406: assertNull(m.next());
407: }
408:
409: /**
410: * Some checks for the getStart() method.
411: */
412: public void testGetStart() {
413: Locale saved = Locale.getDefault();
414: Locale.setDefault(Locale.ITALY);
415: Calendar cal = Calendar.getInstance(Locale.ITALY);
416: cal.set(2006, Calendar.MARCH, 1, 0, 0, 0);
417: cal.set(Calendar.MILLISECOND, 0);
418: Month m = new Month(3, 2006);
419: assertEquals(cal.getTime(), m.getStart());
420: Locale.setDefault(saved);
421: }
422:
423: /**
424: * Some checks for the getEnd() method.
425: */
426: public void testGetEnd() {
427: Locale saved = Locale.getDefault();
428: Locale.setDefault(Locale.ITALY);
429: Calendar cal = Calendar.getInstance(Locale.ITALY);
430: cal.set(2006, Calendar.JANUARY, 31, 23, 59, 59);
431: cal.set(Calendar.MILLISECOND, 999);
432: Month m = new Month(1, 2006);
433: assertEquals(cal.getTime(), m.getEnd());
434: Locale.setDefault(saved);
435: }
436:
437: }
|