MillisecondTest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * MillisecondTest.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. * 21-Oct-2003 : Added hashCode tests (DG);
  40. * 29-Apr-2004 : Added test for getMiddleMillisecond() method (DG);
  41. * 11-Jan-2005 : Added test for non-clonability (DG);
  42. * 05-Oct-2006 : Added some 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 Millisecond} class.
  61. */
  62. public class MillisecondTest {
  63. /**
  64. * Check that a {@link Millisecond} instance is equal to itself.
  65. *
  66. * SourceForge Bug ID: 558850.
  67. */
  68. @Test
  69. public void testEqualsSelf() {
  70. Millisecond millisecond = new Millisecond();
  71. assertTrue(millisecond.equals(millisecond));
  72. }
  73. /**
  74. * Tests the equals method.
  75. */
  76. @Test
  77. public void testEquals() {
  78. Day day1 = new Day(29, MonthConstants.MARCH, 2002);
  79. Hour hour1 = new Hour(15, day1);
  80. Minute minute1 = new Minute(15, hour1);
  81. Second second1 = new Second(34, minute1);
  82. Millisecond milli1 = new Millisecond(999, second1);
  83. Day day2 = new Day(29, MonthConstants.MARCH, 2002);
  84. Hour hour2 = new Hour(15, day2);
  85. Minute minute2 = new Minute(15, hour2);
  86. Second second2 = new Second(34, minute2);
  87. Millisecond milli2 = new Millisecond(999, second2);
  88. assertTrue(milli1.equals(milli2));
  89. }
  90. /**
  91. * In GMT, the 4.55:59.123pm on 21 Mar 2002 is
  92. * java.util.Date(1016729759123L). Use this to check the Millisecond
  93. * constructor.
  94. */
  95. @Test
  96. public void testDateConstructor1() {
  97. TimeZone zone = TimeZone.getTimeZone("GMT");
  98. Locale locale = Locale.getDefault(); // locale should not matter here
  99. Millisecond m1 = new Millisecond(new Date(1016729759122L), zone,
  100. locale);
  101. Millisecond m2 = new Millisecond(new Date(1016729759123L), zone,
  102. locale);
  103. assertEquals(122, m1.getMillisecond());
  104. assertEquals(1016729759122L, m1.getLastMillisecond(zone));
  105. assertEquals(123, m2.getMillisecond());
  106. assertEquals(1016729759123L, m2.getFirstMillisecond(zone));
  107. }
  108. /**
  109. * In Tallinn, the 4.55:59.123pm on 21 Mar 2002 is
  110. * java.util.Date(1016722559123L). Use this to check the Millisecond
  111. * constructor.
  112. */
  113. @Test
  114. public void testDateConstructor2() {
  115. TimeZone zone = TimeZone.getTimeZone("Europe/Tallinn");
  116. Locale locale = Locale.getDefault(); // locale should not matter here
  117. Millisecond m1 = new Millisecond(new Date(1016722559122L), zone,
  118. locale);
  119. Millisecond m2 = new Millisecond(new Date(1016722559123L), zone,
  120. locale);
  121. assertEquals(122, m1.getMillisecond());
  122. assertEquals(1016722559122L, m1.getLastMillisecond(zone));
  123. assertEquals(123, m2.getMillisecond());
  124. assertEquals(1016722559123L, m2.getFirstMillisecond(zone));
  125. }
  126. /**
  127. * Serialize an instance, restore it, and check for equality.
  128. */
  129. @Test
  130. public void testSerialization() {
  131. Millisecond m1 = new Millisecond();
  132. Millisecond m2 = (Millisecond) TestUtilities.serialised(m1);
  133. assertEquals(m1, m2);
  134. }
  135. /**
  136. * Two objects that are equal are required to return the same hashCode.
  137. */
  138. @Test
  139. public void testHashcode() {
  140. Millisecond m1 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
  141. Millisecond m2 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
  142. assertTrue(m1.equals(m2));
  143. int hash1 = m1.hashCode();
  144. int hash2 = m2.hashCode();
  145. assertEquals(hash1, hash2);
  146. }
  147. /**
  148. * A test for bug report 943985 - the calculation for the middle
  149. * millisecond is incorrect for odd milliseconds.
  150. */
  151. @Test
  152. public void test943985() {
  153. Millisecond ms = new Millisecond(new java.util.Date(4));
  154. assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond());
  155. assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
  156. ms = new Millisecond(new java.util.Date(5));
  157. assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond());
  158. assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
  159. }
  160. /**
  161. * The {@link Millisecond} class is immutable, so should not be
  162. * {@link Cloneable}.
  163. */
  164. @Test
  165. public void testNotCloneable() {
  166. Millisecond m = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
  167. assertFalse(m instanceof Cloneable);
  168. }
  169. /**
  170. * Some checks for the getFirstMillisecond() method.
  171. */
  172. @Test
  173. public void testGetFirstMillisecond() {
  174. Locale saved = Locale.getDefault();
  175. Locale.setDefault(Locale.UK);
  176. TimeZone savedZone = TimeZone.getDefault();
  177. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  178. Millisecond m = new Millisecond(500, 15, 43, 15, 1, 4, 2006);
  179. assertEquals(1143902595500L, m.getFirstMillisecond());
  180. Locale.setDefault(saved);
  181. TimeZone.setDefault(savedZone);
  182. }
  183. /**
  184. * Some checks for the getFirstMillisecond(TimeZone) method.
  185. */
  186. @Test
  187. public void testGetFirstMillisecondWithTimeZone() {
  188. Millisecond m = new Millisecond(500, 50, 59, 15, 1, 4, 1950);
  189. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  190. assertEquals(-623289609500L, m.getFirstMillisecond(zone));
  191. // try null calendar
  192. boolean pass = false;
  193. try {
  194. m.getFirstMillisecond((TimeZone) null);
  195. }
  196. catch (NullPointerException e) {
  197. pass = true;
  198. }
  199. assertTrue(pass);
  200. }
  201. /**
  202. * Some checks for the getFirstMillisecond(TimeZone) method.
  203. */
  204. @Test
  205. public void testGetFirstMillisecondWithCalendar() {
  206. Millisecond m = new Millisecond(500, 55, 40, 2, 15, 4, 2000);
  207. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  208. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  209. assertEquals(955766455500L, m.getFirstMillisecond(calendar));
  210. // try null calendar
  211. boolean pass = false;
  212. try {
  213. m.getFirstMillisecond((Calendar) null);
  214. }
  215. catch (NullPointerException e) {
  216. pass = true;
  217. }
  218. assertTrue(pass);
  219. }
  220. /**
  221. * Some checks for the getLastMillisecond() method.
  222. */
  223. @Test
  224. public void testGetLastMillisecond() {
  225. Locale saved = Locale.getDefault();
  226. Locale.setDefault(Locale.UK);
  227. TimeZone savedZone = TimeZone.getDefault();
  228. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  229. Millisecond m = new Millisecond(750, 1, 1, 1, 1, 1, 1970);
  230. assertEquals(61750L, m.getLastMillisecond());
  231. Locale.setDefault(saved);
  232. TimeZone.setDefault(savedZone);
  233. }
  234. /**
  235. * Some checks for the getLastMillisecond(TimeZone) method.
  236. */
  237. @Test
  238. public void testGetLastMillisecondWithTimeZone() {
  239. Millisecond m = new Millisecond(750, 55, 1, 2, 7, 7, 1950);
  240. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  241. assertEquals(-614962684250L, m.getLastMillisecond(zone));
  242. // try null calendar
  243. boolean pass = false;
  244. try {
  245. m.getLastMillisecond((TimeZone) null);
  246. }
  247. catch (NullPointerException e) {
  248. pass = true;
  249. }
  250. assertTrue(pass);
  251. }
  252. /**
  253. * Some checks for the getLastMillisecond(TimeZone) method.
  254. */
  255. @Test
  256. public void testGetLastMillisecondWithCalendar() {
  257. Millisecond m = new Millisecond(250, 50, 45, 21, 21, 4, 2001);
  258. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  259. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  260. assertEquals(987889550250L, m.getLastMillisecond(calendar));
  261. // try null calendar
  262. boolean pass = false;
  263. try {
  264. m.getLastMillisecond((Calendar) null);
  265. }
  266. catch (NullPointerException e) {
  267. pass = true;
  268. }
  269. assertTrue(pass);
  270. }
  271. /**
  272. * Some checks for the getSerialIndex() method.
  273. */
  274. @Test
  275. public void testGetSerialIndex() {
  276. Millisecond m = new Millisecond(500, 1, 1, 1, 1, 1, 2000);
  277. assertEquals(3155850061500L, m.getSerialIndex());
  278. m = new Millisecond(500, 1, 1, 1, 1, 1, 1900);
  279. // TODO: this must be wrong...
  280. assertEquals(176461500L, m.getSerialIndex());
  281. }
  282. /**
  283. * Some checks for the testNext() method.
  284. */
  285. @Test
  286. public void testNext() {
  287. Millisecond m = new Millisecond(555, 55, 30, 1, 12, 12, 2000);
  288. m = (Millisecond) m.next();
  289. assertEquals(2000, m.getSecond().getMinute().getHour().getYear());
  290. assertEquals(12, m.getSecond().getMinute().getHour().getMonth());
  291. assertEquals(12, m.getSecond().getMinute().getHour().getDayOfMonth());
  292. assertEquals(1, m.getSecond().getMinute().getHour().getHour());
  293. assertEquals(30, m.getSecond().getMinute().getMinute());
  294. assertEquals(55, m.getSecond().getSecond());
  295. assertEquals(556, m.getMillisecond());
  296. m = new Millisecond(999, 59, 59, 23, 31, 12, 9999);
  297. assertNull(m.next());
  298. }
  299. /**
  300. * Some checks for the getStart() method.
  301. */
  302. @Test
  303. public void testGetStart() {
  304. Locale saved = Locale.getDefault();
  305. Locale.setDefault(Locale.ITALY);
  306. Calendar cal = Calendar.getInstance(Locale.ITALY);
  307. cal.set(2006, Calendar.JANUARY, 16, 3, 47, 55);
  308. cal.set(Calendar.MILLISECOND, 555);
  309. Millisecond m = new Millisecond(555, 55, 47, 3, 16, 1, 2006);
  310. assertEquals(cal.getTime(), m.getStart());
  311. Locale.setDefault(saved);
  312. }
  313. /**
  314. * Some checks for the getEnd() method.
  315. */
  316. @Test
  317. public void testGetEnd() {
  318. Locale saved = Locale.getDefault();
  319. Locale.setDefault(Locale.ITALY);
  320. Calendar cal = Calendar.getInstance(Locale.ITALY);
  321. cal.set(2006, Calendar.JANUARY, 16, 3, 47, 55);
  322. cal.set(Calendar.MILLISECOND, 555);
  323. Millisecond m = new Millisecond(555, 55, 47, 3, 16, 1, 2006);
  324. assertEquals(cal.getTime(), m.getEnd());
  325. Locale.setDefault(saved);
  326. }
  327. }