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