XYPolygonAnnotationTest.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * XYPolygonAnnotationTest.java
  29. * ----------------------------
  30. * (C) Copyright 2006-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. * 10-Jul-2006 : Version 1 (DG);
  38. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  39. *
  40. */
  41. package org.jfree.chart.annotations;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertTrue;
  44. import static org.junit.Assert.assertFalse;
  45. import java.awt.BasicStroke;
  46. import java.awt.Color;
  47. import java.awt.GradientPaint;
  48. import java.awt.Stroke;
  49. import org.jfree.chart.TestUtilities;
  50. import org.jfree.util.PublicCloneable;
  51. import org.junit.Test;
  52. /**
  53. * Tests for the {@link XYPolygonAnnotation} class.
  54. */
  55. public class XYPolygonAnnotationTest {
  56. /**
  57. * Confirm that the equals method can distinguish all the required fields.
  58. */
  59. @Test
  60. public void testEquals() {
  61. Stroke stroke1 = new BasicStroke(2.0f);
  62. Stroke stroke2 = new BasicStroke(2.5f);
  63. XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
  64. 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
  65. XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {1.0,
  66. 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
  67. assertTrue(a1.equals(a2));
  68. assertTrue(a2.equals(a1));
  69. a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  70. 6.0}, stroke1, Color.red, Color.blue);
  71. assertFalse(a1.equals(a2));
  72. a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  73. 6.0}, stroke1, Color.red, Color.blue);
  74. assertTrue(a1.equals(a2));
  75. a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  76. 6.0}, stroke2, Color.red, Color.blue);
  77. assertFalse(a1.equals(a2));
  78. a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  79. 6.0}, stroke2, Color.red, Color.blue);
  80. assertTrue(a1.equals(a2));
  81. GradientPaint gp1 = new GradientPaint(1.0f, 2.0f, Color.yellow, 3.0f,
  82. 4.0f, Color.white);
  83. GradientPaint gp2 = new GradientPaint(1.0f, 2.0f, Color.yellow, 3.0f,
  84. 4.0f, Color.white);
  85. a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  86. 6.0}, stroke2, gp1, Color.blue);
  87. assertFalse(a1.equals(a2));
  88. a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  89. 6.0}, stroke2, gp2, Color.blue);
  90. assertTrue(a1.equals(a2));
  91. GradientPaint gp3 = new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
  92. 4.0f, Color.white);
  93. GradientPaint gp4 = new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
  94. 4.0f, Color.white);
  95. a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  96. 6.0}, stroke2, gp1, gp3);
  97. assertFalse(a1.equals(a2));
  98. a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
  99. 6.0}, stroke2, gp2, gp4);
  100. assertTrue(a1.equals(a2));
  101. }
  102. /**
  103. * Two objects that are equal are required to return the same hashCode.
  104. */
  105. @Test
  106. public void testHashCode() {
  107. Stroke stroke = new BasicStroke(2.0f);
  108. XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
  109. 2.0, 3.0, 4.0, 5.0, 6.0}, stroke, Color.red, Color.blue);
  110. XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {1.0,
  111. 2.0, 3.0, 4.0, 5.0, 6.0}, stroke, Color.red, Color.blue);
  112. assertTrue(a1.equals(a2));
  113. int h1 = a1.hashCode();
  114. int h2 = a2.hashCode();
  115. assertEquals(h1, h2);
  116. }
  117. /**
  118. * Confirm that cloning works.
  119. */
  120. @Test
  121. public void testCloning() throws CloneNotSupportedException {
  122. Stroke stroke1 = new BasicStroke(2.0f);
  123. XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
  124. 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
  125. XYPolygonAnnotation a2 = (XYPolygonAnnotation) a1.clone();
  126. assertTrue(a1 != a2);
  127. assertTrue(a1.getClass() == a2.getClass());
  128. assertTrue(a1.equals(a2));
  129. }
  130. /**
  131. * Checks that this class implements PublicCloneable.
  132. */
  133. @Test
  134. public void testPublicCloneable() {
  135. Stroke stroke1 = new BasicStroke(2.0f);
  136. XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
  137. 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
  138. assertTrue(a1 instanceof PublicCloneable);
  139. }
  140. /**
  141. * Serialize an instance, restore it, and check for equality.
  142. */
  143. @Test
  144. public void testSerialization() {
  145. Stroke stroke1 = new BasicStroke(2.0f);
  146. XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
  147. 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
  148. XYPolygonAnnotation a2 = (XYPolygonAnnotation) TestUtilities.serialised(a1);
  149. assertEquals(a1, a2);
  150. }
  151. }