SecondTest.java 11 KB

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