SimpleTimePeriodTest.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -------------------------
  28. * SimpleTimePeriodTest.java
  29. * -------------------------
  30. * (C) Copyright 2003-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 13-Mar-2003 : Version 1 (DG);
  38. * 21-Oct-2003 : Added hashCode() test (DG);
  39. * 02-Jun-2008 : Added a test for immutability (DG);
  40. *
  41. */
  42. package org.jfree.data.time;
  43. import static org.junit.Assert.assertTrue;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertEquals;
  46. import java.util.Date;
  47. import org.jfree.chart.TestUtilities;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link SimpleTimePeriod} class.
  51. */
  52. public class SimpleTimePeriodTest {
  53. /**
  54. * Check that an instance is equal to itself.
  55. *
  56. * SourceForge Bug ID: 558850.
  57. */
  58. @Test
  59. public void testEqualsSelf() {
  60. SimpleTimePeriod p = new SimpleTimePeriod(new Date(1000L),
  61. new Date(1001L));
  62. assertTrue(p.equals(p));
  63. }
  64. /**
  65. * Test the equals() method.
  66. */
  67. @Test
  68. public void testEquals() {
  69. SimpleTimePeriod p1 = new SimpleTimePeriod(new Date(1000L),
  70. new Date(1004L));
  71. SimpleTimePeriod p2 = new SimpleTimePeriod(new Date(1000L),
  72. new Date(1004L));
  73. assertTrue(p1.equals(p2));
  74. assertTrue(p2.equals(p1));
  75. p1 = new SimpleTimePeriod(new Date(1002L), new Date(1004L));
  76. assertFalse(p1.equals(p2));
  77. p2 = new SimpleTimePeriod(new Date(1002L), new Date(1004L));
  78. assertTrue(p1.equals(p2));
  79. p1 = new SimpleTimePeriod(new Date(1002L), new Date(1003L));
  80. assertFalse(p1.equals(p2));
  81. p2 = new SimpleTimePeriod(new Date(1002L), new Date(1003L));
  82. assertTrue(p1.equals(p2));
  83. }
  84. /**
  85. * Serialize an instance, restore it, and check for equality.
  86. */
  87. @Test
  88. public void testSerialization() {
  89. SimpleTimePeriod p1 = new SimpleTimePeriod(new Date(1000L),
  90. new Date(1001L));
  91. SimpleTimePeriod p2 = (SimpleTimePeriod) TestUtilities.serialised(p1);
  92. assertEquals(p1, p2);
  93. }
  94. /**
  95. * Two objects that are equal are required to return the same hashCode.
  96. */
  97. @Test
  98. public void testHashcode() {
  99. SimpleTimePeriod s1 = new SimpleTimePeriod(new Date(10L),
  100. new Date(20L));
  101. SimpleTimePeriod s2 = new SimpleTimePeriod(new Date(10L),
  102. new Date(20L));
  103. assertTrue(s1.equals(s2));
  104. int h1 = s1.hashCode();
  105. int h2 = s2.hashCode();
  106. assertEquals(h1, h2);
  107. }
  108. /**
  109. * This class is immutable, so it should not implement Cloneable.
  110. */
  111. @Test
  112. public void testClone() {
  113. SimpleTimePeriod s1 = new SimpleTimePeriod(new Date(10L),
  114. new Date(20));
  115. assertFalse(s1 instanceof Cloneable);
  116. }
  117. /**
  118. * Some simple checks for immutability.
  119. */
  120. @Test
  121. public void testImmutable() {
  122. SimpleTimePeriod p1 = new SimpleTimePeriod(new Date(10L),
  123. new Date(20L));
  124. SimpleTimePeriod p2 = new SimpleTimePeriod(new Date(10L),
  125. new Date(20L));
  126. assertEquals(p1, p2);
  127. p1.getStart().setTime(11L);
  128. assertEquals(p1, p2);
  129. Date d1 = new Date(10L);
  130. Date d2 = new Date(20L);
  131. p1 = new SimpleTimePeriod(d1, d2);
  132. d1.setTime(11L);
  133. assertEquals(new Date(10L), p1.getStart());
  134. }
  135. /**
  136. * Some checks for the compareTo() method.
  137. */
  138. @Test
  139. public void testCompareTo() {
  140. SimpleTimePeriod s1 = new SimpleTimePeriod(new Date(10L),
  141. new Date(20L));
  142. SimpleTimePeriod s2 = new SimpleTimePeriod(new Date(10L),
  143. new Date(20L));
  144. assertEquals(0, s1.compareTo(s2));
  145. s1 = new SimpleTimePeriod(new Date(9L), new Date(21L));
  146. s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
  147. assertEquals(-1, s1.compareTo(s2));
  148. s1 = new SimpleTimePeriod(new Date(11L), new Date(19L));
  149. s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
  150. assertEquals(1, s1.compareTo(s2));
  151. s1 = new SimpleTimePeriod(new Date(9L), new Date(19L));
  152. s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
  153. assertEquals(-1, s1.compareTo(s2));
  154. s1 = new SimpleTimePeriod(new Date(11L), new Date(21));
  155. s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
  156. assertEquals(1, s1.compareTo(s2));
  157. s1 = new SimpleTimePeriod(new Date(10L), new Date(18));
  158. s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
  159. assertEquals(-1, s1.compareTo(s2));
  160. s1 = new SimpleTimePeriod(new Date(10L), new Date(22));
  161. s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
  162. assertEquals(1, s1.compareTo(s2));
  163. }
  164. }