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. * MillisecondTests.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: MillisecondTests.java,v 1.5 2005/01/14 17:28:38 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 29-Jan-2002 : Version 1 (DG);
  37. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. * 21-Oct-2003 : Added hashCode tests (DG);
  39. * 29-Apr-2004 : Added test for getMiddleMillisecond() method (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.data.time.Millisecond;
  58. import org.jfree.data.time.Minute;
  59. import org.jfree.data.time.Second;
  60. import org.jfree.date.MonthConstants;
  61. /**
  62. * Tests for the {@link Millisecond} class.
  63. */
  64. public class MillisecondTests extends TestCase {
  65. /**
  66. * Returns the tests as a test suite.
  67. *
  68. * @return The test suite.
  69. */
  70. public static Test suite() {
  71. return new TestSuite(MillisecondTests.class);
  72. }
  73. /**
  74. * Constructs a new set of tests.
  75. *
  76. * @param name the name of the tests.
  77. */
  78. public MillisecondTests(String name) {
  79. super(name);
  80. }
  81. /**
  82. * Common test setup.
  83. */
  84. protected void setUp() {
  85. // no setup
  86. }
  87. /**
  88. * Check that a {@link Millisecond} instance is equal to itself.
  89. *
  90. * SourceForge Bug ID: 558850.
  91. */
  92. public void testEqualsSelf() {
  93. Millisecond millisecond = new Millisecond();
  94. assertTrue(millisecond.equals(millisecond));
  95. }
  96. /**
  97. * Tests the equals method.
  98. */
  99. public void testEquals() {
  100. Day day1 = new Day(29, MonthConstants.MARCH, 2002);
  101. Hour hour1 = new Hour(15, day1);
  102. Minute minute1 = new Minute(15, hour1);
  103. Second second1 = new Second(34, minute1);
  104. Millisecond milli1 = new Millisecond(999, second1);
  105. Day day2 = new Day(29, MonthConstants.MARCH, 2002);
  106. Hour hour2 = new Hour(15, day2);
  107. Minute minute2 = new Minute(15, hour2);
  108. Second second2 = new Second(34, minute2);
  109. Millisecond milli2 = new Millisecond(999, second2);
  110. assertTrue(milli1.equals(milli2));
  111. }
  112. /**
  113. * In GMT, the 4.55:59.123pm on 21 Mar 2002 is java.util.Date(1016729759123L).
  114. * Use this to check the Second constructor.
  115. */
  116. public void testDateConstructor1() {
  117. TimeZone zone = TimeZone.getTimeZone("GMT");
  118. Millisecond m1 = new Millisecond(new Date(1016729759122L), zone);
  119. Millisecond m2 = new Millisecond(new Date(1016729759123L), zone);
  120. assertEquals(122, m1.getMillisecond());
  121. assertEquals(1016729759122L, m1.getLastMillisecond(zone));
  122. assertEquals(123, m2.getMillisecond());
  123. assertEquals(1016729759123L, m2.getFirstMillisecond(zone));
  124. }
  125. /**
  126. * In Tallinn, the 4.55:59.123pm on 21 Mar 2002 is java.util.Date(1016722559123L).
  127. * Use this to check the Second constructor.
  128. */
  129. public void testDateConstructor2() {
  130. TimeZone zone = TimeZone.getTimeZone("Europe/Tallinn");
  131. Millisecond m1 = new Millisecond(new Date(1016722559122L), zone);
  132. Millisecond m2 = new Millisecond(new Date(1016722559123L), zone);
  133. assertEquals(122, m1.getMillisecond());
  134. assertEquals(1016722559122L, m1.getLastMillisecond(zone));
  135. assertEquals(123, m2.getMillisecond());
  136. assertEquals(1016722559123L, m2.getFirstMillisecond(zone));
  137. }
  138. /**
  139. * Serialize an instance, restore it, and check for equality.
  140. */
  141. public void testSerialization() {
  142. Millisecond m1 = new Millisecond();
  143. Millisecond m2 = null;
  144. try {
  145. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  146. ObjectOutput out = new ObjectOutputStream(buffer);
  147. out.writeObject(m1);
  148. out.close();
  149. ObjectInput in = new ObjectInputStream(
  150. new ByteArrayInputStream(buffer.toByteArray())
  151. );
  152. m2 = (Millisecond) in.readObject();
  153. in.close();
  154. }
  155. catch (Exception e) {
  156. System.out.println(e.toString());
  157. }
  158. assertEquals(m1, m2);
  159. }
  160. /**
  161. * Two objects that are equal are required to return the same hashCode.
  162. */
  163. public void testHashcode() {
  164. Millisecond m1 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
  165. Millisecond m2 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
  166. assertTrue(m1.equals(m2));
  167. int hash1 = m1.hashCode();
  168. int hash2 = m2.hashCode();
  169. assertEquals(hash1, hash2);
  170. }
  171. /**
  172. * A test for bug report 943985 - the calculation for the middle millisecond is
  173. * incorrect for odd milliseconds.
  174. */
  175. public void test943985() {
  176. Millisecond ms = new Millisecond(new java.util.Date(4));
  177. assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond());
  178. assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
  179. ms = new Millisecond(new java.util.Date(5));
  180. assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond());
  181. assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
  182. }
  183. /**
  184. * The {@link Millisecond} class is immutable, so should not be {@link Cloneable}.
  185. */
  186. public void testNotCloneable() {
  187. Millisecond m = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
  188. assertFalse(m instanceof Cloneable);
  189. }
  190. }