1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
6  *
7  * Project Info:  http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22  * USA.
23  *
24  * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
25  * Other names may be trademarks of their respective owners.]
26  *
27  * -------------
28  * YearTest.java
29  * -------------
30  * (C) Copyright 2001-2013, by Object Refinery Limited.
31  *
32  * Original Author:  David Gilbert (for Object Refinery Limited);
33  * Contributor(s):   -;
34  *
35  * Changes
36  * -------
37  * 16-Nov-2001 : Version 1 (DG);
38  * 19-Mar-2002 : Added tests for constructor that uses java.util.Date to ensure
39  *               it is consistent with the getStart() and getEnd() methods (DG);
40  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  * 13-Mar-2003 : Added serialization test (DG);
42  * 11-Jan-2005 : Added test for non-clonability (DG);
43  * 05-Oct-2006 : Added some new tests (DG);
44  * 11-Jul-2007 : Fixed bad time zone assumption (DG);
45  *
46  */
47 
48 package org.jfree.data.time;
49 
50 import static org.junit.Assert.assertTrue;
51 import static org.junit.Assert.assertFalse;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertNull;
54 
55 import java.util.Calendar;
56 import java.util.Date;
57 import java.util.GregorianCalendar;
58 import java.util.Locale;
59 import java.util.TimeZone;
60 
61 import org.jfree.chart.TestUtilities;
62 import org.junit.Test;
63 
64 /**
65  * Tests for the {@link Year} class.
66  */
67 public class YearTest {
68 
69     /**
70      * Check that a Year instance is equal to itself.
71      *
72      * SourceForge Bug ID: 558850.
73      */
74     @Test
testEqualsSelf()75     public void testEqualsSelf() {
76         Year year = new Year();
77         assertTrue(year.equals(year));
78     }
79 
80     /**
81      * Tests the equals method.
82      */
83     @Test
testEquals()84     public void testEquals() {
85         Year year1 = new Year(2002);
86         Year year2 = new Year(2002);
87         assertTrue(year1.equals(year2));
88 
89         year1 = new Year(1999);
90         assertFalse(year1.equals(year2));
91         year2 = new Year(1999);
92         assertTrue(year1.equals(year2));
93     }
94 
95     /**
96      * In GMT, the end of 2001 is java.util.Date(1009843199999L).  Use this to
97      * check the year constructor.
98      */
99     @Test
testDateConstructor1()100     public void testDateConstructor1() {
101 
102         TimeZone zone = TimeZone.getTimeZone("GMT");
103         Date d1 = new Date(1009843199999L);
104         Date d2 = new Date(1009843200000L);
105         Year y1 = new Year(d1, zone);
106         Year y2 = new Year(d2, zone);
107 
108         assertEquals(2001, y1.getYear());
109         assertEquals(1009843199999L, y1.getLastMillisecond(zone));
110 
111         assertEquals(2002, y2.getYear());
112         assertEquals(1009843200000L, y2.getFirstMillisecond(zone));
113 
114     }
115 
116     /**
117      * In Los Angeles, the end of 2001 is java.util.Date(1009871999999L).  Use
118      * this to check the year constructor.
119      */
120     @Test
testDateConstructor2()121     public void testDateConstructor2() {
122 
123         TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
124         Year y1 = new Year(new Date(1009871999999L), zone);
125         Year y2 = new Year(new Date(1009872000000L), zone);
126 
127         assertEquals(2001, y1.getYear());
128         assertEquals(1009871999999L, y1.getLastMillisecond(zone));
129 
130         assertEquals(2002, y2.getYear());
131         assertEquals(1009872000000L, y2.getFirstMillisecond(zone));
132 
133     }
134 
135     /**
136      * Set up a year equal to 1900.  Request the previous year, it should be
137      * null.
138      */
139     @Test
testMinuss9999Previous()140     public void testMinuss9999Previous() {
141         Year current = new Year(-9999);
142         Year previous = (Year) current.previous();
143         assertNull(previous);
144     }
145 
146     /**
147      * Set up a year equal to 1900.  Request the next year, it should be 1901.
148      */
149     @Test
test1900Next()150     public void test1900Next() {
151         Year current = new Year(1900);
152         Year next = (Year) current.next();
153         assertEquals(1901, next.getYear());
154     }
155 
156     /**
157      * Set up a year equal to 9999.  Request the previous year, it should be
158      * 9998.
159      */
160     @Test
test9999Previous()161     public void test9999Previous() {
162         Year current = new Year(9999);
163         Year previous = (Year) current.previous();
164         assertEquals(9998, previous.getYear());
165     }
166 
167     /**
168      * Set up a year equal to 9999.  Request the next year, it should be null.
169      */
170     @Test
test9999Next()171     public void test9999Next() {
172         Year current = new Year(9999);
173         Year next = (Year) current.next();
174         assertNull(next);
175     }
176 
177     /**
178      * Tests the year string parser.
179      */
180     @Test
testParseYear()181     public void testParseYear() {
182 
183         Year year = null;
184 
185         // test 1...
186         try {
187             year = Year.parseYear("2000");
188         }
189         catch (TimePeriodFormatException e) {
190             year = new Year(1900);
191         }
192         assertEquals(2000, year.getYear());
193 
194         // test 2...
195         try {
196             year = Year.parseYear(" 2001 ");
197         }
198         catch (TimePeriodFormatException e) {
199             year = new Year(1900);
200         }
201         assertEquals(2001, year.getYear());
202 
203         // test 3...
204         try {
205             year = Year.parseYear("99");
206         }
207         catch (TimePeriodFormatException e) {
208             year = new Year(1900);
209         }
210         assertEquals(99, year.getYear());
211 
212     }
213 
214     /**
215      * Serialize an instance, restore it, and check for equality.
216      */
217     @Test
testSerialization()218     public void testSerialization() {
219         Year y1 = new Year(1999);
220         Year y2 = (Year) TestUtilities.serialised(y1);
221         assertEquals(y1, y2);
222     }
223 
224     /**
225      * The {@link Year} class is immutable, so should not be {@link Cloneable}.
226      */
227     @Test
testNotCloneable()228     public void testNotCloneable() {
229         Year y = new Year(1999);
230         assertFalse(y instanceof Cloneable);
231     }
232 
233     /**
234      * Two objects that are equal are required to return the same hashCode.
235      */
236     @Test
testHashcode()237     public void testHashcode() {
238         Year y1 = new Year(1988);
239         Year y2 = new Year(1988);
240         assertTrue(y1.equals(y2));
241         int h1 = y1.hashCode();
242         int h2 = y2.hashCode();
243         assertEquals(h1, h2);
244     }
245 
246     /**
247      * Some checks for the getFirstMillisecond() method.
248      */
249     @Test
testGetFirstMillisecond()250     public void testGetFirstMillisecond() {
251         Locale saved = Locale.getDefault();
252         Locale.setDefault(Locale.UK);
253         TimeZone savedZone = TimeZone.getDefault();
254         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
255         Year y = new Year(1970);
256         // TODO: Check this result...
257         assertEquals(-3600000L, y.getFirstMillisecond());
258         Locale.setDefault(saved);
259         TimeZone.setDefault(savedZone);
260     }
261 
262     /**
263      * Some checks for the getFirstMillisecond(TimeZone) method.
264      */
265     @Test
testGetFirstMillisecondWithTimeZone()266     public void testGetFirstMillisecondWithTimeZone() {
267         Year y = new Year(1950);
268         TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
269         assertEquals(-631123200000L, y.getFirstMillisecond(zone));
270 
271         // try null calendar
272         boolean pass = false;
273         try {
274             y.getFirstMillisecond((TimeZone) null);
275         }
276         catch (NullPointerException e) {
277             pass = true;
278         }
279         assertTrue(pass);
280     }
281 
282     /**
283      * Some checks for the getFirstMillisecond(TimeZone) method.
284      */
285     @Test
testGetFirstMillisecondWithCalendar()286     public void testGetFirstMillisecondWithCalendar() {
287         Year y = new Year(2001);
288         GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
289         calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
290         assertEquals(978307200000L, y.getFirstMillisecond(calendar));
291 
292         // try null calendar
293         boolean pass = false;
294         try {
295             y.getFirstMillisecond((Calendar) null);
296         }
297         catch (NullPointerException e) {
298             pass = true;
299         }
300         assertTrue(pass);
301     }
302 
303     /**
304      * Some checks for the getLastMillisecond() method.
305      */
306     @Test
testGetLastMillisecond()307     public void testGetLastMillisecond() {
308         Locale saved = Locale.getDefault();
309         Locale.setDefault(Locale.UK);
310         TimeZone savedZone = TimeZone.getDefault();
311         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
312         Year y = new Year(1970);
313         // TODO: Check this result...
314         assertEquals(31532399999L, y.getLastMillisecond());
315         Locale.setDefault(saved);
316         TimeZone.setDefault(savedZone);
317     }
318 
319     /**
320      * Some checks for the getLastMillisecond(TimeZone) method.
321      */
322     @Test
testGetLastMillisecondWithTimeZone()323     public void testGetLastMillisecondWithTimeZone() {
324         Year y = new Year(1950);
325         TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
326         assertEquals(-599587200001L, y.getLastMillisecond(zone));
327 
328         // try null calendar
329         boolean pass = false;
330         try {
331             y.getLastMillisecond((TimeZone) null);
332         }
333         catch (NullPointerException e) {
334             pass = true;
335         }
336         assertTrue(pass);
337     }
338 
339     /**
340      * Some checks for the getLastMillisecond(TimeZone) method.
341      */
342     @Test
testGetLastMillisecondWithCalendar()343     public void testGetLastMillisecondWithCalendar() {
344         Year y = new Year(2001);
345         GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
346         calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
347         assertEquals(1009843199999L, y.getLastMillisecond(calendar));
348 
349         // try null calendar
350         boolean pass = false;
351         try {
352             y.getLastMillisecond((Calendar) null);
353         }
354         catch (NullPointerException e) {
355             pass = true;
356         }
357         assertTrue(pass);
358     }
359 
360     /**
361      * Some checks for the getSerialIndex() method.
362      */
363     @Test
testGetSerialIndex()364     public void testGetSerialIndex() {
365         Year y = new Year(2000);
366         assertEquals(2000L, y.getSerialIndex());
367     }
368 
369     /**
370      * Some checks for the testNext() method.
371      */
372     @Test
testNext()373     public void testNext() {
374         Year y = new Year(2000);
375         y = (Year) y.next();
376         assertEquals(2001, y.getYear());
377         y = new Year(9999);
378         assertNull(y.next());
379     }
380 
381     /**
382      * Some checks for the getStart() method.
383      */
384     @Test
testGetStart()385     public void testGetStart() {
386         Locale saved = Locale.getDefault();
387         Locale.setDefault(Locale.ITALY);
388         Calendar cal = Calendar.getInstance(Locale.ITALY);
389         cal.set(2006, Calendar.JANUARY, 1, 0, 0, 0);
390         cal.set(Calendar.MILLISECOND, 0);
391         Year y = new Year(2006);
392         assertEquals(cal.getTime(), y.getStart());
393         Locale.setDefault(saved);
394     }
395 
396     /**
397      * Some checks for the getEnd() method.
398      */
399     @Test
testGetEnd()400     public void testGetEnd() {
401         Locale saved = Locale.getDefault();
402         Locale.setDefault(Locale.ITALY);
403         Calendar cal = Calendar.getInstance(Locale.ITALY);
404         cal.set(2006, Calendar.DECEMBER, 31, 23, 59, 59);
405         cal.set(Calendar.MILLISECOND, 999);
406         Year y = new Year(2006);
407         assertEquals(cal.getTime(), y.getEnd());
408         Locale.setDefault(saved);
409     }
410 
411 }
412