YearTest.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. * YearTest.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. * 16-Nov-2001 : Version 1 (DG);
  38. * 19-Mar-2002 : Added tests for constructor that uses java.util.Date to ensure
  39. * it is consistent with the getStart() and getEnd() methods (DG);
  40. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. * 13-Mar-2003 : Added serialization test (DG);
  42. * 11-Jan-2005 : Added test for non-clonability (DG);
  43. * 05-Oct-2006 : Added some new tests (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.junit.Test;
  59. /**
  60. * Tests for the {@link Year} class.
  61. */
  62. public class YearTest {
  63. /**
  64. * Check that a Year instance is equal to itself.
  65. *
  66. * SourceForge Bug ID: 558850.
  67. */
  68. @Test
  69. public void testEqualsSelf() {
  70. Year year = new Year();
  71. assertTrue(year.equals(year));
  72. }
  73. /**
  74. * Tests the equals method.
  75. */
  76. @Test
  77. public void testEquals() {
  78. Year year1 = new Year(2002);
  79. Year year2 = new Year(2002);
  80. assertTrue(year1.equals(year2));
  81. year1 = new Year(1999);
  82. assertFalse(year1.equals(year2));
  83. year2 = new Year(1999);
  84. assertTrue(year1.equals(year2));
  85. }
  86. /**
  87. * In GMT, the end of 2001 is java.util.Date(1009843199999L). Use this to
  88. * check the year constructor.
  89. */
  90. @Test
  91. public void testDateConstructor1() {
  92. TimeZone zone = TimeZone.getTimeZone("GMT");
  93. Date d1 = new Date(1009843199999L);
  94. Date d2 = new Date(1009843200000L);
  95. Year y1 = new Year(d1, zone);
  96. Year y2 = new Year(d2, zone);
  97. assertEquals(2001, y1.getYear());
  98. assertEquals(1009843199999L, y1.getLastMillisecond(zone));
  99. assertEquals(2002, y2.getYear());
  100. assertEquals(1009843200000L, y2.getFirstMillisecond(zone));
  101. }
  102. /**
  103. * In Los Angeles, the end of 2001 is java.util.Date(1009871999999L). Use
  104. * this to check the year constructor.
  105. */
  106. @Test
  107. public void testDateConstructor2() {
  108. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  109. Year y1 = new Year(new Date(1009871999999L), zone);
  110. Year y2 = new Year(new Date(1009872000000L), zone);
  111. assertEquals(2001, y1.getYear());
  112. assertEquals(1009871999999L, y1.getLastMillisecond(zone));
  113. assertEquals(2002, y2.getYear());
  114. assertEquals(1009872000000L, y2.getFirstMillisecond(zone));
  115. }
  116. /**
  117. * Set up a year equal to 1900. Request the previous year, it should be
  118. * null.
  119. */
  120. @Test
  121. public void testMinuss9999Previous() {
  122. Year current = new Year(-9999);
  123. Year previous = (Year) current.previous();
  124. assertNull(previous);
  125. }
  126. /**
  127. * Set up a year equal to 1900. Request the next year, it should be 1901.
  128. */
  129. @Test
  130. public void test1900Next() {
  131. Year current = new Year(1900);
  132. Year next = (Year) current.next();
  133. assertEquals(1901, next.getYear());
  134. }
  135. /**
  136. * Set up a year equal to 9999. Request the previous year, it should be
  137. * 9998.
  138. */
  139. @Test
  140. public void test9999Previous() {
  141. Year current = new Year(9999);
  142. Year previous = (Year) current.previous();
  143. assertEquals(9998, previous.getYear());
  144. }
  145. /**
  146. * Set up a year equal to 9999. Request the next year, it should be null.
  147. */
  148. @Test
  149. public void test9999Next() {
  150. Year current = new Year(9999);
  151. Year next = (Year) current.next();
  152. assertNull(next);
  153. }
  154. /**
  155. * Tests the year string parser.
  156. */
  157. @Test
  158. public void testParseYear() {
  159. Year year = null;
  160. // test 1...
  161. try {
  162. year = Year.parseYear("2000");
  163. }
  164. catch (TimePeriodFormatException e) {
  165. year = new Year(1900);
  166. }
  167. assertEquals(2000, year.getYear());
  168. // test 2...
  169. try {
  170. year = Year.parseYear(" 2001 ");
  171. }
  172. catch (TimePeriodFormatException e) {
  173. year = new Year(1900);
  174. }
  175. assertEquals(2001, year.getYear());
  176. // test 3...
  177. try {
  178. year = Year.parseYear("99");
  179. }
  180. catch (TimePeriodFormatException e) {
  181. year = new Year(1900);
  182. }
  183. assertEquals(99, year.getYear());
  184. }
  185. /**
  186. * Serialize an instance, restore it, and check for equality.
  187. */
  188. @Test
  189. public void testSerialization() {
  190. Year y1 = new Year(1999);
  191. Year y2 = (Year) TestUtilities.serialised(y1);
  192. assertEquals(y1, y2);
  193. }
  194. /**
  195. * The {@link Year} class is immutable, so should not be {@link Cloneable}.
  196. */
  197. @Test
  198. public void testNotCloneable() {
  199. Year y = new Year(1999);
  200. assertFalse(y instanceof Cloneable);
  201. }
  202. /**
  203. * Two objects that are equal are required to return the same hashCode.
  204. */
  205. @Test
  206. public void testHashcode() {
  207. Year y1 = new Year(1988);
  208. Year y2 = new Year(1988);
  209. assertTrue(y1.equals(y2));
  210. int h1 = y1.hashCode();
  211. int h2 = y2.hashCode();
  212. assertEquals(h1, h2);
  213. }
  214. /**
  215. * Some checks for the getFirstMillisecond() method.
  216. */
  217. @Test
  218. public void testGetFirstMillisecond() {
  219. Locale saved = Locale.getDefault();
  220. Locale.setDefault(Locale.UK);
  221. TimeZone savedZone = TimeZone.getDefault();
  222. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  223. Year y = new Year(1970);
  224. // TODO: Check this result...
  225. assertEquals(-3600000L, y.getFirstMillisecond());
  226. Locale.setDefault(saved);
  227. TimeZone.setDefault(savedZone);
  228. }
  229. /**
  230. * Some checks for the getFirstMillisecond(TimeZone) method.
  231. */
  232. @Test
  233. public void testGetFirstMillisecondWithTimeZone() {
  234. Year y = new Year(1950);
  235. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  236. assertEquals(-631123200000L, y.getFirstMillisecond(zone));
  237. // try null calendar
  238. boolean pass = false;
  239. try {
  240. y.getFirstMillisecond((TimeZone) null);
  241. }
  242. catch (NullPointerException e) {
  243. pass = true;
  244. }
  245. assertTrue(pass);
  246. }
  247. /**
  248. * Some checks for the getFirstMillisecond(TimeZone) method.
  249. */
  250. @Test
  251. public void testGetFirstMillisecondWithCalendar() {
  252. Year y = new Year(2001);
  253. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  254. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  255. assertEquals(978307200000L, y.getFirstMillisecond(calendar));
  256. // try null calendar
  257. boolean pass = false;
  258. try {
  259. y.getFirstMillisecond((Calendar) null);
  260. }
  261. catch (NullPointerException e) {
  262. pass = true;
  263. }
  264. assertTrue(pass);
  265. }
  266. /**
  267. * Some checks for the getLastMillisecond() method.
  268. */
  269. @Test
  270. public void testGetLastMillisecond() {
  271. Locale saved = Locale.getDefault();
  272. Locale.setDefault(Locale.UK);
  273. TimeZone savedZone = TimeZone.getDefault();
  274. TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
  275. Year y = new Year(1970);
  276. // TODO: Check this result...
  277. assertEquals(31532399999L, y.getLastMillisecond());
  278. Locale.setDefault(saved);
  279. TimeZone.setDefault(savedZone);
  280. }
  281. /**
  282. * Some checks for the getLastMillisecond(TimeZone) method.
  283. */
  284. @Test
  285. public void testGetLastMillisecondWithTimeZone() {
  286. Year y = new Year(1950);
  287. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  288. assertEquals(-599587200001L, y.getLastMillisecond(zone));
  289. // try null calendar
  290. boolean pass = false;
  291. try {
  292. y.getLastMillisecond((TimeZone) null);
  293. }
  294. catch (NullPointerException e) {
  295. pass = true;
  296. }
  297. assertTrue(pass);
  298. }
  299. /**
  300. * Some checks for the getLastMillisecond(TimeZone) method.
  301. */
  302. @Test
  303. public void testGetLastMillisecondWithCalendar() {
  304. Year y = new Year(2001);
  305. GregorianCalendar calendar = new GregorianCalendar(Locale.GERMANY);
  306. calendar.setTimeZone(TimeZone.getTimeZone("Europe/Frankfurt"));
  307. assertEquals(1009843199999L, y.getLastMillisecond(calendar));
  308. // try null calendar
  309. boolean pass = false;
  310. try {
  311. y.getLastMillisecond((Calendar) null);
  312. }
  313. catch (NullPointerException e) {
  314. pass = true;
  315. }
  316. assertTrue(pass);
  317. }
  318. /**
  319. * Some checks for the getSerialIndex() method.
  320. */
  321. @Test
  322. public void testGetSerialIndex() {
  323. Year y = new Year(2000);
  324. assertEquals(2000L, y.getSerialIndex());
  325. }
  326. /**
  327. * Some checks for the testNext() method.
  328. */
  329. @Test
  330. public void testNext() {
  331. Year y = new Year(2000);
  332. y = (Year) y.next();
  333. assertEquals(2001, y.getYear());
  334. y = new Year(9999);
  335. assertNull(y.next());
  336. }
  337. /**
  338. * Some checks for the getStart() method.
  339. */
  340. @Test
  341. public void testGetStart() {
  342. Locale saved = Locale.getDefault();
  343. Locale.setDefault(Locale.ITALY);
  344. Calendar cal = Calendar.getInstance(Locale.ITALY);
  345. cal.set(2006, Calendar.JANUARY, 1, 0, 0, 0);
  346. cal.set(Calendar.MILLISECOND, 0);
  347. Year y = new Year(2006);
  348. assertEquals(cal.getTime(), y.getStart());
  349. Locale.setDefault(saved);
  350. }
  351. /**
  352. * Some checks for the getEnd() method.
  353. */
  354. @Test
  355. public void testGetEnd() {
  356. Locale saved = Locale.getDefault();
  357. Locale.setDefault(Locale.ITALY);
  358. Calendar cal = Calendar.getInstance(Locale.ITALY);
  359. cal.set(2006, Calendar.DECEMBER, 31, 23, 59, 59);
  360. cal.set(Calendar.MILLISECOND, 999);
  361. Year y = new Year(2006);
  362. assertEquals(cal.getTime(), y.getEnd());
  363. Locale.setDefault(saved);
  364. }
  365. }