XYPointerAnnotationTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * XYPointerAnnotationTest.java
  29. * ----------------------------
  30. * (C) Copyright 2003-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. * 19-Aug-2003 : Version 1 (DG);
  38. * 13-Oct-2003 : Expanded test for equals() method (DG);
  39. * 07-Jan-2005 : Added hashCode() test (DG);
  40. * 20-Feb-2006 : Added 'x' and 'y' checks to testEquals() (DG);
  41. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  42. *
  43. */
  44. package org.jfree.chart.annotations;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.assertFalse;
  48. import java.awt.BasicStroke;
  49. import java.awt.Color;
  50. import java.awt.Stroke;
  51. import org.jfree.chart.TestUtilities;
  52. import org.jfree.util.PublicCloneable;
  53. import org.junit.Test;
  54. /**
  55. * Tests for the {@link XYPointerAnnotation} class.
  56. */
  57. public class XYPointerAnnotationTest {
  58. /**
  59. * Confirm that the equals method can distinguish all the required fields.
  60. */
  61. @Test
  62. public void testEquals() {
  63. XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0, 20.0,
  64. Math.PI);
  65. XYPointerAnnotation a2 = new XYPointerAnnotation("Label", 10.0, 20.0,
  66. Math.PI);
  67. assertTrue(a1.equals(a2));
  68. a1 = new XYPointerAnnotation("Label2", 10.0, 20.0, Math.PI);
  69. assertFalse(a1.equals(a2));
  70. a2 = new XYPointerAnnotation("Label2", 10.0, 20.0, Math.PI);
  71. assertTrue(a1.equals(a2));
  72. a1.setX(11.0);
  73. assertFalse(a1.equals(a2));
  74. a2.setX(11.0);
  75. assertTrue(a1.equals(a2));
  76. a1.setY(22.0);
  77. assertFalse(a1.equals(a2));
  78. a2.setY(22.0);
  79. assertTrue(a1.equals(a2));
  80. //private double angle;
  81. a1.setAngle(Math.PI / 4.0);
  82. assertFalse(a1.equals(a2));
  83. a2.setAngle(Math.PI / 4.0);
  84. assertTrue(a1.equals(a2));
  85. //private double tipRadius;
  86. a1.setTipRadius(20.0);
  87. assertFalse(a1.equals(a2));
  88. a2.setTipRadius(20.0);
  89. assertTrue(a1.equals(a2));
  90. //private double baseRadius;
  91. a1.setBaseRadius(5.0);
  92. assertFalse(a1.equals(a2));
  93. a2.setBaseRadius(5.0);
  94. assertTrue(a1.equals(a2));
  95. //private double arrowLength;
  96. a1.setArrowLength(33.0);
  97. assertFalse(a1.equals(a2));
  98. a2.setArrowLength(33.0);
  99. assertTrue(a1.equals(a2));
  100. //private double arrowWidth;
  101. a1.setArrowWidth(9.0);
  102. assertFalse(a1.equals(a2));
  103. a2.setArrowWidth(9.0);
  104. assertTrue(a1.equals(a2));
  105. //private Stroke arrowStroke;
  106. Stroke stroke = new BasicStroke(1.5f);
  107. a1.setArrowStroke(stroke);
  108. assertFalse(a1.equals(a2));
  109. a2.setArrowStroke(stroke);
  110. assertTrue(a1.equals(a2));
  111. //private Paint arrowPaint;
  112. a1.setArrowPaint(Color.blue);
  113. assertFalse(a1.equals(a2));
  114. a2.setArrowPaint(Color.blue);
  115. assertTrue(a1.equals(a2));
  116. //private double labelOffset;
  117. a1.setLabelOffset(10.0);
  118. assertFalse(a1.equals(a2));
  119. a2.setLabelOffset(10.0);
  120. assertTrue(a1.equals(a2));
  121. }
  122. /**
  123. * Two objects that are equal are required to return the same hashCode.
  124. */
  125. @Test
  126. public void testHashCode() {
  127. XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0, 20.0,
  128. Math.PI);
  129. XYPointerAnnotation a2 = new XYPointerAnnotation("Label", 10.0, 20.0,
  130. Math.PI);
  131. assertTrue(a1.equals(a2));
  132. int h1 = a1.hashCode();
  133. int h2 = a2.hashCode();
  134. assertEquals(h1, h2);
  135. }
  136. /**
  137. * Confirm that cloning works.
  138. */
  139. @Test
  140. public void testCloning() throws CloneNotSupportedException {
  141. XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0, 20.0,
  142. Math.PI);
  143. XYPointerAnnotation a2 = a2 = (XYPointerAnnotation) a1.clone();
  144. assertTrue(a1 != a2);
  145. assertTrue(a1.getClass() == a2.getClass());
  146. assertTrue(a1.equals(a2));
  147. }
  148. /**
  149. * Checks that this class implements PublicCloneable.
  150. */
  151. @Test
  152. public void testPublicCloneable() {
  153. XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0, 20.0,
  154. Math.PI);
  155. assertTrue(a1 instanceof PublicCloneable);
  156. }
  157. /**
  158. * Serialize an instance, restore it, and check for equality.
  159. */
  160. @Test
  161. public void testSerialization() {
  162. XYPointerAnnotation a1 = new XYPointerAnnotation("Label", 10.0, 20.0,
  163. Math.PI);
  164. XYPointerAnnotation a2 = (XYPointerAnnotation) TestUtilities.serialised(a1);
  165. assertEquals(a1, a2);
  166. }
  167. }