MonthTest.java 12 KB

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