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. * MinuteTests.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: MinuteTests.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.data.time.Minute;
  58. import org.jfree.date.MonthConstants;
  59. /**
  60. * Tests for the {@link Minute} class.
  61. */
  62. public class MinuteTests 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(MinuteTests.class);
  70. }
  71. /**
  72. * Constructs a new set of tests.
  73. *
  74. * @param name the name of the tests.
  75. */
  76. public MinuteTests(String name) {
  77. super(name);
  78. }
  79. /**
  80. * Common test setup.
  81. */
  82. protected void setUp() {
  83. // no setup
  84. }
  85. /**
  86. * Check that a Minute instance is equal to itself.
  87. *
  88. * SourceForge Bug ID: 558850.
  89. */
  90. public void testEqualsSelf() {
  91. Minute minute = new Minute();
  92. assertTrue(minute.equals(minute));
  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. Day day2 = new Day(29, MonthConstants.MARCH, 2002);
  102. Hour hour2 = new Hour(15, day2);
  103. Minute minute2 = new Minute(15, hour2);
  104. assertTrue(minute1.equals(minute2));
  105. }
  106. /**
  107. * In GMT, the 4.55pm on 21 Mar 2002 is java.util.Date(1016729700000L).
  108. * Use this to check the Minute constructor.
  109. */
  110. public void testDateConstructor1() {
  111. TimeZone zone = TimeZone.getTimeZone("GMT");
  112. Minute m1 = new Minute(new Date(1016729699999L), zone);
  113. Minute m2 = new Minute(new Date(1016729700000L), zone);
  114. assertEquals(54, m1.getMinute());
  115. assertEquals(1016729699999L, m1.getLastMillisecond(zone));
  116. assertEquals(55, m2.getMinute());
  117. assertEquals(1016729700000L, m2.getFirstMillisecond(zone));
  118. }
  119. /**
  120. * In Singapore, the 4.55pm on 21 Mar 2002 is java.util.Date(1,014,281,700,000L).
  121. * Use this to check the Minute constructor.
  122. */
  123. public void testDateConstructor2() {
  124. TimeZone zone = TimeZone.getTimeZone("Asia/Singapore");
  125. Minute m1 = new Minute(new Date(1016700899999L), zone);
  126. Minute m2 = new Minute(new Date(1016700900000L), zone);
  127. assertEquals(54, m1.getMinute());
  128. assertEquals(1016700899999L, m1.getLastMillisecond(zone));
  129. assertEquals(55, m2.getMinute());
  130. assertEquals(1016700900000L, m2.getFirstMillisecond(zone));
  131. }
  132. /**
  133. * Serialize an instance, restore it, and check for equality.
  134. */
  135. public void testSerialization() {
  136. Minute m1 = new Minute();
  137. Minute m2 = null;
  138. try {
  139. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  140. ObjectOutput out = new ObjectOutputStream(buffer);
  141. out.writeObject(m1);
  142. out.close();
  143. ObjectInput in = new ObjectInputStream(
  144. new ByteArrayInputStream(buffer.toByteArray())
  145. );
  146. m2 = (Minute) in.readObject();
  147. in.close();
  148. }
  149. catch (Exception e) {
  150. System.out.println(e.toString());
  151. }
  152. assertEquals(m1, m2);
  153. }
  154. /**
  155. * Two objects that are equal are required to return the same hashCode.
  156. */
  157. public void testHashcode() {
  158. Minute m1 = new Minute(45, 5, 1, 2, 2003);
  159. Minute m2 = new Minute(45, 5, 1, 2, 2003);
  160. assertTrue(m1.equals(m2));
  161. int h1 = m1.hashCode();
  162. int h2 = m2.hashCode();
  163. assertEquals(h1, h2);
  164. }
  165. /**
  166. * The {@link Minute} class is immutable, so should not be {@link Cloneable}.
  167. */
  168. public void testNotCloneable() {
  169. Minute m = new Minute(45, 5, 1, 2, 2003);
  170. assertFalse(m instanceof Cloneable);
  171. }
  172. }