XYDrawableAnnotationTest.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * XYDrawableAnnotationTest.java
  29. * -----------------------------
  30. * (C) Copyright 2003-2013, by Object Refinery Limited.
  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. * 01-Oct-2004 : Fixed bugs in tests (DG);
  39. * 07-Jan-2005 : Added hashCode() test (DG);
  40. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  41. *
  42. */
  43. package org.jfree.chart.annotations;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.assertFalse;
  47. import java.awt.Graphics2D;
  48. import java.awt.geom.Rectangle2D;
  49. import java.io.Serializable;
  50. import org.jfree.chart.TestUtilities;
  51. import org.jfree.ui.Drawable;
  52. import org.jfree.util.PublicCloneable;
  53. import org.junit.Test;
  54. /**
  55. * Tests for the {@link XYDrawableAnnotation} class.
  56. */
  57. public class XYDrawableAnnotationTest {
  58. static class TestDrawable implements Drawable, Cloneable, Serializable {
  59. /**
  60. * Default constructor.
  61. */
  62. public TestDrawable() {
  63. }
  64. /**
  65. * Draws something.
  66. * @param g2 the graphics device.
  67. * @param area the area in which to draw.
  68. */
  69. @Override
  70. public void draw(Graphics2D g2, Rectangle2D area) {
  71. // do nothing
  72. }
  73. /**
  74. * Tests this object for equality with an arbitrary object.
  75. * @param obj the object to test against (<code>null</code> permitted).
  76. * @return A boolean.
  77. */
  78. @Override
  79. public boolean equals(Object obj) {
  80. if (obj == this) {
  81. return true;
  82. }
  83. if (!(obj instanceof TestDrawable)) {
  84. return false;
  85. }
  86. return true;
  87. }
  88. /**
  89. * Returns a clone.
  90. *
  91. * @return A clone.
  92. *
  93. * @throws CloneNotSupportedException if there is a problem cloning.
  94. */
  95. @Override
  96. public Object clone() throws CloneNotSupportedException {
  97. return super.clone();
  98. }
  99. }
  100. /**
  101. * Confirm that the equals method can distinguish all the required fields.
  102. */
  103. @Test
  104. public void testEquals() {
  105. XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0, 100.0,
  106. 200.0, new TestDrawable());
  107. XYDrawableAnnotation a2 = new XYDrawableAnnotation(10.0, 20.0, 100.0,
  108. 200.0, new TestDrawable());
  109. assertTrue(a1.equals(a2));
  110. a1 = new XYDrawableAnnotation(11.0, 20.0, 100.0, 200.0,
  111. new TestDrawable());
  112. assertFalse(a1.equals(a2));
  113. a2 = new XYDrawableAnnotation(11.0, 20.0, 100.0, 200.0,
  114. new TestDrawable());
  115. assertTrue(a1.equals(a2));
  116. a1 = new XYDrawableAnnotation(11.0, 22.0, 100.0, 200.0,
  117. new TestDrawable());
  118. assertFalse(a1.equals(a2));
  119. a2 = new XYDrawableAnnotation(11.0, 22.0, 100.0, 200.0,
  120. new TestDrawable());
  121. assertTrue(a1.equals(a2));
  122. a1 = new XYDrawableAnnotation(11.0, 22.0, 101.0, 200.0,
  123. new TestDrawable());
  124. assertFalse(a1.equals(a2));
  125. a2 = new XYDrawableAnnotation(11.0, 22.0, 101.0, 200.0,
  126. new TestDrawable());
  127. assertTrue(a1.equals(a2));
  128. a1 = new XYDrawableAnnotation(11.0, 22.0, 101.0, 202.0,
  129. new TestDrawable());
  130. assertFalse(a1.equals(a2));
  131. a2 = new XYDrawableAnnotation(11.0, 22.0, 101.0, 202.0,
  132. new TestDrawable());
  133. assertTrue(a1.equals(a2));
  134. a1 = new XYDrawableAnnotation(11.0, 22.0, 101.0, 202.0, 2.0,
  135. new TestDrawable());
  136. assertFalse(a1.equals(a2));
  137. a2 = new XYDrawableAnnotation(11.0, 22.0, 101.0, 202.0, 2.0,
  138. new TestDrawable());
  139. assertTrue(a1.equals(a2));
  140. }
  141. /**
  142. * Two objects that are equal are required to return the same hashCode.
  143. */
  144. @Test
  145. public void testHashCode() {
  146. XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0, 100.0,
  147. 200.0, new TestDrawable());
  148. XYDrawableAnnotation a2 = new XYDrawableAnnotation(10.0, 20.0, 100.0,
  149. 200.0, new TestDrawable());
  150. assertTrue(a1.equals(a2));
  151. int h1 = a1.hashCode();
  152. int h2 = a2.hashCode();
  153. assertEquals(h1, h2);
  154. }
  155. /**
  156. * Confirm that cloning works.
  157. */
  158. @Test
  159. public void testCloning() throws CloneNotSupportedException {
  160. XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0, 100.0,
  161. 200.0, new TestDrawable());
  162. XYDrawableAnnotation a2 = (XYDrawableAnnotation) a1.clone();
  163. assertTrue(a1 != a2);
  164. assertTrue(a1.getClass() == a2.getClass());
  165. assertTrue(a1.equals(a2));
  166. }
  167. /**
  168. * Checks that this class implements PublicCloneable.
  169. */
  170. @Test
  171. public void testPublicCloneable() {
  172. XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0, 100.0,
  173. 200.0, new TestDrawable());
  174. assertTrue(a1 instanceof PublicCloneable);
  175. }
  176. /**
  177. * Serialize an instance, restore it, and check for equality.
  178. */
  179. @Test
  180. public void testSerialization() {
  181. XYDrawableAnnotation a1 = new XYDrawableAnnotation(10.0, 20.0, 100.0,
  182. 200.0, new TestDrawable());
  183. XYDrawableAnnotation a2 = (XYDrawableAnnotation) TestUtilities.serialised(a1);
  184. assertEquals(a1, a2);
  185. }
  186. }