XYLineAnnotationTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * XYLineAnnotationTest.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. * 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.Stroke;
  50. import org.jfree.chart.TestUtilities;
  51. import org.jfree.util.PublicCloneable;
  52. import org.junit.Test;
  53. /**
  54. * Tests for the {@link XYLineAnnotation} class.
  55. */
  56. public class XYLineAnnotationTest {
  57. /**
  58. * Confirm that the equals method can distinguish all the required fields.
  59. */
  60. @Test
  61. public void testEquals() {
  62. Stroke stroke = new BasicStroke(2.0f);
  63. XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0, 200.0,
  64. stroke, Color.blue);
  65. XYLineAnnotation a2 = new XYLineAnnotation(10.0, 20.0, 100.0, 200.0,
  66. stroke, Color.blue);
  67. assertTrue(a1.equals(a2));
  68. assertTrue(a2.equals(a1));
  69. a1 = new XYLineAnnotation(11.0, 20.0, 100.0, 200.0, stroke, Color.blue);
  70. assertFalse(a1.equals(a2));
  71. a2 = new XYLineAnnotation(11.0, 20.0, 100.0, 200.0, stroke, Color.blue);
  72. assertTrue(a1.equals(a2));
  73. a1 = new XYLineAnnotation(11.0, 21.0, 100.0, 200.0, stroke, Color.blue);
  74. assertFalse(a1.equals(a2));
  75. a2 = new XYLineAnnotation(11.0, 21.0, 100.0, 200.0, stroke, Color.blue);
  76. assertTrue(a1.equals(a2));
  77. a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke, Color.blue);
  78. assertFalse(a1.equals(a2));
  79. a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke, Color.blue);
  80. assertTrue(a1.equals(a2));
  81. a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 201.0, stroke, Color.blue);
  82. assertFalse(a1.equals(a2));
  83. a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 201.0, stroke, Color.blue);
  84. assertTrue(a1.equals(a2));
  85. Stroke stroke2 = new BasicStroke(0.99f);
  86. a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2, Color.blue);
  87. assertFalse(a1.equals(a2));
  88. a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2, Color.blue);
  89. assertTrue(a1.equals(a2));
  90. GradientPaint g1 = new GradientPaint(1.0f, 2.0f, Color.red,
  91. 3.0f, 4.0f, Color.white);
  92. GradientPaint g2 = new GradientPaint(1.0f, 2.0f, Color.red,
  93. 3.0f, 4.0f, Color.white);
  94. a1 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2, g1);
  95. assertFalse(a1.equals(a2));
  96. a2 = new XYLineAnnotation(11.0, 21.0, 101.0, 200.0, stroke2, g2);
  97. assertTrue(a1.equals(a2));
  98. }
  99. /**
  100. * Two objects that are equal are required to return the same hashCode.
  101. */
  102. @Test
  103. public void testHashCode() {
  104. Stroke stroke = new BasicStroke(2.0f);
  105. XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0, 200.0,
  106. stroke, Color.blue);
  107. XYLineAnnotation a2 = new XYLineAnnotation(10.0, 20.0, 100.0, 200.0,
  108. stroke, Color.blue);
  109. assertTrue(a1.equals(a2));
  110. int h1 = a1.hashCode();
  111. int h2 = a2.hashCode();
  112. assertEquals(h1, h2);
  113. }
  114. /**
  115. * Confirm that cloning works.
  116. */
  117. @Test
  118. public void testCloning() throws CloneNotSupportedException {
  119. Stroke stroke = new BasicStroke(2.0f);
  120. XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0, 200.0,
  121. stroke, Color.blue);
  122. XYLineAnnotation a2 = (XYLineAnnotation) a1.clone();
  123. assertTrue(a1 != a2);
  124. assertTrue(a1.getClass() == a2.getClass());
  125. assertTrue(a1.equals(a2));
  126. }
  127. /**
  128. * Checks that this class implements PublicCloneable.
  129. */
  130. @Test
  131. public void testPublicCloneable() {
  132. Stroke stroke = new BasicStroke(2.0f);
  133. XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0, 200.0,
  134. stroke, Color.blue);
  135. assertTrue(a1 instanceof PublicCloneable);
  136. }
  137. /**
  138. * Serialize an instance, restore it, and check for equality.
  139. */
  140. @Test
  141. public void testSerialization() {
  142. Stroke stroke = new BasicStroke(2.0f);
  143. XYLineAnnotation a1 = new XYLineAnnotation(10.0, 20.0, 100.0, 200.0,
  144. stroke, Color.blue);
  145. XYLineAnnotation a2 = (XYLineAnnotation) TestUtilities.serialised(a1);
  146. assertEquals(a1, a2);
  147. }
  148. }