ComparableObjectSeriesTest.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * ComparableObjectSeriesTest.java
  29. * -------------------------------
  30. * (C) Copyright 2006-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 20-Oct-2006 : Version 1 (DG);
  38. * 31-Oct-2007 : New hashCode() test (DG);
  39. *
  40. */
  41. package org.jfree.data;
  42. import org.jfree.chart.TestUtilities;
  43. import org.junit.Test;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotEquals;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.assertNull;
  48. /**
  49. * Tests for the {@link ComparableObjectSeries} class.
  50. */
  51. public class ComparableObjectSeriesTest {
  52. static class MyComparableObjectSeries extends ComparableObjectSeries {
  53. /**
  54. * Creates a new instance.
  55. *
  56. * @param key the series key.
  57. */
  58. public MyComparableObjectSeries(Comparable key) {
  59. super(key);
  60. }
  61. /**
  62. * Creates a new instance.
  63. *
  64. * @param key the series key.
  65. * @param autoSort automatically sort by x-value?
  66. * @param allowDuplicateXValues allow duplicate values?
  67. */
  68. public MyComparableObjectSeries(Comparable key, boolean autoSort,
  69. boolean allowDuplicateXValues) {
  70. super(key, autoSort, allowDuplicateXValues);
  71. }
  72. @Override
  73. public void add(Comparable x, Object y) {
  74. super.add(x, y);
  75. }
  76. @Override
  77. public ComparableObjectItem remove(Comparable x) {
  78. return super.remove(x);
  79. }
  80. }
  81. /**
  82. * Some checks for the constructor.
  83. */
  84. @Test
  85. public void testConstructor1() {
  86. ComparableObjectSeries s1 = new ComparableObjectSeries("s1");
  87. assertEquals("s1", s1.getKey());
  88. assertNull(s1.getDescription());
  89. assertTrue(s1.getAllowDuplicateXValues());
  90. assertTrue(s1.getAutoSort());
  91. assertEquals(0, s1.getItemCount());
  92. assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
  93. // try null key
  94. boolean pass = false;
  95. try {
  96. /*s1 = */new ComparableObjectSeries(null);
  97. }
  98. catch (IllegalArgumentException e) {
  99. pass = true;
  100. }
  101. assertTrue(pass);
  102. }
  103. /**
  104. * Confirm that the equals method can distinguish all the required fields.
  105. */
  106. @Test
  107. public void testEquals() {
  108. MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
  109. MyComparableObjectSeries s2 = new MyComparableObjectSeries("A");
  110. assertTrue(s1.equals(s2));
  111. assertTrue(s2.equals(s1));
  112. // key
  113. s1 = new MyComparableObjectSeries("B");
  114. assertNotEquals(s1, s2);
  115. s2 = new MyComparableObjectSeries("B");
  116. assertTrue(s1.equals(s2));
  117. // autoSort
  118. s1 = new MyComparableObjectSeries("B", false, true);
  119. assertNotEquals(s1, s2);
  120. s2 = new MyComparableObjectSeries("B", false, true);
  121. assertTrue(s1.equals(s2));
  122. // allowDuplicateXValues
  123. s1 = new MyComparableObjectSeries("B", false, false);
  124. assertNotEquals(s1, s2);
  125. s2 = new MyComparableObjectSeries("B", false, false);
  126. assertTrue(s1.equals(s2));
  127. // add a value
  128. s1.add(new Integer(1), "ABC");
  129. assertNotEquals(s1, s2);
  130. s2.add(new Integer(1), "ABC");
  131. assertTrue(s1.equals(s2));
  132. // add another value
  133. s1.add(new Integer(0), "DEF");
  134. assertNotEquals(s1, s2);
  135. s2.add(new Integer(0), "DEF");
  136. assertTrue(s1.equals(s2));
  137. // remove an item
  138. s1.remove(new Integer(1));
  139. assertNotEquals(s1, s2);
  140. s2.remove(new Integer(1));
  141. assertTrue(s1.equals(s2));
  142. }
  143. /**
  144. * Some checks for the clone() method.
  145. */
  146. @Test
  147. public void testCloning() throws CloneNotSupportedException {
  148. MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
  149. s1.add(new Integer(1), "ABC");
  150. MyComparableObjectSeries s2 = (MyComparableObjectSeries) s1.clone();
  151. assertTrue(s1 != s2);
  152. assertTrue(s1.getClass() == s2.getClass());
  153. assertTrue(s1.equals(s2));
  154. }
  155. /**
  156. * Serialize an instance, restore it, and check for equality.
  157. */
  158. @Test
  159. public void testSerialization() {
  160. MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
  161. s1.add(new Integer(1), "ABC");
  162. MyComparableObjectSeries s2 = (MyComparableObjectSeries)
  163. TestUtilities.serialised(s1);
  164. assertEquals(s1, s2);
  165. }
  166. /**
  167. * Some simple checks for the hashCode() method.
  168. */
  169. @Test
  170. public void testHashCode() {
  171. MyComparableObjectSeries s1 = new MyComparableObjectSeries("Test");
  172. MyComparableObjectSeries s2 = new MyComparableObjectSeries("Test");
  173. assertEquals(s1, s2);
  174. assertEquals(s1.hashCode(), s2.hashCode());
  175. s1.add("A", "1");
  176. s2.add("A", "1");
  177. assertEquals(s1, s2);
  178. assertEquals(s1.hashCode(), s2.hashCode());
  179. s1.add("B", null);
  180. s2.add("B", null);
  181. assertEquals(s1, s2);
  182. assertEquals(s1.hashCode(), s2.hashCode());
  183. s1.add("C", "3");
  184. s2.add("C", "3");
  185. assertEquals(s1, s2);
  186. assertEquals(s1.hashCode(), s2.hashCode());
  187. s1.add("D", "4");
  188. s2.add("D", "4");
  189. assertEquals(s1, s2);
  190. assertEquals(s1.hashCode(), s2.hashCode());
  191. }
  192. }