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. * DayTests.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: DayTests.java,v 1.5 2005/01/14 17:28:37 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 15-Nov-2001 : Version 1 (DG);
  37. * 20-Mar-2002 : Added new tests for Day constructor and getStart() and getEnd() in different
  38. * time zones (DG);
  39. * 26-Jun-2002 : Removed unnecessary imports (DG);
  40. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. * 13-Mar-2003 : Added serialization test (DG);
  42. * 21-Oct-2003 : Added hashCode test (DG);
  43. * 11-Jan-2005 : Added test for non-clonability (DG);
  44. *
  45. */
  46. package org.jfree.data.time.junit;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.ObjectInput;
  50. import java.io.ObjectInputStream;
  51. import java.io.ObjectOutput;
  52. import java.io.ObjectOutputStream;
  53. import java.text.ParseException;
  54. import java.text.SimpleDateFormat;
  55. import java.util.Date;
  56. import java.util.GregorianCalendar;
  57. import java.util.TimeZone;
  58. import junit.framework.Test;
  59. import junit.framework.TestCase;
  60. import junit.framework.TestSuite;
  61. import org.jfree.data.time.Day;
  62. import org.jfree.date.MonthConstants;
  63. /**
  64. * Tests for the {@link Day} class.
  65. */
  66. public class DayTests extends TestCase {
  67. /**
  68. * Returns the tests as a test suite.
  69. *
  70. * @return The test suite.
  71. */
  72. public static Test suite() {
  73. return new TestSuite(DayTests.class);
  74. }
  75. /**
  76. * Constructs a new set of tests.
  77. *
  78. * @param name the name of the tests.
  79. */
  80. public DayTests(String name) {
  81. super(name);
  82. }
  83. /**
  84. * Common test setup.
  85. */
  86. protected void setUp() {
  87. // no setup required
  88. }
  89. /**
  90. * Check that a Day instance is equal to itself.
  91. *
  92. * SourceForge Bug ID: 558850.
  93. */
  94. public void testEqualsSelf() {
  95. Day day = new Day();
  96. assertTrue(day.equals(day));
  97. }
  98. /**
  99. * Tests the equals method.
  100. */
  101. public void testEquals() {
  102. Day day1 = new Day(29, MonthConstants.MARCH, 2002);
  103. Day day2 = new Day(29, MonthConstants.MARCH, 2002);
  104. assertTrue(day1.equals(day2));
  105. }
  106. /**
  107. * In GMT, the end of 29 Feb 2004 is java.util.Date(1,078,099,199,999L). Use this to check the
  108. * day constructor.
  109. */
  110. public void testDateConstructor1() {
  111. TimeZone zone = TimeZone.getTimeZone("GMT");
  112. Day d1 = new Day(new Date(1078099199999L), zone);
  113. Day d2 = new Day(new Date(1078099200000L), zone);
  114. assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
  115. assertEquals(1078099199999L, d1.getLastMillisecond(zone));
  116. assertEquals(MonthConstants.MARCH, d2.getMonth());
  117. assertEquals(1078099200000L, d2.getFirstMillisecond(zone));
  118. }
  119. /**
  120. * In Helsinki, the end of 29 Feb 2004 is java.util.Date(1,078,091,999,999L). Use this to
  121. * check the Day constructor.
  122. */
  123. public void testDateConstructor2() {
  124. TimeZone zone = TimeZone.getTimeZone("Europe/Helsinki");
  125. Day d1 = new Day(new Date(1078091999999L), zone);
  126. Day d2 = new Day(new Date(1078092000000L), zone);
  127. assertEquals(MonthConstants.FEBRUARY, d1.getMonth());
  128. assertEquals(1078091999999L, d1.getLastMillisecond(zone));
  129. assertEquals(MonthConstants.MARCH, d2.getMonth());
  130. assertEquals(1078092000000L, d2.getFirstMillisecond(zone));
  131. }
  132. /**
  133. * Set up a day equal to 1 January 1900. Request the previous day, it should be null.
  134. */
  135. public void test1Jan1900Previous() {
  136. Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
  137. Day previous = (Day) jan1st1900.previous();
  138. assertNull(previous);
  139. }
  140. /**
  141. * Set up a day equal to 1 January 1900. Request the next day, it should be 2 January 1900.
  142. */
  143. public void test1Jan1900Next() {
  144. Day jan1st1900 = new Day(1, MonthConstants.JANUARY, 1900);
  145. Day next = (Day) jan1st1900.next();
  146. assertEquals(2, next.getDayOfMonth());
  147. }
  148. /**
  149. * Set up a day equal to 31 December 9999. Request the previous day, it should be 30 December
  150. * 9999.
  151. */
  152. public void test31Dec9999Previous() {
  153. Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
  154. Day previous = (Day) dec31st9999.previous();
  155. assertEquals(30, previous.getDayOfMonth());
  156. }
  157. /**
  158. * Set up a day equal to 31 December 9999. Request the next day, it should be null.
  159. */
  160. public void test31Dec9999Next() {
  161. Day dec31st9999 = new Day(31, MonthConstants.DECEMBER, 9999);
  162. Day next = (Day) dec31st9999.next();
  163. assertNull(next);
  164. }
  165. /**
  166. * Problem for date parsing.
  167. * <p>
  168. * This test works only correct if the short pattern of the date
  169. * format is "dd/MM/yyyy". If not, this test will result in a
  170. * false negative.
  171. *
  172. * @throws ParseException on parsing errors.
  173. */
  174. public void testParseDay() throws ParseException {
  175. GregorianCalendar gc = new GregorianCalendar(2001, 12, 31);
  176. SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
  177. Date reference = format.parse("31/12/2001");
  178. if (reference.equals(gc.getTime())) {
  179. // test 1...
  180. Day d = Day.parseDay("31/12/2001");
  181. assertEquals(37256, d.getSerialDate().toSerial());
  182. }
  183. // test 2...
  184. Day d = Day.parseDay("2001-12-31");
  185. assertEquals(37256, d.getSerialDate().toSerial());
  186. }
  187. /**
  188. * Serialize an instance, restore it, and check for equality.
  189. */
  190. public void testSerialization() {
  191. Day d1 = new Day(15, 4, 2000);
  192. Day d2 = null;
  193. try {
  194. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  195. ObjectOutput out = new ObjectOutputStream(buffer);
  196. out.writeObject(d1);
  197. out.close();
  198. ObjectInput in = new ObjectInputStream(
  199. new ByteArrayInputStream(buffer.toByteArray())
  200. );
  201. d2 = (Day) in.readObject();
  202. in.close();
  203. }
  204. catch (Exception e) {
  205. System.out.println(e.toString());
  206. }
  207. assertEquals(d1, d2);
  208. }
  209. /**
  210. * Two objects that are equal are required to return the same hashCode.
  211. */
  212. public void testHashcode() {
  213. Day d1 = new Day(1, 2, 2003);
  214. Day d2 = new Day(1, 2, 2003);
  215. assertTrue(d1.equals(d2));
  216. int h1 = d1.hashCode();
  217. int h2 = d2.hashCode();
  218. assertEquals(h1, h2);
  219. }
  220. /**
  221. * The {@link Day} class is immutable, so should not be {@link Cloneable}.
  222. */
  223. public void testNotCloneable() {
  224. Day d = new Day(1, 2, 2003);
  225. assertFalse(d instanceof Cloneable);
  226. }
  227. }