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