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 * MonthTest.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 * 14-Feb-2002 : Order of parameters in Month(int, int) constructor 39 * changed (DG); 40 * 26-Jun-2002 : Removed unnecessary import (DG); 41 * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG); 42 * 13-Mar-2003 : Added serialization test (DG); 43 * 21-Oct-2003 : Added hashCode test (DG); 44 * 11-Jan-2005 : Added non-clonability test (DG); 45 * 05-Oct-2006 : Added some new tests (DG); 46 * 11-Jul-2007 : Fixed bad time zone assumption (DG); 47 * 48 */ 49 50 package org.jfree.data.time; 51 52 import static org.junit.Assert.assertTrue; 53 import static org.junit.Assert.assertFalse; 54 import static org.junit.Assert.assertEquals; 55 import static org.junit.Assert.assertNull; 56 57 import java.util.Calendar; 58 import java.util.Date; 59 import java.util.GregorianCalendar; 60 import java.util.Locale; 61 import java.util.TimeZone; 62 63 import org.jfree.chart.TestUtilities; 64 65 import org.jfree.date.MonthConstants; 66 import org.junit.Before; 67 import org.junit.Test; 68 69 /** 70 * Tests for the {@link Month} class. 71 */ 72 public class MonthTest { 73 74 /** A month. */ 75 private Month jan1900; 76 77 /** A month. */ 78 private Month feb1900; 79 80 /** A month. */ 81 private Month nov9999; 82 83 /** A month. */ 84 private Month dec9999; 85 86 /** 87 * Common test setup. 88 */ 89 @Before setUp()90 public void setUp() { 91 this.jan1900 = new Month(MonthConstants.JANUARY, 1900); 92 this.feb1900 = new Month(MonthConstants.FEBRUARY, 1900); 93 this.nov9999 = new Month(MonthConstants.NOVEMBER, 9999); 94 this.dec9999 = new Month(MonthConstants.DECEMBER, 9999); 95 } 96 97 /** 98 * Check that a Month instance is equal to itself. 99 * 100 * SourceForge Bug ID: 558850. 101 */ 102 @Test testEqualsSelf()103 public void testEqualsSelf() { 104 Month month = new Month(); 105 assertTrue(month.equals(month)); 106 } 107 108 /** 109 * Tests the equals method. 110 */ 111 @Test testEquals()112 public void testEquals() { 113 Month m1 = new Month(MonthConstants.MAY, 2002); 114 Month m2 = new Month(MonthConstants.MAY, 2002); 115 assertTrue(m1.equals(m2)); 116 } 117 118 /** 119 * In GMT, the end of Feb 2000 is java.util.Date(951,868,799,999L). Use 120 * this to check the Month constructor. 121 */ 122 @Test testDateConstructor1()123 public void testDateConstructor1() { 124 125 TimeZone zone = TimeZone.getTimeZone("GMT"); 126 Month m1 = new Month(new Date(951868799999L), zone); 127 Month m2 = new Month(new Date(951868800000L), zone); 128 129 assertEquals(MonthConstants.FEBRUARY, m1.getMonth()); 130 assertEquals(951868799999L, m1.getLastMillisecond(zone)); 131 132 assertEquals(MonthConstants.MARCH, m2.getMonth()); 133 assertEquals(951868800000L, m2.getFirstMillisecond(zone)); 134 135 } 136 137 /** 138 * In Auckland, the end of Feb 2000 is java.util.Date(951,821,999,999L). 139 * Use this to check the Month constructor. 140 */ 141 @Test testDateConstructor2()142 public void testDateConstructor2() { 143 144 TimeZone zone = TimeZone.getTimeZone("Pacific/Auckland"); 145 Month m1 = new Month(new Date(951821999999L), zone); 146 Month m2 = new Month(new Date(951822000000L), zone); 147 148 assertEquals(MonthConstants.FEBRUARY, m1.getMonth()); 149 assertEquals(951821999999L, m1.getLastMillisecond(zone)); 150 151 assertEquals(MonthConstants.MARCH, m2.getMonth()); 152 assertEquals(951822000000L, m2.getFirstMillisecond(zone)); 153 154 } 155 156 /** 157 * Set up a month equal to Jan 1900. Request the previous month, it should 158 * be null. 159 */ 160 @Test testJan1900Previous()161 public void testJan1900Previous() { 162 Month previous = (Month) this.jan1900.previous(); 163 assertNull(previous); 164 } 165 166 /** 167 * Set up a month equal to Jan 1900. Request the next month, it should be 168 * Feb 1900. 169 */ 170 @Test testJan1900Next()171 public void testJan1900Next() { 172 Month next = (Month) this.jan1900.next(); 173 assertEquals(this.feb1900, next); 174 } 175 176 /** 177 * Set up a month equal to Dec 9999. Request the previous month, it should 178 * be Nov 9999. 179 */ 180 @Test testDec9999Previous()181 public void testDec9999Previous() { 182 Month previous = (Month) this.dec9999.previous(); 183 assertEquals(this.nov9999, previous); 184 } 185 186 /** 187 * Set up a month equal to Dec 9999. Request the next month, it should be 188 * null. 189 */ 190 @Test testDec9999Next()191 public void testDec9999Next() { 192 Month next = (Month) this.dec9999.next(); 193 assertNull(next); 194 } 195 196 /** 197 * Tests the string parsing code... 198 */ 199 @Test testParseMonth()200 public void testParseMonth() { 201 202 Month month = null; 203 204 // test 1... 205 try { 206 month = Month.parseMonth("1990-01"); 207 } 208 catch (TimePeriodFormatException e) { 209 month = new Month(1, 1900); 210 } 211 assertEquals(1, month.getMonth()); 212 assertEquals(1990, month.getYear().getYear()); 213 214 // test 2... 215 try { 216 month = Month.parseMonth("02-1991"); 217 } 218 catch (TimePeriodFormatException e) { 219 month = new Month(1, 1900); 220 } 221 assertEquals(2, month.getMonth()); 222 assertEquals(1991, month.getYear().getYear()); 223 224 // test 3... 225 try { 226 month = Month.parseMonth("March 1993"); 227 } 228 catch (TimePeriodFormatException e) { 229 month = new Month(1, 1900); 230 } 231 assertEquals(3, month.getMonth()); 232 assertEquals(1993, month.getYear().getYear()); 233 234 } 235 236 /** 237 * Serialize an instance, restore it, and check for equality. 238 */ 239 @Test testSerialization()240 public void testSerialization() { 241 Month m1 = new Month(12, 1999); 242 Month m2 = (Month) TestUtilities.serialised(m1); 243 assertEquals(m1, m2); 244 } 245 246 /** 247 * Two objects that are equal are required to return the same hashCode. 248 */ 249 @Test testHashcode()250 public void testHashcode() { 251 Month m1 = new Month(2, 2003); 252 Month m2 = new Month(2, 2003); 253 assertTrue(m1.equals(m2)); 254 int h1 = m1.hashCode(); 255 int h2 = m2.hashCode(); 256 assertEquals(h1, h2); 257 } 258 259 /** 260 * The {@link Month} class is immutable, so should not be {@link Cloneable}. 261 */ 262 @Test testNotCloneable()263 public void testNotCloneable() { 264 Month m = new Month(2, 2003); 265 assertFalse(m instanceof Cloneable); 266 } 267 268 /** 269 * Some checks for the getFirstMillisecond() method. 270 */ 271 @Test testGetFirstMillisecond()272 public void testGetFirstMillisecond() { 273 Locale saved = Locale.getDefault(); 274 Locale.setDefault(Locale.UK); 275 TimeZone savedZone = TimeZone.getDefault(); 276 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 277 Month m = new Month(3, 1970); 278 assertEquals(5094000000L, m.getFirstMillisecond()); 279 Locale.setDefault(saved); 280 TimeZone.setDefault(savedZone); 281 } 282 283 /** 284 * Some checks for the getFirstMillisecond(TimeZone) method. 285 */ 286 @Test testGetFirstMillisecondWithTimeZone()287 public void testGetFirstMillisecondWithTimeZone() { 288 Month m = new Month(2, 1950); 289 TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles"); 290 assertEquals(-628444800000L, m.getFirstMillisecond(zone)); 291 292 // try null calendar 293 boolean pass = false; 294 try { 295 m.getFirstMillisecond((TimeZone) null); 296 } 297 catch (NullPointerException e) { 298 pass = true; 299 } 300 assertTrue(pass); 301 } 302 303 /** 304 * Some checks for the getFirstMillisecond(TimeZone) method. 305 */ 306 @Test testGetFirstMillisecondWithCalendar()307 public void testGetFirstMillisecondWithCalendar() { 308 Month m = new Month(1, 2001); 309 GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY); 310 calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt")); 311 assertEquals(978307200000L, m.getFirstMillisecond(calendar)); 312 313 // try null calendar 314 boolean pass = false; 315 try { 316 m.getFirstMillisecond((Calendar) null); 317 } 318 catch (NullPointerException e) { 319 pass = true; 320 } 321 assertTrue(pass); 322 } 323 324 /** 325 * Some checks for the getLastMillisecond() method. 326 */ 327 @Test testGetLastMillisecond()328 public void testGetLastMillisecond() { 329 Locale saved = Locale.getDefault(); 330 Locale.setDefault(Locale.UK); 331 TimeZone savedZone = TimeZone.getDefault(); 332 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 333 Month m = new Month(3, 1970); 334 assertEquals(7772399999L, m.getLastMillisecond()); 335 Locale.setDefault(saved); 336 TimeZone.setDefault(savedZone); 337 } 338 339 /** 340 * Some checks for the getLastMillisecond(TimeZone) method. 341 */ 342 @Test testGetLastMillisecondWithTimeZone()343 public void testGetLastMillisecondWithTimeZone() { 344 Month m = new Month(2, 1950); 345 TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles"); 346 assertEquals(-626025600001L, m.getLastMillisecond(zone)); 347 348 // try null calendar 349 boolean pass = false; 350 try { 351 m.getLastMillisecond((TimeZone) null); 352 } 353 catch (NullPointerException e) { 354 pass = true; 355 } 356 assertTrue(pass); 357 } 358 359 /** 360 * Some checks for the getLastMillisecond(TimeZone) method. 361 */ 362 @Test testGetLastMillisecondWithCalendar()363 public void testGetLastMillisecondWithCalendar() { 364 Month m = new Month(3, 2001); 365 GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY); 366 calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt")); 367 assertEquals(986083199999L, m.getLastMillisecond(calendar)); 368 369 // try null calendar 370 boolean pass = false; 371 try { 372 m.getLastMillisecond((Calendar) null); 373 } 374 catch (NullPointerException e) { 375 pass = true; 376 } 377 assertTrue(pass); 378 } 379 380 /** 381 * Some checks for the getSerialIndex() method. 382 */ 383 @Test testGetSerialIndex()384 public void testGetSerialIndex() { 385 Month m = new Month(1, 2000); 386 assertEquals(24001L, m.getSerialIndex()); 387 m = new Month(1, 1900); 388 assertEquals(22801L, m.getSerialIndex()); 389 } 390 391 /** 392 * Some checks for the testNext() method. 393 */ 394 @Test testNext()395 public void testNext() { 396 Month m = new Month(12, 2000); 397 m = (Month) m.next(); 398 assertEquals(new Year(2001), m.getYear()); 399 assertEquals(1, m.getMonth()); 400 m = new Month(12, 9999); 401 assertNull(m.next()); 402 } 403 404 /** 405 * Some checks for the getStart() method. 406 */ 407 @Test testGetStart()408 public void testGetStart() { 409 Locale saved = Locale.getDefault(); 410 Locale.setDefault(Locale.ITALY); 411 Calendar cal = Calendar.getInstance(Locale.ITALY); 412 cal.set(2006, Calendar.MARCH, 1, 0, 0, 0); 413 cal.set(Calendar.MILLISECOND, 0); 414 Month m = new Month(3, 2006); 415 assertEquals(cal.getTime(), m.getStart()); 416 Locale.setDefault(saved); 417 } 418 419 /** 420 * Some checks for the getEnd() method. 421 */ 422 @Test testGetEnd()423 public void testGetEnd() { 424 Locale saved = Locale.getDefault(); 425 Locale.setDefault(Locale.ITALY); 426 Calendar cal = Calendar.getInstance(Locale.ITALY); 427 cal.set(2006, Calendar.JANUARY, 31, 23, 59, 59); 428 cal.set(Calendar.MILLISECOND, 999); 429 Month m = new Month(1, 2006); 430 assertEquals(cal.getTime(), m.getEnd()); 431 Locale.setDefault(saved); 432 } 433 434 } 435