1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------
  25. * YearTests.java
  26. * --------------
  27. * (C) Copyright 2001-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: YearTests.java,v 1.3 2005/01/11 18:11:22 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 16-Nov-2001 : Version 1 (DG);
  37. * 19-Mar-2002 : Added tests for constructor that uses java.util.Date to ensure it is
  38. * consistent with the getStart() and getEnd() methods (DG);
  39. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  40. * 13-Mar-2003 : Added serialization test (DG);
  41. * 11-Jan-2005 : Added test for non-clonability (DG);
  42. *
  43. */
  44. package org.jfree.data.time.junit;
  45. import java.io.ByteArrayInputStream;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.ObjectInput;
  48. import java.io.ObjectInputStream;
  49. import java.io.ObjectOutput;
  50. import java.io.ObjectOutputStream;
  51. import java.util.Date;
  52. import java.util.TimeZone;
  53. import junit.framework.Test;
  54. import junit.framework.TestCase;
  55. import junit.framework.TestSuite;
  56. import org.jfree.data.time.TimePeriodFormatException;
  57. import org.jfree.data.time.Year;
  58. /**
  59. * Tests for the {@link Year} class.
  60. */
  61. public class YearTests extends TestCase {
  62. /**
  63. * Returns the tests as a test suite.
  64. *
  65. * @return The test suite.
  66. */
  67. public static Test suite() {
  68. return new TestSuite(YearTests.class);
  69. }
  70. /**
  71. * Constructs a new set of tests.
  72. *
  73. * @param name the name of the tests.
  74. */
  75. public YearTests(String name) {
  76. super(name);
  77. }
  78. /**
  79. * Common test setup.
  80. */
  81. protected void setUp() {
  82. // no setup
  83. }
  84. /**
  85. * Check that a Year instance is equal to itself.
  86. *
  87. * SourceForge Bug ID: 558850.
  88. */
  89. public void testEqualsSelf() {
  90. Year year = new Year();
  91. assertTrue(year.equals(year));
  92. }
  93. /**
  94. * Tests the equals method.
  95. */
  96. public void testEquals() {
  97. Year year1 = new Year(2002);
  98. Year year2 = new Year(2002);
  99. assertTrue(year1.equals(year2));
  100. }
  101. /**
  102. * In GMT, the end of 2001 is java.util.Date(1009843199999L). Use this to check the
  103. * year constructor.
  104. */
  105. public void testDateConstructor1() {
  106. TimeZone zone = TimeZone.getTimeZone("GMT");
  107. Date d1 = new Date(1009843199999L);
  108. Date d2 = new Date(1009843200000L);
  109. Year y1 = new Year(d1, zone);
  110. Year y2 = new Year(d2, zone);
  111. assertEquals(2001, y1.getYear());
  112. assertEquals(1009843199999L, y1.getLastMillisecond(zone));
  113. assertEquals(2002, y2.getYear());
  114. assertEquals(1009843200000L, y2.getFirstMillisecond(zone));
  115. }
  116. /**
  117. * In Los Angeles, the end of 2001 is java.util.Date(1009871999999L). Use this to check the
  118. * year constructor.
  119. */
  120. public void testDateConstructor2() {
  121. TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
  122. Year y1 = new Year(new Date(1009871999999L), zone);
  123. Year y2 = new Year(new Date(1009872000000L), zone);
  124. assertEquals(2001, y1.getYear());
  125. assertEquals(1009871999999L, y1.getLastMillisecond(zone));
  126. assertEquals(2002, y2.getYear());
  127. assertEquals(1009872000000L, y2.getFirstMillisecond(zone));
  128. }
  129. /**
  130. * Set up a year equal to 1900. Request the previous year, it should be null.
  131. */
  132. public void test1900Previous() {
  133. Year current = new Year(1900);
  134. Year previous = (Year) current.previous();
  135. assertNull(previous);
  136. }
  137. /**
  138. * Set up a year equal to 1900. Request the next year, it should be 1901.
  139. */
  140. public void test1900Next() {
  141. Year current = new Year(1900);
  142. Year next = (Year) current.next();
  143. assertEquals(1901, next.getYear());
  144. }
  145. /**
  146. * Set up a year equal to 9999. Request the previous year, it should be 9998.
  147. */
  148. public void test9999Previous() {
  149. Year current = new Year(9999);
  150. Year previous = (Year) current.previous();
  151. assertEquals(9998, previous.getYear());
  152. }
  153. /**
  154. * Set up a year equal to 9999. Request the next year, it should be null.
  155. */
  156. public void test9999Next() {
  157. Year current = new Year(9999);
  158. Year next = (Year) current.next();
  159. assertNull(next);
  160. }
  161. /**
  162. * Tests the year string parser.
  163. */
  164. public void testParseYear() {
  165. Year year = null;
  166. // test 1...
  167. try {
  168. year = Year.parseYear("2000");
  169. }
  170. catch (TimePeriodFormatException e) {
  171. year = new Year(1900);
  172. }
  173. assertEquals(2000, year.getYear());
  174. // test 2...
  175. try {
  176. year = Year.parseYear(" 2001 ");
  177. }
  178. catch (TimePeriodFormatException e) {
  179. year = new Year(1900);
  180. }
  181. assertEquals(2001, year.getYear());
  182. // test 3...
  183. try {
  184. year = Year.parseYear("99");
  185. }
  186. catch (TimePeriodFormatException e) {
  187. year = new Year(1900);
  188. }
  189. assertEquals(1900, year.getYear());
  190. }
  191. /**
  192. * Serialize an instance, restore it, and check for equality.
  193. */
  194. public void testSerialization() {
  195. Year y1 = new Year(1999);
  196. Year y2 = null;
  197. try {
  198. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  199. ObjectOutput out = new ObjectOutputStream(buffer);
  200. out.writeObject(y1);
  201. out.close();
  202. ObjectInput in = new ObjectInputStream(
  203. new ByteArrayInputStream(buffer.toByteArray())
  204. );
  205. y2 = (Year) in.readObject();
  206. in.close();
  207. }
  208. catch (Exception e) {
  209. System.out.println(e.toString());
  210. }
  211. assertEquals(y1, y2);
  212. }
  213. /**
  214. * The {@link Year} class is immutable, so should not be {@link Cloneable}.
  215. */
  216. public void testNotCloneable() {
  217. Year y = new Year(1999);
  218. assertFalse(y instanceof Cloneable);
  219. }
  220. /**
  221. * Two objects that are equal are required to return the same hashCode.
  222. */
  223. public void testHashcode() {
  224. Year y1 = new Year(1988);
  225. Year y2 = new Year(1988);
  226. assertTrue(y1.equals(y2));
  227. int h1 = y1.hashCode();
  228. int h2 = y2.hashCode();
  229. assertEquals(h1, h2);
  230. }
  231. }