OHLCSeriesTest.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * OHLCSeriesTest.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, based on XYSeriesTests (DG);
  38. * 27-Nov-2007 : Added testClear() method (DG);
  39. * 23-May-2009 : Added testHashCode() (DG);
  40. * 17-Jun-2009 : Added testRemove_int() (DG);
  41. *
  42. */
  43. package org.jfree.data.time.ohlc;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertNotNull;
  49. import org.jfree.chart.TestUtilities;
  50. import org.jfree.data.general.SeriesChangeEvent;
  51. import org.jfree.data.general.SeriesChangeListener;
  52. import org.jfree.data.general.SeriesException;
  53. import org.jfree.data.time.Year;
  54. import org.junit.Test;
  55. /**
  56. * Tests for the {@link OHLCSeries} class.
  57. */
  58. public class OHLCSeriesTest implements SeriesChangeListener {
  59. SeriesChangeEvent lastEvent;
  60. /**
  61. * Records a change event.
  62. *
  63. * @param event the event.
  64. */
  65. @Override
  66. public void seriesChanged(SeriesChangeEvent event) {
  67. this.lastEvent = event;
  68. }
  69. /**
  70. * Confirm that the equals method can distinguish all the required fields.
  71. */
  72. @Test
  73. public void testEquals() {
  74. OHLCSeries s1 = new OHLCSeries("s1");
  75. OHLCSeries s2 = new OHLCSeries("s1");
  76. assertTrue(s1.equals(s2));
  77. // seriesKey
  78. s1 = new OHLCSeries("s2");
  79. assertFalse(s1.equals(s2));
  80. s2 = new OHLCSeries("s2");
  81. assertTrue(s1.equals(s2));
  82. // add a value
  83. s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  84. assertFalse(s1.equals(s2));
  85. s2.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  86. assertTrue(s2.equals(s1));
  87. // add another value
  88. s1.add(new Year(2008), 2.0, 4.0, 1.0, 3.0);
  89. assertFalse(s1.equals(s2));
  90. s2.add(new Year(2008), 2.0, 4.0, 1.0, 3.0);
  91. assertTrue(s2.equals(s1));
  92. // remove a value
  93. s1.remove(new Year(2008));
  94. assertFalse(s1.equals(s2));
  95. s2.remove(new Year(2008));
  96. assertTrue(s2.equals(s1));
  97. }
  98. /**
  99. * Two objects that are equal are required to return the same hashCode.
  100. */
  101. @Test
  102. public void testHashcode() {
  103. OHLCSeries s1 = new OHLCSeries("Test");
  104. s1.add(new Year(2009), 1.0, 3.0, 2.0, 1.4);
  105. OHLCSeries s2 = new OHLCSeries("Test");
  106. s2.add(new Year(2009), 1.0, 3.0, 2.0, 1.4);
  107. assertTrue(s1.equals(s2));
  108. int h1 = s1.hashCode();
  109. int h2 = s2.hashCode();
  110. assertEquals(h1, h2);
  111. }
  112. /**
  113. * Confirm that cloning works.
  114. */
  115. @Test
  116. public void testCloning() throws CloneNotSupportedException {
  117. OHLCSeries s1 = new OHLCSeries("s1");
  118. s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  119. OHLCSeries s2 = (OHLCSeries) s1.clone();
  120. assertTrue(s1 != s2);
  121. assertTrue(s1.getClass() == s2.getClass());
  122. assertTrue(s1.equals(s2));
  123. }
  124. /**
  125. * Serialize an instance, restore it, and check for equality.
  126. */
  127. @Test
  128. public void testSerialization() {
  129. OHLCSeries s1 = new OHLCSeries("s1");
  130. s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  131. OHLCSeries s2 = (OHLCSeries) TestUtilities.serialised(s1);
  132. assertEquals(s1, s2);
  133. }
  134. /**
  135. * Simple test for the indexOf() method.
  136. */
  137. @Test
  138. public void testIndexOf() {
  139. OHLCSeries s1 = new OHLCSeries("s1");
  140. s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  141. s1.add(new Year(2011), 2.0, 4.0, 1.0, 3.0);
  142. s1.add(new Year(2010), 2.0, 4.0, 1.0, 3.0);
  143. assertEquals(0, s1.indexOf(new Year(2006)));
  144. assertEquals(1, s1.indexOf(new Year(2010)));
  145. assertEquals(2, s1.indexOf(new Year(2011)));
  146. }
  147. /**
  148. * Simple test for the remove() method.
  149. */
  150. @Test
  151. public void testRemove() {
  152. OHLCSeries s1 = new OHLCSeries("s1");
  153. s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  154. s1.add(new Year(2011), 2.1, 4.1, 1.1, 3.1);
  155. s1.add(new Year(2010), 2.2, 4.2, 1.2, 3.2);
  156. assertEquals(3, s1.getItemCount());
  157. s1.remove(new Year(2010));
  158. assertEquals(new Year(2011), s1.getPeriod(1));
  159. s1.remove(new Year(2006));
  160. assertEquals(new Year(2011), s1.getPeriod(0));
  161. }
  162. /**
  163. * A check for the remove(int) method.
  164. */
  165. @Test
  166. public void testRemove_int() {
  167. OHLCSeries s1 = new OHLCSeries("s1");
  168. s1.add(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  169. s1.add(new Year(2011), 2.1, 4.1, 1.1, 3.1);
  170. s1.add(new Year(2010), 2.2, 4.2, 1.2, 3.2);
  171. assertEquals(3, s1.getItemCount());
  172. s1.remove(s1.getItemCount() - 1);
  173. assertEquals(2, s1.getItemCount());
  174. assertEquals(new Year(2010), s1.getPeriod(1));
  175. }
  176. /**
  177. * If you add a duplicate period, an exception should be thrown.
  178. */
  179. @Test
  180. public void testAdditionOfDuplicatePeriod() {
  181. OHLCSeries s1 = new OHLCSeries("s1");
  182. s1.add(new Year(2006), 1.0, 1.0, 1.0, 1.0);
  183. boolean pass = false;
  184. try {
  185. s1.add(new Year(2006), 1.0, 1.0, 1.0, 1.0);
  186. }
  187. catch (SeriesException e) {
  188. pass = true;
  189. }
  190. assertTrue(pass);
  191. }
  192. /**
  193. * A simple check that the maximumItemCount attribute is working.
  194. */
  195. @Test
  196. public void testSetMaximumItemCount() {
  197. OHLCSeries s1 = new OHLCSeries("s1");
  198. assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
  199. s1.setMaximumItemCount(2);
  200. assertEquals(2, s1.getMaximumItemCount());
  201. s1.add(new Year(2006), 1.0, 1.1, 1.1, 1.1);
  202. s1.add(new Year(2007), 2.0, 2.2, 2.2, 2.2);
  203. s1.add(new Year(2008), 3.0, 3.3, 3.3, 3.3);
  204. assertEquals(new Year(2007), s1.getPeriod(0));
  205. assertEquals(new Year(2008), s1.getPeriod(1));
  206. }
  207. /**
  208. * Check that the maximum item count can be applied retrospectively.
  209. */
  210. @Test
  211. public void testSetMaximumItemCount2() {
  212. OHLCSeries s1 = new OHLCSeries("s1");
  213. s1.add(new Year(2006), 1.0, 1.1, 1.1, 1.1);
  214. s1.add(new Year(2007), 2.0, 2.2, 2.2, 2.2);
  215. s1.add(new Year(2008), 3.0, 3.3, 3.3, 3.3);
  216. s1.setMaximumItemCount(2);
  217. assertEquals(new Year(2007), s1.getPeriod(0));
  218. assertEquals(new Year(2008), s1.getPeriod(1));
  219. }
  220. /**
  221. * Some checks for the clear() method.
  222. */
  223. @Test
  224. public void testClear() {
  225. OHLCSeries s1 = new OHLCSeries("S1");
  226. s1.addChangeListener(this);
  227. s1.clear();
  228. assertNull(this.lastEvent);
  229. assertTrue(s1.isEmpty());
  230. s1.add(new Year(2006), 1.0, 1.1, 1.1, 1.1);
  231. assertFalse(s1.isEmpty());
  232. s1.clear();
  233. assertNotNull(this.lastEvent);
  234. assertTrue(s1.isEmpty());
  235. }
  236. }