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: * MillisecondTests.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: MillisecondTests.java,v 1.1.2.2 2006/10/05 15:35:56 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 29-Jan-2002 : Version 1 (DG);
040: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041: * 21-Oct-2003 : Added hashCode tests (DG);
042: * 29-Apr-2004 : Added test for getMiddleMillisecond() method (DG);
043: * 11-Jan-2005 : Added test for non-clonability (DG);
044: * 05-Oct-2006 : Added some 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.data.time.Millisecond;
069: import org.jfree.data.time.Minute;
070: import org.jfree.data.time.Second;
071: import org.jfree.date.MonthConstants;
072:
073: /**
074: * Tests for the {@link Millisecond} class.
075: */
076: public class MillisecondTests extends TestCase {
077:
078: /**
079: * Returns the tests as a test suite.
080: *
081: * @return The test suite.
082: */
083: public static Test suite() {
084: return new TestSuite(MillisecondTests.class);
085: }
086:
087: /**
088: * Constructs a new set of tests.
089: *
090: * @param name the name of the tests.
091: */
092: public MillisecondTests(String name) {
093: super (name);
094: }
095:
096: /**
097: * Common test setup.
098: */
099: protected void setUp() {
100: // no setup
101: }
102:
103: /**
104: * Check that a {@link Millisecond} instance is equal to itself.
105: *
106: * SourceForge Bug ID: 558850.
107: */
108: public void testEqualsSelf() {
109: Millisecond millisecond = new Millisecond();
110: assertTrue(millisecond.equals(millisecond));
111: }
112:
113: /**
114: * Tests the equals method.
115: */
116: public void testEquals() {
117: Day day1 = new Day(29, MonthConstants.MARCH, 2002);
118: Hour hour1 = new Hour(15, day1);
119: Minute minute1 = new Minute(15, hour1);
120: Second second1 = new Second(34, minute1);
121: Millisecond milli1 = new Millisecond(999, second1);
122: Day day2 = new Day(29, MonthConstants.MARCH, 2002);
123: Hour hour2 = new Hour(15, day2);
124: Minute minute2 = new Minute(15, hour2);
125: Second second2 = new Second(34, minute2);
126: Millisecond milli2 = new Millisecond(999, second2);
127: assertTrue(milli1.equals(milli2));
128: }
129:
130: /**
131: * In GMT, the 4.55:59.123pm on 21 Mar 2002 is
132: * java.util.Date(1016729759123L). Use this to check the Millisecond
133: * constructor.
134: */
135: public void testDateConstructor1() {
136:
137: TimeZone zone = TimeZone.getTimeZone("GMT");
138: Millisecond m1 = new Millisecond(new Date(1016729759122L), zone);
139: Millisecond m2 = new Millisecond(new Date(1016729759123L), zone);
140:
141: assertEquals(122, m1.getMillisecond());
142: assertEquals(1016729759122L, m1.getLastMillisecond(zone));
143:
144: assertEquals(123, m2.getMillisecond());
145: assertEquals(1016729759123L, m2.getFirstMillisecond(zone));
146:
147: }
148:
149: /**
150: * In Tallinn, the 4.55:59.123pm on 21 Mar 2002 is
151: * java.util.Date(1016722559123L). Use this to check the Millisecond
152: * constructor.
153: */
154: public void testDateConstructor2() {
155:
156: TimeZone zone = TimeZone.getTimeZone("Europe/Tallinn");
157: Millisecond m1 = new Millisecond(new Date(1016722559122L), zone);
158: Millisecond m2 = new Millisecond(new Date(1016722559123L), zone);
159:
160: assertEquals(122, m1.getMillisecond());
161: assertEquals(1016722559122L, m1.getLastMillisecond(zone));
162:
163: assertEquals(123, m2.getMillisecond());
164: assertEquals(1016722559123L, m2.getFirstMillisecond(zone));
165:
166: }
167:
168: /**
169: * Serialize an instance, restore it, and check for equality.
170: */
171: public void testSerialization() {
172:
173: Millisecond m1 = new Millisecond();
174: Millisecond m2 = null;
175:
176: try {
177: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
178: ObjectOutput out = new ObjectOutputStream(buffer);
179: out.writeObject(m1);
180: out.close();
181:
182: ObjectInput in = new ObjectInputStream(
183: new ByteArrayInputStream(buffer.toByteArray()));
184: m2 = (Millisecond) in.readObject();
185: in.close();
186: } catch (Exception e) {
187: System.out.println(e.toString());
188: }
189: assertEquals(m1, m2);
190:
191: }
192:
193: /**
194: * Two objects that are equal are required to return the same hashCode.
195: */
196: public void testHashcode() {
197: Millisecond m1 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
198: Millisecond m2 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
199: assertTrue(m1.equals(m2));
200: int hash1 = m1.hashCode();
201: int hash2 = m2.hashCode();
202: assertEquals(hash1, hash2);
203: }
204:
205: /**
206: * A test for bug report 943985 - the calculation for the middle
207: * millisecond is incorrect for odd milliseconds.
208: */
209: public void test943985() {
210: Millisecond ms = new Millisecond(new java.util.Date(4));
211: assertEquals(ms.getFirstMillisecond(), ms
212: .getMiddleMillisecond());
213: assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
214: ms = new Millisecond(new java.util.Date(5));
215: assertEquals(ms.getFirstMillisecond(), ms
216: .getMiddleMillisecond());
217: assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
218: }
219:
220: /**
221: * The {@link Millisecond} class is immutable, so should not be
222: * {@link Cloneable}.
223: */
224: public void testNotCloneable() {
225: Millisecond m = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
226: assertFalse(m instanceof Cloneable);
227: }
228:
229: /**
230: * Some checks for the getFirstMillisecond() method.
231: */
232: public void testGetFirstMillisecond() {
233: Locale saved = Locale.getDefault();
234: Locale.setDefault(Locale.UK);
235: Millisecond m = new Millisecond(500, 15, 43, 15, 1, 4, 2006);
236: assertEquals(1143902595500L, m.getFirstMillisecond());
237: Locale.setDefault(saved);
238: }
239:
240: /**
241: * Some checks for the getFirstMillisecond(TimeZone) method.
242: */
243: public void testGetFirstMillisecondWithTimeZone() {
244: Millisecond m = new Millisecond(500, 50, 59, 15, 1, 4, 1950);
245: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
246: assertEquals(-623289609500L, m.getFirstMillisecond(zone));
247:
248: // try null calendar
249: boolean pass = false;
250: try {
251: m.getFirstMillisecond((TimeZone) null);
252: } catch (NullPointerException e) {
253: pass = true;
254: }
255: assertTrue(pass);
256: }
257:
258: /**
259: * Some checks for the getFirstMillisecond(TimeZone) method.
260: */
261: public void testGetFirstMillisecondWithCalendar() {
262: Millisecond m = new Millisecond(500, 55, 40, 2, 15, 4, 2000);
263: GregorianCalendar calendar = new GregorianCalendar(
264: Locale.GERMANY);
265: assertEquals(955762855500L, m.getFirstMillisecond(calendar));
266:
267: // try null calendar
268: boolean pass = false;
269: try {
270: m.getFirstMillisecond((Calendar) null);
271: } catch (NullPointerException e) {
272: pass = true;
273: }
274: assertTrue(pass);
275: }
276:
277: /**
278: * Some checks for the getLastMillisecond() method.
279: */
280: public void testGetLastMillisecond() {
281: Locale saved = Locale.getDefault();
282: Locale.setDefault(Locale.UK);
283: Millisecond m = new Millisecond(750, 1, 1, 1, 1, 1, 1970);
284: assertEquals(61750L, m.getLastMillisecond());
285: Locale.setDefault(saved);
286: }
287:
288: /**
289: * Some checks for the getLastMillisecond(TimeZone) method.
290: */
291: public void testGetLastMillisecondWithTimeZone() {
292: Millisecond m = new Millisecond(750, 55, 1, 2, 7, 7, 1950);
293: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
294: assertEquals(-614962684250L, m.getLastMillisecond(zone));
295:
296: // try null calendar
297: boolean pass = false;
298: try {
299: m.getLastMillisecond((TimeZone) null);
300: } catch (NullPointerException e) {
301: pass = true;
302: }
303: assertTrue(pass);
304: }
305:
306: /**
307: * Some checks for the getLastMillisecond(TimeZone) method.
308: */
309: public void testGetLastMillisecondWithCalendar() {
310: Millisecond m = new Millisecond(250, 50, 45, 21, 21, 4, 2001);
311: GregorianCalendar calendar = new GregorianCalendar(
312: Locale.GERMANY);
313: assertEquals(987885950250L, m.getLastMillisecond(calendar));
314:
315: // try null calendar
316: boolean pass = false;
317: try {
318: m.getLastMillisecond((Calendar) null);
319: } catch (NullPointerException e) {
320: pass = true;
321: }
322: assertTrue(pass);
323: }
324:
325: /**
326: * Some checks for the getSerialIndex() method.
327: */
328: public void testGetSerialIndex() {
329: Millisecond m = new Millisecond(500, 1, 1, 1, 1, 1, 2000);
330: assertEquals(3155850061500L, m.getSerialIndex());
331: m = new Millisecond(500, 1, 1, 1, 1, 1, 1900);
332: // TODO: this must be wrong...
333: assertEquals(176461500L, m.getSerialIndex());
334: }
335:
336: /**
337: * Some checks for the testNext() method.
338: */
339: public void testNext() {
340: Millisecond m = new Millisecond(555, 55, 30, 1, 12, 12, 2000);
341: m = (Millisecond) m.next();
342: assertEquals(2000, m.getSecond().getMinute().getHour()
343: .getYear());
344: assertEquals(12, m.getSecond().getMinute().getHour().getMonth());
345: assertEquals(12, m.getSecond().getMinute().getHour()
346: .getDayOfMonth());
347: assertEquals(1, m.getSecond().getMinute().getHour().getHour());
348: assertEquals(30, m.getSecond().getMinute().getMinute());
349: assertEquals(55, m.getSecond().getSecond());
350: assertEquals(556, m.getMillisecond());
351: m = new Millisecond(999, 59, 59, 23, 31, 12, 9999);
352: assertNull(m.next());
353: }
354:
355: /**
356: * Some checks for the getStart() method.
357: */
358: public void testGetStart() {
359: Locale saved = Locale.getDefault();
360: Locale.setDefault(Locale.ITALY);
361: Calendar cal = Calendar.getInstance(Locale.ITALY);
362: cal.set(2006, Calendar.JANUARY, 16, 3, 47, 55);
363: cal.set(Calendar.MILLISECOND, 555);
364: Millisecond m = new Millisecond(555, 55, 47, 3, 16, 1, 2006);
365: assertEquals(cal.getTime(), m.getStart());
366: Locale.setDefault(saved);
367: }
368:
369: /**
370: * Some checks for the getEnd() method.
371: */
372: public void testGetEnd() {
373: Locale saved = Locale.getDefault();
374: Locale.setDefault(Locale.ITALY);
375: Calendar cal = Calendar.getInstance(Locale.ITALY);
376: cal.set(2006, Calendar.JANUARY, 16, 3, 47, 55);
377: cal.set(Calendar.MILLISECOND, 555);
378: Millisecond m = new Millisecond(555, 55, 47, 3, 16, 1, 2006);
379: assertEquals(cal.getTime(), m.getEnd());
380: Locale.setDefault(saved);
381: }
382:
383: }
|