OHLCSeriesCollectionTest.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * OHLCSeriesCollectionTest.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. * 04-Dec-2006 : Version 1 (DG);
  38. * 10-Jul-2008 : Updated testEquals() method (DG);
  39. * 26-Jun-2009 : Added tests for removeSeries() methods (DG);
  40. *
  41. */
  42. package org.jfree.data.time.ohlc;
  43. import static org.junit.Assert.assertTrue;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertNotNull;
  48. import org.jfree.chart.TestUtilities;
  49. import org.jfree.data.general.DatasetChangeEvent;
  50. import org.jfree.data.general.DatasetChangeListener;
  51. import org.jfree.data.time.TimePeriodAnchor;
  52. import org.jfree.data.time.Year;
  53. import org.junit.Test;
  54. /**
  55. * Tests for the {@link OHLCSeriesCollectionTests} class.
  56. */
  57. public class OHLCSeriesCollectionTest implements DatasetChangeListener {
  58. /**
  59. * Confirm that the equals method can distinguish all the required fields.
  60. */
  61. @Test
  62. public void testEquals() {
  63. OHLCSeriesCollection c1 = new OHLCSeriesCollection();
  64. OHLCSeriesCollection c2 = new OHLCSeriesCollection();
  65. assertEquals(c1, c2);
  66. // add a series
  67. OHLCSeries s1 = new OHLCSeries("Series");
  68. s1.add(new Year(2006), 1.0, 1.1, 1.2, 1.3);
  69. c1.addSeries(s1);
  70. assertFalse(c1.equals(c2));
  71. OHLCSeries s2 = new OHLCSeries("Series");
  72. s2.add(new Year(2006), 1.0, 1.1, 1.2, 1.3);
  73. c2.addSeries(s2);
  74. assertTrue(c1.equals(c2));
  75. // add an empty series
  76. c1.addSeries(new OHLCSeries("Empty Series"));
  77. assertFalse(c1.equals(c2));
  78. c2.addSeries(new OHLCSeries("Empty Series"));
  79. assertTrue(c1.equals(c2));
  80. c1.setXPosition(TimePeriodAnchor.END);
  81. assertFalse(c1.equals(c2));
  82. c2.setXPosition(TimePeriodAnchor.END);
  83. assertTrue(c1.equals(c2));
  84. }
  85. /**
  86. * Confirm that cloning works.
  87. */
  88. @Test
  89. public void testCloning() throws CloneNotSupportedException {
  90. OHLCSeriesCollection c1 = new OHLCSeriesCollection();
  91. OHLCSeries s1 = new OHLCSeries("Series");
  92. s1.add(new Year(2006), 1.0, 1.1, 1.2, 1.3);
  93. c1.addSeries(s1);
  94. OHLCSeriesCollection c2 = (OHLCSeriesCollection) c1.clone();
  95. assertTrue(c1 != c2);
  96. assertTrue(c1.getClass() == c2.getClass());
  97. assertTrue(c1.equals(c2));
  98. // check independence
  99. s1.setDescription("XYZ");
  100. assertFalse(c1.equals(c2));
  101. }
  102. /**
  103. * Serialize an instance, restore it, and check for equality.
  104. */
  105. @Test
  106. public void testSerialization() {
  107. OHLCSeriesCollection c1 = new OHLCSeriesCollection();
  108. OHLCSeries s1 = new OHLCSeries("Series");
  109. s1.add(new Year(2006), 1.0, 1.1, 1.2, 1.3);
  110. c1.addSeries(s1);
  111. OHLCSeriesCollection c2 = (OHLCSeriesCollection)
  112. TestUtilities.serialised(c1);
  113. assertEquals(c1, c2);
  114. }
  115. /**
  116. * A test for bug report 1170825 (originally affected XYSeriesCollection,
  117. * this test is just copied over).
  118. */
  119. @Test
  120. public void test1170825() {
  121. OHLCSeries s1 = new OHLCSeries("Series1");
  122. OHLCSeriesCollection dataset = new OHLCSeriesCollection();
  123. dataset.addSeries(s1);
  124. try {
  125. /* XYSeries s = */ dataset.getSeries(1);
  126. }
  127. catch (IllegalArgumentException e) {
  128. // correct outcome
  129. }
  130. catch (IndexOutOfBoundsException e) {
  131. assertTrue(false); // wrong outcome
  132. }
  133. }
  134. /**
  135. * Two objects that are equal are required to return the same hashCode.
  136. */
  137. @Test
  138. public void testHashcode() {
  139. OHLCSeriesCollection c1 = new OHLCSeriesCollection();
  140. OHLCSeries s1 = new OHLCSeries("S");
  141. s1.add(new Year(2009), 1.0, 4.0, 0.5, 2.0);
  142. c1.addSeries(s1);
  143. OHLCSeriesCollection c2 = new OHLCSeriesCollection();
  144. OHLCSeries s2 = new OHLCSeries("S");
  145. s2.add(new Year(2009), 1.0, 4.0, 0.5, 2.0);
  146. c2.addSeries(s2);
  147. assertTrue(c1.equals(c2));
  148. int h1 = c1.hashCode();
  149. int h2 = c2.hashCode();
  150. assertEquals(h1, h2);
  151. }
  152. /**
  153. * Some checks for the {@link OHLCSeriesCollection#removeSeries(int)}
  154. * method.
  155. */
  156. @Test
  157. public void testRemoveSeries_int() {
  158. OHLCSeriesCollection c1 = new OHLCSeriesCollection();
  159. OHLCSeries s1 = new OHLCSeries("Series 1");
  160. OHLCSeries s2 = new OHLCSeries("Series 2");
  161. OHLCSeries s3 = new OHLCSeries("Series 3");
  162. OHLCSeries s4 = new OHLCSeries("Series 4");
  163. c1.addSeries(s1);
  164. c1.addSeries(s2);
  165. c1.addSeries(s3);
  166. c1.addSeries(s4);
  167. c1.removeSeries(2);
  168. assertTrue(c1.getSeries(2).equals(s4));
  169. c1.removeSeries(0);
  170. assertTrue(c1.getSeries(0).equals(s2));
  171. assertEquals(2, c1.getSeriesCount());
  172. }
  173. /**
  174. * Some checks for the
  175. * {@link OHLCSeriesCollection#removeSeries(OHLCSeries)} method.
  176. */
  177. @Test
  178. public void testRemoveSeries() {
  179. OHLCSeriesCollection c1 = new OHLCSeriesCollection();
  180. OHLCSeries s1 = new OHLCSeries("Series 1");
  181. OHLCSeries s2 = new OHLCSeries("Series 2");
  182. OHLCSeries s3 = new OHLCSeries("Series 3");
  183. OHLCSeries s4 = new OHLCSeries("Series 4");
  184. c1.addSeries(s1);
  185. c1.addSeries(s2);
  186. c1.addSeries(s3);
  187. c1.addSeries(s4);
  188. c1.removeSeries(s3);
  189. assertTrue(c1.getSeries(2).equals(s4));
  190. c1.removeSeries(s1);
  191. assertTrue(c1.getSeries(0).equals(s2));
  192. assertEquals(2, c1.getSeriesCount());
  193. }
  194. /**
  195. * A simple check for the removeAllSeries() method.
  196. */
  197. @Test
  198. public void testRemoveAllSeries() {
  199. OHLCSeriesCollection c1 = new OHLCSeriesCollection();
  200. c1.addChangeListener(this);
  201. // there should be no change event when clearing an empty series
  202. this.lastEvent = null;
  203. c1.removeAllSeries();
  204. assertNull(this.lastEvent);
  205. OHLCSeries s1 = new OHLCSeries("Series 1");
  206. OHLCSeries s2 = new OHLCSeries("Series 2");
  207. c1.addSeries(s1);
  208. c1.addSeries(s2);
  209. c1.removeAllSeries();
  210. assertEquals(0, c1.getSeriesCount());
  211. assertNotNull(this.lastEvent);
  212. this.lastEvent = null; // clean up
  213. }
  214. /** The last received event. */
  215. private DatasetChangeEvent lastEvent;
  216. /**
  217. * Receives dataset change events.
  218. *
  219. * @param event the event.
  220. */
  221. @Override
  222. public void datasetChanged(DatasetChangeEvent event) {
  223. this.lastEvent = event;
  224. }
  225. }