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: * YearTests.java
029: * --------------
030: * (C) Copyright 2001-2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: YearTests.java,v 1.1.2.3 2006/10/06 13:13:10 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 16-Nov-2001 : Version 1 (DG);
040: * 19-Mar-2002 : Added tests for constructor that uses java.util.Date to ensure
041: * it is consistent with the getStart() and getEnd() methods (DG);
042: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
043: * 13-Mar-2003 : Added serialization test (DG);
044: * 11-Jan-2005 : Added test for non-clonability (DG);
045: * 05-Oct-2006 : Added some new tests (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.TimePeriodFormatException;
068: import org.jfree.data.time.Year;
069:
070: /**
071: * Tests for the {@link Year} class.
072: */
073: public class YearTests 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(YearTests.class);
082: }
083:
084: /**
085: * Constructs a new set of tests.
086: *
087: * @param name the name of the tests.
088: */
089: public YearTests(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 a Year instance is equal to itself.
102: *
103: * SourceForge Bug ID: 558850.
104: */
105: public void testEqualsSelf() {
106: Year year = new Year();
107: assertTrue(year.equals(year));
108: }
109:
110: /**
111: * Tests the equals method.
112: */
113: public void testEquals() {
114: Year year1 = new Year(2002);
115: Year year2 = new Year(2002);
116: assertTrue(year1.equals(year2));
117: }
118:
119: /**
120: * In GMT, the end of 2001 is java.util.Date(1009843199999L). Use this to
121: * check the year constructor.
122: */
123: public void testDateConstructor1() {
124:
125: TimeZone zone = TimeZone.getTimeZone("GMT");
126: Date d1 = new Date(1009843199999L);
127: Date d2 = new Date(1009843200000L);
128: Year y1 = new Year(d1, zone);
129: Year y2 = new Year(d2, zone);
130:
131: assertEquals(2001, y1.getYear());
132: assertEquals(1009843199999L, y1.getLastMillisecond(zone));
133:
134: assertEquals(2002, y2.getYear());
135: assertEquals(1009843200000L, y2.getFirstMillisecond(zone));
136:
137: }
138:
139: /**
140: * In Los Angeles, the end of 2001 is java.util.Date(1009871999999L). Use
141: * this to check the year constructor.
142: */
143: public void testDateConstructor2() {
144:
145: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
146: Year y1 = new Year(new Date(1009871999999L), zone);
147: Year y2 = new Year(new Date(1009872000000L), zone);
148:
149: assertEquals(2001, y1.getYear());
150: assertEquals(1009871999999L, y1.getLastMillisecond(zone));
151:
152: assertEquals(2002, y2.getYear());
153: assertEquals(1009872000000L, y2.getFirstMillisecond(zone));
154:
155: }
156:
157: /**
158: * Set up a year equal to 1900. Request the previous year, it should be
159: * null.
160: */
161: public void test1900Previous() {
162: Year current = new Year(1900);
163: Year previous = (Year) current.previous();
164: assertNull(previous);
165: }
166:
167: /**
168: * Set up a year equal to 1900. Request the next year, it should be 1901.
169: */
170: public void test1900Next() {
171: Year current = new Year(1900);
172: Year next = (Year) current.next();
173: assertEquals(1901, next.getYear());
174: }
175:
176: /**
177: * Set up a year equal to 9999. Request the previous year, it should be
178: * 9998.
179: */
180: public void test9999Previous() {
181: Year current = new Year(9999);
182: Year previous = (Year) current.previous();
183: assertEquals(9998, previous.getYear());
184: }
185:
186: /**
187: * Set up a year equal to 9999. Request the next year, it should be null.
188: */
189: public void test9999Next() {
190: Year current = new Year(9999);
191: Year next = (Year) current.next();
192: assertNull(next);
193: }
194:
195: /**
196: * Tests the year string parser.
197: */
198: public void testParseYear() {
199:
200: Year year = null;
201:
202: // test 1...
203: try {
204: year = Year.parseYear("2000");
205: } catch (TimePeriodFormatException e) {
206: year = new Year(1900);
207: }
208: assertEquals(2000, year.getYear());
209:
210: // test 2...
211: try {
212: year = Year.parseYear(" 2001 ");
213: } catch (TimePeriodFormatException e) {
214: year = new Year(1900);
215: }
216: assertEquals(2001, year.getYear());
217:
218: // test 3...
219: try {
220: year = Year.parseYear("99");
221: } catch (TimePeriodFormatException e) {
222: year = new Year(1900);
223: }
224: assertEquals(1900, year.getYear());
225:
226: }
227:
228: /**
229: * Serialize an instance, restore it, and check for equality.
230: */
231: public void testSerialization() {
232:
233: Year y1 = new Year(1999);
234: Year y2 = null;
235:
236: try {
237: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
238: ObjectOutput out = new ObjectOutputStream(buffer);
239: out.writeObject(y1);
240: out.close();
241:
242: ObjectInput in = new ObjectInputStream(
243: new ByteArrayInputStream(buffer.toByteArray()));
244: y2 = (Year) in.readObject();
245: in.close();
246: } catch (Exception e) {
247: System.out.println(e.toString());
248: }
249: assertEquals(y1, y2);
250:
251: }
252:
253: /**
254: * The {@link Year} class is immutable, so should not be {@link Cloneable}.
255: */
256: public void testNotCloneable() {
257: Year y = new Year(1999);
258: assertFalse(y instanceof Cloneable);
259: }
260:
261: /**
262: * Two objects that are equal are required to return the same hashCode.
263: */
264: public void testHashcode() {
265: Year y1 = new Year(1988);
266: Year y2 = new Year(1988);
267: assertTrue(y1.equals(y2));
268: int h1 = y1.hashCode();
269: int h2 = y2.hashCode();
270: assertEquals(h1, h2);
271: }
272:
273: /**
274: * Some checks for the getFirstMillisecond() method.
275: */
276: public void testGetFirstMillisecond() {
277: Locale saved = Locale.getDefault();
278: Locale.setDefault(Locale.UK);
279: Year y = new Year(1970);
280: // TODO: Check this result...
281: assertEquals(-3600000L, y.getFirstMillisecond());
282: Locale.setDefault(saved);
283: }
284:
285: /**
286: * Some checks for the getFirstMillisecond(TimeZone) method.
287: */
288: public void testGetFirstMillisecondWithTimeZone() {
289: Year y = new Year(1950);
290: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
291: assertEquals(-631123200000L, y.getFirstMillisecond(zone));
292:
293: // try null calendar
294: boolean pass = false;
295: try {
296: y.getFirstMillisecond((TimeZone) null);
297: } catch (NullPointerException e) {
298: pass = true;
299: }
300: assertTrue(pass);
301: }
302:
303: /**
304: * Some checks for the getFirstMillisecond(TimeZone) method.
305: */
306: public void testGetFirstMillisecondWithCalendar() {
307: Year y = new Year(2001);
308: GregorianCalendar calendar = new GregorianCalendar(
309: Locale.GERMANY);
310: assertEquals(978307200000L, y.getFirstMillisecond(calendar));
311:
312: // try null calendar
313: boolean pass = false;
314: try {
315: y.getFirstMillisecond((Calendar) null);
316: } catch (NullPointerException e) {
317: pass = true;
318: }
319: assertTrue(pass);
320: }
321:
322: /**
323: * Some checks for the getLastMillisecond() method.
324: */
325: public void testGetLastMillisecond() {
326: Locale saved = Locale.getDefault();
327: Locale.setDefault(Locale.UK);
328: Year y = new Year(1970);
329: // TODO: Check this result...
330: assertEquals(31532399999L, y.getLastMillisecond());
331: Locale.setDefault(saved);
332: }
333:
334: /**
335: * Some checks for the getLastMillisecond(TimeZone) method.
336: */
337: public void testGetLastMillisecondWithTimeZone() {
338: Year y = new Year(1950);
339: TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
340: assertEquals(-599587200001L, y.getLastMillisecond(zone));
341:
342: // try null calendar
343: boolean pass = false;
344: try {
345: y.getLastMillisecond((TimeZone) null);
346: } catch (NullPointerException e) {
347: pass = true;
348: }
349: assertTrue(pass);
350: }
351:
352: /**
353: * Some checks for the getLastMillisecond(TimeZone) method.
354: */
355: public void testGetLastMillisecondWithCalendar() {
356: Year y = new Year(2001);
357: GregorianCalendar calendar = new GregorianCalendar(
358: Locale.GERMANY);
359: assertEquals(1009843199999L, y.getLastMillisecond(calendar));
360:
361: // try null calendar
362: boolean pass = false;
363: try {
364: y.getLastMillisecond((Calendar) null);
365: } catch (NullPointerException e) {
366: pass = true;
367: }
368: assertTrue(pass);
369: }
370:
371: /**
372: * Some checks for the getSerialIndex() method.
373: */
374: public void testGetSerialIndex() {
375: Year y = new Year(2000);
376: assertEquals(2000L, y.getSerialIndex());
377: }
378:
379: /**
380: * Some checks for the testNext() method.
381: */
382: public void testNext() {
383: Year y = new Year(2000);
384: y = (Year) y.next();
385: assertEquals(2001, y.getYear());
386: y = new Year(9999);
387: assertNull(y.next());
388: }
389:
390: /**
391: * Some checks for the getStart() method.
392: */
393: public void testGetStart() {
394: Locale saved = Locale.getDefault();
395: Locale.setDefault(Locale.ITALY);
396: Calendar cal = Calendar.getInstance(Locale.ITALY);
397: cal.set(2006, Calendar.JANUARY, 1, 0, 0, 0);
398: cal.set(Calendar.MILLISECOND, 0);
399: Year y = new Year(2006);
400: assertEquals(cal.getTime(), y.getStart());
401: Locale.setDefault(saved);
402: }
403:
404: /**
405: * Some checks for the getEnd() method.
406: */
407: public void testGetEnd() {
408: Locale saved = Locale.getDefault();
409: Locale.setDefault(Locale.ITALY);
410: Calendar cal = Calendar.getInstance(Locale.ITALY);
411: cal.set(2006, Calendar.DECEMBER, 31, 23, 59, 59);
412: cal.set(Calendar.MILLISECOND, 999);
413: Year y = new Year(2006);
414: assertEquals(cal.getTime(), y.getEnd());
415: Locale.setDefault(saved);
416: }
417:
418: }
|