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: * WeekTests.java
029: * --------------
030: * (C) Copyright 2002-2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: WeekTests.java,v 1.1.2.2 2006/10/05 10:49:29 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 05-Apr-2002 : Version 1 (DG);
040: * 26-Jun-2002 : Removed unnecessary imports (DG);
041: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
042: * 13-Mar-2003 : Added serialization test (DG);
043: * 21-Oct-2003 : Added hashCode test (DG);
044: * 06-Apr-2006 : Added testBug1448828() method (DG);
045: * 01-Jun-2006 : Added testBug1498805() method (DG);
046: *
047: */
048:
049: package org.jfree.data.time.junit;
050:
051: import java.io.ByteArrayInputStream;
052: import java.io.ByteArrayOutputStream;
053: import java.io.ObjectInput;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutput;
056: import java.io.ObjectOutputStream;
057: import java.util.Calendar;
058: import java.util.Date;
059: import java.util.GregorianCalendar;
060: import java.util.Locale;
061: import java.util.TimeZone;
062:
063: import junit.framework.Test;
064: import junit.framework.TestCase;
065: import junit.framework.TestSuite;
066:
067: import org.jfree.data.time.Week;
068: import org.jfree.data.time.Year;
069:
070: /**
071: * Tests for the {@link Week} class.
072: */
073: public class WeekTests extends TestCase {
074:
075: /** A week. */
076: private Week w1Y1900;
077:
078: /** A week. */
079: private Week w2Y1900;
080:
081: /** A week. */
082: private Week w51Y9999;
083:
084: /** A week. */
085: private Week w52Y9999;
086:
087: /**
088: * Returns the tests as a test suite.
089: *
090: * @return The test suite.
091: */
092: public static Test suite() {
093: return new TestSuite(WeekTests.class);
094: }
095:
096: /**
097: * Constructs a new set of tests.
098: *
099: * @param name the name of the tests.
100: */
101: public WeekTests(String name) {
102: super (name);
103: }
104:
105: /**
106: * Common test setup.
107: */
108: protected void setUp() {
109: this .w1Y1900 = new Week(1, 1900);
110: this .w2Y1900 = new Week(2, 1900);
111: this .w51Y9999 = new Week(51, 9999);
112: this .w52Y9999 = new Week(52, 9999);
113: }
114:
115: /**
116: * Tests the equals method.
117: */
118: public void testEquals() {
119: Week w1 = new Week(1, 2002);
120: Week w2 = new Week(1, 2002);
121: assertTrue(w1.equals(w2));
122: assertTrue(w2.equals(w1));
123:
124: w1 = new Week(2, 2002);
125: assertFalse(w1.equals(w2));
126: w2 = new Week(2, 2002);
127: assertTrue(w1.equals(w2));
128:
129: w1 = new Week(2, 2003);
130: assertFalse(w1.equals(w2));
131: w2 = new Week(2, 2003);
132: assertTrue(w1.equals(w2));
133: }
134:
135: /**
136: * Request the week before week 1, 1900: it should be <code>null</code>.
137: */
138: public void testW1Y1900Previous() {
139: Week previous = (Week) this .w1Y1900.previous();
140: assertNull(previous);
141: }
142:
143: /**
144: * Request the week after week 1, 1900: it should be week 2, 1900.
145: */
146: public void testW1Y1900Next() {
147: Week next = (Week) this .w1Y1900.next();
148: assertEquals(this .w2Y1900, next);
149: }
150:
151: /**
152: * Request the week before w52, 9999: it should be week 51, 9999.
153: */
154: public void testW52Y9999Previous() {
155: Week previous = (Week) this .w52Y9999.previous();
156: assertEquals(this .w51Y9999, previous);
157: }
158:
159: /**
160: * Request the week after w52, 9999: it should be <code>null</code>.
161: */
162: public void testW52Y9999Next() {
163: Week next = (Week) this .w52Y9999.next();
164: assertNull(next);
165: }
166:
167: /**
168: * Serialize an instance, restore it, and check for equality.
169: */
170: public void testSerialization() {
171:
172: Week w1 = new Week(24, 1999);
173: Week w2 = null;
174:
175: try {
176: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
177: ObjectOutput out = new ObjectOutputStream(buffer);
178: out.writeObject(w1);
179: out.close();
180:
181: ObjectInput in = new ObjectInputStream(
182: new ByteArrayInputStream(buffer.toByteArray()));
183: w2 = (Week) in.readObject();
184: in.close();
185: } catch (Exception e) {
186: System.out.println(e.toString());
187: }
188: assertEquals(w1, w2);
189:
190: }
191:
192: /**
193: * Two objects that are equal are required to return the same hashCode.
194: */
195: public void testHashcode() {
196: Week w1 = new Week(2, 2003);
197: Week w2 = new Week(2, 2003);
198: assertTrue(w1.equals(w2));
199: int h1 = w1.hashCode();
200: int h2 = w2.hashCode();
201: assertEquals(h1, h2);
202: }
203:
204: /**
205: * The {@link Week} class is immutable, so should not be {@link Cloneable}.
206: */
207: public void testNotCloneable() {
208: Week w = new Week(1, 1999);
209: assertFalse(w instanceof Cloneable);
210: }
211:
212: /**
213: * The first week in 2005 should span the range:
214: *
215: * TimeZone | Start Millis | End Millis | Start Date | End Date
216: * -----------------+---------------+---------------+-------------+------------
217: * Europe/London | 1104710400000 | 1105315199999 | 3-Jan-2005 | 9-Jan-2005
218: * Europe/Paris | 1104706800000 | 1105311599999 | 3-Jan-2005 | 2-Jan-2005
219: * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
220: *
221: * In London and Paris, Monday is the first day of the week, while in the
222: * US it is Sunday.
223: *
224: * Previously, we were using these values, but see Java Bug ID 4960215:
225: *
226: * TimeZone | Start Millis | End Millis | Start Date | End Date
227: * -----------------+---------------+---------------+-------------+------------
228: * Europe/London | 1104105600000 | 1104710399999 | 27-Dec-2004 | 2-Jan-2005
229: * Europe/Paris | 1104102000000 | 1104706799999 | 27-Dec-2004 | 2-Jan-2005
230: * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
231: */
232: public void testWeek12005() {
233: Week w1 = new Week(1, 2005);
234: Calendar c1 = Calendar.getInstance(TimeZone
235: .getTimeZone("Europe/London"), Locale.UK);
236: c1.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
237: assertEquals(1104710400000L, w1.getFirstMillisecond(c1));
238: assertEquals(1105315199999L, w1.getLastMillisecond(c1));
239: Calendar c2 = Calendar.getInstance(TimeZone
240: .getTimeZone("Europe/Paris"), Locale.FRANCE);
241: c2.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
242: assertEquals(1104706800000L, w1.getFirstMillisecond(c2));
243: assertEquals(1105311599999L, w1.getLastMillisecond(c2));
244: Calendar c3 = Calendar.getInstance(TimeZone
245: .getTimeZone("America/New_York"), Locale.US);
246: assertEquals(1104037200000L, w1.getFirstMillisecond(c3));
247: assertEquals(1104641999999L, w1.getLastMillisecond(c3));
248: }
249:
250: /**
251: * The 53rd week in 2004 in London and Paris should span the range:
252: *
253: * TimeZone | Start Millis | End Millis | Start Date | End Date
254: * -----------------+---------------+---------------+-------------+------------
255: * Europe/London | 1104105600000 | 1104710399999 | 27-Dec-2004 | 02-Jan-2005
256: * Europe/Paris | 1104102000000 | 1104706799999 | 27-Dec-2004 | 02-Jan-2005
257: *
258: * The 53rd week in 2005 in New York should span the range:
259: *
260: * TimeZone | Start Millis | End Millis | Start Date | End Date
261: * -----------------+---------------+---------------+-------------+------------
262: * America/New_York | 1135486800000 | 1136091599999 | 25-Dec-2005 | 31-Dec-2005
263: *
264: * In London and Paris, Monday is the first day of the week, while in the
265: * US it is Sunday.
266: */
267: public void testWeek532005() {
268: Week w1 = new Week(53, 2004);
269: Calendar c1 = Calendar.getInstance(TimeZone
270: .getTimeZone("Europe/London"), Locale.UK);
271: c1.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
272: assertEquals(1104105600000L, w1.getFirstMillisecond(c1));
273: assertEquals(1104710399999L, w1.getLastMillisecond(c1));
274: Calendar c2 = Calendar.getInstance(TimeZone
275: .getTimeZone("Europe/Paris"), Locale.FRANCE);
276: c2.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
277: assertEquals(1104102000000L, w1.getFirstMillisecond(c2));
278: assertEquals(1104706799999L, w1.getLastMillisecond(c2));
279: w1 = new Week(53, 2005);
280: Calendar c3 = Calendar.getInstance(TimeZone
281: .getTimeZone("America/New_York"), Locale.US);
282: assertEquals(1135486800000L, w1.getFirstMillisecond(c3));
283: assertEquals(1136091599999L, w1.getLastMillisecond(c3));
284: }
285:
286: /**
287: * A test case for bug 1448828.
288: */
289: public void testBug1448828() {
290: Week w = new Week(new Date(1136109830000l), TimeZone
291: .getTimeZone("GMT"));
292: assertEquals(2005, w.getYearValue());
293: assertEquals(52, w.getWeek());
294: }
295:
296: /**
297: * A test case for bug 1498805.
298: */
299: public void testBug1498805() {
300: TimeZone zone = TimeZone.getTimeZone("GMT");
301: GregorianCalendar gc = new GregorianCalendar(zone);
302: gc.set(2005, Calendar.JANUARY, 1, 12, 0, 0);
303: Week w = new Week(gc.getTime(), zone);
304: assertEquals(53, w.getWeek());
305: assertEquals(new Year(2004), w.getYear());
306: }
307:
308: /**
309: * Some checks for the getFirstMillisecond() method.
310: */
311: public void testGetFirstMillisecond() {
312: Locale saved = Locale.getDefault();
313: Locale.setDefault(Locale.UK);
314: Week w = new Week(3, 1970);
315: assertEquals(946800000L, w.getFirstMillisecond());
316: Locale.setDefault(saved);
317: }
318:
319: /**
320: * Some checks for the getFirstMillisecond(TimeZone) method.
321: */
322: public void testGetFirstMillisecondWithTimeZone() {
323: Week w = new Week(47, 1950);
324: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
325: assertEquals(-603216000000L, w.getFirstMillisecond(zone));
326:
327: // try null calendar
328: boolean pass = false;
329: try {
330: w.getFirstMillisecond((TimeZone) null);
331: } catch (NullPointerException e) {
332: pass = true;
333: }
334: assertTrue(pass);
335: }
336:
337: /**
338: * Some checks for the getFirstMillisecond(TimeZone) method.
339: */
340: public void testGetFirstMillisecondWithCalendar() {
341: Week w = new Week(1, 2001);
342: GregorianCalendar calendar = new GregorianCalendar(
343: Locale.GERMANY);
344: assertEquals(978307200000L, w.getFirstMillisecond(calendar));
345:
346: // try null calendar
347: boolean pass = false;
348: try {
349: w.getFirstMillisecond((Calendar) null);
350: } catch (NullPointerException e) {
351: pass = true;
352: }
353: assertTrue(pass);
354: }
355:
356: /**
357: * Some checks for the getLastMillisecond() method.
358: */
359: public void testGetLastMillisecond() {
360: Locale saved = Locale.getDefault();
361: Locale.setDefault(Locale.UK);
362: Week w = new Week(31, 1970);
363: assertEquals(18485999999L, w.getLastMillisecond());
364: Locale.setDefault(saved);
365: }
366:
367: /**
368: * Some checks for the getLastMillisecond(TimeZone) method.
369: */
370: public void testGetLastMillisecondWithTimeZone() {
371: Week w = new Week(2, 1950);
372: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
373: assertEquals(-629827200001L, w.getLastMillisecond(zone));
374:
375: // try null calendar
376: boolean pass = false;
377: try {
378: w.getLastMillisecond((TimeZone) null);
379: } catch (NullPointerException e) {
380: pass = true;
381: }
382: assertTrue(pass);
383: }
384:
385: /**
386: * Some checks for the getLastMillisecond(TimeZone) method.
387: */
388: public void testGetLastMillisecondWithCalendar() {
389: Week w = new Week(52, 2001);
390: GregorianCalendar calendar = new GregorianCalendar(
391: Locale.GERMANY);
392: assertEquals(1009756799999L, w.getLastMillisecond(calendar));
393:
394: // try null calendar
395: boolean pass = false;
396: try {
397: w.getLastMillisecond((Calendar) null);
398: } catch (NullPointerException e) {
399: pass = true;
400: }
401: assertTrue(pass);
402: }
403:
404: /**
405: * Some checks for the getSerialIndex() method.
406: */
407: public void testGetSerialIndex() {
408: Week w = new Week(1, 2000);
409: assertEquals(106001L, w.getSerialIndex());
410: w = new Week(1, 1900);
411: assertEquals(100701L, w.getSerialIndex());
412: }
413:
414: /**
415: * Some checks for the testNext() method.
416: */
417: public void testNext() {
418: Week w = new Week(12, 2000);
419: w = (Week) w.next();
420: assertEquals(new Year(2000), w.getYear());
421: assertEquals(13, w.getWeek());
422: w = new Week(53, 9999);
423: assertNull(w.next());
424: }
425:
426: /**
427: * Some checks for the getStart() method.
428: */
429: public void testGetStart() {
430: Locale saved = Locale.getDefault();
431: Locale.setDefault(Locale.ITALY);
432: Calendar cal = Calendar.getInstance(Locale.ITALY);
433: cal.set(2006, Calendar.JANUARY, 16, 0, 0, 0);
434: cal.set(Calendar.MILLISECOND, 0);
435: Week w = new Week(3, 2006);
436: assertEquals(cal.getTime(), w.getStart());
437: Locale.setDefault(saved);
438: }
439:
440: /**
441: * Some checks for the getEnd() method.
442: */
443: public void testGetEnd() {
444: Locale saved = Locale.getDefault();
445: Locale.setDefault(Locale.ITALY);
446: Calendar cal = Calendar.getInstance(Locale.ITALY);
447: cal.set(2006, Calendar.JANUARY, 8, 23, 59, 59);
448: cal.set(Calendar.MILLISECOND, 999);
449: Week w = new Week(1, 2006);
450: assertEquals(cal.getTime(), w.getEnd());
451: Locale.setDefault(saved);
452: }
453:
454: }
|