XYTextAnnotationTest.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * XYTextAnnotationTest.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. * 07-Jan-2005 : Added hashCode() test (DG);
  39. * 26-Jan-2006 : Extended equals() test (DG);
  40. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  41. * 12-Feb-2009 : Updated testEquals() (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.Font;
  51. import java.awt.GradientPaint;
  52. import org.jfree.chart.TestUtilities;
  53. import org.jfree.ui.TextAnchor;
  54. import org.jfree.util.PublicCloneable;
  55. import org.junit.Test;
  56. /**
  57. * Tests for the {@link XYTextAnnotation} class.
  58. */
  59. public class XYTextAnnotationTest {
  60. /**
  61. * Confirm that the equals method can distinguish all the required fields.
  62. */
  63. @Test
  64. public void testEquals() {
  65. XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
  66. XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0);
  67. assertTrue(a1.equals(a2));
  68. // text
  69. a1 = new XYTextAnnotation("ABC", 10.0, 20.0);
  70. assertFalse(a1.equals(a2));
  71. a2 = new XYTextAnnotation("ABC", 10.0, 20.0);
  72. assertTrue(a1.equals(a2));
  73. // x
  74. a1 = new XYTextAnnotation("ABC", 11.0, 20.0);
  75. assertFalse(a1.equals(a2));
  76. a2 = new XYTextAnnotation("ABC", 11.0, 20.0);
  77. assertTrue(a1.equals(a2));
  78. // y
  79. a1 = new XYTextAnnotation("ABC", 11.0, 22.0);
  80. assertFalse(a1.equals(a2));
  81. a2 = new XYTextAnnotation("ABC", 11.0, 22.0);
  82. assertTrue(a1.equals(a2));
  83. // font
  84. a1.setFont(new Font("Serif", Font.PLAIN, 23));
  85. assertFalse(a1.equals(a2));
  86. a2.setFont(new Font("Serif", Font.PLAIN, 23));
  87. assertTrue(a1.equals(a2));
  88. // paint
  89. GradientPaint gp1 = new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  90. 4.0f, Color.yellow);
  91. GradientPaint gp2 = new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  92. 4.0f, Color.yellow);
  93. a1.setPaint(gp1);
  94. assertFalse(a1.equals(a2));
  95. a2.setPaint(gp2);
  96. assertTrue(a1.equals(a2));
  97. // rotation anchor
  98. a1.setRotationAnchor(TextAnchor.BASELINE_RIGHT);
  99. assertFalse(a1.equals(a2));
  100. a2.setRotationAnchor(TextAnchor.BASELINE_RIGHT);
  101. assertTrue(a1.equals(a2));
  102. // rotation angle
  103. a1.setRotationAngle(12.3);
  104. assertFalse(a1.equals(a2));
  105. a2.setRotationAngle(12.3);
  106. assertTrue(a1.equals(a2));
  107. // text anchor
  108. a1.setTextAnchor(TextAnchor.BASELINE_RIGHT);
  109. assertFalse(a1.equals(a2));
  110. a2.setTextAnchor(TextAnchor.BASELINE_RIGHT);
  111. assertTrue(a1.equals(a2));
  112. a1.setBackgroundPaint(gp1);
  113. assertFalse(a1.equals(a2));
  114. a2.setBackgroundPaint(gp1);
  115. assertTrue(a1.equals(a2));
  116. a1.setOutlinePaint(gp1);
  117. assertFalse(a1.equals(a2));
  118. a2.setOutlinePaint(gp1);
  119. assertTrue(a1.equals(a2));
  120. a1.setOutlineStroke(new BasicStroke(1.2f));
  121. assertFalse(a1.equals(a2));
  122. a2.setOutlineStroke(new BasicStroke(1.2f));
  123. assertTrue(a1.equals(a2));
  124. a1.setOutlineVisible(!a1.isOutlineVisible());
  125. assertFalse(a1.equals(a2));
  126. a2.setOutlineVisible(a1.isOutlineVisible());
  127. assertTrue(a1.equals(a2));
  128. }
  129. /**
  130. * Two objects that are equal are required to return the same hashCode.
  131. */
  132. @Test
  133. public void testHashCode() {
  134. XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
  135. XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0);
  136. assertTrue(a1.equals(a2));
  137. int h1 = a1.hashCode();
  138. int h2 = a2.hashCode();
  139. assertEquals(h1, h2);
  140. }
  141. /**
  142. * Confirm that cloning works.
  143. */
  144. @Test
  145. public void testCloning() throws CloneNotSupportedException {
  146. XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
  147. XYTextAnnotation a2 = (XYTextAnnotation) a1.clone();
  148. assertTrue(a1 != a2);
  149. assertTrue(a1.getClass() == a2.getClass());
  150. assertTrue(a1.equals(a2));
  151. }
  152. /**
  153. * Checks that this class implements PublicCloneable.
  154. */
  155. @Test
  156. public void testPublicCloneable() {
  157. XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
  158. assertTrue(a1 instanceof PublicCloneable);
  159. }
  160. /**
  161. * Serialize an instance, restore it, and check for equality.
  162. */
  163. @Test
  164. public void testSerialization() {
  165. XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
  166. a1.setOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
  167. Color.blue));
  168. XYTextAnnotation a2 = (XYTextAnnotation) TestUtilities.serialised(a1);
  169. assertEquals(a1, a2);
  170. }
  171. }