HourTest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * HourTest.java
  29. * -------------
  30. * (C) Copyright 2002-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 29-Jan-2002 : Version 1 (DG);
  38. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39. * 13-Mar-2003 : Added serialization test (DG);
  40. * 21-Oct-2003 : Added hashCode test (DG);
  41. * 11-Jan-2005 : Added test for non-clonability (DG);
  42. * 05-Oct-2006 : Added new tests (DG);
  43. * 11-Jul-2007 : Fixed bad time zone assumption (DG);
  44. *
  45. */
  46. package org.jfree.data.time;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.assertFalse;
  49. import static org.junit.Assert.assertEquals;
  50. import static org.junit.Assert.assertNull;
  51. import java.util.Calendar;
  52. import java.util.Date;
  53. import java.util.GregorianCalendar;
  54. import java.util.Locale;
  55. import java.util.TimeZone;
  56. import org.jfree.chart.TestUtilities;
  57. import org.jfree.date.MonthConstants;
  58. import org.junit.Test;
  59. /**
  60. * Tests for the {@link Hour} class.
  61. */
  62. public class HourTest {
  63. /**
  64. * Check that an Hour instance is equal to itself.
  65. *
  66. * SourceForge Bug ID: 558850.
  67. */
  68. @Test
  69. public void testEqualsSelf() {
  70. Hour hour = new Hour();
  71. assertTrue(hour.equals(hour));
  72. }
  73. /**
  74. * Tests the equals method.
  75. */
  76. @Test
  77. public void testEquals() {
  78. Hour hour1 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002));
  79. Hour hour2 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002));
  80. assertTrue(hour1.equals(hour2));
  81. }
  82. /**
  83. * In GMT, the 4pm on 21 Mar 2002 is java.util.Date(1,014,307,200,000L).
  84. * Use this to check the hour constructor.
  85. */
  86. @Test
  87. public void testDateConstructor1() {
  88. TimeZone zone = TimeZone.getTimeZone("GMT");
  89. Locale locale = Locale.getDefault(); // locale should not matter here
  90. Hour h1 = new Hour(new Date(1014307199999L), zone, locale);
  91. Hour h2 = new Hour(new Date(1014307200000L), zone, locale);
  92. assertEquals(15, h1.getHour());
  93. assertEquals(1014307199999L, h1.getLastMillisecond(zone));
  94. assertEquals(16, h2.getHour());
  95. assertEquals(1014307200000L, h2.getFirstMillisecond(zone));
  96. }
  97. /**
  98. * In Sydney, the 4pm on 21 Mar 2002 is java.util.Date(1,014,267,600,000L).
  99. * Use this to check the hour constructor.
  100. */
  101. @Test
  102. public void testDateConstructor2() {
  103. TimeZone zone = TimeZone.getTimeZone("Australia/Sydney");
  104. Locale locale = Locale.getDefault(); // locale should not matter here
  105. Hour h1 = new Hour(new Date(1014267599999L), zone, locale);
  106. Hour h2 = new Hour (new Date(1014267600000L), zone, locale);
  107. assertEquals(15, h1.getHour());
  108. assertEquals(1014267599999L, h1.getLastMillisecond(zone));
  109. assertEquals(16, h2.getHour());
  110. assertEquals(1014267600000L, h2.getFirstMillisecond(zone));
  111. }
  112. /**
  113. * Set up an hour equal to hour zero, 1 January 1900. Request the
  114. * previous hour, it should be null.
  115. */
  116. @Test
  117. public void testFirstHourPrevious() {
  118. Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900));
  119. Hour previous = (Hour) first.previous();
  120. assertNull(previous);
  121. }
  122. /**
  123. * Set up an hour equal to hour zero, 1 January 1900. Request the next
  124. * hour, it should be null.
  125. */
  126. @Test
  127. public void testFirstHourNext() {
  128. Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900));
  129. Hour next = (Hour) first.next();
  130. assertEquals(1, next.getHour());
  131. assertEquals(1900, next.getYear());
  132. }
  133. /**
  134. * Set up an hour equal to hour zero, 1 January 1900. Request the previous
  135. * hour, it should be null.
  136. */
  137. @Test
  138. public void testLastHourPrevious() {
  139. Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999));
  140. Hour previous = (Hour) last.previous();
  141. assertEquals(22, previous.getHour());
  142. assertEquals(9999, previous.getYear());
  143. }
  144. /**
  145. * Set up an hour equal to hour zero, 1 January 1900. Request the next
  146. * hour, it should be null.
  147. */
  148. @Test
  149. public void testLastHourNext() {
  150. Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999));
  151. Hour next = (Hour) last.next();
  152. assertNull(next);
  153. }
  154. /**
  155. * Problem for date parsing.
  156. */
  157. @Test
  158. public void testParseHour() {
  159. // test 1...
  160. Hour h = Hour.parseHour("2002-01-29 13");
  161. assertEquals(13, h.getHour());
  162. }
  163. /**
  164. * Serialize an instance, restore it, and check for equality.
  165. */
  166. @Test
  167. public void testSerialization() {
  168. Hour h1 = new Hour();
  169. Hour h2 = (Hour) TestUtilities.serialised(h1);
  170. assertEquals(h1, h2);
  171. }
  172. /**
  173. * Two objects that are equal are required to return the same hashCode.
  174. */
  175. @Test
  176. public void testHashcode() {
  177. Hour h1 = new Hour(7, 9, 10, 1999);
  178. Hour h2 = new Hour(7, 9, 10, 1999);
  179. assertTrue(h1.equals(h2));
  180. int hash1 = h1.hashCode();
  181. int hash2 = h2.hashCode();
  182. assertEquals(hash1, hash2);
  183. }
  184. /**
  185. * The {@link Hour} class is immutable, so should not be {@link Cloneable}.
  186. */
  187. @Test
  188. public void testNotCloneable() {
  189. Hour h = new Hour(7, 9, 10, 1999);
  190. assertFalse(h instanceof Cloneable);
  191. }
  192. /**
  193. * Some checks for the getFirstMillisecond() method.
  194. */
  195. @Test
  196. public void testGetFirstMillisecond() {
  197. Locale saved = Locale.getDefault();
  198. Locale.setDefault(Locale.UK);
  199. TimeZone savedZone = TimeZone.getDefault();
  200. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  201. Hour h = new Hour(15, 1, 4, 2006);
  202. assertEquals(1143900000000L, h.getFirstMillisecond());
  203. Locale.setDefault(saved);
  204. TimeZone.setDefault(savedZone);
  205. }
  206. /**
  207. * Some checks for the getFirstMillisecond(TimeZone) method.
  208. */
  209. @Test
  210. public void testGetFirstMillisecondWithTimeZone() {
  211. Hour h = new Hour(15, 1, 4, 1950);
  212. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  213. assertEquals(-623293200000L, h.getFirstMillisecond(zone));
  214. // try null calendar
  215. boolean pass = false;
  216. try {
  217. h.getFirstMillisecond((TimeZone) null);
  218. }
  219. catch (NullPointerException e) {
  220. pass = true;
  221. }
  222. assertTrue(pass);
  223. }
  224. /**
  225. * Some checks for the getFirstMillisecond(TimeZone) method.
  226. */
  227. @Test
  228. public void testGetFirstMillisecondWithCalendar() {
  229. Hour h = new Hour(2, 15, 4, 2000);
  230. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  231. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  232. assertEquals(955764000000L, h.getFirstMillisecond(calendar));
  233. // try null calendar
  234. boolean pass = false;
  235. try {
  236. h.getFirstMillisecond((Calendar) null);
  237. }
  238. catch (NullPointerException e) {
  239. pass = true;
  240. }
  241. assertTrue(pass);
  242. }
  243. /**
  244. * Some checks for the getLastMillisecond() method.
  245. */
  246. @Test
  247. public void testGetLastMillisecond() {
  248. Locale saved = Locale.getDefault();
  249. Locale.setDefault(Locale.UK);
  250. TimeZone savedZone = TimeZone.getDefault();
  251. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  252. Hour h = new Hour(1, 1, 1, 1970);
  253. assertEquals(3599999L, h.getLastMillisecond());
  254. Locale.setDefault(saved);
  255. TimeZone.setDefault(savedZone);
  256. }
  257. /**
  258. * Some checks for the getLastMillisecond(TimeZone) method.
  259. */
  260. @Test
  261. public void testGetLastMillisecondWithTimeZone() {
  262. Hour h = new Hour(2, 7, 7, 1950);
  263. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  264. assertEquals(-614959200001L, h.getLastMillisecond(zone));
  265. // try null calendar
  266. boolean pass = false;
  267. try {
  268. h.getLastMillisecond((TimeZone) null);
  269. }
  270. catch (NullPointerException e) {
  271. pass = true;
  272. }
  273. assertTrue(pass);
  274. }
  275. /**
  276. * Some checks for the getLastMillisecond(TimeZone) method.
  277. */
  278. @Test
  279. public void testGetLastMillisecondWithCalendar() {
  280. Hour h = new Hour(21, 21, 4, 2001);
  281. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  282. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  283. assertEquals(987890399999L, h.getLastMillisecond(calendar));
  284. // try null calendar
  285. boolean pass = false;
  286. try {
  287. h.getLastMillisecond((Calendar) null);
  288. }
  289. catch (NullPointerException e) {
  290. pass = true;
  291. }
  292. assertTrue(pass);
  293. }
  294. /**
  295. * Some checks for the getSerialIndex() method.
  296. */
  297. @Test
  298. public void testGetSerialIndex() {
  299. Hour h = new Hour(1, 1, 1, 2000);
  300. assertEquals(876625L, h.getSerialIndex());
  301. h = new Hour(1, 1, 1, 1900);
  302. assertEquals(49L, h.getSerialIndex());
  303. }
  304. /**
  305. * Some checks for the testNext() method.
  306. */
  307. @Test
  308. public void testNext() {
  309. Hour h = new Hour(1, 12, 12, 2000);
  310. h = (Hour) h.next();
  311. assertEquals(2000, h.getYear());
  312. assertEquals(12, h.getMonth());
  313. assertEquals(12, h.getDayOfMonth());
  314. assertEquals(2, h.getHour());
  315. h = new Hour(23, 31, 12, 9999);
  316. assertNull(h.next());
  317. }
  318. /**
  319. * Some checks for the getStart() method.
  320. */
  321. @Test
  322. public void testGetStart() {
  323. Locale saved = Locale.getDefault();
  324. Locale.setDefault(Locale.ITALY);
  325. Calendar cal = Calendar.getInstance(Locale.ITALY);
  326. cal.set(2006, Calendar.JANUARY, 16, 3, 0, 0);
  327. cal.set(Calendar.MILLISECOND, 0);
  328. Hour h = new Hour(3, 16, 1, 2006);
  329. assertEquals(cal.getTime(), h.getStart());
  330. Locale.setDefault(saved);
  331. }
  332. /**
  333. * Some checks for the getEnd() method.
  334. */
  335. @Test
  336. public void testGetEnd() {
  337. Locale saved = Locale.getDefault();
  338. Locale.setDefault(Locale.ITALY);
  339. Calendar cal = Calendar.getInstance(Locale.ITALY);
  340. cal.set(2006, Calendar.JANUARY, 8, 1, 59, 59);
  341. cal.set(Calendar.MILLISECOND, 999);
  342. Hour h = new Hour(1, 8, 1, 2006);
  343. assertEquals(cal.getTime(), h.getEnd());
  344. Locale.setDefault(saved);
  345. }
  346. }