OHLCItemTest.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * OHLCItemTest.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. * 23-May-2009 : Added testHashCode() (DG);
  39. *
  40. */
  41. package org.jfree.data.time.ohlc;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertEquals;
  45. import org.jfree.chart.TestUtilities;
  46. import org.jfree.data.time.Year;
  47. import org.junit.Test;
  48. /**
  49. * Tests for the {@link OHLCItem} class.
  50. */
  51. public class OHLCItemTest {
  52. private static final double EPSILON = 0.00000000001;
  53. /**
  54. * Some checks for the constructor.
  55. */
  56. @Test
  57. public void testConstructor1() {
  58. OHLCItem item1 = new OHLCItem(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  59. assertEquals(new Year(2006), item1.getPeriod());
  60. assertEquals(2.0, item1.getOpenValue(), EPSILON);
  61. assertEquals(4.0, item1.getHighValue(), EPSILON);
  62. assertEquals(1.0, item1.getLowValue(), EPSILON);
  63. assertEquals(3.0, item1.getCloseValue(), EPSILON);
  64. }
  65. /**
  66. * Confirm that the equals method can distinguish all the required fields.
  67. */
  68. @Test
  69. public void testEquals() {
  70. OHLCItem item1 = new OHLCItem(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  71. OHLCItem item2 = new OHLCItem(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  72. assertTrue(item1.equals(item2));
  73. assertTrue(item2.equals(item1));
  74. // period
  75. item1 = new OHLCItem(new Year(2007), 2.0, 4.0, 1.0, 3.0);
  76. assertFalse(item1.equals(item2));
  77. item2 = new OHLCItem(new Year(2007), 2.0, 4.0, 1.0, 3.0);
  78. assertTrue(item1.equals(item2));
  79. // open
  80. item1 = new OHLCItem(new Year(2007), 2.2, 4.0, 1.0, 3.0);
  81. assertFalse(item1.equals(item2));
  82. item2 = new OHLCItem(new Year(2007), 2.2, 4.0, 1.0, 3.0);
  83. assertTrue(item1.equals(item2));
  84. // high
  85. item1 = new OHLCItem(new Year(2007), 2.2, 4.4, 1.0, 3.0);
  86. assertFalse(item1.equals(item2));
  87. item2 = new OHLCItem(new Year(2007), 2.2, 4.4, 1.0, 3.0);
  88. assertTrue(item1.equals(item2));
  89. // low
  90. item1 = new OHLCItem(new Year(2007), 2.2, 4.4, 1.1, 3.0);
  91. assertFalse(item1.equals(item2));
  92. item2 = new OHLCItem(new Year(2007), 2.2, 4.4, 1.1, 3.0);
  93. assertTrue(item1.equals(item2));
  94. // close
  95. item1 = new OHLCItem(new Year(2007), 2.2, 4.4, 1.1, 3.3);
  96. assertFalse(item1.equals(item2));
  97. item2 = new OHLCItem(new Year(2007), 2.2, 4.4, 1.1, 3.3);
  98. assertTrue(item1.equals(item2));
  99. }
  100. /**
  101. * Some checks for the clone() method.
  102. */
  103. @Test
  104. public void testCloning() throws CloneNotSupportedException {
  105. OHLCItem item1 = new OHLCItem(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  106. OHLCItem item2 = (OHLCItem) item1.clone();
  107. assertTrue(item1 != item2);
  108. assertTrue(item1.getClass() == item2.getClass());
  109. assertTrue(item1.equals(item2));
  110. }
  111. /**
  112. * Serialize an instance, restore it, and check for equality.
  113. */
  114. @Test
  115. public void testSerialization() {
  116. OHLCItem item1 = new OHLCItem(new Year(2006), 2.0, 4.0, 1.0, 3.0);
  117. OHLCItem item2 = (OHLCItem) TestUtilities.serialised(item1);
  118. assertEquals(item1, item2);
  119. }
  120. /**
  121. * Two objects that are equal are required to return the same hashCode.
  122. */
  123. @Test
  124. public void testHashcode() {
  125. OHLCItem i1 = new OHLCItem(new Year(2009), 2.0, 4.0, 1.0, 3.0);
  126. OHLCItem i2 = new OHLCItem(new Year(2009), 2.0, 4.0, 1.0, 3.0);
  127. assertTrue(i1.equals(i2));
  128. int h1 = i1.hashCode();
  129. int h2 = i2.hashCode();
  130. assertEquals(h1, h2);
  131. }
  132. }