XYShapeAnnotationTest.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * XYShapeAnnotationTest.java
  29. * --------------------------
  30. * (C) Copyright 2004-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. * 29-Sep-2004 : Version 1 (DG);
  38. * 07-Jan-2005 : Added hashCode() test (DG);
  39. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  40. *
  41. */
  42. package org.jfree.chart.annotations;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.junit.Assert.assertFalse;
  46. import java.awt.BasicStroke;
  47. import java.awt.Color;
  48. import java.awt.GradientPaint;
  49. import java.awt.geom.Rectangle2D;
  50. import org.jfree.chart.TestUtilities;
  51. import org.jfree.util.PublicCloneable;
  52. import org.junit.Test;
  53. /**
  54. * Some tests for the {@link XYShapeAnnotation} class.
  55. */
  56. public class XYShapeAnnotationTest {
  57. /**
  58. * Confirm that the equals method can distinguish all the required fields.
  59. */
  60. @Test
  61. public void testEquals() {
  62. XYShapeAnnotation a1 = new XYShapeAnnotation(
  63. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  64. new BasicStroke(1.2f), Color.red, Color.blue);
  65. XYShapeAnnotation a2 = new XYShapeAnnotation(
  66. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  67. new BasicStroke(1.2f), Color.red, Color.blue);
  68. assertTrue(a1.equals(a2));
  69. assertTrue(a2.equals(a1));
  70. // shape
  71. a1 = new XYShapeAnnotation(
  72. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  73. new BasicStroke(1.2f), Color.red, Color.blue);
  74. assertFalse(a1.equals(a2));
  75. a2 = new XYShapeAnnotation(
  76. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  77. new BasicStroke(1.2f), Color.red, Color.blue);
  78. assertTrue(a1.equals(a2));
  79. // stroke
  80. a1 = new XYShapeAnnotation(
  81. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  82. new BasicStroke(2.3f), Color.red, Color.blue);
  83. assertFalse(a1.equals(a2));
  84. a2 = new XYShapeAnnotation(
  85. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  86. new BasicStroke(2.3f), Color.red, Color.blue);
  87. assertTrue(a1.equals(a2));
  88. GradientPaint gp1a = new GradientPaint(1.0f, 2.0f, Color.blue,
  89. 3.0f, 4.0f, Color.red);
  90. GradientPaint gp1b = new GradientPaint(1.0f, 2.0f, Color.blue,
  91. 3.0f, 4.0f, Color.red);
  92. GradientPaint gp2a = new GradientPaint(5.0f, 6.0f, Color.pink,
  93. 7.0f, 8.0f, Color.white);
  94. GradientPaint gp2b = new GradientPaint(5.0f, 6.0f, Color.pink,
  95. 7.0f, 8.0f, Color.white);
  96. // outlinePaint
  97. a1 = new XYShapeAnnotation(
  98. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  99. new BasicStroke(2.3f), gp1a, Color.blue);
  100. assertFalse(a1.equals(a2));
  101. a2 = new XYShapeAnnotation(
  102. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  103. new BasicStroke(2.3f), gp1b, Color.blue);
  104. assertTrue(a1.equals(a2));
  105. // fillPaint
  106. a1 = new XYShapeAnnotation(
  107. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  108. new BasicStroke(2.3f), gp1a, gp2a);
  109. assertFalse(a1.equals(a2));
  110. a2 = new XYShapeAnnotation(
  111. new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  112. new BasicStroke(2.3f), gp1b, gp2b);
  113. assertTrue(a1.equals(a2));
  114. }
  115. /**
  116. * Two objects that are equal are required to return the same hashCode.
  117. */
  118. @Test
  119. public void testHashCode() {
  120. XYShapeAnnotation a1 = new XYShapeAnnotation(
  121. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  122. new BasicStroke(1.2f), Color.red, Color.blue);
  123. XYShapeAnnotation a2 = new XYShapeAnnotation(
  124. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  125. new BasicStroke(1.2f), Color.red, Color.blue);
  126. assertTrue(a1.equals(a2));
  127. int h1 = a1.hashCode();
  128. int h2 = a2.hashCode();
  129. assertEquals(h1, h2);
  130. }
  131. /**
  132. * Confirm that cloning works.
  133. */
  134. @Test
  135. public void testCloning() throws CloneNotSupportedException {
  136. XYShapeAnnotation a1 = new XYShapeAnnotation(
  137. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  138. new BasicStroke(1.2f), Color.red, Color.blue);
  139. XYShapeAnnotation a2 = (XYShapeAnnotation) a1.clone();
  140. assertTrue(a1 != a2);
  141. assertTrue(a1.getClass() == a2.getClass());
  142. assertTrue(a1.equals(a2));
  143. }
  144. /**
  145. * Checks that this class implements PublicCloneable.
  146. */
  147. @Test
  148. public void testPublicCloneable() {
  149. XYShapeAnnotation a1 = new XYShapeAnnotation(
  150. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  151. new BasicStroke(1.2f), Color.red, Color.blue);
  152. assertTrue(a1 instanceof PublicCloneable);
  153. }
  154. /**
  155. * Serialize an instance, restore it, and check for equality.
  156. */
  157. @Test
  158. public void testSerialization() {
  159. XYShapeAnnotation a1 = new XYShapeAnnotation(
  160. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  161. new BasicStroke(1.2f), Color.red, Color.blue);
  162. XYShapeAnnotation a2 = (XYShapeAnnotation) TestUtilities.serialised(a1);
  163. assertEquals(a1, a2);
  164. }
  165. }