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