MinuteTest.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * MinuteTest.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-Dec-2006 : Added test1611872() (DG);
  44. * 11-Jul-2007 : Fixed bad time zone assumption (DG);
  45. *
  46. */
  47. package org.jfree.data.time;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.assertFalse;
  50. import static org.junit.Assert.assertEquals;
  51. import static org.junit.Assert.assertNull;
  52. import java.util.Calendar;
  53. import java.util.Date;
  54. import java.util.GregorianCalendar;
  55. import java.util.Locale;
  56. import java.util.TimeZone;
  57. import org.jfree.chart.TestUtilities;
  58. import org.jfree.date.MonthConstants;
  59. import org.junit.Test;
  60. /**
  61. * Tests for the {@link Minute} class.
  62. */
  63. public class MinuteTest {
  64. /**
  65. * Check that a Minute instance is equal to itself.
  66. *
  67. * SourceForge Bug ID: 558850.
  68. */
  69. @Test
  70. public void testEqualsSelf() {
  71. Minute minute = new Minute();
  72. assertTrue(minute.equals(minute));
  73. }
  74. /**
  75. * Tests the equals method.
  76. */
  77. @Test
  78. public void testEquals() {
  79. Day day1 = new Day(29, MonthConstants.MARCH, 2002);
  80. Hour hour1 = new Hour(15, day1);
  81. Minute minute1 = new Minute(15, hour1);
  82. Day day2 = new Day(29, MonthConstants.MARCH, 2002);
  83. Hour hour2 = new Hour(15, day2);
  84. Minute minute2 = new Minute(15, hour2);
  85. assertTrue(minute1.equals(minute2));
  86. }
  87. /**
  88. * In GMT, the 4.55pm on 21 Mar 2002 is java.util.Date(1016729700000L).
  89. * Use this to check the Minute constructor.
  90. */
  91. @Test
  92. public void testDateConstructor1() {
  93. TimeZone zone = TimeZone.getTimeZone("GMT");
  94. Locale locale = Locale.getDefault(); // locale should not matter here
  95. Minute m1 = new Minute(new Date(1016729699999L), zone, locale);
  96. Minute m2 = new Minute(new Date(1016729700000L), zone, locale);
  97. assertEquals(54, m1.getMinute());
  98. assertEquals(1016729699999L, m1.getLastMillisecond(zone));
  99. assertEquals(55, m2.getMinute());
  100. assertEquals(1016729700000L, m2.getFirstMillisecond(zone));
  101. }
  102. /**
  103. * In Singapore, the 4.55pm on 21 Mar 2002 is
  104. * java.util.Date(1,014,281,700,000L). Use this to check the Minute
  105. * constructor.
  106. */
  107. @Test
  108. public void testDateConstructor2() {
  109. TimeZone zone = TimeZone.getTimeZone("Asia/Singapore");
  110. Locale locale = Locale.getDefault(); // locale should not matter here
  111. Minute m1 = new Minute(new Date(1016700899999L), zone, locale);
  112. Minute m2 = new Minute(new Date(1016700900000L), zone, locale);
  113. assertEquals(54, m1.getMinute());
  114. assertEquals(1016700899999L, m1.getLastMillisecond(zone));
  115. assertEquals(55, m2.getMinute());
  116. assertEquals(1016700900000L, m2.getFirstMillisecond(zone));
  117. }
  118. /**
  119. * Serialize an instance, restore it, and check for equality.
  120. */
  121. @Test
  122. public void testSerialization() {
  123. Minute m1 = new Minute();
  124. Minute m2 = (Minute) TestUtilities.serialised(m1);
  125. assertEquals(m1, m2);
  126. }
  127. /**
  128. * Two objects that are equal are required to return the same hashCode.
  129. */
  130. @Test
  131. public void testHashcode() {
  132. Minute m1 = new Minute(45, 5, 1, 2, 2003);
  133. Minute m2 = new Minute(45, 5, 1, 2, 2003);
  134. assertTrue(m1.equals(m2));
  135. int h1 = m1.hashCode();
  136. int h2 = m2.hashCode();
  137. assertEquals(h1, h2);
  138. }
  139. /**
  140. * The {@link Minute} class is immutable, so should not be
  141. * {@link Cloneable}.
  142. */
  143. @Test
  144. public void testNotCloneable() {
  145. Minute m = new Minute(45, 5, 1, 2, 2003);
  146. assertFalse(m instanceof Cloneable);
  147. }
  148. /**
  149. * Some checks for the getFirstMillisecond() method.
  150. */
  151. @Test
  152. public void testGetFirstMillisecond() {
  153. Locale saved = Locale.getDefault();
  154. Locale.setDefault(Locale.UK);
  155. TimeZone savedZone = TimeZone.getDefault();
  156. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  157. Minute m = new Minute(43, 15, 1, 4, 2006);
  158. assertEquals(1143902580000L, m.getFirstMillisecond());
  159. Locale.setDefault(saved);
  160. TimeZone.setDefault(savedZone);
  161. }
  162. /**
  163. * Some checks for the getFirstMillisecond(TimeZone) method.
  164. */
  165. @Test
  166. public void testGetFirstMillisecondWithTimeZone() {
  167. Minute m = new Minute(59, 15, 1, 4, 1950);
  168. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  169. assertEquals(-623289660000L, m.getFirstMillisecond(zone));
  170. // try null calendar
  171. boolean pass = false;
  172. try {
  173. m.getFirstMillisecond((TimeZone) null);
  174. }
  175. catch (NullPointerException e) {
  176. pass = true;
  177. }
  178. assertTrue(pass);
  179. }
  180. /**
  181. * Some checks for the getFirstMillisecond(TimeZone) method.
  182. */
  183. @Test
  184. public void testGetFirstMillisecondWithCalendar() {
  185. Minute m = new Minute(40, 2, 15, 4, 2000);
  186. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  187. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  188. assertEquals(955766400000L, m.getFirstMillisecond(calendar));
  189. // try null calendar
  190. boolean pass = false;
  191. try {
  192. m.getFirstMillisecond((Calendar) null);
  193. }
  194. catch (NullPointerException e) {
  195. pass = true;
  196. }
  197. assertTrue(pass);
  198. }
  199. /**
  200. * Some checks for the getLastMillisecond() method.
  201. */
  202. @Test
  203. public void testGetLastMillisecond() {
  204. Locale saved = Locale.getDefault();
  205. Locale.setDefault(Locale.UK);
  206. TimeZone savedZone = TimeZone.getDefault();
  207. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  208. Minute m = new Minute(1, 1, 1, 1, 1970);
  209. assertEquals(119999L, m.getLastMillisecond());
  210. Locale.setDefault(saved);
  211. TimeZone.setDefault(savedZone);
  212. }
  213. /**
  214. * Some checks for the getLastMillisecond(TimeZone) method.
  215. */
  216. @Test
  217. public void testGetLastMillisecondWithTimeZone() {
  218. Minute m = new Minute(1, 2, 7, 7, 1950);
  219. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  220. assertEquals(-614962680001L, m.getLastMillisecond(zone));
  221. // try null calendar
  222. boolean pass = false;
  223. try {
  224. m.getLastMillisecond((TimeZone) null);
  225. }
  226. catch (NullPointerException e) {
  227. pass = true;
  228. }
  229. assertTrue(pass);
  230. }
  231. /**
  232. * Some checks for the getLastMillisecond(TimeZone) method.
  233. */
  234. @Test
  235. public void testGetLastMillisecondWithCalendar() {
  236. Minute m = new Minute(45, 21, 21, 4, 2001);
  237. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  238. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  239. assertEquals(987889559999L, m.getLastMillisecond(calendar));
  240. // try null calendar
  241. boolean pass = false;
  242. try {
  243. m.getLastMillisecond((Calendar) null);
  244. }
  245. catch (NullPointerException e) {
  246. pass = true;
  247. }
  248. assertTrue(pass);
  249. }
  250. /**
  251. * Some checks for the getSerialIndex() method.
  252. */
  253. @Test
  254. public void testGetSerialIndex() {
  255. Minute m = new Minute(1, 1, 1, 1, 2000);
  256. assertEquals(52597501L, m.getSerialIndex());
  257. m = new Minute(1, 1, 1, 1, 1900);
  258. assertEquals(2941L, m.getSerialIndex());
  259. }
  260. /**
  261. * Some checks for the testNext() method.
  262. */
  263. @Test
  264. public void testNext() {
  265. Minute m = new Minute(30, 1, 12, 12, 2000);
  266. m = (Minute) m.next();
  267. assertEquals(2000, m.getHour().getYear());
  268. assertEquals(12, m.getHour().getMonth());
  269. assertEquals(12, m.getHour().getDayOfMonth());
  270. assertEquals(1, m.getHour().getHour());
  271. assertEquals(31, m.getMinute());
  272. m = new Minute(59, 23, 31, 12, 9999);
  273. assertNull(m.next());
  274. }
  275. /**
  276. * Some checks for the getStart() method.
  277. */
  278. @Test
  279. public void testGetStart() {
  280. Locale saved = Locale.getDefault();
  281. Locale.setDefault(Locale.ITALY);
  282. TimeZone savedZone = TimeZone.getDefault();
  283. TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
  284. Calendar cal = Calendar.getInstance(Locale.ITALY);
  285. cal.set(2006, Calendar.JANUARY, 16, 3, 47, 0);
  286. cal.set(Calendar.MILLISECOND, 0);
  287. Minute m = new Minute(47, 3, 16, 1, 2006);
  288. assertEquals(cal.getTime(), m.getStart());
  289. Locale.setDefault(saved);
  290. TimeZone.setDefault(savedZone);
  291. }
  292. /**
  293. * Some checks for the getEnd() method.
  294. */
  295. @Test
  296. public void testGetEnd() {
  297. Locale saved = Locale.getDefault();
  298. Locale.setDefault(Locale.ITALY);
  299. TimeZone savedZone = TimeZone.getDefault();
  300. TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
  301. Calendar cal = Calendar.getInstance(Locale.ITALY);
  302. cal.set(2006, Calendar.JANUARY, 16, 3, 47, 59);
  303. cal.set(Calendar.MILLISECOND, 999);
  304. Minute m = new Minute(47, 3, 16, 1, 2006);
  305. assertEquals(cal.getTime(), m.getEnd());
  306. Locale.setDefault(saved);
  307. TimeZone.setDefault(savedZone);
  308. }
  309. /**
  310. * Test for bug 1611872 - previous() fails for first minute in hour.
  311. */
  312. @Test
  313. public void test1611872() {
  314. Minute m1 = new Minute(0, 10, 15, 4, 2000);
  315. Minute m2 = (Minute) m1.previous();
  316. assertEquals(m2, new Minute(59, 9, 15, 4, 2000));
  317. }
  318. }