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: * SecondTests.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: SecondTests.java,v 1.1.2.2 2006/10/05 15:08:27 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-Oct-2003 : Added serialization test (DG);
042: * 11-Jan-2005 : Added test for non-clonability (DG);
043: * 06-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.Day;
066: import org.jfree.data.time.Hour;
067: import org.jfree.data.time.Minute;
068: import org.jfree.data.time.Second;
069: import org.jfree.date.MonthConstants;
070:
071: /**
072: * Tests for the {@link Second} class.
073: */
074: public class SecondTests extends TestCase {
075:
076: /**
077: * Returns the tests as a test suite.
078: *
079: * @return The test suite.
080: */
081: public static Test suite() {
082: return new TestSuite(SecondTests.class);
083: }
084:
085: /**
086: * Constructs a new set of tests.
087: *
088: * @param name the name of the tests.
089: */
090: public SecondTests(String name) {
091: super (name);
092: }
093:
094: /**
095: * Common test setup.
096: */
097: protected void setUp() {
098: // no setup
099: }
100:
101: /**
102: * Test that a Second instance is equal to itself.
103: *
104: * SourceForge Bug ID: 558850.
105: */
106: public void testEqualsSelf() {
107: Second second = new Second();
108: assertTrue(second.equals(second));
109: }
110:
111: /**
112: * Tests the equals method.
113: */
114: public void testEquals() {
115: Day day1 = new Day(29, MonthConstants.MARCH, 2002);
116: Hour hour1 = new Hour(15, day1);
117: Minute minute1 = new Minute(15, hour1);
118: Second second1 = new Second(34, minute1);
119: Day day2 = new Day(29, MonthConstants.MARCH, 2002);
120: Hour hour2 = new Hour(15, day2);
121: Minute minute2 = new Minute(15, hour2);
122: Second second2 = new Second(34, minute2);
123: assertTrue(second1.equals(second2));
124: }
125:
126: /**
127: * In GMT, the 4.55:59pm on 21 Mar 2002 is java.util.Date(1016729759000L).
128: * Use this to check the Second constructor.
129: */
130: public void testDateConstructor1() {
131:
132: TimeZone zone = TimeZone.getTimeZone("GMT");
133: Second s1 = new Second(new Date(1016729758999L), zone);
134: Second s2 = new Second(new Date(1016729759000L), zone);
135:
136: assertEquals(58, s1.getSecond());
137: assertEquals(1016729758999L, s1.getLastMillisecond(zone));
138:
139: assertEquals(59, s2.getSecond());
140: assertEquals(1016729759000L, s2.getFirstMillisecond(zone));
141:
142: }
143:
144: /**
145: * In Chicago, the 4.55:59pm on 21 Mar 2002 is
146: * java.util.Date(1016751359000L). Use this to check the Second constructor.
147: */
148: public void testDateConstructor2() {
149:
150: TimeZone zone = TimeZone.getTimeZone("America/Chicago");
151: Second s1 = new Second(new Date(1016751358999L), zone);
152: Second s2 = new Second(new Date(1016751359000L), zone);
153:
154: assertEquals(58, s1.getSecond());
155: assertEquals(1016751358999L, s1.getLastMillisecond(zone));
156:
157: assertEquals(59, s2.getSecond());
158: assertEquals(1016751359000L, s2.getFirstMillisecond(zone));
159:
160: }
161:
162: /**
163: * Serialize an instance, restore it, and check for equality.
164: */
165: public void testSerialization() {
166:
167: Second s1 = new Second();
168: Second s2 = null;
169:
170: try {
171: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
172: ObjectOutput out = new ObjectOutputStream(buffer);
173: out.writeObject(s1);
174: out.close();
175:
176: ObjectInput in = new ObjectInputStream(
177: new ByteArrayInputStream(buffer.toByteArray()));
178: s2 = (Second) in.readObject();
179: in.close();
180: } catch (Exception e) {
181: System.out.println(e.toString());
182: }
183: assertEquals(s1, s2);
184:
185: }
186:
187: /**
188: * Two objects that are equal are required to return the same hashCode.
189: */
190: public void testHashcode() {
191: Second s1 = new Second(13, 45, 5, 1, 2, 2003);
192: Second s2 = new Second(13, 45, 5, 1, 2, 2003);
193: assertTrue(s1.equals(s2));
194: int h1 = s1.hashCode();
195: int h2 = s2.hashCode();
196: assertEquals(h1, h2);
197: }
198:
199: /**
200: * The {@link Second} class is immutable, so should not be
201: * {@link Cloneable}.
202: */
203: public void testNotCloneable() {
204: Second s = new Second(13, 45, 5, 1, 2, 2003);
205: assertFalse(s 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: Second s = new Second(15, 43, 15, 1, 4, 2006);
215: assertEquals(1143902595000L, s.getFirstMillisecond());
216: Locale.setDefault(saved);
217: }
218:
219: /**
220: * Some checks for the getFirstMillisecond(TimeZone) method.
221: */
222: public void testGetFirstMillisecondWithTimeZone() {
223: Second s = new Second(50, 59, 15, 1, 4, 1950);
224: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
225: assertEquals(-623289610000L, s.getFirstMillisecond(zone));
226:
227: // try null calendar
228: boolean pass = false;
229: try {
230: s.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: Second s = new Second(55, 40, 2, 15, 4, 2000);
242: GregorianCalendar calendar = new GregorianCalendar(
243: Locale.GERMANY);
244: assertEquals(955762855000L, s.getFirstMillisecond(calendar));
245:
246: // try null calendar
247: boolean pass = false;
248: try {
249: s.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: Second s = new Second(1, 1, 1, 1, 1, 1970);
263: assertEquals(61999L, s.getLastMillisecond());
264: Locale.setDefault(saved);
265: }
266:
267: /**
268: * Some checks for the getLastMillisecond(TimeZone) method.
269: */
270: public void testGetLastMillisecondWithTimeZone() {
271: Second s = new Second(55, 1, 2, 7, 7, 1950);
272: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
273: assertEquals(-614962684001L, s.getLastMillisecond(zone));
274:
275: // try null calendar
276: boolean pass = false;
277: try {
278: s.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: Second s = new Second(50, 45, 21, 21, 4, 2001);
290: GregorianCalendar calendar = new GregorianCalendar(
291: Locale.GERMANY);
292: assertEquals(987885950999L, s.getLastMillisecond(calendar));
293:
294: // try null calendar
295: boolean pass = false;
296: try {
297: s.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: Second s = new Second(1, 1, 1, 1, 1, 2000);
309: assertEquals(3155850061L, s.getSerialIndex());
310: s = new Second(1, 1, 1, 1, 1, 1900);
311: assertEquals(176461L, s.getSerialIndex());
312: }
313:
314: /**
315: * Some checks for the testNext() method.
316: */
317: public void testNext() {
318: Second s = new Second(55, 30, 1, 12, 12, 2000);
319: s = (Second) s.next();
320: assertEquals(2000, s.getMinute().getHour().getYear());
321: assertEquals(12, s.getMinute().getHour().getMonth());
322: assertEquals(12, s.getMinute().getHour().getDayOfMonth());
323: assertEquals(1, s.getMinute().getHour().getHour());
324: assertEquals(30, s.getMinute().getMinute());
325: assertEquals(56, s.getSecond());
326: s = new Second(59, 59, 23, 31, 12, 9999);
327: assertNull(s.next());
328: }
329:
330: /**
331: * Some checks for the getStart() method.
332: */
333: public void testGetStart() {
334: Locale saved = Locale.getDefault();
335: Locale.setDefault(Locale.ITALY);
336: Calendar cal = Calendar.getInstance(Locale.ITALY);
337: cal.set(2006, Calendar.JANUARY, 16, 3, 47, 55);
338: cal.set(Calendar.MILLISECOND, 0);
339: Second s = new Second(55, 47, 3, 16, 1, 2006);
340: assertEquals(cal.getTime(), s.getStart());
341: Locale.setDefault(saved);
342: }
343:
344: /**
345: * Some checks for the getEnd() method.
346: */
347: public void testGetEnd() {
348: Locale saved = Locale.getDefault();
349: Locale.setDefault(Locale.ITALY);
350: Calendar cal = Calendar.getInstance(Locale.ITALY);
351: cal.set(2006, Calendar.JANUARY, 16, 3, 47, 55);
352: cal.set(Calendar.MILLISECOND, 999);
353: Second s = new Second(55, 47, 3, 16, 1, 2006);
354: assertEquals(cal.getTime(), s.getEnd());
355: Locale.setDefault(saved);
356: }
357:
358: }
|