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. * HourTests.java
  26. * --------------
  27. * (C) Copyright 2002-2005 by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: HourTests.java,v 1.4 2005/01/11 18:11:22 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 29-Jan-2002 : Version 1 (DG);
  37. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. * 13-Mar-2003 : Added serialization test (DG);
  39. * 21-Oct-2003 : Added hashCode test (DG);
  40. * 11-Jan-2005 : Added test for non-clonability (DG);
  41. *
  42. */
  43. package org.jfree.data.time.junit;
  44. import java.io.ByteArrayInputStream;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.ObjectInput;
  47. import java.io.ObjectInputStream;
  48. import java.io.ObjectOutput;
  49. import java.io.ObjectOutputStream;
  50. import java.util.Date;
  51. import java.util.TimeZone;
  52. import junit.framework.Test;
  53. import junit.framework.TestCase;
  54. import junit.framework.TestSuite;
  55. import org.jfree.data.time.Day;
  56. import org.jfree.data.time.Hour;
  57. import org.jfree.date.MonthConstants;
  58. /**
  59. * Tests for the {@link Hour} class.
  60. */
  61. public class HourTests 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(HourTests.class);
  69. }
  70. /**
  71. * Constructs a new set of tests.
  72. *
  73. * @param name the name of the tests.
  74. */
  75. public HourTests(String name) {
  76. super(name);
  77. }
  78. /**
  79. * Common test setup.
  80. */
  81. protected void setUp() {
  82. // no setup
  83. }
  84. /**
  85. * Check that an Hour instance is equal to itself.
  86. *
  87. * SourceForge Bug ID: 558850.
  88. */
  89. public void testEqualsSelf() {
  90. Hour hour = new Hour();
  91. assertTrue(hour.equals(hour));
  92. }
  93. /**
  94. * Tests the equals method.
  95. */
  96. public void testEquals() {
  97. Hour hour1 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002));
  98. Hour hour2 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002));
  99. assertTrue(hour1.equals(hour2));
  100. }
  101. /**
  102. * In GMT, the 4pm on 21 Mar 2002 is java.util.Date(1,014,307,200,000L). Use this to check the
  103. * hour constructor.
  104. */
  105. public void testDateConstructor1() {
  106. TimeZone zone = TimeZone.getTimeZone("GMT");
  107. Hour h1 = new Hour(new Date(1014307199999L), zone);
  108. Hour h2 = new Hour(new Date(1014307200000L), zone);
  109. assertEquals(15, h1.getHour());
  110. assertEquals(1014307199999L, h1.getLastMillisecond(zone));
  111. assertEquals(16, h2.getHour());
  112. assertEquals(1014307200000L, h2.getFirstMillisecond(zone));
  113. }
  114. /**
  115. * In Sydney, the 4pm on 21 Mar 2002 is java.util.Date(1,014,267,600,000L).
  116. * Use this to check the hour constructor.
  117. */
  118. public void testDateConstructor2() {
  119. TimeZone zone = TimeZone.getTimeZone("Australia/Sydney");
  120. Hour h1 = new Hour(new Date(1014267599999L), zone);
  121. Hour h2 = new Hour (new Date(1014267600000L), zone);
  122. assertEquals(15, h1.getHour());
  123. assertEquals(1014267599999L, h1.getLastMillisecond(zone));
  124. assertEquals(16, h2.getHour());
  125. assertEquals(1014267600000L, h2.getFirstMillisecond(zone));
  126. }
  127. /**
  128. * Set up an hour equal to hour zero, 1 January 1900. Request the previous hour, it should be
  129. * null.
  130. */
  131. public void testFirstHourPrevious() {
  132. Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900));
  133. Hour previous = (Hour) first.previous();
  134. assertNull(previous);
  135. }
  136. /**
  137. * Set up an hour equal to hour zero, 1 January 1900. Request the next hour, it should be
  138. * null.
  139. */
  140. public void testFirstHourNext() {
  141. Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900));
  142. Hour next = (Hour) first.next();
  143. assertEquals(1, next.getHour());
  144. assertEquals(1900, next.getYear());
  145. }
  146. /**
  147. * Set up an hour equal to hour zero, 1 January 1900. Request the previous hour, it should be
  148. * null.
  149. */
  150. public void testLastHourPrevious() {
  151. Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999));
  152. Hour previous = (Hour) last.previous();
  153. assertEquals(22, previous.getHour());
  154. assertEquals(9999, previous.getYear());
  155. }
  156. /**
  157. * Set up an hour equal to hour zero, 1 January 1900. Request the next hour, it should be
  158. * null.
  159. */
  160. public void testLastHourNext() {
  161. Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999));
  162. Hour next = (Hour) last.next();
  163. assertNull(next);
  164. }
  165. /**
  166. * Problem for date parsing.
  167. */
  168. public void testParseHour() {
  169. // test 1...
  170. Hour h = Hour.parseHour("2002-01-29 13");
  171. assertEquals(13, h.getHour());
  172. }
  173. /**
  174. * Serialize an instance, restore it, and check for equality.
  175. */
  176. public void testSerialization() {
  177. Hour h1 = new Hour();
  178. Hour h2 = null;
  179. try {
  180. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  181. ObjectOutput out = new ObjectOutputStream(buffer);
  182. out.writeObject(h1);
  183. out.close();
  184. ObjectInput in = new ObjectInputStream(
  185. new ByteArrayInputStream(buffer.toByteArray())
  186. );
  187. h2 = (Hour) in.readObject();
  188. in.close();
  189. }
  190. catch (Exception e) {
  191. System.out.println(e.toString());
  192. }
  193. assertEquals(h1, h2);
  194. }
  195. /**
  196. * Two objects that are equal are required to return the same hashCode.
  197. */
  198. public void testHashcode() {
  199. Hour h1 = new Hour(7, 9, 10, 1999);
  200. Hour h2 = new Hour(7, 9, 10, 1999);
  201. assertTrue(h1.equals(h2));
  202. int hash1 = h1.hashCode();
  203. int hash2 = h2.hashCode();
  204. assertEquals(hash1, hash2);
  205. }
  206. /**
  207. * The {@link Hour} class is immutable, so should not be {@link Cloneable}.
  208. */
  209. public void testNotCloneable() {
  210. Hour h = new Hour(7, 9, 10, 1999);
  211. assertFalse(h instanceof Cloneable);
  212. }
  213. }