DayTest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. * DayTest.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. * 15-Nov-2001 : Version 1 (DG);
  38. * 20-Mar-2002 : Added new tests for Day constructor and getStart() and
  39. * getEnd() in different time zones (DG);
  40. * 26-Jun-2002 : Removed unnecessary imports (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 test for non-clonability (DG);
  45. * 03-Oct-2006 : Added testGetSerialIndex() (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.text.ParseException;
  55. import java.text.SimpleDateFormat;
  56. import java.util.Calendar;
  57. import java.util.Date;
  58. import java.util.GregorianCalendar;
  59. import java.util.Locale;
  60. import java.util.TimeZone;
  61. import org.jfree.chart.TestUtilities;
  62. import org.jfree.date.MonthConstants;
  63. import org.junit.Test;
  64. /**
  65. * Tests for the {@link Day} class.
  66. */
  67. public class DayTest {
  68. /**
  69. * Check that a Day instance is equal to itself.
  70. *
  71. * SourceForge Bug ID: 558850.
  72. */
  73. @Test
  74. public void testEqualsSelf() {
  75. Day day = new Day();
  76. assertTrue(day.equals(day));
  77. }
  78. /**
  79. * Tests the equals method.
  80. */
  81. @Test
  82. public void testEquals() {
  83. Day day1 = new Day(29, MonthConstants.MARCH, 2002);
  84. Day day2 = new Day(29, MonthConstants.MARCH, 2002);
  85. assertTrue(day1.equals(day2));
  86. }
  87. /**
  88. * In GMT, the end of 29 Feb 2004 is java.util.Date(1,078,099,199,999L).
  89. * Use this to check the day constructor.
  90. */
  91. @Test
  92. public void testDateConstructor1() {
  93. TimeZone zone = TimeZone.getTimeZone("GMT");
  94. Locale locale = Locale.UK;
  95. Day d1 = new Day(new Date(1078099199999L), zone, locale);
  96. Day d2 = new Day(new Date(1078099200000L), zone, locale);
  97. assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
  98. assertEquals(1078099199999L, d1.getLastMillisecond(zone));
  99. assertEquals(MonthConstants.MARCH, d2.getMonth());
  100. assertEquals(1078099200000L, d2.getFirstMillisecond(zone));
  101. }
  102. /**
  103. * In Helsinki, the end of 29 Feb 2004 is
  104. * java.util.Date(1,078,091,999,999L). Use this to check the Day
  105. * constructor.
  106. */
  107. @Test
  108. public void testDateConstructor2() {
  109. TimeZone zone = TimeZone.getTimeZone("Europe/Helsinki");
  110. Locale locale = Locale.getDefault(); // locale shouldn't matter here
  111. Day d1 = new Day(new Date(1078091999999L), zone, locale);
  112. Day d2 = new Day(new Date(1078092000000L), zone, locale);
  113. assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
  114. assertEquals(1078091999999L, d1.getLastMillisecond(zone));
  115. assertEquals(MonthConstants.MARCH, d2.getMonth());
  116. assertEquals(1078092000000L, d2.getFirstMillisecond(zone));
  117. }
  118. /**
  119. * Set up a day equal to 1 January 1900. Request the previous day, it
  120. * should be null.
  121. */
  122. @Test
  123. public void test1Jan1900Previous() {
  124. Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
  125. Day previous = (Day) jan1st1900.previous();
  126. assertNull(previous);
  127. }
  128. /**
  129. * Set up a day equal to 1 January 1900. Request the next day, it should
  130. * be 2 January 1900.
  131. */
  132. @Test
  133. public void test1Jan1900Next() {
  134. Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
  135. Day next = (Day) jan1st1900.next();
  136. assertEquals(2, next.getDayOfMonth());
  137. }
  138. /**
  139. * Set up a day equal to 31 December 9999. Request the previous day, it
  140. * should be 30 December 9999.
  141. */
  142. @Test
  143. public void test31Dec9999Previous() {
  144. Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
  145. Day previous = (Day) dec31st9999.previous();
  146. assertEquals(30, previous.getDayOfMonth());
  147. }
  148. /**
  149. * Set up a day equal to 31 December 9999. Request the next day, it should
  150. * be null.
  151. */
  152. @Test
  153. public void test31Dec9999Next() {
  154. Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
  155. Day next = (Day) dec31st9999.next();
  156. assertNull(next);
  157. }
  158. /**
  159. * Problem for date parsing.
  160. * <p>
  161. * This test works only correct if the short pattern of the date
  162. * format is "dd/MM/yyyy". If not, this test will result in a
  163. * false negative.
  164. *
  165. * @throws ParseException on parsing errors.
  166. */
  167. @Test
  168. public void testParseDay() throws ParseException {
  169. GregorianCalendar gc = new GregorianCalendar(2001, 12, 31);
  170. SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
  171. Date reference = format.parse("31/12/2001");
  172. if (reference.equals(gc.getTime())) {
  173. // test 1...
  174. Day d = Day.parseDay("31/12/2001");
  175. assertEquals(37256, d.getSerialDate().toSerial());
  176. }
  177. // test 2...
  178. Day d = Day.parseDay("2001-12-31");
  179. assertEquals(37256, d.getSerialDate().toSerial());
  180. }
  181. /**
  182. * Serialize an instance, restore it, and check for equality.
  183. */
  184. @Test
  185. public void testSerialization() {
  186. Day d1 = new Day(15, 4, 2000);
  187. Day d2 = (Day) TestUtilities.serialised(d1);
  188. assertEquals(d1, d2);
  189. }
  190. /**
  191. * Two objects that are equal are required to return the same hashCode.
  192. */
  193. @Test
  194. public void testHashcode() {
  195. Day d1 = new Day(1, 2, 2003);
  196. Day d2 = new Day(1, 2, 2003);
  197. assertTrue(d1.equals(d2));
  198. int h1 = d1.hashCode();
  199. int h2 = d2.hashCode();
  200. assertEquals(h1, h2);
  201. }
  202. /**
  203. * The {@link Day} class is immutable, so should not be {@link Cloneable}.
  204. */
  205. @Test
  206. public void testNotCloneable() {
  207. Day d = new Day(1, 2, 2003);
  208. assertFalse(d instanceof Cloneable);
  209. }
  210. /**
  211. * Some checks for the getSerialIndex() method.
  212. */
  213. @Test
  214. public void testGetSerialIndex() {
  215. Day d = new Day(1, 1, 1900);
  216. assertEquals(2, d.getSerialIndex());
  217. d = new Day(15, 4, 2000);
  218. assertEquals(36631, d.getSerialIndex());
  219. }
  220. /**
  221. * Some checks for the getFirstMillisecond() method.
  222. */
  223. @Test
  224. public void testGetFirstMillisecond() {
  225. Locale saved = Locale.getDefault();
  226. Locale.setDefault(Locale.UK);
  227. TimeZone savedZone = TimeZone.getDefault();
  228. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  229. Day d = new Day(1, 3, 1970);
  230. assertEquals(5094000000L, d.getFirstMillisecond());
  231. Locale.setDefault(saved);
  232. TimeZone.setDefault(savedZone);
  233. }
  234. /**
  235. * Some checks for the getFirstMillisecond(TimeZone) method.
  236. */
  237. @Test
  238. public void testGetFirstMillisecondWithTimeZone() {
  239. Day d = new Day(26, 4, 1950);
  240. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  241. assertEquals(-621187200000L, d.getFirstMillisecond(zone));
  242. // try null calendar
  243. boolean pass = false;
  244. try {
  245. d.getFirstMillisecond((TimeZone) null);
  246. }
  247. catch (NullPointerException e) {
  248. pass = true;
  249. }
  250. assertTrue(pass);
  251. }
  252. /**
  253. * Some checks for the getFirstMillisecond(TimeZone) method.
  254. */
  255. @Test
  256. public void testGetFirstMillisecondWithCalendar() {
  257. Day d = new Day(1, 12, 2001);
  258. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  259. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  260. assertEquals(1007164800000L, d.getFirstMillisecond(calendar));
  261. // try null calendar
  262. boolean pass = false;
  263. try {
  264. d.getFirstMillisecond((Calendar) null);
  265. }
  266. catch (NullPointerException e) {
  267. pass = true;
  268. }
  269. assertTrue(pass);
  270. }
  271. /**
  272. * Some checks for the getLastMillisecond() method.
  273. */
  274. @Test
  275. public void testGetLastMillisecond() {
  276. Locale saved = Locale.getDefault();
  277. Locale.setDefault(Locale.UK);
  278. TimeZone savedZone = TimeZone.getDefault();
  279. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  280. Day d = new Day(1, 1, 1970);
  281. assertEquals(82799999L, d.getLastMillisecond());
  282. Locale.setDefault(saved);
  283. TimeZone.setDefault(savedZone);
  284. }
  285. /**
  286. * Some checks for the getLastMillisecond(TimeZone) method.
  287. */
  288. @Test
  289. public void testGetLastMillisecondWithTimeZone() {
  290. Day d = new Day(1, 2, 1950);
  291. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  292. assertEquals(-628358400001L, d.getLastMillisecond(zone));
  293. // try null calendar
  294. boolean pass = false;
  295. try {
  296. d.getLastMillisecond((TimeZone) null);
  297. }
  298. catch (NullPointerException e) {
  299. pass = true;
  300. }
  301. assertTrue(pass);
  302. }
  303. /**
  304. * Some checks for the getLastMillisecond(TimeZone) method.
  305. */
  306. @Test
  307. public void testGetLastMillisecondWithCalendar() {
  308. Day d = new Day(4, 5, 2001);
  309. Calendar calendar = Calendar.getInstance(
  310. TimeZone.getTimeZone("Europe/London"), Locale.UK);
  311. assertEquals(989017199999L, d.getLastMillisecond(calendar));
  312. // try null calendar
  313. boolean pass = false;
  314. try {
  315. d.getLastMillisecond((Calendar) null);
  316. }
  317. catch (NullPointerException e) {
  318. pass = true;
  319. }
  320. assertTrue(pass);
  321. }
  322. /**
  323. * Some checks for the testNext() method.
  324. */
  325. @Test
  326. public void testNext() {
  327. Day d = new Day(25, 12, 2000);
  328. d = (Day) d.next();
  329. assertEquals(2000, d.getYear());
  330. assertEquals(12, d.getMonth());
  331. assertEquals(26, d.getDayOfMonth());
  332. d = new Day(31, 12, 9999);
  333. assertNull(d.next());
  334. }
  335. /**
  336. * Some checks for the getStart() method.
  337. */
  338. @Test
  339. public void testGetStart() {
  340. Locale saved = Locale.getDefault();
  341. Locale.setDefault(Locale.ITALY);
  342. Calendar cal = Calendar.getInstance(Locale.ITALY);
  343. cal.set(2006, Calendar.NOVEMBER, 3, 0, 0, 0);
  344. cal.set(Calendar.MILLISECOND, 0);
  345. Day d = new Day(3, 11, 2006);
  346. assertEquals(cal.getTime(), d.getStart());
  347. Locale.setDefault(saved);
  348. }
  349. /**
  350. * Some checks for the getEnd() method.
  351. */
  352. @Test
  353. public void testGetEnd() {
  354. Locale saved = Locale.getDefault();
  355. Locale.setDefault(Locale.ITALY);
  356. Calendar cal = Calendar.getInstance(Locale.ITALY);
  357. cal.set(1900, Calendar.JANUARY, 1, 23, 59, 59);
  358. cal.set(Calendar.MILLISECOND, 999);
  359. Day d = new Day(1, 1, 1900);
  360. assertEquals(cal.getTime(), d.getEnd());
  361. Locale.setDefault(saved);
  362. }
  363. }