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. * SecondTests.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: SecondTests.java,v 1.3 2005/01/11 16:00:35 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-Oct-2003 : Added serialization test (DG);
  39. * 11-Jan-2005 : Added test for non-clonability (DG);
  40. *
  41. */
  42. package org.jfree.data.time.junit;
  43. import java.io.ByteArrayInputStream;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.ObjectInput;
  46. import java.io.ObjectInputStream;
  47. import java.io.ObjectOutput;
  48. import java.io.ObjectOutputStream;
  49. import java.util.Date;
  50. import java.util.TimeZone;
  51. import junit.framework.Test;
  52. import junit.framework.TestCase;
  53. import junit.framework.TestSuite;
  54. import org.jfree.data.time.Day;
  55. import org.jfree.data.time.Hour;
  56. import org.jfree.data.time.Minute;
  57. import org.jfree.data.time.Second;
  58. import org.jfree.date.MonthConstants;
  59. /**
  60. * Tests for the {@link Second} class.
  61. */
  62. public class SecondTests extends TestCase {
  63. /**
  64. * Returns the tests as a test suite.
  65. *
  66. * @return The test suite.
  67. */
  68. public static Test suite() {
  69. return new TestSuite(SecondTests.class);
  70. }
  71. /**
  72. * Constructs a new set of tests.
  73. *
  74. * @param name the name of the tests.
  75. */
  76. public SecondTests(String name) {
  77. super(name);
  78. }
  79. /**
  80. * Common test setup.
  81. */
  82. protected void setUp() {
  83. // no setup
  84. }
  85. /**
  86. * Test that a Second instance is equal to itself.
  87. *
  88. * SourceForge Bug ID: 558850.
  89. */
  90. public void testEqualsSelf() {
  91. Second second = new Second();
  92. assertTrue(second.equals(second));
  93. }
  94. /**
  95. * Tests the equals method.
  96. */
  97. public void testEquals() {
  98. Day day1 = new Day(29, MonthConstants.MARCH, 2002);
  99. Hour hour1 = new Hour(15, day1);
  100. Minute minute1 = new Minute(15, hour1);
  101. Second second1 = new Second(34, minute1);
  102. Day day2 = new Day(29, MonthConstants.MARCH, 2002);
  103. Hour hour2 = new Hour(15, day2);
  104. Minute minute2 = new Minute(15, hour2);
  105. Second second2 = new Second(34, minute2);
  106. assertTrue(second1.equals(second2));
  107. }
  108. /**
  109. * In GMT, the 4.55:59pm on 21 Mar 2002 is java.util.Date(1016729759000L).
  110. * Use this to check the Second constructor.
  111. */
  112. public void testDateConstructor1() {
  113. TimeZone zone = TimeZone.getTimeZone("GMT");
  114. Second s1 = new Second(new Date(1016729758999L), zone);
  115. Second s2 = new Second(new Date(1016729759000L), zone);
  116. assertEquals(58, s1.getSecond());
  117. assertEquals(1016729758999L, s1.getLastMillisecond(zone));
  118. assertEquals(59, s2.getSecond());
  119. assertEquals(1016729759000L, s2.getFirstMillisecond(zone));
  120. }
  121. /**
  122. * In Chicago, the 4.55:59pm on 21 Mar 2002 is java.util.Date(1016751359000L).
  123. * Use this to check the Second constructor.
  124. */
  125. public void testDateConstructor2() {
  126. TimeZone zone = TimeZone.getTimeZone("America/Chicago");
  127. Second s1 = new Second(new Date(1016751358999L), zone);
  128. Second s2 = new Second(new Date(1016751359000L), zone);
  129. assertEquals(58, s1.getSecond());
  130. assertEquals(1016751358999L, s1.getLastMillisecond(zone));
  131. assertEquals(59, s2.getSecond());
  132. assertEquals(1016751359000L, s2.getFirstMillisecond(zone));
  133. }
  134. /**
  135. * Serialize an instance, restore it, and check for equality.
  136. */
  137. public void testSerialization() {
  138. Second s1 = new Second();
  139. Second s2 = null;
  140. try {
  141. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  142. ObjectOutput out = new ObjectOutputStream(buffer);
  143. out.writeObject(s1);
  144. out.close();
  145. ObjectInput in = new ObjectInputStream(
  146. new ByteArrayInputStream(buffer.toByteArray())
  147. );
  148. s2 = (Second) in.readObject();
  149. in.close();
  150. }
  151. catch (Exception e) {
  152. System.out.println(e.toString());
  153. }
  154. assertEquals(s1, s2);
  155. }
  156. /**
  157. * Two objects that are equal are required to return the same hashCode.
  158. */
  159. public void testHashcode() {
  160. Second s1 = new Second(13, 45, 5, 1, 2, 2003);
  161. Second s2 = new Second(13, 45, 5, 1, 2, 2003);
  162. assertTrue(s1.equals(s2));
  163. int h1 = s1.hashCode();
  164. int h2 = s2.hashCode();
  165. assertEquals(h1, h2);
  166. }
  167. /**
  168. * The {@link Second} class is immutable, so should not be {@link Cloneable}.
  169. */
  170. public void testNotCloneable() {
  171. Second s = new Second(13, 45, 5, 1, 2, 2003);
  172. assertFalse(s instanceof Cloneable);
  173. }
  174. }